1
0
forked from 0ad/0ad
0ad/source/maths/Vector2D.h
janwas 5814e10126 # complete revamp of build system in preparation for automated self tests.
* now splits everything up into independent static libraries.
* fixed a great deal of incorrect #include statements. all headers must
now be specified with their full path relative to source. exception: if
file being included and including file are in the same directory, no
path needed.
use <> when relying on the build system's include path (e.g. for system
headers and external libraries, e.g. boost), otherwise "".

* temporarily renamed maths/Vector2D to Vector2D_Maths to avoid
conflict. these should be merged.
* hacked around VC linker stupidness when building static libs; texture
codecs must now be registered manually.

This was SVN commit r3931.
2006-06-02 03:56:24 +00:00

100 lines
1.9 KiB
C++

//***********************************************************
//
// Name: Vector2D.h
// Author: Matei Zaharia
//
// Description: Provides an interface for a vector in R4 and
// allows vector and scalar operations on it
//
//***********************************************************
#ifndef VECTOR2D_H
#define VECTOR2D_H
#include <math.h>
///////////////////////////////////////////////////////////////////////////////
// CVector2D_Maths:
class CVector2D_Maths
{
public:
CVector2D_Maths() {}
CVector2D_Maths(float x,float y) { X=x; Y=y; }
CVector2D_Maths(const CVector2D_Maths& p) { X=p.X; Y=p.Y; }
operator float*() {
return &X;
}
operator const float*() const {
return &X;
}
CVector2D_Maths operator-() const {
return CVector2D_Maths(-X, -Y);
}
CVector2D_Maths operator+(const CVector2D_Maths& t) const {
return CVector2D_Maths(X+t.X, Y+t.Y);
}
CVector2D_Maths operator-(const CVector2D_Maths& t) const {
return CVector2D_Maths(X-t.X, Y-t.Y);
}
CVector2D_Maths operator*(float f) const {
return CVector2D_Maths(X*f, Y*f);
}
CVector2D_Maths operator/(float f) const {
float inv=1.0f/f;
return CVector2D_Maths(X*inv, Y*inv);
}
CVector2D_Maths& operator+=(const CVector2D_Maths& t) {
X+=t.X; Y+=t.Y;
return *this;
}
CVector2D_Maths& operator-=(const CVector2D_Maths& t) {
X-=t.X; Y-=t.Y;
return *this;
}
CVector2D_Maths& operator*=(float f) {
X*=f; Y*=f;
return *this;
}
CVector2D_Maths& operator/=(float f) {
float invf=1.0f/f;
X*=invf; Y*=invf;
return *this;
}
float Dot(const CVector2D_Maths& a) const {
return X*a.X + Y*a.Y;
}
float LengthSquared() const {
return Dot(*this);
}
float Length() const {
return (float) sqrt(LengthSquared());
}
void Normalize() {
float mag=Length();
X/=mag; Y/=mag;
}
public:
float X, Y;
};
//////////////////////////////////////////////////////////////////////////////////
#endif