39 |
T x, y; |
T x, y; |
40 |
Vector() : x(0), y(0) { } |
Vector() : x(0), y(0) { } |
41 |
Vector(T x, T y) : x(x), y(y) { } |
Vector(T x, T y) : x(x), y(y) { } |
42 |
Vector(Vector3<T> &v) : x(v.x), y(v.y) { } |
template<class F> |
43 |
|
Vector(const Vector3<F> &v) : x(v.x), y(v.y) { } |
44 |
Vector operator*(const double &s) const { return Vector(s * x, s * y); } |
Vector operator*(const double &s) const { return Vector(s * x, s * y); } |
45 |
template<class U>const Vector &operator*=(const U &s) { x *= s; y *= s; return *this; } |
template<class U>const Vector &operator*=(const U &s) { x *= s; y *= s; return *this; } |
46 |
Vector normalize() { return (1/length()) * *this ; } |
Vector normalize() { return (1/length()) * *this ; } |
71 |
Vector3(const Vector<T> &v, float z = 0) : x(v.x), y(v.y), z(z) { } |
Vector3(const Vector<T> &v, float z = 0) : x(v.x), y(v.y), z(z) { } |
72 |
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); } ; |
73 |
|
|
74 |
template<class U> const Vector3 &operator*=(const U &s) { x *= s; y *= s; z *= s; |
template<class U> const Vector3 &operator*=(const U &s) { |
75 |
return *this; } ; |
x *= s; y *= s; z *= s; |
76 |
|
return *this; |
77 |
|
} ; |
78 |
|
template<class U> const Vector3 &operator/=(const U &s) { |
79 |
|
x /= s; y /= s; z /= s; |
80 |
|
return *this; |
81 |
|
} ; |
82 |
Vector3 normalize() const { return (1/length()) * *this ; } |
Vector3 normalize() const { return (1/length()) * *this ; } |
83 |
Vector3<T> operator-() const { return Vector3<T>(-x, -y, -z); } |
Vector3<T> operator-() const { return Vector3<T>(-x, -y, -z); } |
84 |
Vector3 operator+(const Vector3<T>&v) const { return Vector3(x+v.x, y+v.y, z+v.z); } |
Vector3 operator+(const Vector3<T>&v) const { return Vector3(x+v.x, y+v.y, z+v.z); } |
138 |
return a + fract * (b-a); |
return a + fract * (b-a); |
139 |
} |
} |
140 |
|
|
141 |
|
|
142 |
|
|
143 |
|
template<class T> void cross3(T x1, T y1, T z1, T x2, T y2, T z2, T &xr, T &yr, T &zr) { |
144 |
|
xr = y1 * z2 - y2 * z1; |
145 |
|
yr = z1 * x2 - z2 * x1; |
146 |
|
zr = x1 * y2 - x2 * y1; |
147 |
|
} |
148 |
|
|
149 |
|
#define VEC23_EPS 0.000001 |
150 |
|
|
151 |
|
template<class T> class HLine2; |
152 |
|
|
153 |
|
/** A homogeneous 2D point. |
154 |
|
* Useful for computing: no fear of bad divisions! |
155 |
|
*/ |
156 |
|
template<class T> struct HPoint2 { |
157 |
|
T x, y, w; |
158 |
|
HPoint2() {} |
159 |
|
HPoint2(T x, T y, T w) : x(x), y(y), w(w) { } |
160 |
|
HPoint2(ZPt p) : x(p.x), y(p.y), w(1) { } |
161 |
|
|
162 |
|
HLine2<T> line(HPoint2 p) { |
163 |
|
T a, b, c; |
164 |
|
cross3(x, y, w, p.x, p.y, p.w, a, b, c); |
165 |
|
return HLine2<T>(a, b, c); |
166 |
|
} |
167 |
|
|
168 |
|
bool finite() { |
169 |
|
T r = fabs(x) + fabs(y); |
170 |
|
T i = fabs(w); |
171 |
|
if(i < r * VEC23_EPS) return false; |
172 |
|
return true; |
173 |
|
} |
174 |
|
|
175 |
|
operator Vector<T> () { |
176 |
|
return Vector<T>(x/w, y/w); |
177 |
|
} |
178 |
|
|
179 |
|
}; |
180 |
|
template<class T> inline ostream& operator<<(ostream &o, const HPoint2<T> &p) { |
181 |
|
return o << "[2hv "<<p.x<<" "<<p.y<<" "<<p.w<<"]"; |
182 |
|
}; |
183 |
|
|
184 |
|
/** A homogeneous 2D line. |
185 |
|
*/ |
186 |
|
template<class T> struct HLine2 { |
187 |
|
T a, b, c; |
188 |
|
HLine2() {} |
189 |
|
HLine2(T a, T b, T c) : a(a), b(b), c(c) { } |
190 |
|
|
191 |
|
HPoint2<T> intersection(HLine2 l) { |
192 |
|
T x, y, w; |
193 |
|
cross3(a, b, c, l.a, l.b, l.c, x, y, w); |
194 |
|
return HPoint2<T>(x, y, w); |
195 |
|
} |
196 |
|
|
197 |
|
bool finite() { |
198 |
|
T r = fabs(a) + fabs(b); |
199 |
|
T i = fabs(c); |
200 |
|
if(r < i * VEC23_EPS) return false; |
201 |
|
return true; |
202 |
|
} |
203 |
|
|
204 |
|
}; |
205 |
|
template<class T> inline ostream& operator<<(ostream &o, const HLine2<T> &p) { |
206 |
|
return o << "[2hl "<<p.a<<" "<<p.b<<" "<<p.c<<"]"; |
207 |
|
}; |
208 |
|
|
209 |
|
typedef HPoint2<float> HPt; |
210 |
|
typedef HLine2<float> HL; |
211 |
|
|
212 |
} |
} |
213 |
#endif |
#endif |