Geometries.h 5.2 KB

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