mPoint2.h 19 KB

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