Files
wagic/JGE/include/Vector3D.h
Tobias Loose 352e3c2daa Remove makros and add undefs to avoid clashing names
FRAND and Clamp were not used and Clamp collided when doing unit builds.
SQUARE(x) is longer than x*x and if x is an expression it gets evaluated
twice.
2013-11-23 16:03:04 +01:00

87 lines
1.6 KiB
C

#ifndef __VECTOR3D_H_
#define __VECTOR3D_H_
#include <math.h>
struct Vector3D
{
Vector3D(float x, float y, float z) : x(x), y(y), z(z) {}
Vector3D(const Vector3D &v) : x(v.x), y(v.y), z(v.z) {}
Vector3D() : x(0.0f), y(0.0f), z(0.0f) {}
Vector3D& operator=(const Vector3D &rhs)
{
x = rhs.x;
y = rhs.y;
z = rhs.z;
return *this;
}
// vector add
Vector3D operator+(const Vector3D &rhs) const
{
return Vector3D(x + rhs.x, y + rhs.y, z + rhs.z);
}
// vector subtract
Vector3D operator-(const Vector3D &rhs) const
{
return Vector3D(x - rhs.x, y - rhs.y, z - rhs.z);
}
// scalar multiplication
Vector3D operator*(const float scalar) const
{
return Vector3D(x * scalar, y * scalar, z * scalar);
}
// dot product
float operator*(const Vector3D &rhs) const
{
return x * rhs.x + y * rhs.y + z * rhs.z;
}
// cross product
Vector3D operator^(const Vector3D &rhs) const
{
return Vector3D(y * rhs.z - rhs.y * z, rhs.x * z - x * rhs.z, x * rhs.y - rhs.x * y);
}
float& operator[](int index)
{
return v[index];
}
float Length()
{
float length = sqrt(x*x + y*y + z*z);
return (length != 0.0f) ? length : 1.0f;
}
/*****************************************************************************
Normalize()
Helper function to normalize vectors
*****************************************************************************/
Vector3D Normalize()
{
*this = *this * (1.0f/Length());
return *this;
}
union
{
struct
{
float x;
float y;
float z;
};
float v[3];
};
};
#endif