Geometries.h 5.2 KB

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