## Saturday, January 24, 2015

### Orbital Aero Model: Vector Model

** Update 21 Jan 2017: This Vector class has been deprecated, new version posted here. **

I created a Vector class that is used extensively in the code. Absolute locations, relative locations, velocities, accelerations, and forces all use this same class to hold values in 3 dimensional space. My vector is a simple 1 dimensional array of 3 values. Using an array makes things easier when dealing with matrix math (which will be introduced later). Also keeping it generic means those values could be considered an x/y/z or phi/theta/psi (orientation).

Operator overloading allows the programmer to use standard math operators on structures or classes that the compiler would normally not know what to do with. I use them extensively with vectors in the Orbital Aero Model; it helps to both condense the code and make it more readable.

For example, I'm trying to add vectors b and c together to store them in a.

Instead of writing code such as:
a.x = b.x + c.x;
a.y = b.y + c.y;
a.z = b.z + c.z;

I could write:
a = b + c;

The overload for that case would be:
{
}

The addition operation is being performed on vector b (this) and vector c (vectorToAdd). It is returning a new vector of the sum that is stored in a.

A similar example is to add the value of b into a.

Instead of writing code such as:
a.x += b.x;
a.y += b.y;
a.z += b.z;

I could write:
a += b;

The overload for that case would be:
{
return *this;
}

The addition operation is being performed on vector a (this) and vector b (vectorToAdd). The summed value is stored directly in a.

Since so much of the code uses my vector class that has operator overloading, I figured it would be best to share that class first. As I extend the functionality of my vector math, I'll update this post to include the latest additions.

### More Resources

For more information on vector math, I recommend learning the following concepts:
Vector
Dot Product
Cross Product

If you've never seen a Dot Product or Cross Product before, you may be questioning what they're for. They're actually both VERY important concepts when doing 3-dimensional math. You'll see examples of them in action in the code that follows.

### VectorModel.h

#pragma once

class Vector
{
private:

public:
double value;

Vector();

Vector(double value0, double value1, double value2);

Vector &Vector::operator=(double newValue);

Vector &Vector::operator=(const Vector &thatVector);

Vector Vector::operator-();

Vector Vector::operator-(const Vector &vectorToSubtract);

Vector Vector::operator*(double valueToMultiply);

Vector Vector::operator*(const Vector &vectorToMultiply);

Vector Vector::operator/(double valueToDivide);

Vector &Vector::operator-=(const Vector &vectorToSubtract);

Vector &Vector::operator*=(double valueToMultiply);

Vector &Vector::operator/=(double valueToDivide);

double magnitude();

Vector unit();

Vector sign();

static double dotProduct(Vector vector1, Vector vector2);
};

### VectorModel.c

#include "math.h"
#include "VectorModel.h"

Vector::Vector()
{
this->value = 0.0;
this->value = 0.0;
this->value = 0.0;
return;
}

Vector::Vector(double value0, double value1, double value2)
{
this->value = value0;
this->value = value1;
this->value = value2;
return;
}

Vector &Vector::operator=(double newValue)
{
this->value = newValue;
this->value = newValue;
this->value = newValue;
return *this;
}

Vector &Vector::operator=(const Vector &thatVector)
{
// Protect against self-assignment. (Otherwise bad things happen when it's reading from memory it has cleared.)
if (this != &thatVector)
{
this->value = thatVector.value;
this->value = thatVector.value;
this->value = thatVector.value;
}
return *this;
}

Vector Vector::operator-()
{
return Vector(-this->value, -this->value, -this->value);
}

{
}

Vector Vector::operator-(const Vector &vectorToSubtract)
{
return Vector(this->value - vectorToSubtract.value, this->value - vectorToSubtract.value, this->value - vectorToSubtract.value);
}

Vector Vector::operator*(double valueToMultiply)
{
return Vector(this->value * valueToMultiply, this->value * valueToMultiply, this->value * valueToMultiply);
}

Vector Vector::operator*(const Vector &vectorToMultiply)
{
return Vector(this->value * vectorToMultiply.value, this->value * vectorToMultiply.value, this->value * vectorToMultiply.value);
}

Vector Vector::operator/(double valueToDivide)
{
return Vector(this->value / valueToDivide, this->value / valueToDivide, this->value / valueToDivide);
}

{
return *this;
}

Vector &Vector::operator-=(const Vector &vectorToSubtract)
{
this->value -= vectorToSubtract.value;
this->value -= vectorToSubtract.value;
this->value -= vectorToSubtract.value;
return *this;
}

Vector &Vector::operator*=(double valueToMultiply)
{
this->value *= valueToMultiply;
this->value *= valueToMultiply;
this->value *= valueToMultiply;
return *this;
}

Vector &Vector::operator/=(double valueToDivide)
{
this->value /= valueToDivide;
this->value /= valueToDivide;
this->value /= valueToDivide;
return *this;
}

double Vector::magnitude()
{
return sqrt( (this->value * this->value) + (this->value * this->value) + (this->value * this->value) );
}

Vector Vector::unit()
{
double mag = magnitude();
Vector unitVector;
unitVector.value = this->value / mag;
unitVector.value = this->value / mag;
unitVector.value = this->value / mag;
return unitVector;
}

Vector Vector::sign()
{
Vector signVector;
signVector.value = sgn(this->value);
signVector.value = sgn(this->value);
signVector.value = sgn(this->value);
return signVector;
}

double Vector::dotProduct(Vector vector1, Vector vector2)
{
return ((vector1.value * vector2.value) + (vector1.value * vector2.value) + (vector1.value * vector2.value));
}