暫無描述
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Rectangle.cs 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. using System;
  2. namespace PDNWrapper
  3. {
  4. // Mimics System.Drawing.Rectangle
  5. internal struct Rectangle
  6. {
  7. public static readonly Rectangle Empty = new Rectangle();
  8. private int x;
  9. private int y;
  10. private int width;
  11. private int height;
  12. public Rectangle(int x, int y, int width, int height)
  13. {
  14. this.x = x;
  15. this.y = y;
  16. this.width = width;
  17. this.height = height;
  18. }
  19. public static Rectangle FromLTRB(int left, int top, int right, int bottom)
  20. {
  21. return new Rectangle(left,
  22. top,
  23. right - left,
  24. bottom - top);
  25. }
  26. public Size Size
  27. {
  28. get
  29. {
  30. return new Size(Width, Height);
  31. }
  32. set
  33. {
  34. this.Width = value.Width;
  35. this.Height = value.Height;
  36. }
  37. }
  38. public int X
  39. {
  40. get
  41. {
  42. return x;
  43. }
  44. set
  45. {
  46. x = value;
  47. }
  48. }
  49. public int Y
  50. {
  51. get
  52. {
  53. return y;
  54. }
  55. set
  56. {
  57. y = value;
  58. }
  59. }
  60. public int Width
  61. {
  62. get
  63. {
  64. return width;
  65. }
  66. set
  67. {
  68. width = value;
  69. }
  70. }
  71. public int Height
  72. {
  73. get
  74. {
  75. return height;
  76. }
  77. set
  78. {
  79. height = value;
  80. }
  81. }
  82. public int Left
  83. {
  84. get
  85. {
  86. return X;
  87. }
  88. }
  89. public int Top
  90. {
  91. get
  92. {
  93. return Y;
  94. }
  95. }
  96. public int Right
  97. {
  98. get
  99. {
  100. return X + Width;
  101. }
  102. }
  103. public int Bottom
  104. {
  105. get
  106. {
  107. return Y + Height;
  108. }
  109. }
  110. public bool IsEmpty
  111. {
  112. get
  113. {
  114. return height == 0 && width == 0 && x == 0 && y == 0;
  115. }
  116. }
  117. public override bool Equals(object obj)
  118. {
  119. if (!(obj is Rectangle))
  120. return false;
  121. Rectangle comp = (Rectangle)obj;
  122. return (comp.X == this.X) &&
  123. (comp.Y == this.Y) &&
  124. (comp.Width == this.Width) &&
  125. (comp.Height == this.Height);
  126. }
  127. public static bool operator==(Rectangle left, Rectangle right)
  128. {
  129. return (left.X == right.X
  130. && left.Y == right.Y
  131. && left.Width == right.Width
  132. && left.Height == right.Height);
  133. }
  134. public static bool operator!=(Rectangle left, Rectangle right)
  135. {
  136. return !(left == right);
  137. }
  138. public bool Contains(int x, int y)
  139. {
  140. return this.X <= x &&
  141. x < this.X + this.Width &&
  142. this.Y <= y &&
  143. y < this.Y + this.Height;
  144. }
  145. public bool Contains(Rectangle rect)
  146. {
  147. return (this.X <= rect.X) &&
  148. ((rect.X + rect.Width) <= (this.X + this.Width)) &&
  149. (this.Y <= rect.Y) &&
  150. ((rect.Y + rect.Height) <= (this.Y + this.Height));
  151. }
  152. public override int GetHashCode()
  153. {
  154. return (int)((UInt32)X ^
  155. (((UInt32)Y << 13) | ((UInt32)Y >> 19)) ^
  156. (((UInt32)Width << 26) | ((UInt32)Width >> 6)) ^
  157. (((UInt32)Height << 7) | ((UInt32)Height >> 25)));
  158. }
  159. public void Inflate(int width, int height)
  160. {
  161. this.X -= width;
  162. this.Y -= height;
  163. this.Width += 2 * width;
  164. this.Height += 2 * height;
  165. }
  166. public void Inflate(Size size)
  167. {
  168. Inflate(size.Width, size.Height);
  169. }
  170. public static Rectangle Inflate(Rectangle rect, int x, int y)
  171. {
  172. Rectangle r = rect;
  173. r.Inflate(x, y);
  174. return r;
  175. }
  176. public void Intersect(Rectangle rect)
  177. {
  178. Rectangle result = Rectangle.Intersect(rect, this);
  179. this.X = result.X;
  180. this.Y = result.Y;
  181. this.Width = result.Width;
  182. this.Height = result.Height;
  183. }
  184. public static Rectangle Intersect(Rectangle a, Rectangle b)
  185. {
  186. int x1 = Math.Max(a.X, b.X);
  187. int x2 = Math.Min(a.X + a.Width, b.X + b.Width);
  188. int y1 = Math.Max(a.Y, b.Y);
  189. int y2 = Math.Min(a.Y + a.Height, b.Y + b.Height);
  190. if (x2 >= x1
  191. && y2 >= y1)
  192. {
  193. return new Rectangle(x1, y1, x2 - x1, y2 - y1);
  194. }
  195. return Rectangle.Empty;
  196. }
  197. public bool IntersectsWith(Rectangle rect)
  198. {
  199. return (rect.X < this.X + this.Width) &&
  200. (this.X < (rect.X + rect.Width)) &&
  201. (rect.Y < this.Y + this.Height) &&
  202. (this.Y < rect.Y + rect.Height);
  203. }
  204. public static Rectangle Union(Rectangle a, Rectangle b)
  205. {
  206. int x1 = Math.Min(a.X, b.X);
  207. int x2 = Math.Max(a.X + a.Width, b.X + b.Width);
  208. int y1 = Math.Min(a.Y, b.Y);
  209. int y2 = Math.Max(a.Y + a.Height, b.Y + b.Height);
  210. return new Rectangle(x1, y1, x2 - x1, y2 - y1);
  211. }
  212. public void Offset(int x, int y)
  213. {
  214. this.X += x;
  215. this.Y += y;
  216. }
  217. public override string ToString()
  218. {
  219. return "{X=" + X.ToString() + ",Y=" + Y.ToString() +
  220. ",Width=" + Width.ToString() +
  221. ",Height=" + Height.ToString() + "}";
  222. }
  223. }
  224. }