Geometries.h 5.2 KB

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