mPoint2.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922
  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. {
  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. x /= _vec.x;
  348. y /= _vec.y;
  349. return *this;
  350. }
  351. //------------------------------------------------------------------------------
  352. //-------------------------------------- Point2F
  353. //
  354. inline Point2F::Point2F()
  355. {
  356. //
  357. }
  358. inline Point2F::Point2F(const Point2F& _copy)
  359. : x(_copy.x), y(_copy.y)
  360. {
  361. //
  362. }
  363. inline Point2F::Point2F(F32 _x, F32 _y)
  364. : x(_x), y(_y)
  365. {
  366. }
  367. inline void Point2F::set(F32 _x, F32 _y)
  368. {
  369. x = _x;
  370. y = _y;
  371. }
  372. inline void Point2F::setMin(const Point2F& _test)
  373. {
  374. x = (_test.x < x) ? _test.x : x;
  375. y = (_test.y < y) ? _test.y : y;
  376. }
  377. inline void Point2F::setMax(const Point2F& _test)
  378. {
  379. x = (_test.x > x) ? _test.x : x;
  380. y = (_test.y > y) ? _test.y : y;
  381. }
  382. inline void Point2F::interpolate(const Point2F& _rFrom, const Point2F& _to, const F32 _factor)
  383. {
  384. AssertFatal(_factor >= 0.0f && _factor <= 1.0f, "Out of bound interpolation factor");
  385. x = (_rFrom.x * (1.0f - _factor)) + (_to.x * _factor);
  386. y = (_rFrom.y * (1.0f - _factor)) + (_to.y * _factor);
  387. }
  388. inline void Point2F::zero()
  389. {
  390. x = y = 0.0f;
  391. }
  392. inline bool Point2F::isZero() const
  393. {
  394. return (x == 0.0f) && (y == 0.0f);
  395. }
  396. inline F32 Point2F::lenSquared() const
  397. {
  398. return (x * x) + (y * y);
  399. }
  400. inline bool Point2F::equal( const Point2F &compare ) const
  401. {
  402. return( ( mFabs( x - compare.x ) < POINT_EPSILON ) &&
  403. ( mFabs( y - compare.y ) < POINT_EPSILON ) );
  404. }
  405. inline void Point2F::neg()
  406. {
  407. x = -x;
  408. y = -y;
  409. }
  410. inline void Point2F::convolve(const Point2F& c)
  411. {
  412. x *= c.x;
  413. y *= c.y;
  414. }
  415. inline void Point2F::convolveInverse(const Point2F& c)
  416. {
  417. x /= c.x;
  418. y /= c.y;
  419. }
  420. inline void Point2F::rotate( F32 radians )
  421. {
  422. F32 sinTheta, cosTheta;
  423. mSinCos( radians, sinTheta, cosTheta );
  424. set( cosTheta * x - sinTheta * y,
  425. sinTheta * x + cosTheta * y );
  426. }
  427. inline bool Point2F::operator==(const Point2F& _test) const
  428. {
  429. return (x == _test.x) && (y == _test.y);
  430. }
  431. inline bool Point2F::operator!=(const Point2F& _test) const
  432. {
  433. return operator==(_test) == false;
  434. }
  435. inline Point2F Point2F::operator+(const Point2F& _add) const
  436. {
  437. return Point2F(x + _add.x, y + _add.y);
  438. }
  439. inline Point2F Point2F::operator-(const Point2F& _rSub) const
  440. {
  441. return Point2F(x - _rSub.x, y - _rSub.y);
  442. }
  443. inline Point2F& Point2F::operator+=(const Point2F& _add)
  444. {
  445. x += _add.x;
  446. y += _add.y;
  447. return *this;
  448. }
  449. inline Point2F& Point2F::operator-=(const Point2F& _rSub)
  450. {
  451. x -= _rSub.x;
  452. y -= _rSub.y;
  453. return *this;
  454. }
  455. inline Point2F Point2F::operator*(F32 _mul) const
  456. {
  457. return Point2F(x * _mul, y * _mul);
  458. }
  459. inline Point2F Point2F::operator/(F32 _div) const
  460. {
  461. AssertFatal(_div != 0.0f, "Error, div by zero attempted");
  462. F32 inv = 1.0f / _div;
  463. return Point2F(x * inv, y * inv);
  464. }
  465. inline Point2F& Point2F::operator*=(F32 _mul)
  466. {
  467. x *= _mul;
  468. y *= _mul;
  469. return *this;
  470. }
  471. inline Point2F& Point2F::operator/=(F32 _div)
  472. {
  473. AssertFatal(_div != 0.0f, "Error, div by zero attempted");
  474. F32 inv = 1.0f / _div;
  475. x *= inv;
  476. y *= inv;
  477. return *this;
  478. }
  479. inline Point2F Point2F::operator*(const Point2F &_vec) const
  480. {
  481. return Point2F(x * _vec.x, y * _vec.y);
  482. }
  483. inline Point2F& Point2F::operator*=(const Point2F &_vec)
  484. {
  485. x *= _vec.x;
  486. y *= _vec.y;
  487. return *this;
  488. }
  489. inline Point2F Point2F::operator/(const Point2F &_vec) const
  490. {
  491. return Point2F(x / _vec.x, y / _vec.y);
  492. }
  493. inline Point2F& Point2F::operator/=(const Point2F &_vec)
  494. {
  495. x /= _vec.x;
  496. y /= _vec.y;
  497. return *this;
  498. }
  499. inline Point2F Point2F::operator-() const
  500. {
  501. return Point2F(-x, -y);
  502. }
  503. inline F32 Point2F::len() const
  504. {
  505. return mSqrt(x*x + y*y);
  506. }
  507. inline void Point2F::normalize()
  508. {
  509. m_point2F_normalize(*this);
  510. }
  511. inline void Point2F::normalize(F32 val)
  512. {
  513. m_point2F_normalize_f(*this, val);
  514. }
  515. inline F32 Point2F::magnitudeSafe() const
  516. {
  517. if( isZero() )
  518. return 0.0f;
  519. else
  520. return len();
  521. }
  522. inline void Point2F::normalizeSafe()
  523. {
  524. F32 vmag = magnitudeSafe();
  525. if( vmag > POINT_EPSILON )
  526. *this *= F32(1.0 / vmag);
  527. }
  528. //------------------------------------------------------------------------------
  529. //-------------------------------------- Point2D
  530. //
  531. inline Point2D::Point2D()
  532. {
  533. //
  534. }
  535. inline Point2D::Point2D(const Point2D& _copy)
  536. : x(_copy.x), y(_copy.y)
  537. {
  538. //
  539. }
  540. inline Point2D::Point2D(F64 _x, F64 _y)
  541. : x(_x), y(_y)
  542. {
  543. }
  544. inline void Point2D::set(F64 _x, F64 _y)
  545. {
  546. x = _x;
  547. y = _y;
  548. }
  549. inline void Point2D::setMin(const Point2D& _test)
  550. {
  551. x = (_test.x < x) ? _test.x : x;
  552. y = (_test.y < y) ? _test.y : y;
  553. }
  554. inline void Point2D::setMax(const Point2D& _test)
  555. {
  556. x = (_test.x > x) ? _test.x : x;
  557. y = (_test.y > y) ? _test.y : y;
  558. }
  559. inline void Point2D::interpolate(const Point2D& _rFrom, const Point2D& _to, const F64 _factor)
  560. {
  561. AssertFatal(_factor >= 0.0f && _factor <= 1.0f, "Out of bound interpolation factor");
  562. x = (_rFrom.x * (1.0f - _factor)) + (_to.x * _factor);
  563. y = (_rFrom.y * (1.0f - _factor)) + (_to.y * _factor);
  564. }
  565. inline void Point2D::zero()
  566. {
  567. x = y = 0.0;
  568. }
  569. inline bool Point2D::isZero() const
  570. {
  571. return (x == 0.0f) && (y == 0.0f);
  572. }
  573. inline F64 Point2D::lenSquared() const
  574. {
  575. return (x * x) + (y * y);
  576. }
  577. inline void Point2D::neg()
  578. {
  579. x = -x;
  580. y = -y;
  581. }
  582. inline void Point2D::convolve(const Point2D& c)
  583. {
  584. x *= c.x;
  585. y *= c.y;
  586. }
  587. inline void Point2D::convolveInverse(const Point2D& c)
  588. {
  589. x /= c.x;
  590. y /= c.y;
  591. }
  592. inline bool Point2D::operator==(const Point2D& _test) const
  593. {
  594. return (x == _test.x) && (y == _test.y);
  595. }
  596. inline bool Point2D::operator!=(const Point2D& _test) const
  597. {
  598. return operator==(_test) == false;
  599. }
  600. inline Point2D Point2D::operator+(const Point2D& _add) const
  601. {
  602. return Point2D(x + _add.x, y + _add.y);
  603. }
  604. inline Point2D Point2D::operator-(const Point2D& _rSub) const
  605. {
  606. return Point2D(x - _rSub.x, y - _rSub.y);
  607. }
  608. inline Point2D& Point2D::operator+=(const Point2D& _add)
  609. {
  610. x += _add.x;
  611. y += _add.y;
  612. return *this;
  613. }
  614. inline Point2D& Point2D::operator-=(const Point2D& _rSub)
  615. {
  616. x -= _rSub.x;
  617. y -= _rSub.y;
  618. return *this;
  619. }
  620. inline Point2D Point2D::operator*(F64 _mul) const
  621. {
  622. return Point2D(x * _mul, y * _mul);
  623. }
  624. inline Point2D Point2D::operator/(F64 _div) const
  625. {
  626. AssertFatal(_div != 0.0f, "Error, div by zero attempted");
  627. F64 inv = 1.0f / _div;
  628. return Point2D(x * inv, y * inv);
  629. }
  630. inline Point2D& Point2D::operator*=(F64 _mul)
  631. {
  632. x *= _mul;
  633. y *= _mul;
  634. return *this;
  635. }
  636. inline Point2D& Point2D::operator/=(F64 _div)
  637. {
  638. AssertFatal(_div != 0.0f, "Error, div by zero attempted");
  639. F64 inv = 1.0f / _div;
  640. x *= inv;
  641. y *= inv;
  642. return *this;
  643. }
  644. inline Point2D Point2D::operator-() const
  645. {
  646. return Point2D(-x, -y);
  647. }
  648. inline F64 Point2D::len() const
  649. {
  650. return mSqrtD(x*x + y*y);
  651. }
  652. inline void Point2D::normalize()
  653. {
  654. m_point2D_normalize(*this);
  655. }
  656. inline void Point2D::normalize(F64 val)
  657. {
  658. m_point2D_normalize_f(*this, val);
  659. }
  660. //-------------------------------------------------------------------
  661. // Non-Member Operators
  662. //-------------------------------------------------------------------
  663. inline Point2I operator*(S32 mul, const Point2I& multiplicand)
  664. {
  665. return multiplicand * mul;
  666. }
  667. inline Point2F operator*(F32 mul, const Point2F& multiplicand)
  668. {
  669. return multiplicand * mul;
  670. }
  671. inline Point2D operator*(F64 mul, const Point2D& multiplicand)
  672. {
  673. return multiplicand * mul;
  674. }
  675. inline F32 mDot(const Point2F &p1, const Point2F &p2)
  676. {
  677. return (p1.x*p2.x + p1.y*p2.y);
  678. }
  679. inline F32 mDotPerp(const Point2F &p1, const Point2F &p2)
  680. {
  681. return p1.x*p2.y - p2.x*p1.y;
  682. }
  683. inline bool mIsNaN( const Point2F &p )
  684. {
  685. return mIsNaN_F( p.x ) || mIsNaN_F( p.y );
  686. }
  687. namespace DictHash
  688. {
  689. /// Generates a 32bit hash from a Point2I.
  690. /// @see DictHash
  691. inline U32 hash( const Point2I &key )
  692. {
  693. return (key.x * 2230148873u) ^ key.y;
  694. }
  695. }
  696. #endif // _MPOINT2_H_