mPoint2.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _MPOINT2_H_
  23. #define _MPOINT2_H_
  24. #ifndef _MMATHFN_H_
  25. #include "math/mMathFn.h"
  26. #endif
  27. //------------------------------------------------------------------------------
  28. /// 2D integer point
  29. ///
  30. /// Uses S32 internally.
  31. class Point2I
  32. {
  33. //-------------------------------------- Public data
  34. public:
  35. S32 x; ///< X position
  36. S32 y; ///< Y position
  37. //-------------------------------------- Public interface
  38. public:
  39. Point2I(); ///< Create an uninitialized point.
  40. Point2I(const Point2I&); ///< Copy constructor
  41. Point2I(S32 in_x, S32 in_y); ///< Create point from two co-ordinates.
  42. //-------------------------------------- Non-math mutators and misc functions
  43. void set(S32 in_x, S32 in_y); ///< Set (x,y) position
  44. void setMin(const Point2I&); ///< Store lesser co-ordinates from parameter in this point.
  45. void setMax(const Point2I&); ///< Store greater co-ordinates from parameter in this point.
  46. void zero(); ///< Zero all values
  47. //-------------------------------------- Math mutators
  48. void neg(); ///< Invert sign of point's co-ordinates.
  49. void convolve(const Point2I&); ///< Convolve this point by parameter.
  50. //-------------------------------------- Queries
  51. bool isZero() const; ///< Is this point at the origin? (No epsilon used)
  52. F32 len() const; ///< Get the length of the point
  53. S32 lenSquared() const; ///< Get the length-squared of the point
  54. //-------------------------------------- Overloaded operators
  55. public:
  56. operator S32*() { return &x; }
  57. operator const S32*() const { return &x; }
  58. // Comparison operators
  59. bool operator==(const Point2I&) const;
  60. bool operator!=(const Point2I&) const;
  61. // Arithmetic w/ other points
  62. Point2I operator+(const Point2I&) const;
  63. Point2I operator-(const Point2I&) const;
  64. Point2I& operator+=(const Point2I&);
  65. Point2I& operator-=(const Point2I&);
  66. // Arithmetic w/ scalars
  67. Point2I operator*(S32) const;
  68. Point2I& operator*=(S32);
  69. Point2I operator/(S32) const;
  70. Point2I& operator/=(S32);
  71. Point2I operator*(const Point2I&) const;
  72. Point2I& operator*=(const Point2I&);
  73. Point2I operator/(const Point2I&) const;
  74. Point2I& operator/=(const Point2I&);
  75. // Unary operators
  76. Point2I operator-() const;
  77. //-------------------------------------- Public static constants
  78. public:
  79. const static Point2I One;
  80. const static Point2I Zero;
  81. const static Point2I Min;
  82. const static Point2I Max;
  83. };
  84. //------------------------------------------------------------------------------
  85. /// 2D floating-point point.
  86. class Point2F
  87. {
  88. //-------------------------------------- Public data
  89. public:
  90. F32 x;
  91. F32 y;
  92. public:
  93. Point2F(); ///< Create uninitialized point.
  94. Point2F(const Point2F&); ///< Copy constructor
  95. Point2F(F32 _x, F32 _y); ///< Create point from co-ordinates.
  96. //-------------------------------------- Non-math mutators and misc functions
  97. public:
  98. void set(F32 _x, F32 _y); ///< Set point's co-ordinates.
  99. void setMin(const Point2F&); ///< Store lesser co-ordinates.
  100. void setMax(const Point2F&); ///< Store greater co-ordinates.
  101. /// Interpolate from a to b, based on c.
  102. ///
  103. /// @param a Starting point.
  104. /// @param b Ending point.
  105. /// @param c Interpolation factor (0.0 .. 1.0).
  106. void interpolate(const Point2F& a, const Point2F& b, const F32 c);
  107. void zero(); ///< Zero all values
  108. operator F32*() { return &x; }
  109. operator const F32*() const { return &x; }
  110. //-------------------------------------- Queries
  111. public:
  112. bool isZero() const; ///< Check for zero coordinates. (No epsilon.)
  113. F32 len() const; ///< Get length.
  114. F32 lenSquared() const; ///< Get squared length (one sqrt less than len()).
  115. bool equal( const Point2F &compare ) const; ///< Is compare within POINT_EPSILON of all of the component of this point
  116. F32 magnitudeSafe() const;
  117. //-------------------------------------- Mathematical mutators
  118. public:
  119. void neg(); ///< Invert signs of co-ordinates.
  120. void normalize(); ///< Normalize vector.
  121. void normalize(F32 val); ///< Normalize, scaling by val.
  122. void normalizeSafe();
  123. void convolve(const Point2F&); ///< Convolve by parameter.
  124. void convolveInverse(const Point2F&); ///< Inversely convolute by parameter. (ie, divide)
  125. void rotate( F32 radians ); ///< Rotate vector around origin.
  126. /// Return a perpendicular vector to this one. The result is equivalent to rotating the
  127. /// vector 90 degrees clockwise around the origin.
  128. Point2F getPerpendicular() const { return Point2F( y, - x ); }
  129. //-------------------------------------- Overloaded operators
  130. public:
  131. // Comparison operators
  132. bool operator==(const Point2F&) const;
  133. bool operator!=(const Point2F&) const;
  134. // Arithmetic w/ other points
  135. Point2F operator+(const Point2F&) const;
  136. Point2F operator-(const Point2F&) const;
  137. Point2F& operator+=(const Point2F&);
  138. Point2F& operator-=(const Point2F&);
  139. // Arithmetic w/ scalars
  140. Point2F operator*(F32) const;
  141. Point2F operator/(F32) const;
  142. Point2F& operator*=(F32);
  143. Point2F& operator/=(F32);
  144. Point2F operator*(const Point2F&) const;
  145. Point2F& operator*=(const Point2F&);
  146. Point2F operator/(const Point2F&) const;
  147. Point2F& operator/=(const Point2F&);
  148. // Unary operators
  149. Point2F operator-() const;
  150. //-------------------------------------- Public static constants
  151. public:
  152. const static Point2F One;
  153. const static Point2F Zero;
  154. const static Point2F Min;
  155. const static Point2F Max;
  156. };
  157. //------------------------------------------------------------------------------
  158. /// 2D high-precision point.
  159. ///
  160. /// Uses F64 internally.
  161. class Point2D
  162. {
  163. //-------------------------------------- Public data
  164. public:
  165. F64 x; ///< X co-ordinate.
  166. F64 y; ///< Y co-ordinate.
  167. public:
  168. Point2D(); ///< Create uninitialized point.
  169. Point2D(const Point2D&); ///< Copy constructor
  170. Point2D(F64 _x, F64 _y); ///< Create point from coordinates.
  171. //-------------------------------------- Non-math mutators and misc functions
  172. public:
  173. void set(F64 _x, F64 _y); ///< Set point's coordinates.
  174. void setMin(const Point2D&); ///< Store lesser co-ordinates.
  175. void setMax(const Point2D&); ///< Store greater co-ordinates.
  176. /// Interpolate from a to b, based on c.
  177. ///
  178. /// @param a Starting point.
  179. /// @param b Ending point.
  180. /// @param c Interpolation factor (0.0 .. 1.0).
  181. void interpolate(const Point2D &a, const Point2D &b, const F64 c);
  182. void zero(); ///< Zero all values
  183. operator F64*() { return &x; }
  184. operator const F64*() const { return &x; }
  185. //-------------------------------------- Queries
  186. public:
  187. bool isZero() const;
  188. F64 len() const;
  189. F64 lenSquared() const;
  190. //-------------------------------------- Mathematical mutators
  191. public:
  192. void neg();
  193. void normalize();
  194. void normalize(F64 val);
  195. void convolve(const Point2D&);
  196. void convolveInverse(const Point2D&);
  197. //-------------------------------------- Overloaded operators
  198. public:
  199. // Comparison operators
  200. bool operator==(const Point2D&) const;
  201. bool operator!=(const Point2D&) const;
  202. // Arithmetic w/ other points
  203. Point2D operator+(const Point2D&) const;
  204. Point2D operator-(const Point2D&) const;
  205. Point2D& operator+=(const Point2D&);
  206. Point2D& operator-=(const Point2D&);
  207. // Arithmetic w/ scalars
  208. Point2D operator*(F64) const;
  209. Point2D operator/(F64) const;
  210. Point2D& operator*=(F64);
  211. Point2D& operator/=(F64);
  212. // Unary operators
  213. Point2D operator-() const;
  214. //-------------------------------------- Public static constants
  215. public:
  216. const static Point2D One;
  217. const static Point2D Zero;
  218. };
  219. //------------------------------------------------------------------------------
  220. //-------------------------------------- Point2I
  221. //
  222. inline Point2I::Point2I()
  223. :x(0),y(0)
  224. {
  225. }
  226. inline Point2I::Point2I(const Point2I& _copy)
  227. : x(_copy.x), y(_copy.y)
  228. {
  229. //
  230. }
  231. inline Point2I::Point2I(S32 _x, S32 _y)
  232. : x(_x), y(_y)
  233. {
  234. //
  235. }
  236. inline void Point2I::set(S32 _x, S32 _y)
  237. {
  238. x = _x;
  239. y = _y;
  240. }
  241. inline void Point2I::setMin(const Point2I& _test)
  242. {
  243. x = (_test.x < x) ? _test.x : x;
  244. y = (_test.y < y) ? _test.y : y;
  245. }
  246. inline void Point2I::setMax(const Point2I& _test)
  247. {
  248. x = (_test.x > x) ? _test.x : x;
  249. y = (_test.y > y) ? _test.y : y;
  250. }
  251. inline void Point2I::zero()
  252. {
  253. x = y = 0;
  254. }
  255. inline void Point2I::neg()
  256. {
  257. x = -x;
  258. y = -y;
  259. }
  260. inline void Point2I::convolve(const Point2I& c)
  261. {
  262. x *= c.x;
  263. y *= c.y;
  264. }
  265. inline bool Point2I::isZero() const
  266. {
  267. return ((x == 0) && (y == 0));
  268. }
  269. inline F32 Point2I::len() const
  270. {
  271. return mSqrt(F32(x*x + y*y));
  272. }
  273. inline S32 Point2I::lenSquared() const
  274. {
  275. return x*x + y*y;
  276. }
  277. inline bool Point2I::operator==(const Point2I& _test) const
  278. {
  279. return ((x == _test.x) && (y == _test.y));
  280. }
  281. inline bool Point2I::operator!=(const Point2I& _test) const
  282. {
  283. return (operator==(_test) == false);
  284. }
  285. inline Point2I Point2I::operator+(const Point2I& _add) const
  286. {
  287. return Point2I(x + _add.x, y + _add.y);
  288. }
  289. inline Point2I Point2I::operator-(const Point2I& _rSub) const
  290. {
  291. return Point2I(x - _rSub.x, y - _rSub.y);
  292. }
  293. inline Point2I& Point2I::operator+=(const Point2I& _add)
  294. {
  295. x += _add.x;
  296. y += _add.y;
  297. return *this;
  298. }
  299. inline Point2I& Point2I::operator-=(const Point2I& _rSub)
  300. {
  301. x -= _rSub.x;
  302. y -= _rSub.y;
  303. return *this;
  304. }
  305. inline Point2I Point2I::operator-() const
  306. {
  307. return Point2I(-x, -y);
  308. }
  309. inline Point2I Point2I::operator*(S32 mul) const
  310. {
  311. return Point2I(x * mul, y * mul);
  312. }
  313. inline Point2I Point2I::operator/(S32 div) const
  314. {
  315. AssertFatal(div != 0, "Error, div by zero attempted");
  316. return Point2I(x/div, y/div);
  317. }
  318. inline Point2I& Point2I::operator*=(S32 mul)
  319. {
  320. x *= mul;
  321. y *= mul;
  322. return *this;
  323. }
  324. inline Point2I& Point2I::operator/=(S32 div)
  325. {
  326. AssertFatal(div != 0, "Error, div by zero attempted");
  327. x /= div;
  328. y /= div;
  329. return *this;
  330. }
  331. inline Point2I Point2I::operator*(const Point2I &_vec) const
  332. {
  333. return Point2I(x * _vec.x, y * _vec.y);
  334. }
  335. inline Point2I& Point2I::operator*=(const Point2I &_vec)
  336. {
  337. x *= _vec.x;
  338. y *= _vec.y;
  339. return *this;
  340. }
  341. inline Point2I Point2I::operator/(const Point2I &_vec) const
  342. {
  343. return Point2I(x / _vec.x, y / _vec.y);
  344. }
  345. inline Point2I& Point2I::operator/=(const Point2I &_vec)
  346. {
  347. AssertFatal(_vec.x != 0 && _vec.y != 0, "Error, div by zero attempted");
  348. x /= _vec.x;
  349. y /= _vec.y;
  350. return *this;
  351. }
  352. //------------------------------------------------------------------------------
  353. //-------------------------------------- Point2F
  354. //
  355. inline Point2F::Point2F()
  356. :x(0.0f), y(0.0f)
  357. {
  358. }
  359. inline Point2F::Point2F(const Point2F& _copy)
  360. : x(_copy.x), y(_copy.y)
  361. {
  362. //
  363. }
  364. inline Point2F::Point2F(F32 _x, F32 _y)
  365. : x(_x), y(_y)
  366. {
  367. }
  368. inline void Point2F::set(F32 _x, F32 _y)
  369. {
  370. x = _x;
  371. y = _y;
  372. }
  373. inline void Point2F::setMin(const Point2F& _test)
  374. {
  375. x = (_test.x < x) ? _test.x : x;
  376. y = (_test.y < y) ? _test.y : y;
  377. }
  378. inline void Point2F::setMax(const Point2F& _test)
  379. {
  380. x = (_test.x > x) ? _test.x : x;
  381. y = (_test.y > y) ? _test.y : y;
  382. }
  383. inline void Point2F::interpolate(const Point2F& _rFrom, const Point2F& _to, const F32 _factor)
  384. {
  385. AssertFatal(_factor >= 0.0f && _factor <= 1.0f, "Out of bound interpolation factor");
  386. x = (_rFrom.x * (1.0f - _factor)) + (_to.x * _factor);
  387. y = (_rFrom.y * (1.0f - _factor)) + (_to.y * _factor);
  388. }
  389. inline void Point2F::zero()
  390. {
  391. x = y = 0.0f;
  392. }
  393. inline bool Point2F::isZero() const
  394. {
  395. return (x == 0.0f) && (y == 0.0f);
  396. }
  397. inline F32 Point2F::lenSquared() const
  398. {
  399. return (x * x) + (y * y);
  400. }
  401. inline bool Point2F::equal( const Point2F &compare ) const
  402. {
  403. return( ( mFabs( x - compare.x ) < POINT_EPSILON ) &&
  404. ( mFabs( y - compare.y ) < POINT_EPSILON ) );
  405. }
  406. inline void Point2F::neg()
  407. {
  408. x = -x;
  409. y = -y;
  410. }
  411. inline void Point2F::convolve(const Point2F& c)
  412. {
  413. x *= c.x;
  414. y *= c.y;
  415. }
  416. inline void Point2F::convolveInverse(const Point2F& c)
  417. {
  418. x /= c.x;
  419. y /= c.y;
  420. }
  421. inline void Point2F::rotate( F32 radians )
  422. {
  423. F32 sinTheta, cosTheta;
  424. mSinCos( radians, sinTheta, cosTheta );
  425. set( cosTheta * x - sinTheta * y,
  426. sinTheta * x + cosTheta * y );
  427. }
  428. inline bool Point2F::operator==(const Point2F& _test) const
  429. {
  430. return (x == _test.x) && (y == _test.y);
  431. }
  432. inline bool Point2F::operator!=(const Point2F& _test) const
  433. {
  434. return operator==(_test) == false;
  435. }
  436. inline Point2F Point2F::operator+(const Point2F& _add) const
  437. {
  438. return Point2F(x + _add.x, y + _add.y);
  439. }
  440. inline Point2F Point2F::operator-(const Point2F& _rSub) const
  441. {
  442. return Point2F(x - _rSub.x, y - _rSub.y);
  443. }
  444. inline Point2F& Point2F::operator+=(const Point2F& _add)
  445. {
  446. x += _add.x;
  447. y += _add.y;
  448. return *this;
  449. }
  450. inline Point2F& Point2F::operator-=(const Point2F& _rSub)
  451. {
  452. x -= _rSub.x;
  453. y -= _rSub.y;
  454. return *this;
  455. }
  456. inline Point2F Point2F::operator*(F32 _mul) const
  457. {
  458. return Point2F(x * _mul, y * _mul);
  459. }
  460. inline Point2F Point2F::operator/(F32 _div) const
  461. {
  462. AssertFatal(_div != 0.0f, "Error, div by zero attempted");
  463. F32 inv = 1.0f / _div;
  464. return Point2F(x * inv, y * inv);
  465. }
  466. inline Point2F& Point2F::operator*=(F32 _mul)
  467. {
  468. x *= _mul;
  469. y *= _mul;
  470. return *this;
  471. }
  472. inline Point2F& Point2F::operator/=(F32 _div)
  473. {
  474. AssertFatal(_div != 0.0f, "Error, div by zero attempted");
  475. F32 inv = 1.0f / _div;
  476. x *= inv;
  477. y *= inv;
  478. return *this;
  479. }
  480. inline Point2F Point2F::operator*(const Point2F &_vec) const
  481. {
  482. return Point2F(x * _vec.x, y * _vec.y);
  483. }
  484. inline Point2F& Point2F::operator*=(const Point2F &_vec)
  485. {
  486. x *= _vec.x;
  487. y *= _vec.y;
  488. return *this;
  489. }
  490. inline Point2F Point2F::operator/(const Point2F &_vec) const
  491. {
  492. return Point2F(x / _vec.x, y / _vec.y);
  493. }
  494. inline Point2F& Point2F::operator/=(const Point2F &_vec)
  495. {
  496. AssertFatal(_vec.x != 0 && _vec.y != 0, "Error, div by zero attempted");
  497. x /= _vec.x;
  498. y /= _vec.y;
  499. return *this;
  500. }
  501. inline Point2F Point2F::operator-() const
  502. {
  503. return Point2F(-x, -y);
  504. }
  505. inline F32 Point2F::len() const
  506. {
  507. return mSqrt(x*x + y*y);
  508. }
  509. inline void Point2F::normalize()
  510. {
  511. m_point2F_normalize(*this);
  512. }
  513. inline void Point2F::normalize(F32 val)
  514. {
  515. m_point2F_normalize_f(*this, val);
  516. }
  517. inline F32 Point2F::magnitudeSafe() const
  518. {
  519. if( isZero() )
  520. return 0.0f;
  521. else
  522. return len();
  523. }
  524. inline void Point2F::normalizeSafe()
  525. {
  526. F32 vmag = magnitudeSafe();
  527. if( vmag > POINT_EPSILON )
  528. *this *= F32(1.0 / vmag);
  529. }
  530. //------------------------------------------------------------------------------
  531. //-------------------------------------- Point2D
  532. //
  533. inline Point2D::Point2D()
  534. :x(0.0), y(0.0)
  535. {
  536. }
  537. inline Point2D::Point2D(const Point2D& _copy)
  538. : x(_copy.x), y(_copy.y)
  539. {
  540. //
  541. }
  542. inline Point2D::Point2D(F64 _x, F64 _y)
  543. : x(_x), y(_y)
  544. {
  545. }
  546. inline void Point2D::set(F64 _x, F64 _y)
  547. {
  548. x = _x;
  549. y = _y;
  550. }
  551. inline void Point2D::setMin(const Point2D& _test)
  552. {
  553. x = (_test.x < x) ? _test.x : x;
  554. y = (_test.y < y) ? _test.y : y;
  555. }
  556. inline void Point2D::setMax(const Point2D& _test)
  557. {
  558. x = (_test.x > x) ? _test.x : x;
  559. y = (_test.y > y) ? _test.y : y;
  560. }
  561. inline void Point2D::interpolate(const Point2D& _rFrom, const Point2D& _to, const F64 _factor)
  562. {
  563. AssertFatal(_factor >= 0.0f && _factor <= 1.0f, "Out of bound interpolation factor");
  564. x = (_rFrom.x * (1.0f - _factor)) + (_to.x * _factor);
  565. y = (_rFrom.y * (1.0f - _factor)) + (_to.y * _factor);
  566. }
  567. inline void Point2D::zero()
  568. {
  569. x = y = 0.0;
  570. }
  571. inline bool Point2D::isZero() const
  572. {
  573. return (x == 0.0f) && (y == 0.0f);
  574. }
  575. inline F64 Point2D::lenSquared() const
  576. {
  577. return (x * x) + (y * y);
  578. }
  579. inline void Point2D::neg()
  580. {
  581. x = -x;
  582. y = -y;
  583. }
  584. inline void Point2D::convolve(const Point2D& c)
  585. {
  586. x *= c.x;
  587. y *= c.y;
  588. }
  589. inline void Point2D::convolveInverse(const Point2D& c)
  590. {
  591. x /= c.x;
  592. y /= c.y;
  593. }
  594. inline bool Point2D::operator==(const Point2D& _test) const
  595. {
  596. return (x == _test.x) && (y == _test.y);
  597. }
  598. inline bool Point2D::operator!=(const Point2D& _test) const
  599. {
  600. return operator==(_test) == false;
  601. }
  602. inline Point2D Point2D::operator+(const Point2D& _add) const
  603. {
  604. return Point2D(x + _add.x, y + _add.y);
  605. }
  606. inline Point2D Point2D::operator-(const Point2D& _rSub) const
  607. {
  608. return Point2D(x - _rSub.x, y - _rSub.y);
  609. }
  610. inline Point2D& Point2D::operator+=(const Point2D& _add)
  611. {
  612. x += _add.x;
  613. y += _add.y;
  614. return *this;
  615. }
  616. inline Point2D& Point2D::operator-=(const Point2D& _rSub)
  617. {
  618. x -= _rSub.x;
  619. y -= _rSub.y;
  620. return *this;
  621. }
  622. inline Point2D Point2D::operator*(F64 _mul) const
  623. {
  624. return Point2D(x * _mul, y * _mul);
  625. }
  626. inline Point2D Point2D::operator/(F64 _div) const
  627. {
  628. AssertFatal(_div != 0.0f, "Error, div by zero attempted");
  629. F64 inv = 1.0f / _div;
  630. return Point2D(x * inv, y * inv);
  631. }
  632. inline Point2D& Point2D::operator*=(F64 _mul)
  633. {
  634. x *= _mul;
  635. y *= _mul;
  636. return *this;
  637. }
  638. inline Point2D& Point2D::operator/=(F64 _div)
  639. {
  640. AssertFatal(_div != 0.0f, "Error, div by zero attempted");
  641. F64 inv = 1.0f / _div;
  642. x *= inv;
  643. y *= inv;
  644. return *this;
  645. }
  646. inline Point2D Point2D::operator-() const
  647. {
  648. return Point2D(-x, -y);
  649. }
  650. inline F64 Point2D::len() const
  651. {
  652. return mSqrtD(x*x + y*y);
  653. }
  654. inline void Point2D::normalize()
  655. {
  656. m_point2D_normalize(*this);
  657. }
  658. inline void Point2D::normalize(F64 val)
  659. {
  660. m_point2D_normalize_f(*this, val);
  661. }
  662. //-------------------------------------------------------------------
  663. // Non-Member Operators
  664. //-------------------------------------------------------------------
  665. inline Point2I operator*(S32 mul, const Point2I& multiplicand)
  666. {
  667. return multiplicand * mul;
  668. }
  669. inline Point2F operator*(F32 mul, const Point2F& multiplicand)
  670. {
  671. return multiplicand * mul;
  672. }
  673. inline Point2D operator*(F64 mul, const Point2D& multiplicand)
  674. {
  675. return multiplicand * mul;
  676. }
  677. inline F32 mDot(const Point2F &p1, const Point2F &p2)
  678. {
  679. return (p1.x*p2.x + p1.y*p2.y);
  680. }
  681. inline F32 mDotPerp(const Point2F &p1, const Point2F &p2)
  682. {
  683. return p1.x*p2.y - p2.x*p1.y;
  684. }
  685. inline bool mIsNaN( const Point2F &p )
  686. {
  687. return mIsNaN_F( p.x ) || mIsNaN_F( p.y );
  688. }
  689. /// Return 0 if points are colinear
  690. /// Return positive if p0p1p2 are counter-clockwise
  691. /// Return negative if p0p1p2 are clockwise
  692. inline F64 mCross(const Point2F &p0, const Point2F &p1, const Point2F &pt2)
  693. {
  694. return (p1.x - p0.x) * (pt2.y - p0.y) - (p1.y - p0.y) * (pt2.x - p0.x);
  695. }
  696. namespace DictHash
  697. {
  698. /// Generates a 32bit hash from a Point2I.
  699. /// @see DictHash
  700. inline U32 hash( const Point2I &key )
  701. {
  702. return (key.x * 2230148873u) ^ key.y;
  703. }
  704. }
  705. #endif // _MPOINT2_H_