DataTypes.cpp

#include "DataTypes.h"
#include <math.h>

Vector3::Vector3()
{
    x=0;
    y=0;
    z=0;
}
Vector3::Vector3(float fx, float fy, float fz)
{
    x=fx;
    y=fy;
    z=fz;
}
float Vector3::Length()
{
    return sqrt(x*x + y*y + z*z);
}

float Vector3::DistanceTo(Vector3 Target)
{
    return Vector3(fabs(Target.x - this->x), fabs(Target.y - this->y), fabs(Target.z - this->z)).Length();
}

Vector3 Vector3::MidPoint(Vector3 End)
{
    Vector3 mid;
    if(x == End.x)
    {
        mid.x = x;
    } else if(x < End.x) {
        mid.x = x + (End.x - x)/2;
    } else {
        mid.x = End.x + (x - End.x)/2;
    }
    if(y == End.y)
    {
        mid.y = y;
    } else if(y < End.y) {
        mid.y = y + (End.y - y)/2;
    } else {
        mid.y = End.y + (y - End.y)/2;
    }
    if(z == End.z)
    {
        mid.z = z;
    } else if(z < End.z) {
        mid.z = z + (End.z - z)/2;
    } else {
        mid.z = End.z + (z - End.z)/2;
    }
    return mid;
}
Vector3 Vector3::Normalize()
{
    if(Length() == 0) return Vector3(0,0,0);
    return Vector3(x/Length(),y/Length(),z/Length());
}
Vector3 Vector3::operator=(Vector3 v)
{
    x = v.x;
    y = v.y;
    z = v.z;
    return *this;
}
Vector3 Vector3::operator+(Vector3 v)
{
    return Vector3(x+v.x, y+v.y, z+v.y);
}
Vector3 Vector3::operator-(Vector3 v)
{
    return Vector3(x-v.x, y-v.y, z-v.y);
}
Vector3 Vector3::operator*(Vector3 v)
{
    return Vector3(x*v.x, y*v.y, z*v.y);
}
Vector3 Vector3::operator/(Vector3 v)
{
    return Vector3(x/v.x, y/v.y, z/v.y);
}
Vector3 Vector3::operator*(float f)
{
    return Vector3(x*f, y*f, z*f);
}
Vector3 Vector3::operator/(float f)
{
    return Vector3(x/f, y/f, z/f);
}
Vector3 Vector3::operator*(int i)
{
    return Vector3(x*i, y*i, z*i);
}
Vector3 Vector3::operator/(int i)
{
    return Vector3(x/i, y/i, z/i);
}
bool Vector3::operator==(Vector3 v)
{
    return v.x == x && v.y == y && v.z == z;
}
bool Vector3::operator!=(Vector3 v)
{
    return !(v.x == x && v.y == y && v.z == z);
}

Vector2::Vector2()
{
    x = 0;
    y = 0;
}
Vector2::Vector2(float fx, float fy)
{
    x=fx;
    y=fy;
}
float Vector2::Length()
{
    return sqrt(x*x + y*y);
}
float Vector2::DistanceTo(Vector2 Target)
{
    return Vector2(fabs(Target.x - this->x), fabs(Target.y - this->y)).Length();
}
Vector2 Vector2::MidPoint(Vector2 End)
{
    Vector2 mid;
    if(x == End.x)
    {
        mid.x = x;
    } else if(x < End.x) {
        mid.x = x + (End.x - x)/2;
    } else {
        mid.x = End.x + (x - End.x)/2;
    }
    if(y == End.y)
    {
        mid.y = y;
    } else if(y < End.y) {
        mid.y = y + (End.y - y)/2;
    } else {
        mid.y = End.y + (y - End.y)/2;
    }
    return mid;
}
Vector2 Vector2::Normalize()
{
    if(Length() == 0) return Vector2(0,0);
    return Vector2(x/Length(),y/Length());
}
Vector2 Vector2::operator=(Vector2 v)
{
    x = v.x;
    y = v.y;
    return *this;
}
Vector2 Vector2::operator+(Vector2 v)
{
    return Vector2(x + v.x, y + v.y);
}
Vector2 Vector2::operator-(Vector2 v)
{
    return Vector2(x - v.x, y - v.y);
}
Vector2 Vector2::operator*(Vector2 v)
{
    return Vector2(x * v.x, y * v.y);
}
Vector2 Vector2::operator/(Vector2 v)
{
    return Vector2(x / v.x, y / v.y);
}
Vector2 Vector2::operator*(float f)
{
    return Vector2(x * f, y * f);
}
Vector2 Vector2::operator/(float f)
{
    return Vector2(x / f, y / f);
}
Vector2 Vector2::operator*(int i)
{
    return Vector2(x * i, y * i);
}
Vector2 Vector2::operator/(int i)
{
    return Vector2(x / i, y / i);
}
bool Vector2::operator==(Vector2 v)
{
    return v.x == x && v.y == y;
}
bool Vector2::operator!=(Vector2 v)
{
    return !(v.x == x && v.y == y);
}