Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008 #ifndef __SP_DIMENSION_POINT2D_H__
00009 #define __SP_DIMENSION_POINT2D_H__
00010
00011
00012 #include "Base/spStandard.hpp"
00013
00014 #include <math.h>
00015
00016
00017 namespace sp
00018 {
00019 namespace dim
00020 {
00021
00022
00023 template <typename T> class vector3d;
00024
00025 template <typename T> class point2d
00026 {
00027
00028 public:
00029
00030 point2d() :
00031 X(0),
00032 Y(0)
00033 {
00034 }
00035 point2d(T Size) :
00036 X(Size),
00037 Y(Size)
00038 {
00039 }
00040 point2d(T PointX, T PointY) :
00041 X(PointX),
00042 Y(PointY)
00043 {
00044 }
00045 point2d(const point2d<T> &other) :
00046 X(other.X),
00047 Y(other.Y)
00048 {
00049 }
00050 point2d(const vector3d<T> &other);
00051 ~point2d()
00052 {
00053 }
00054
00055
00056
00057 inline bool operator == (const point2d<T> &other) const
00058 {
00059 return X == other.X && Y == other.Y;
00060 }
00061 inline bool operator != (const point2d<T> &other) const
00062 {
00063 return X != other.X || Y != other.Y;
00064 }
00065
00066 inline bool operator > (const point2d<T> &other) const
00067 {
00068 return (X == other.X) ? Y > other.Y : X > other.X;
00069 }
00070 inline bool operator < (const point2d<T> &other) const
00071 {
00072 return (X == other.X) ? Y < other.Y : X < other.X;
00073 }
00074
00075 inline bool operator >= (const point2d<T> &other) const
00076 {
00077 return (X == other.X) ? Y >= other.Y : X >= other.X;
00078 }
00079 inline bool operator <= (const point2d<T> &other) const
00080 {
00081 return (X == other.X) ? Y <= other.Y : X <= other.X;
00082 }
00083
00084
00085
00086 inline point2d<T> operator + (const point2d<T> &other) const
00087 {
00088 return point2d<T>(X + other.X, Y + other.Y);
00089 }
00090 inline point2d<T>& operator += (const point2d<T> &other)
00091 {
00092 X += other.X; Y += other.Y; return *this;
00093 }
00094
00095 inline point2d<T> operator - (const point2d<T> &other) const
00096 {
00097 return point2d<T>(X - other.X, Y - other.Y);
00098 }
00099 inline point2d<T>& operator -= (const point2d<T> &other)
00100 {
00101 X -= other.X; Y -= other.Y; return *this;
00102 }
00103
00104 inline point2d<T> operator / (const point2d<T> &other) const
00105 {
00106 return point2d<T>(X / other.X, Y / other.Y);
00107 }
00108 inline point2d<T>& operator /= (const point2d<T> &other)
00109 {
00110 X /= other.X; Y /= other.Y; return *this;
00111 }
00112
00113 inline point2d<T> operator * (const point2d<T> &other) const
00114 {
00115 return point2d<T>(X * other.X, Y * other.Y);
00116 }
00117 inline point2d<T>& operator *= (const point2d<T> &other)
00118 {
00119 X *= other.X; Y *= other.Y; return *this;
00120 }
00121
00122 inline point2d<T> operator / (T Size) const
00123 {
00124 return point2d<T>(X / Size, Y / Size);
00125 }
00126 inline point2d<T>& operator /= (T Size)
00127 {
00128 X /= Size; Y /= Size; return *this;
00129 }
00130
00131 inline point2d<T> operator * (T Size) const
00132 {
00133 return point2d<T>(X * Size, Y * Size);
00134 }
00135 inline point2d<T>& operator *= (T Size)
00136 {
00137 X *= Size; Y *= Size; return *this;
00138 }
00139
00140 inline point2d<T> operator - () const
00141 {
00142 return point2d<T>(-X, -Y);
00143 }
00144
00145
00146
00147 inline const T operator [] (s32 i) const
00148 {
00149 switch (i)
00150 {
00151 case 0: return X;
00152 case 1: return Y;
00153 }
00154 return 0;
00155 }
00156
00157 inline T& operator [] (s32 i)
00158 {
00159 return *(&X + i);
00160 }
00161
00162
00163
00164 static inline void make3DProjection(T &NewX, T &NewY, s32 ScreenWidth, s32 ScreenHeight)
00165 {
00166 NewX = static_cast<T>( static_cast<f32>( NewX - ScreenWidth /2) / (ScreenWidth /2) );
00167 NewY = static_cast<T>( static_cast<f32>(- NewY + ScreenHeight/2) / (ScreenHeight/2) );
00168 }
00169
00170
00171
00172 inline T dot(const point2d<T> &other) const
00173 {
00174 return X*other.X + Y*other.Y;
00175 }
00176
00177 inline T getLength() const
00178 {
00179 return sqrt(X*X + Y*Y);
00180 }
00181 inline T getLengthSq() const
00182 {
00183 return X*X + Y*Y;
00184 }
00185
00186 inline void make3DProjection(s32 ScreenWidth, s32 ScreenHeight)
00187 {
00188 X = static_cast<T>( static_cast<f32>( X - ScreenWidth /2) / (ScreenWidth /2) );
00189 Y = static_cast<T>( static_cast<f32>(-Y + ScreenHeight/2) / (ScreenHeight/2) );
00190 }
00191
00192 inline void make3DProjectionOrigin(s32 ScreenWidth, s32 ScreenHeight)
00193 {
00194 X = static_cast<T>( static_cast<f32>(X) / (ScreenWidth /2) );
00195 Y = static_cast<T>( static_cast<f32>(Y) / (ScreenHeight/2) );
00196 }
00197
00198 inline void make3DFrustum(f32 Width, f32 Height)
00199 {
00200 const f32 aspect = Width / Height;
00201 const f32 stdasp = 4.0f / 3.0f;
00202
00203 X = static_cast<T>( f32( X - Width /2) / (Width/2) * aspect / stdasp );
00204 Y = static_cast<T>( f32(-Y + Height/2) / (Width/2) * aspect / stdasp );
00205 }
00206
00207 inline point2d<T>& setAbs()
00208 {
00209 X = X > 0 ? X : -X;
00210 Y = Y > 0 ? Y : -Y;
00211 return *this;
00212 }
00213 inline point2d<T> getAbs() const
00214 {
00215 return point2d<T>(
00216 X > 0 ? X : -X,
00217 Y > 0 ? Y : -Y
00218 );
00219 }
00220
00221 inline point2d<T>& normalize()
00222 {
00223 T n = X*X + Y*Y;
00224
00225 if (n == 1 || n == 0)
00226 return *this;
00227
00228 n = static_cast<T>(1.0 / sqrt(n));
00229
00230 X *= n;
00231 Y *= n;
00232
00233 return *this;
00234 }
00235
00236 inline void set(T NewX, T NewY)
00237 {
00238 X = NewX; Y = NewY;
00239 }
00240 inline void get(T &NewX, T &NewY) const
00241 {
00242 NewX = X; NewY = Y;
00243 }
00244
00245 inline void setLength(T Length)
00246 {
00247 normalize();
00248 *this *= Length;
00249 }
00250
00251 inline point2d<T>& getCircleCollision(f32 ThisRadius, point2d<T> &OtherPoint, f32 OtherRadius)
00252 {
00253 f32 Distance = sqrt( (OtherPoint.X - X)*(OtherPoint.X - X) + (OtherPoint.Y - Y)*(OtherPoint.Y - Y) );
00254 f32 Degree = asin( (OtherPoint.X - X) / Distance )*180.0f/M_PI;
00255
00256 if (Y < OtherPoint.Y)
00257 Degree = 180 - Degree;
00258
00259 if (Distance < ThisRadius + OtherRadius) {
00260 OtherPoint.X = X + sin(Degree*M_PI/180) * (ThisRadius + OtherRadius);
00261 OtherPoint.Y = Y + cos(Degree*M_PI/180) * (ThisRadius + OtherRadius);
00262 }
00263
00264 return OtherPoint;
00265 }
00266
00267 inline bool isPointInsideCircle(const point2d<T> &Center, const f32 Radius) const
00268 {
00269 return (X - Center.X)*(X - Center.X) + (Y - Center.Y)*(Y - Center.Y) < Radius*Radius;
00270 }
00271
00272 inline T getMin() const
00273 {
00274 return (X <= Y) ? X : Y;
00275 }
00276 inline T getMax() const
00277 {
00278 return (X >= Y) ? X : Y;
00279 }
00280
00281 template <typename B> inline point2d<B> cast() const
00282 {
00283 return point2d<B>(static_cast<B>(X), static_cast<B>(Y));
00284 }
00285
00286
00287
00288 T X, Y;
00289
00290 };
00291
00292 typedef point2d<s32> point2di;
00293 typedef point2d<f32> point2df;
00294
00295
00296 }
00297
00298 }
00299
00300
00301 #endif
00302
00303
00304
00305