DISTRHO Plugin Framework
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

997 lines
21KB

  1. /*
  2. * DISTRHO Plugin Framework (DPF)
  3. * Copyright (C) 2012-2019 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * Permission to use, copy, modify, and/or distribute this software for any purpose with
  6. * or without fee is hereby granted, provided that the above copyright notice and this
  7. * permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
  10. * TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
  11. * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  12. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
  13. * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  14. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include "../Geometry.hpp"
  17. #include <cmath>
  18. START_NAMESPACE_DGL
  19. static const float M_2PIf = 3.14159265358979323846f*2.0f;
  20. // -----------------------------------------------------------------------
  21. // Point
  22. template<typename T>
  23. Point<T>::Point() noexcept
  24. : fX(0),
  25. fY(0) {}
  26. template<typename T>
  27. Point<T>::Point(const T& x, const T& y) noexcept
  28. : fX(x),
  29. fY(y) {}
  30. template<typename T>
  31. Point<T>::Point(const Point<T>& pos) noexcept
  32. : fX(pos.fX),
  33. fY(pos.fY) {}
  34. template<typename T>
  35. const T& Point<T>::getX() const noexcept
  36. {
  37. return fX;
  38. }
  39. template<typename T>
  40. const T& Point<T>::getY() const noexcept
  41. {
  42. return fY;
  43. }
  44. template<typename T>
  45. void Point<T>::setX(const T& x) noexcept
  46. {
  47. fX = x;
  48. }
  49. template<typename T>
  50. void Point<T>::setY(const T& y) noexcept
  51. {
  52. fY = y;
  53. }
  54. template<typename T>
  55. void Point<T>::setPos(const T& x, const T& y) noexcept
  56. {
  57. fX = x;
  58. fY = y;
  59. }
  60. template<typename T>
  61. void Point<T>::setPos(const Point<T>& pos) noexcept
  62. {
  63. fX = pos.fX;
  64. fY = pos.fY;
  65. }
  66. template<typename T>
  67. void Point<T>::moveBy(const T& x, const T& y) noexcept
  68. {
  69. fX = static_cast<T>(fX+x);
  70. fY = static_cast<T>(fY+y);
  71. }
  72. template<typename T>
  73. void Point<T>::moveBy(const Point<T>& pos) noexcept
  74. {
  75. fX = static_cast<T>(fX+pos.fX);
  76. fY = static_cast<T>(fY+pos.fY);
  77. }
  78. template<typename T>
  79. bool Point<T>::isZero() const noexcept
  80. {
  81. return fX == 0 && fY == 0;
  82. }
  83. template<typename T>
  84. bool Point<T>::isNotZero() const noexcept
  85. {
  86. return fX != 0 || fY != 0;
  87. }
  88. template<typename T>
  89. Point<T> Point<T>::operator+(const Point<T>& pos) noexcept
  90. {
  91. return Point<T>(fX+pos.fX, fY+pos.fY);
  92. }
  93. template<typename T>
  94. Point<T> Point<T>::operator-(const Point<T>& pos) noexcept
  95. {
  96. return Point<T>(fX-pos.fX, fY-pos.fY);
  97. }
  98. template<typename T>
  99. Point<T>& Point<T>::operator=(const Point<T>& pos) noexcept
  100. {
  101. fX = pos.fX;
  102. fY = pos.fY;
  103. return *this;
  104. }
  105. template<typename T>
  106. Point<T>& Point<T>::operator+=(const Point<T>& pos) noexcept
  107. {
  108. fX = static_cast<T>(fX+pos.fX);
  109. fY = static_cast<T>(fY+pos.fY);
  110. return *this;
  111. }
  112. template<typename T>
  113. Point<T>& Point<T>::operator-=(const Point<T>& pos) noexcept
  114. {
  115. fX = static_cast<T>(fX-pos.fX);
  116. fY = static_cast<T>(fY-pos.fY);
  117. return *this;
  118. }
  119. template<typename T>
  120. bool Point<T>::operator==(const Point<T>& pos) const noexcept
  121. {
  122. return (fX == pos.fX && fY == pos.fY);
  123. }
  124. template<typename T>
  125. bool Point<T>::operator!=(const Point<T>& pos) const noexcept
  126. {
  127. return (fX != pos.fX || fY != pos.fY);
  128. }
  129. // -----------------------------------------------------------------------
  130. // Size
  131. template<typename T>
  132. Size<T>::Size() noexcept
  133. : fWidth(0),
  134. fHeight(0) {}
  135. template<typename T>
  136. Size<T>::Size(const T& width, const T& height) noexcept
  137. : fWidth(width),
  138. fHeight(height) {}
  139. template<typename T>
  140. Size<T>::Size(const Size<T>& size) noexcept
  141. : fWidth(size.fWidth),
  142. fHeight(size.fHeight) {}
  143. template<typename T>
  144. const T& Size<T>::getWidth() const noexcept
  145. {
  146. return fWidth;
  147. }
  148. template<typename T>
  149. const T& Size<T>::getHeight() const noexcept
  150. {
  151. return fHeight;
  152. }
  153. template<typename T>
  154. void Size<T>::setWidth(const T& width) noexcept
  155. {
  156. fWidth = width;
  157. }
  158. template<typename T>
  159. void Size<T>::setHeight(const T& height) noexcept
  160. {
  161. fHeight = height;
  162. }
  163. template<typename T>
  164. void Size<T>::setSize(const T& width, const T& height) noexcept
  165. {
  166. fWidth = width;
  167. fHeight = height;
  168. }
  169. template<typename T>
  170. void Size<T>::setSize(const Size<T>& size) noexcept
  171. {
  172. fWidth = size.fWidth;
  173. fHeight = size.fHeight;
  174. }
  175. template<typename T>
  176. void Size<T>::growBy(double multiplier) noexcept
  177. {
  178. fWidth = static_cast<T>(static_cast<double>(fWidth)*multiplier);
  179. fHeight = static_cast<T>(static_cast<double>(fHeight)*multiplier);
  180. }
  181. template<typename T>
  182. void Size<T>::shrinkBy(double divider) noexcept
  183. {
  184. fWidth = static_cast<T>(static_cast<double>(fWidth)/divider);
  185. fHeight = static_cast<T>(static_cast<double>(fHeight)/divider);
  186. }
  187. template<typename T>
  188. bool Size<T>::isNull() const noexcept
  189. {
  190. return fWidth == 0 && fHeight == 0;
  191. }
  192. template<typename T>
  193. bool Size<T>::isNotNull() const noexcept
  194. {
  195. return fWidth != 0 || fHeight != 0;
  196. }
  197. template<typename T>
  198. bool Size<T>::isValid() const noexcept
  199. {
  200. return fWidth > 1 && fHeight > 1;
  201. }
  202. template<typename T>
  203. bool Size<T>::isInvalid() const noexcept
  204. {
  205. return fWidth <= 0 || fHeight <= 0;
  206. }
  207. template<typename T>
  208. Size<T> Size<T>::operator+(const Size<T>& size) noexcept
  209. {
  210. return Size<T>(fWidth+size.fWidth, fHeight+size.fHeight);
  211. }
  212. template<typename T>
  213. Size<T> Size<T>::operator-(const Size<T>& size) noexcept
  214. {
  215. return Size<T>(fWidth-size.fWidth, fHeight-size.fHeight);
  216. }
  217. template<typename T>
  218. Size<T>& Size<T>::operator=(const Size<T>& size) noexcept
  219. {
  220. fWidth = size.fWidth;
  221. fHeight = size.fHeight;
  222. return *this;
  223. }
  224. template<typename T>
  225. Size<T>& Size<T>::operator+=(const Size<T>& size) noexcept
  226. {
  227. fWidth = static_cast<T>(fWidth+size.fWidth);
  228. fHeight = static_cast<T>(fHeight+size.fHeight);
  229. return *this;
  230. }
  231. template<typename T>
  232. Size<T>& Size<T>::operator-=(const Size<T>& size) noexcept
  233. {
  234. fWidth = static_cast<T>(fWidth-size.fWidth);
  235. fHeight = static_cast<T>(fHeight-size.fHeight);
  236. return *this;
  237. }
  238. template<typename T>
  239. Size<T>& Size<T>::operator*=(double m) noexcept
  240. {
  241. fWidth = static_cast<T>(static_cast<double>(fWidth)*m);
  242. fHeight = static_cast<T>(static_cast<double>(fHeight)*m);
  243. return *this;
  244. }
  245. template<typename T>
  246. Size<T>& Size<T>::operator/=(double d) noexcept
  247. {
  248. fWidth = static_cast<T>(static_cast<double>(fWidth)/d);
  249. fHeight = static_cast<T>(static_cast<double>(fHeight)/d);
  250. return *this;
  251. }
  252. template<typename T>
  253. bool Size<T>::operator==(const Size<T>& size) const noexcept
  254. {
  255. return (fWidth == size.fWidth && fHeight == size.fHeight);
  256. }
  257. template<typename T>
  258. bool Size<T>::operator!=(const Size<T>& size) const noexcept
  259. {
  260. return (fWidth != size.fWidth || fHeight != size.fHeight);
  261. }
  262. // -----------------------------------------------------------------------
  263. // Line
  264. template<typename T>
  265. Line<T>::Line() noexcept
  266. : fPosStart(0, 0),
  267. fPosEnd(0, 0) {}
  268. template<typename T>
  269. Line<T>::Line(const T& startX, const T& startY, const T& endX, const T& endY) noexcept
  270. : fPosStart(startX, startY),
  271. fPosEnd(endX, endY) {}
  272. template<typename T>
  273. Line<T>::Line(const T& startX, const T& startY, const Point<T>& endPos) noexcept
  274. : fPosStart(startX, startY),
  275. fPosEnd(endPos) {}
  276. template<typename T>
  277. Line<T>::Line(const Point<T>& startPos, const T& endX, const T& endY) noexcept
  278. : fPosStart(startPos),
  279. fPosEnd(endX, endY) {}
  280. template<typename T>
  281. Line<T>::Line(const Point<T>& startPos, const Point<T>& endPos) noexcept
  282. : fPosStart(startPos),
  283. fPosEnd(endPos) {}
  284. template<typename T>
  285. Line<T>::Line(const Line<T>& line) noexcept
  286. : fPosStart(line.fPosStart),
  287. fPosEnd(line.fPosEnd) {}
  288. template<typename T>
  289. const T& Line<T>::getStartX() const noexcept
  290. {
  291. return fPosStart.fX;
  292. }
  293. template<typename T>
  294. const T& Line<T>::getStartY() const noexcept
  295. {
  296. return fPosStart.fY;
  297. }
  298. template<typename T>
  299. const T& Line<T>::getEndX() const noexcept
  300. {
  301. return fPosEnd.fX;
  302. }
  303. template<typename T>
  304. const T& Line<T>::getEndY() const noexcept
  305. {
  306. return fPosEnd.fY;
  307. }
  308. template<typename T>
  309. const Point<T>& Line<T>::getStartPos() const noexcept
  310. {
  311. return fPosStart;
  312. }
  313. template<typename T>
  314. const Point<T>& Line<T>::getEndPos() const noexcept
  315. {
  316. return fPosEnd;
  317. }
  318. template<typename T>
  319. void Line<T>::setStartX(const T& x) noexcept
  320. {
  321. fPosStart.fX = x;
  322. }
  323. template<typename T>
  324. void Line<T>::setStartY(const T& y) noexcept
  325. {
  326. fPosStart.fY = y;
  327. }
  328. template<typename T>
  329. void Line<T>::setStartPos(const T& x, const T& y) noexcept
  330. {
  331. fPosStart = Point<T>(x, y);
  332. }
  333. template<typename T>
  334. void Line<T>::setStartPos(const Point<T>& pos) noexcept
  335. {
  336. fPosStart = pos;
  337. }
  338. template<typename T>
  339. void Line<T>::setEndX(const T& x) noexcept
  340. {
  341. fPosEnd.fX = x;
  342. }
  343. template<typename T>
  344. void Line<T>::setEndY(const T& y) noexcept
  345. {
  346. fPosEnd.fY = y;
  347. }
  348. template<typename T>
  349. void Line<T>::setEndPos(const T& x, const T& y) noexcept
  350. {
  351. fPosEnd = Point<T>(x, y);
  352. }
  353. template<typename T>
  354. void Line<T>::setEndPos(const Point<T>& pos) noexcept
  355. {
  356. fPosEnd = pos;
  357. }
  358. template<typename T>
  359. void Line<T>::moveBy(const T& x, const T& y) noexcept
  360. {
  361. fPosStart.moveBy(x, y);
  362. fPosEnd.moveBy(x, y);
  363. }
  364. template<typename T>
  365. void Line<T>::moveBy(const Point<T>& pos) noexcept
  366. {
  367. fPosStart.moveBy(pos);
  368. fPosEnd.moveBy(pos);
  369. }
  370. template<typename T>
  371. bool Line<T>::isNull() const noexcept
  372. {
  373. return fPosStart == fPosEnd;
  374. }
  375. template<typename T>
  376. bool Line<T>::isNotNull() const noexcept
  377. {
  378. return fPosStart != fPosEnd;
  379. }
  380. template<typename T>
  381. Line<T>& Line<T>::operator=(const Line<T>& line) noexcept
  382. {
  383. fPosStart = line.fPosStart;
  384. fPosEnd = line.fPosEnd;
  385. return *this;
  386. }
  387. template<typename T>
  388. bool Line<T>::operator==(const Line<T>& line) const noexcept
  389. {
  390. return (fPosStart == line.fPosStart && fPosEnd == line.fPosEnd);
  391. }
  392. template<typename T>
  393. bool Line<T>::operator!=(const Line<T>& line) const noexcept
  394. {
  395. return (fPosStart != line.fPosStart || fPosEnd != line.fPosEnd);
  396. }
  397. // -----------------------------------------------------------------------
  398. // Circle
  399. template<typename T>
  400. Circle<T>::Circle() noexcept
  401. : fPos(0, 0),
  402. fSize(0.0f),
  403. fNumSegments(0),
  404. fTheta(0.0f),
  405. fCos(0.0f),
  406. fSin(0.0f) {}
  407. template<typename T>
  408. Circle<T>::Circle(const T& x, const T& y, const float size, const uint numSegments)
  409. : fPos(x, y),
  410. fSize(size),
  411. fNumSegments(numSegments >= 3 ? numSegments : 3),
  412. fTheta(M_2PIf / static_cast<float>(fNumSegments)),
  413. fCos(std::cos(fTheta)),
  414. fSin(std::sin(fTheta))
  415. {
  416. DISTRHO_SAFE_ASSERT(fSize > 0.0f);
  417. }
  418. template<typename T>
  419. Circle<T>::Circle(const Point<T>& pos, const float size, const uint numSegments)
  420. : fPos(pos),
  421. fSize(size),
  422. fNumSegments(numSegments >= 3 ? numSegments : 3),
  423. fTheta(M_2PIf / static_cast<float>(fNumSegments)),
  424. fCos(std::cos(fTheta)),
  425. fSin(std::sin(fTheta))
  426. {
  427. DISTRHO_SAFE_ASSERT(fSize > 0.0f);
  428. }
  429. template<typename T>
  430. Circle<T>::Circle(const Circle<T>& cir) noexcept
  431. : fPos(cir.fPos),
  432. fSize(cir.fSize),
  433. fNumSegments(cir.fNumSegments),
  434. fTheta(cir.fTheta),
  435. fCos(cir.fCos),
  436. fSin(cir.fSin)
  437. {
  438. DISTRHO_SAFE_ASSERT(fSize > 0.0f);
  439. }
  440. template<typename T>
  441. const T& Circle<T>::getX() const noexcept
  442. {
  443. return fPos.fX;
  444. }
  445. template<typename T>
  446. const T& Circle<T>::getY() const noexcept
  447. {
  448. return fPos.fY;
  449. }
  450. template<typename T>
  451. const Point<T>& Circle<T>::getPos() const noexcept
  452. {
  453. return fPos;
  454. }
  455. template<typename T>
  456. void Circle<T>::setX(const T& x) noexcept
  457. {
  458. fPos.fX = x;
  459. }
  460. template<typename T>
  461. void Circle<T>::setY(const T& y) noexcept
  462. {
  463. fPos.fY = y;
  464. }
  465. template<typename T>
  466. void Circle<T>::setPos(const T& x, const T& y) noexcept
  467. {
  468. fPos.fX = x;
  469. fPos.fY = y;
  470. }
  471. template<typename T>
  472. void Circle<T>::setPos(const Point<T>& pos) noexcept
  473. {
  474. fPos = pos;
  475. }
  476. template<typename T>
  477. float Circle<T>::getSize() const noexcept
  478. {
  479. return fSize;
  480. }
  481. template<typename T>
  482. void Circle<T>::setSize(const float size) noexcept
  483. {
  484. DISTRHO_SAFE_ASSERT_RETURN(size > 0.0f,);
  485. fSize = size;
  486. }
  487. template<typename T>
  488. uint Circle<T>::getNumSegments() const noexcept
  489. {
  490. return fNumSegments;
  491. }
  492. template<typename T>
  493. void Circle<T>::setNumSegments(const uint num)
  494. {
  495. DISTRHO_SAFE_ASSERT_RETURN(num >= 3,);
  496. if (fNumSegments == num)
  497. return;
  498. fNumSegments = num;
  499. fTheta = M_2PIf / static_cast<float>(fNumSegments);
  500. fCos = std::cos(fTheta);
  501. fSin = std::sin(fTheta);
  502. }
  503. template<typename T>
  504. void Circle<T>::draw()
  505. {
  506. _draw(false);
  507. }
  508. template<typename T>
  509. void Circle<T>::drawOutline()
  510. {
  511. _draw(true);
  512. }
  513. template<typename T>
  514. Circle<T>& Circle<T>::operator=(const Circle<T>& cir) noexcept
  515. {
  516. fPos = cir.fPos;
  517. fSize = cir.fSize;
  518. fTheta = cir.fTheta;
  519. fCos = cir.fCos;
  520. fSin = cir.fSin;
  521. fNumSegments = cir.fNumSegments;
  522. return *this;
  523. }
  524. template<typename T>
  525. bool Circle<T>::operator==(const Circle<T>& cir) const noexcept
  526. {
  527. return (fPos == cir.fPos && d_isEqual(fSize, cir.fSize) && fNumSegments == cir.fNumSegments);
  528. }
  529. template<typename T>
  530. bool Circle<T>::operator!=(const Circle<T>& cir) const noexcept
  531. {
  532. return (fPos != cir.fPos || d_isNotEqual(fSize, cir.fSize) || fNumSegments != cir.fNumSegments);
  533. }
  534. // -----------------------------------------------------------------------
  535. // Triangle
  536. template<typename T>
  537. Triangle<T>::Triangle() noexcept
  538. : fPos1(0, 0),
  539. fPos2(0, 0),
  540. fPos3(0, 0) {}
  541. template<typename T>
  542. Triangle<T>::Triangle(const T& x1, const T& y1, const T& x2, const T& y2, const T& x3, const T& y3) noexcept
  543. : fPos1(x1, y1),
  544. fPos2(x2, y2),
  545. fPos3(x3, y3) {}
  546. template<typename T>
  547. Triangle<T>::Triangle(const Point<T>& pos1, const Point<T>& pos2, const Point<T>& pos3) noexcept
  548. : fPos1(pos1),
  549. fPos2(pos2),
  550. fPos3(pos3) {}
  551. template<typename T>
  552. Triangle<T>::Triangle(const Triangle<T>& tri) noexcept
  553. : fPos1(tri.fPos1),
  554. fPos2(tri.fPos2),
  555. fPos3(tri.fPos3) {}
  556. template<typename T>
  557. bool Triangle<T>::isNull() const noexcept
  558. {
  559. return fPos1 == fPos2 && fPos1 == fPos3;
  560. }
  561. template<typename T>
  562. bool Triangle<T>::isNotNull() const noexcept
  563. {
  564. return fPos1 != fPos2 || fPos1 != fPos3;
  565. }
  566. template<typename T>
  567. bool Triangle<T>::isValid() const noexcept
  568. {
  569. return fPos1 != fPos2 && fPos1 != fPos3;
  570. }
  571. template<typename T>
  572. bool Triangle<T>::isInvalid() const noexcept
  573. {
  574. return fPos1 == fPos2 || fPos1 == fPos3;
  575. }
  576. template<typename T>
  577. void Triangle<T>::draw()
  578. {
  579. _draw(false);
  580. }
  581. template<typename T>
  582. void Triangle<T>::drawOutline()
  583. {
  584. _draw(true);
  585. }
  586. template<typename T>
  587. Triangle<T>& Triangle<T>::operator=(const Triangle<T>& tri) noexcept
  588. {
  589. fPos1 = tri.fPos1;
  590. fPos2 = tri.fPos2;
  591. fPos3 = tri.fPos3;
  592. return *this;
  593. }
  594. template<typename T>
  595. bool Triangle<T>::operator==(const Triangle<T>& tri) const noexcept
  596. {
  597. return (fPos1 == tri.fPos1 && fPos2 == tri.fPos2 && fPos3 == tri.fPos3);
  598. }
  599. template<typename T>
  600. bool Triangle<T>::operator!=(const Triangle<T>& tri) const noexcept
  601. {
  602. return (fPos1 != tri.fPos1 || fPos2 != tri.fPos2 || fPos3 != tri.fPos3);
  603. }
  604. // -----------------------------------------------------------------------
  605. // Rectangle
  606. template<typename T>
  607. Rectangle<T>::Rectangle() noexcept
  608. : fPos(0, 0),
  609. fSize(0, 0) {}
  610. template<typename T>
  611. Rectangle<T>::Rectangle(const T& x, const T& y, const T& width, const T& height) noexcept
  612. : fPos(x, y),
  613. fSize(width, height) {}
  614. template<typename T>
  615. Rectangle<T>::Rectangle(const T& x, const T& y, const Size<T>& size) noexcept
  616. : fPos(x, y),
  617. fSize(size) {}
  618. template<typename T>
  619. Rectangle<T>::Rectangle(const Point<T>& pos, const T& width, const T& height) noexcept
  620. : fPos(pos),
  621. fSize(width, height) {}
  622. template<typename T>
  623. Rectangle<T>::Rectangle(const Point<T>& pos, const Size<T>& size) noexcept
  624. : fPos(pos),
  625. fSize(size) {}
  626. template<typename T>
  627. Rectangle<T>::Rectangle(const Rectangle<T>& rect) noexcept
  628. : fPos(rect.fPos),
  629. fSize(rect.fSize) {}
  630. template<typename T>
  631. const T& Rectangle<T>::getX() const noexcept
  632. {
  633. return fPos.fX;
  634. }
  635. template<typename T>
  636. const T& Rectangle<T>::getY() const noexcept
  637. {
  638. return fPos.fY;
  639. }
  640. template<typename T>
  641. const T& Rectangle<T>::getWidth() const noexcept
  642. {
  643. return fSize.fWidth;
  644. }
  645. template<typename T>
  646. const T& Rectangle<T>::getHeight() const noexcept
  647. {
  648. return fSize.fHeight;
  649. }
  650. template<typename T>
  651. const Point<T>& Rectangle<T>::getPos() const noexcept
  652. {
  653. return fPos;
  654. }
  655. template<typename T>
  656. const Size<T>& Rectangle<T>::getSize() const noexcept
  657. {
  658. return fSize;
  659. }
  660. template<typename T>
  661. void Rectangle<T>::setX(const T& x) noexcept
  662. {
  663. fPos.fX = x;
  664. }
  665. template<typename T>
  666. void Rectangle<T>::setY(const T& y) noexcept
  667. {
  668. fPos.fY = y;
  669. }
  670. template<typename T>
  671. void Rectangle<T>::setPos(const T& x, const T& y) noexcept
  672. {
  673. fPos.fX = x;
  674. fPos.fY = y;
  675. }
  676. template<typename T>
  677. void Rectangle<T>::setPos(const Point<T>& pos) noexcept
  678. {
  679. fPos = pos;
  680. }
  681. template<typename T>
  682. void Rectangle<T>::moveBy(const T& x, const T& y) noexcept
  683. {
  684. fPos.moveBy(x, y);
  685. }
  686. template<typename T>
  687. void Rectangle<T>::moveBy(const Point<T>& pos) noexcept
  688. {
  689. fPos.moveBy(pos);
  690. }
  691. template<typename T>
  692. void Rectangle<T>::setWidth(const T& width) noexcept
  693. {
  694. fSize.fWidth = width;
  695. }
  696. template<typename T>
  697. void Rectangle<T>::setHeight(const T& height) noexcept
  698. {
  699. fSize.fHeight = height;
  700. }
  701. template<typename T>
  702. void Rectangle<T>::setSize(const T& width, const T& height) noexcept
  703. {
  704. fSize.fWidth = width;
  705. fSize.fHeight = height;
  706. }
  707. template<typename T>
  708. void Rectangle<T>::setSize(const Size<T>& size) noexcept
  709. {
  710. fSize = size;
  711. }
  712. template<typename T>
  713. void Rectangle<T>::growBy(double multiplier) noexcept
  714. {
  715. fSize.growBy(multiplier);
  716. }
  717. template<typename T>
  718. void Rectangle<T>::shrinkBy(double divider) noexcept
  719. {
  720. fSize.shrinkBy(divider);
  721. }
  722. template<typename T>
  723. void Rectangle<T>::setRectangle(const Point<T>& pos, const Size<T>& size) noexcept
  724. {
  725. fPos = pos;
  726. fSize = size;
  727. }
  728. template<typename T>
  729. void Rectangle<T>::setRectangle(const Rectangle<T>& rect) noexcept
  730. {
  731. fPos = rect.fPos;
  732. fSize = rect.fSize;
  733. }
  734. template<typename T>
  735. bool Rectangle<T>::contains(const T& x, const T& y) const noexcept
  736. {
  737. return (x >= fPos.fX && y >= fPos.fY && x <= fPos.fX+fSize.fWidth && y <= fPos.fY+fSize.fHeight);
  738. }
  739. template<typename T>
  740. bool Rectangle<T>::contains(const Point<T>& pos) const noexcept
  741. {
  742. return contains(pos.fX, pos.fY);
  743. }
  744. template<typename T>
  745. bool Rectangle<T>::containsX(const T& x) const noexcept
  746. {
  747. return (x >= fPos.fX && x <= fPos.fX + fSize.fWidth);
  748. }
  749. template<typename T>
  750. bool Rectangle<T>::containsY(const T& y) const noexcept
  751. {
  752. return (y >= fPos.fY && y <= fPos.fY + fSize.fHeight);
  753. }
  754. template<typename T>
  755. void Rectangle<T>::draw()
  756. {
  757. _draw(false);
  758. }
  759. template<typename T>
  760. void Rectangle<T>::drawOutline()
  761. {
  762. _draw(true);
  763. }
  764. template<typename T>
  765. Rectangle<T>& Rectangle<T>::operator=(const Rectangle<T>& rect) noexcept
  766. {
  767. fPos = rect.fPos;
  768. fSize = rect.fSize;
  769. return *this;
  770. }
  771. template<typename T>
  772. Rectangle<T>& Rectangle<T>::operator*=(double m) noexcept
  773. {
  774. fSize *= m;
  775. return *this;
  776. }
  777. template<typename T>
  778. Rectangle<T>& Rectangle<T>::operator/=(double d) noexcept
  779. {
  780. fSize /= d;
  781. return *this;
  782. }
  783. template<typename T>
  784. bool Rectangle<T>::operator==(const Rectangle<T>& rect) const noexcept
  785. {
  786. return (fPos == rect.fPos && fSize == rect.fSize);
  787. }
  788. template<typename T>
  789. bool Rectangle<T>::operator!=(const Rectangle<T>& rect) const noexcept
  790. {
  791. return (fPos != rect.fPos || fSize != rect.fSize);
  792. }
  793. // -----------------------------------------------------------------------
  794. // Possible template data types
  795. template class Point<double>;
  796. template class Point<float>;
  797. template class Point<int>;
  798. template class Point<uint>;
  799. template class Point<short>;
  800. template class Point<ushort>;
  801. template class Size<double>;
  802. template class Size<float>;
  803. template class Size<int>;
  804. template class Size<uint>;
  805. template class Size<short>;
  806. template class Size<ushort>;
  807. template class Line<double>;
  808. template class Line<float>;
  809. template class Line<int>;
  810. template class Line<uint>;
  811. template class Line<short>;
  812. template class Line<ushort>;
  813. template class Circle<double>;
  814. template class Circle<float>;
  815. template class Circle<int>;
  816. template class Circle<uint>;
  817. template class Circle<short>;
  818. template class Circle<ushort>;
  819. template class Triangle<double>;
  820. template class Triangle<float>;
  821. template class Triangle<int>;
  822. template class Triangle<uint>;
  823. template class Triangle<short>;
  824. template class Triangle<ushort>;
  825. template class Rectangle<double>;
  826. template class Rectangle<float>;
  827. template class Rectangle<int>;
  828. template class Rectangle<uint>;
  829. template class Rectangle<short>;
  830. template class Rectangle<ushort>;
  831. // -----------------------------------------------------------------------
  832. END_NAMESPACE_DGL