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.

417 lines
7.7KB

  1. /*
  2. * DISTRHO Plugin Toolkit (DPT)
  3. * Copyright (C) 2012-2013 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. START_NAMESPACE_DGL
  18. // -----------------------------------------------------------------------
  19. // Point
  20. template<typename T>
  21. Point<T>::Point() noexcept
  22. : fX(0),
  23. fY(0)
  24. {
  25. }
  26. template<typename T>
  27. Point<T>::Point(T x, T y) noexcept
  28. : fX(x),
  29. fY(y)
  30. {
  31. }
  32. template<typename T>
  33. Point<T>::Point(const Point& pos) noexcept
  34. : fX(pos.fX),
  35. fY(pos.fY)
  36. {
  37. }
  38. template<typename T>
  39. T Point<T>::getX() const noexcept
  40. {
  41. return fX;
  42. }
  43. template<typename T>
  44. T Point<T>::getY() const noexcept
  45. {
  46. return fY;
  47. }
  48. template<typename T>
  49. void Point<T>::setX(T x) noexcept
  50. {
  51. fX = x;
  52. }
  53. template<typename T>
  54. void Point<T>::setY(T y) noexcept
  55. {
  56. fY = y;
  57. }
  58. template<typename T>
  59. void Point<T>::move(T x, T y) noexcept
  60. {
  61. fX += x;
  62. fY += y;
  63. }
  64. template<typename T>
  65. void Point<T>::move(const Point& pos) noexcept
  66. {
  67. fX += pos.fX;
  68. fY += pos.fY;
  69. }
  70. template<typename T>
  71. Point<T>& Point<T>::operator=(const Point<T>& pos) noexcept
  72. {
  73. fX = pos.fX;
  74. fY = pos.fY;
  75. return *this;
  76. }
  77. template<typename T>
  78. Point<T>& Point<T>::operator+=(const Point<T>& pos) noexcept
  79. {
  80. fX += pos.fX;
  81. fY += pos.fY;
  82. return *this;
  83. }
  84. template<typename T>
  85. Point<T>& Point<T>::operator-=(const Point<T>& pos) noexcept
  86. {
  87. fX -= pos.fX;
  88. fY -= pos.fY;
  89. return *this;
  90. }
  91. template<typename T>
  92. bool Point<T>::operator==(const Point<T>& pos) const noexcept
  93. {
  94. return (fX == pos.fX && fY== pos.fY);
  95. }
  96. template<typename T>
  97. bool Point<T>::operator!=(const Point<T>& pos) const noexcept
  98. {
  99. return !operator==(pos);
  100. }
  101. // -----------------------------------------------------------------------
  102. // Size
  103. template<typename T>
  104. Size<T>::Size() noexcept
  105. : fWidth(0),
  106. fHeight(0)
  107. {
  108. }
  109. template<typename T>
  110. Size<T>::Size(T width, T height) noexcept
  111. : fWidth(width),
  112. fHeight(height)
  113. {
  114. }
  115. template<typename T>
  116. Size<T>::Size(const Size<T>& size) noexcept
  117. : fWidth(size.fWidth),
  118. fHeight(size.fHeight)
  119. {
  120. }
  121. template<typename T>
  122. T Size<T>::getWidth() const noexcept
  123. {
  124. return fWidth;
  125. }
  126. template<typename T>
  127. T Size<T>::getHeight() const noexcept
  128. {
  129. return fHeight;
  130. }
  131. template<typename T>
  132. void Size<T>::setWidth(T width) noexcept
  133. {
  134. fWidth = width;
  135. }
  136. template<typename T>
  137. void Size<T>::setHeight(T height) noexcept
  138. {
  139. fHeight = height;
  140. }
  141. template<typename T>
  142. Size<T>& Size<T>::operator=(const Size<T>& size) noexcept
  143. {
  144. fWidth = size.fWidth;
  145. fHeight = size.fHeight;
  146. return *this;
  147. }
  148. template<typename T>
  149. Size<T>& Size<T>::operator+=(const Size<T>& size) noexcept
  150. {
  151. fWidth += size.fWidth;
  152. fHeight += size.fHeight;
  153. return *this;
  154. }
  155. template<typename T>
  156. Size<T>& Size<T>::operator-=(const Size<T>& size) noexcept
  157. {
  158. fWidth -= size.fWidth;
  159. fHeight -= size.fHeight;
  160. return *this;
  161. }
  162. template<typename T>
  163. Size<T>& Size<T>::operator*=(T m) noexcept
  164. {
  165. fWidth *= m;
  166. fHeight *= m;
  167. return *this;
  168. }
  169. template<typename T>
  170. Size<T>& Size<T>::operator/=(T d) noexcept
  171. {
  172. fWidth /= d;
  173. fHeight /= d;
  174. return *this;
  175. }
  176. template<typename T>
  177. bool Size<T>::operator==(const Size<T>& size) const noexcept
  178. {
  179. return (fWidth == size.fWidth && fHeight == size.fHeight);
  180. }
  181. template<typename T>
  182. bool Size<T>::operator!=(const Size<T>& size) const noexcept
  183. {
  184. return !operator==(size);
  185. }
  186. // -----------------------------------------------------------------------
  187. // Rectangle
  188. template<typename T>
  189. Rectangle<T>::Rectangle() noexcept
  190. : fPos(0, 0),
  191. fSize(0, 0)
  192. {
  193. }
  194. template<typename T>
  195. Rectangle<T>::Rectangle(T x, T y, T width, T height) noexcept
  196. : fPos(x, y),
  197. fSize(width, height)
  198. {
  199. }
  200. template<typename T>
  201. Rectangle<T>::Rectangle(T x, T y, const Size<T>& size) noexcept
  202. : fPos(x, y),
  203. fSize(size)
  204. {
  205. }
  206. template<typename T>
  207. Rectangle<T>::Rectangle(const Point<T>& pos, T width, T height) noexcept
  208. : fPos(pos),
  209. fSize(width, height)
  210. {
  211. }
  212. template<typename T>
  213. Rectangle<T>::Rectangle(const Point<T>& pos, const Size<T>& size) noexcept
  214. : fPos(pos),
  215. fSize(size)
  216. {
  217. }
  218. template<typename T>
  219. Rectangle<T>::Rectangle(const Rectangle<T>& rect) noexcept
  220. : fPos(rect.fPos),
  221. fSize(rect.fSize)
  222. {
  223. }
  224. template<typename T>
  225. T Rectangle<T>::getX() const noexcept
  226. {
  227. return fPos.fX;
  228. }
  229. template<typename T>
  230. T Rectangle<T>::getY() const noexcept
  231. {
  232. return fPos.fY;
  233. }
  234. template<typename T>
  235. T Rectangle<T>::getWidth() const noexcept
  236. {
  237. return fSize.fWidth;
  238. }
  239. template<typename T>
  240. T Rectangle<T>::getHeight() const noexcept
  241. {
  242. return fSize.fHeight;
  243. }
  244. template<typename T>
  245. const Point<T>& Rectangle<T>::getPos() const noexcept
  246. {
  247. return fPos;
  248. }
  249. template<typename T>
  250. const Size<T>& Rectangle<T>::getSize() const noexcept
  251. {
  252. return fSize;
  253. }
  254. template<typename T>
  255. bool Rectangle<T>::contains(T x, T y) const noexcept
  256. {
  257. return (x >= fPos.fX && y >= fPos.fY && x <= fPos.fX+fSize.fWidth && y <= fPos.fY+fSize.fHeight);
  258. }
  259. template<typename T>
  260. bool Rectangle<T>::contains(const Point<T>& pos) const noexcept
  261. {
  262. return contains(pos.fX, pos.fY);
  263. }
  264. template<typename T>
  265. bool Rectangle<T>::containsX(T x) const noexcept
  266. {
  267. return (x >= fPos.fX && x <= fPos.fX + fSize.fWidth);
  268. }
  269. template<typename T>
  270. bool Rectangle<T>::containsY(T y) const noexcept
  271. {
  272. return (y >= fPos.fY && y <= fPos.fY + fSize.fHeight);
  273. }
  274. template<typename T>
  275. void Rectangle<T>::setX(T x) noexcept
  276. {
  277. fPos.fX = x;
  278. }
  279. template<typename T>
  280. void Rectangle<T>::setY(T y) noexcept
  281. {
  282. fPos.fY = y;
  283. }
  284. template<typename T>
  285. void Rectangle<T>::setPos(T x, T y) noexcept
  286. {
  287. fPos.fX = x;
  288. fPos.fY = y;
  289. }
  290. template<typename T>
  291. void Rectangle<T>::setPos(const Point<T>& pos) noexcept
  292. {
  293. fPos = pos;
  294. }
  295. template<typename T>
  296. void Rectangle<T>::move(T x, T y) noexcept
  297. {
  298. fPos.fX += x;
  299. fPos.fY += y;
  300. }
  301. template<typename T>
  302. void Rectangle<T>::move(const Point<T>& pos) noexcept
  303. {
  304. fPos += pos;
  305. }
  306. template<typename T>
  307. void Rectangle<T>::setWidth(T width) noexcept
  308. {
  309. fSize.fWidth = width;
  310. }
  311. template<typename T>
  312. void Rectangle<T>::setHeight(T height) noexcept
  313. {
  314. fSize.fHeight = height;
  315. }
  316. template<typename T>
  317. void Rectangle<T>::setSize(T width, T height) noexcept
  318. {
  319. fSize.fWidth = width;
  320. fSize.fHeight = height;
  321. }
  322. template<typename T>
  323. void Rectangle<T>::setSize(const Size<T>& size) noexcept
  324. {
  325. fSize = size;
  326. }
  327. template<typename T>
  328. Rectangle<T>& Rectangle<T>::operator=(const Rectangle<T>& rect) noexcept
  329. {
  330. fPos = rect.fPos;
  331. fSize = rect.fSize;
  332. return *this;
  333. }
  334. // -----------------------------------------------------------------------
  335. // Possible template data types
  336. template class Point<int>;
  337. template class Point<long>;
  338. template class Point<float>;
  339. template class Point<double>;
  340. template class Size<int>;
  341. template class Size<long>;
  342. template class Size<float>;
  343. template class Size<double>;
  344. template class Rectangle<int>;
  345. template class Rectangle<long>;
  346. template class Rectangle<float>;
  347. template class Rectangle<double>;
  348. // -----------------------------------------------------------------------
  349. END_NAMESPACE_DGL