TRectangle2D.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace CommonUI.Gemo
  6. {
  7. public struct TRectangle2D
  8. {
  9. public float x;
  10. public float y;
  11. public float width;
  12. public float height;
  13. public float Right
  14. {
  15. get
  16. {
  17. return x + width;
  18. }
  19. }
  20. public float Bottom
  21. {
  22. get
  23. {
  24. return y + height;
  25. }
  26. }
  27. public TRectangle2D(TRectangle2D r)
  28. {
  29. this.x = r.x;
  30. this.y = r.y;
  31. this.width = r.width;
  32. this.height = r.height;
  33. }
  34. public TRectangle2D(float x, float y, float width, float height)
  35. {
  36. this.x = x;
  37. this.y = y;
  38. this.width = width;
  39. this.height = height;
  40. }
  41. public TRectangle2D(float width, float height)
  42. {
  43. this.x = 0;
  44. this.y = 0;
  45. this.width = width;
  46. this.height = height;
  47. }
  48. public void setBounds(TRectangle2D r)
  49. {
  50. setBounds(r.x, r.y, r.width, r.height);
  51. }
  52. public void setBounds(float x, float y, float width, float height)
  53. {
  54. this.x = x;
  55. this.y = y;
  56. this.width = width;
  57. this.height = height;
  58. }
  59. public bool contains(float x, float y)
  60. {
  61. return inside(x, y);
  62. }
  63. public bool contains(TRectangle2D r)
  64. {
  65. return contains(r.x, r.y, r.width, r.height);
  66. }
  67. public bool contains(float X, float Y, float W, float H)
  68. {
  69. float w = this.width;
  70. float h = this.height;
  71. float x = this.x;
  72. float y = this.y;
  73. if(X < x || Y < y)
  74. {
  75. return false;
  76. }
  77. w += x;
  78. W += X;
  79. if(W <= X)
  80. {
  81. if(w >= x || W > w)
  82. return false;
  83. }
  84. else
  85. {
  86. if(w >= x && W > w)
  87. return false;
  88. }
  89. h += y;
  90. H += Y;
  91. if(H <= Y)
  92. {
  93. if(h >= y || H > h)
  94. return false;
  95. }
  96. else
  97. {
  98. if(h >= y && H > h)
  99. return false;
  100. }
  101. return true;
  102. }
  103. public bool inside(float X, float Y)
  104. {
  105. float w = this.width;
  106. float h = this.height;
  107. float x = this.x;
  108. float y = this.y;
  109. if(X < x || Y < y)
  110. {
  111. return false;
  112. }
  113. w += x;
  114. h += y;
  115. return ((w < x || w > X) && (h < y || h > Y));
  116. }
  117. public bool floatersects(TRectangle2D r)
  118. {
  119. float tw = this.width;
  120. float th = this.height;
  121. float rw = r.width;
  122. float rh = r.height;
  123. if(rw <= 0 || rh <= 0 || tw <= 0 || th <= 0)
  124. {
  125. return false;
  126. }
  127. float tx = this.x;
  128. float ty = this.y;
  129. float rx = r.x;
  130. float ry = r.y;
  131. rw += rx;
  132. rh += ry;
  133. tw += tx;
  134. th += ty;
  135. // overflow || floatersect
  136. return ((rw < rx || rw > tx) && (rh < ry || rh > ty)
  137. && (tw < tx || tw > rx) && (th < ty || th > ry));
  138. }
  139. public TRectangle2D floatersection(TRectangle2D r)
  140. {
  141. return floatersection(r.x, r.y, r.width, r.height);
  142. }
  143. public TRectangle2D floatersection(float x, float y, float w, float h)
  144. {
  145. float tx1 = this.x;
  146. float ty1 = this.y;
  147. float rx1 = x;
  148. float ry1 = y;
  149. float tx2 = tx1;
  150. tx2 += this.width;
  151. float ty2 = ty1;
  152. ty2 += this.height;
  153. float rx2 = rx1;
  154. rx2 += w;
  155. float ry2 = ry1;
  156. ry2 += h;
  157. if(tx1 < rx1)
  158. tx1 = rx1;
  159. if(ty1 < ry1)
  160. ty1 = ry1;
  161. if(tx2 > rx2)
  162. tx2 = rx2;
  163. if(ty2 > ry2)
  164. ty2 = ry2;
  165. tx2 -= tx1;
  166. ty2 -= ty1;
  167. // tx2,ty2 will never overflow (they will never be
  168. // larger than the smallest of the two source w,h)
  169. // they might underflow, though...
  170. if(tx2 < float.MinValue)
  171. tx2 = float.MinValue;
  172. if(ty2 < float.MinValue)
  173. ty2 = float.MinValue;
  174. return new TRectangle2D(tx1, ty1, (float)tx2, (float)ty2);
  175. }
  176. public TRectangle2D union(TRectangle2D r)
  177. {
  178. float tx2 = this.width;
  179. float ty2 = this.height;
  180. float rx2 = r.width;
  181. float ry2 = r.height;
  182. float tx1 = this.x;
  183. float ty1 = this.y;
  184. tx2 += tx1;
  185. ty2 += ty1;
  186. float rx1 = r.x;
  187. float ry1 = r.y;
  188. rx2 += rx1;
  189. ry2 += ry1;
  190. if(tx1 > rx1)
  191. tx1 = rx1;
  192. if(ty1 > ry1)
  193. ty1 = ry1;
  194. if(tx2 < rx2)
  195. tx2 = rx2;
  196. if(ty2 < ry2)
  197. ty2 = ry2;
  198. tx2 -= tx1;
  199. ty2 -= ty1;
  200. // tx2,ty2 will never underflow since both original rectangles
  201. // were already proven to be non-empty
  202. // they might overflow, though...
  203. if(tx2 > float.MaxValue)
  204. tx2 = float.MaxValue;
  205. if(ty2 > float.MaxValue)
  206. ty2 = float.MaxValue;
  207. return new TRectangle2D(tx1, ty1, (float)tx2, (float)ty2);
  208. }
  209. public bool isEmpty()
  210. {
  211. return (width <= 0) || (height <= 0);
  212. }
  213. public TRectangle2D createfloatersection(TRectangle2D r)
  214. {
  215. return floatersection(r);
  216. }
  217. public TRectangle2D createUnion(TRectangle2D r)
  218. {
  219. return union(r);
  220. }
  221. public bool Equals(TRectangle2D r)
  222. {
  223. return ((x == r.x) &&
  224. (y == r.y) &&
  225. (width == r.width) &&
  226. (height == r.height));
  227. }
  228. public static bool IntersectRectWH(
  229. float sx1, float sy1, float sw, float sh,
  230. float dx1, float dy1, float dw, float dh)
  231. {
  232. float sx2 = sx1 + sw;
  233. float sy2 = sy1 + sh;
  234. float dx2 = dx1 + dw;
  235. float dy2 = dy1 + dh;
  236. if(sx2 < dx1)
  237. return false;
  238. if(sx1 > dx2)
  239. return false;
  240. if(sy2 < dy1)
  241. return false;
  242. if(sy1 > dy2)
  243. return false;
  244. return true;
  245. }
  246. public static bool IncludeRectPointWH(
  247. float sx1, float sy1,
  248. float sw, float sh,
  249. float dx, float dy)
  250. {
  251. float sx2 = sx1 + sw;
  252. float sy2 = sy1 + sh;
  253. if(sx2 < dx)
  254. return false;
  255. if(sx1 > dx)
  256. return false;
  257. if(sy2 < dy)
  258. return false;
  259. if(sy1 > dy)
  260. return false;
  261. return true;
  262. }
  263. }
  264. }