2
0

Geometries.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*
  2. * Geometries.h, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #pragma once
  11. #include <SDL_video.h>
  12. #include "../../lib/int3.h"
  13. struct SDL_MouseMotionEvent;
  14. // A point with x/y coordinate, used mostly for graphic rendering
  15. struct Point
  16. {
  17. int x, y;
  18. //constructors
  19. Point()
  20. {
  21. x = y = 0;
  22. };
  23. Point(int X, int Y)
  24. :x(X),y(Y)
  25. {};
  26. Point(const int3 &a)
  27. :x(a.x),y(a.y)
  28. {}
  29. Point(const SDL_MouseMotionEvent &a);
  30. template<typename T>
  31. Point operator+(const T &b) const
  32. {
  33. return Point(x+b.x,y+b.y);
  34. }
  35. template<typename T>
  36. Point operator/(const T &div) const
  37. {
  38. return Point(x/div, y/div);
  39. }
  40. template<typename T>
  41. Point operator*(const T &mul) const
  42. {
  43. return Point(x*mul, y*mul);
  44. }
  45. template<typename T>
  46. Point& operator+=(const T &b)
  47. {
  48. x += b.x;
  49. y += b.y;
  50. return *this;
  51. }
  52. template<typename T>
  53. Point operator-(const T &b) const
  54. {
  55. return Point(x - b.x, y - b.y);
  56. }
  57. template<typename T>
  58. Point& operator-=(const T &b)
  59. {
  60. x -= b.x;
  61. y -= b.y;
  62. return *this;
  63. }
  64. bool operator<(const Point &b) const //product order
  65. {
  66. return x < b.x && y < b.y;
  67. }
  68. template<typename T> Point& operator=(const T &t)
  69. {
  70. x = t.x;
  71. y = t.y;
  72. return *this;
  73. }
  74. template<typename T> bool operator==(const T &t) const
  75. {
  76. return x == t.x && y == t.y;
  77. }
  78. template<typename T> bool operator!=(const T &t) const
  79. {
  80. return !(*this == t);
  81. }
  82. };
  83. /// Rectangle class, which have a position and a size
  84. struct Rect : public SDL_Rect
  85. {
  86. Rect()//default c-tor
  87. {
  88. x = y = w = h = -1;
  89. }
  90. Rect(int X, int Y, int W, int H)
  91. {
  92. x = X;
  93. y = Y;
  94. w = W;
  95. h = H;
  96. }
  97. Rect(const Point & position, const Point & size)
  98. {
  99. x = position.x;
  100. y = position.y;
  101. w = size.x;
  102. h = size.y;
  103. }
  104. Rect(const SDL_Rect & r)
  105. {
  106. x = r.x;
  107. y = r.y;
  108. w = r.w;
  109. h = r.h;
  110. }
  111. explicit Rect(const SDL_Surface * const &surf)
  112. {
  113. x = y = 0;
  114. w = surf->w;
  115. h = surf->h;
  116. }
  117. Rect centerIn(const Rect &r);
  118. static Rect createCentered(int w, int h);
  119. static Rect around(const Rect &r, int width = 1); //creates rect around another
  120. bool isIn(int qx, int qy) const //determines if given point lies inside rect
  121. {
  122. if (qx > x && qx<x+w && qy>y && qy<y+h)
  123. return true;
  124. return false;
  125. }
  126. bool isIn(const Point & q) const //determines if given point lies inside rect
  127. {
  128. return isIn(q.x,q.y);
  129. }
  130. Point topLeft() const //top left corner of this rect
  131. {
  132. return Point(x,y);
  133. }
  134. Point topRight() const //top right corner of this rect
  135. {
  136. return Point(x+w,y);
  137. }
  138. Point bottomLeft() const //bottom left corner of this rect
  139. {
  140. return Point(x,y+h);
  141. }
  142. Point bottomRight() const //bottom right corner of this rect
  143. {
  144. return Point(x+w,y+h);
  145. }
  146. Rect operator+(const Rect &p) const //moves this rect by p's rect position
  147. {
  148. return Rect(x+p.x,y+p.y,w,h);
  149. }
  150. Rect operator+(const Point &p) const //moves this rect by p's point position
  151. {
  152. return Rect(x+p.x,y+p.y,w,h);
  153. }
  154. Rect& operator=(const Point &p) //assignment operator
  155. {
  156. x = p.x;
  157. y = p.y;
  158. return *this;
  159. }
  160. Rect& operator=(const Rect &p) //assignment operator
  161. {
  162. x = p.x;
  163. y = p.y;
  164. w = p.w;
  165. h = p.h;
  166. return *this;
  167. }
  168. Rect& operator+=(const Rect &p) //works as operator+
  169. {
  170. x += p.x;
  171. y += p.y;
  172. return *this;
  173. }
  174. Rect& operator+=(const Point &p) //works as operator+
  175. {
  176. x += p.x;
  177. y += p.y;
  178. return *this;
  179. }
  180. Rect& operator-=(const Rect &p) //works as operator+
  181. {
  182. x -= p.x;
  183. y -= p.y;
  184. return *this;
  185. }
  186. Rect& operator-=(const Point &p) //works as operator+
  187. {
  188. x -= p.x;
  189. y -= p.y;
  190. return *this;
  191. }
  192. template<typename T> Rect operator-(const T &t)
  193. {
  194. return Rect(x - t.x, y - t.y, w, h);
  195. }
  196. Rect operator&(const Rect &p) const //rect intersection
  197. {
  198. bool intersect = true;
  199. if(p.topLeft().y < y && p.bottomLeft().y < y) //rect p is above *this
  200. {
  201. intersect = false;
  202. }
  203. else if(p.topLeft().y > y+h && p.bottomLeft().y > y+h) //rect p is below *this
  204. {
  205. intersect = false;
  206. }
  207. else if(p.topLeft().x > x+w && p.topRight().x > x+w) //rect p is on the right hand side of this
  208. {
  209. intersect = false;
  210. }
  211. else if(p.topLeft().x < x && p.topRight().x < x) //rect p is on the left hand side of this
  212. {
  213. intersect = false;
  214. }
  215. if(intersect)
  216. {
  217. Rect ret;
  218. ret.x = std::max(this->x, p.x);
  219. ret.y = std::max(this->y, p.y);
  220. Point bR; //bottomRight point of returned rect
  221. bR.x = std::min(this->w+this->x, p.w+p.x);
  222. bR.y = std::min(this->h+this->y, p.h+p.y);
  223. ret.w = bR.x - ret.x;
  224. ret.h = bR.y - ret.y;
  225. return ret;
  226. }
  227. else
  228. {
  229. return Rect();
  230. }
  231. }
  232. Rect operator|(const Rect &p) const //union of two rects
  233. {
  234. Rect ret;
  235. ret.x = std::min(p.x, this->x);
  236. ret.y = std::min(p.y, this->y);
  237. int x2 = std::max(p.x+p.w, this->x+this->w);
  238. int y2 = std::max(p.y+p.h, this->y+this->h);
  239. ret.w = x2 -ret.x;
  240. ret.h = y2 -ret.y;
  241. return ret;
  242. }
  243. };