Rectangle2D.cs 5.7 KB

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