Geometries.h 5.2 KB

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