Page MenuHomec4science

vect3d.cpp
No OneTemporary

File Metadata

Created
Thu, Dec 5, 04:24

vect3d.cpp

#include "vect3d.h"
#include <math.h>
Vector& operator+=(Vector& v, const Vector& w)
{
for (int i = 0; i < 3; ++i)
v[i] += w[i];
return v;
}
Vector& operator-=(Vector& v, const Vector& w)
{
for (int i = 0; i < 3; ++i)
v[i] -= w[i];
return v;
}
Vector& operator*=(Vector& v, double k)
{
for (int i = 0; i < 3; ++i)
v[i] *= k;
return v;
}
Vector& operator/=(Vector& v, double k)
{
for (int i = 0; i < 3; ++i)
v[i] /= k;
return v;
}
Vector operator+(const Vector& v, const Vector& w)
{
Vector u;
for (int i = 0; i < 3; ++i)
u[i] = v[i] + w[i];
return u;
}
Vector operator-(const Vector& v, const Vector& w)
{
Vector u;
for (int i = 0; i < 3; ++i)
u[i] = v[i] - w[i];
return u;
}
Vector operator*(const Vector& v, double k)
{
Vector u;
for (int i = 0; i < 3; ++i)
u[i] = v[i] * k;
return u;
}
Vector operator/(const Vector& v, double k)
{
Vector u;
for (int i = 0; i < 3; ++i)
u[i] = v[i] / k;
return u;
}
/* cross product */
Vector operator^(const Vector& v, const Vector& w)
{
Vector u;
u[0] = v[1] * w[2] - v[2] * w[1];
u[1] = v[2] * w[0] - v[0] * w[2];
u[2] = v[0] * w[1] - v[1] * w[0];
return u;
}
/* dot product */
double operator*(const Vector& v, const Vector& w)
{
double x = 0;
for (int i = 0; i < 3; ++i)
x += v[i] * w[i];
return x;
}
bool operator==(const Vector& v, const Vector& w)
{
for (int i = 0; i < 3; ++i)
{
if (v[i] != w[i])
return false;
}
return true;
}
bool operator!=(const Vector& v, const Vector& w)
{
return !(v == w);
}
double module(const Vector& v)
{
return sqrt(sq_module(v));
}
double sq_module(const Vector& v)
{
double x = 0;
for (int i = 0; i < 3; ++i)
x += v[i] * v[i];
return x;
}
Vector unit(const Vector& v)
{
return v / module(v);
}

Event Timeline