27 |
#include <math.h> |
#include <math.h> |
28 |
|
|
29 |
/** Simple vectors for 2D / 3D graphics. |
/** Simple vectors for 2D / 3D graphics. |
|
* XXX reorg (remove Point, Point3D, doc) |
|
30 |
*/ |
*/ |
31 |
namespace Vec23 { |
namespace Vec23 { |
32 |
using std::ostream; |
using std::ostream; |
33 |
|
|
34 |
template <class T>class Vector; |
template <class T>class Vector; |
35 |
template <class T>class Vector3; |
template <class T>class Vector3; |
|
template <class T>class Point3 ; |
|
|
|
|
|
template <class T>class Point { |
|
|
public: |
|
|
T x, y; |
|
|
Point() : x(0), y(0) { } |
|
|
Point(T x, T y) : x(x), y(y) { } |
|
|
Point(Point3<T> &p) : x(p.x), y(p.y) { } |
|
|
Vector<T> operator-(const Point &p) const { return Vector<T>(x - p.x, y - p.y); } |
|
|
Point<T> operator+(const Vector<T> v) |
|
|
const { return Point<T>(x + v.x, y + v.y); } |
|
|
Point<T> operator-(const Vector<T> v) |
|
|
const { return Point<T>(x - v.x, y - v.y); } |
|
|
Point<T> &operator+=(const Vector<T> v) { |
|
|
x += v.x; y += v.y; |
|
|
return *this; |
|
|
} |
|
|
Point<T> &operator-=(const Vector<T> v) { |
|
|
x -= v.x; y -= v.y; |
|
|
return *this; |
|
|
} |
|
|
}; |
|
|
template<class T> inline ostream& operator<<(ostream &o, const Point<T> &p) { |
|
|
return o << "[point "<<p.x<<" "<<p.y<<"]"; |
|
|
} |
|
|
|
|
|
template <class T>class Point3 { |
|
|
public: |
|
|
T x, y, z; |
|
|
Point3() : x(0), y(0), z(0) { } |
|
|
Point3(T x, T y, T z) : x(x), y(y), z(z) { } |
|
|
Point3(const Point<T> &p, T z = 0) : x(p.x), y(p.y), z(z) { } |
|
|
Point3(const Vector3<T> &v) : x(v.x), y(v.y), z(v.z) { } |
|
|
Vector3<T> operator-(const Point3 &p) const { |
|
|
return Vector3<T>(x - p.x, y - p.y, z - p.z); } |
|
|
Point3<T> operator+(const Vector3<T> v) |
|
|
const { return Point3<T>(x + v.x, y + v.y, z + v.z); } |
|
|
Point3<T> operator-(const Vector3<T> v) |
|
|
const { return Point3<T>(x - v.x, y - v.y, z - v.z); } |
|
|
Point3<T> &operator+=(const Vector3<T> v) { |
|
|
x += v.x; y += v.y; z += v.z; |
|
|
return *this; |
|
|
} |
|
|
Point3<T> &operator-=(const Vector3<T> v) { |
|
|
x -= v.x; y -= v.y; z -= v.z; |
|
|
return *this; |
|
|
} |
|
|
}; |
|
|
template<class T> inline ostream& operator<<(ostream &o, const Point3<T> &p) { |
|
|
return o << "[point "<<p.x<<" "<<p.y<<" "<<p.z<<"]"; |
|
|
} |
|
|
|
|
36 |
|
|
37 |
template <class T>class Vector { |
template <class T>class Vector { |
38 |
public: |
public: |
67 |
T x, y, z; |
T x, y, z; |
68 |
Vector3() : x(0), y(0), z(0) { } |
Vector3() : x(0), y(0), z(0) { } |
69 |
Vector3(T x, T y, T z) : x(x), y(y), z(z) { } |
Vector3(T x, T y, T z) : x(x), y(y), z(z) { } |
70 |
Vector3(const Vector<T> &v, float z) : x(v.x), y(v.y), z(z) { } |
Vector3(const Vector<T> &v, float z = 0) : x(v.x), y(v.y), z(z) { } |
|
Vector3(const Point3<T> &v) : x(v.x), y(v.y), z(v.z) { } |
|
71 |
Vector3 operator*(const double &s) const { return Vector3(s * x, s * y, s * z); } ; |
Vector3 operator*(const double &s) const { return Vector3(s * x, s * y, s * z); } ; |
72 |
|
|
73 |
template<class U> const Vector3 &operator*=(const U &s) { x *= s; y *= s; z *= s; |
template<class U> const Vector3 &operator*=(const U &s) { x *= s; y *= s; z *= s; |
113 |
T y0() { return y; } |
T y0() { return y; } |
114 |
T y1() { return y + h; } |
T y1() { return y + h; } |
115 |
|
|
116 |
Point<T> ul() { return Point<T>(x, y); } |
Vector<T> ul() { return Vector<T>(x, y); } |
117 |
Point<T> lr() { return Point<T>(x +w, y +h); } |
Vector<T> lr() { return Vector<T>(x +w, y +h); } |
118 |
}; |
}; |
119 |
|
|
120 |
template<class T>inline ostream& operator<<(ostream &o, const Rectangle<T> &r) { |
template<class T>inline ostream& operator<<(ostream &o, const Rectangle<T> &r) { |
122 |
} |
} |
123 |
|
|
124 |
|
|
125 |
typedef Point<float> Pt; |
typedef Vector<float> Pt; |
126 |
typedef Point3<float> ZPt; |
typedef Vector3<float> ZPt; |
127 |
typedef Vector<float> Vec; |
typedef Vector<float> Vec; |
128 |
typedef Vector3<float> ZVec; |
typedef Vector3<float> ZVec; |
129 |
|
|