Rect.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. //
  2. // System.Drawing.Rectangle.cs
  3. //
  4. // Author:
  5. // Mike Kestner ([email protected])
  6. //
  7. // Copyright (C) 2001 Mike Kestner
  8. // Copyright (C) 2004 Novell, Inc. http://www.novell.com
  9. //
  10. using System;
  11. namespace Terminal
  12. {
  13. public struct Rect
  14. {
  15. public int X, Y, Width, Height;
  16. /// <summary>
  17. /// Empty Shared Field
  18. /// </summary>
  19. ///
  20. /// <remarks>
  21. /// An uninitialized Rectangle Structure.
  22. /// </remarks>
  23. public static readonly Rect Empty;
  24. /// <summary>
  25. /// FromLTRB Shared Method
  26. /// </summary>
  27. ///
  28. /// <remarks>
  29. /// Produces a Rectangle structure from left, top, right
  30. /// and bottom coordinates.
  31. /// </remarks>
  32. public static Rect FromLTRB (int left, int top,
  33. int right, int bottom)
  34. {
  35. return new Rect (left, top, right - left,
  36. bottom - top);
  37. }
  38. /// <summary>
  39. /// Inflate Shared Method
  40. /// </summary>
  41. ///
  42. /// <remarks>
  43. /// Produces a new Rectangle by inflating an existing
  44. /// Rectangle by the specified coordinate values.
  45. /// </remarks>
  46. public static Rect Inflate (Rect rect, int x, int y)
  47. {
  48. Rect r = new Rect (rect.Location, rect.Size);
  49. r.Inflate (x, y);
  50. return r;
  51. }
  52. /// <summary>
  53. /// Inflate Method
  54. /// </summary>
  55. ///
  56. /// <remarks>
  57. /// Inflates the Rectangle by a specified width and height.
  58. /// </remarks>
  59. public void Inflate (int width, int height)
  60. {
  61. Inflate (new Size (width, height));
  62. }
  63. /// <summary>
  64. /// Inflate Method
  65. /// </summary>
  66. ///
  67. /// <remarks>
  68. /// Inflates the Rectangle by a specified Size.
  69. /// </remarks>
  70. public void Inflate (Size size)
  71. {
  72. X -= size.Width;
  73. Y -= size.Height;
  74. Width += size.Width * 2;
  75. Height += size.Height * 2;
  76. }
  77. /// <summary>
  78. /// Intersect Shared Method
  79. /// </summary>
  80. ///
  81. /// <remarks>
  82. /// Produces a new Rectangle by intersecting 2 existing
  83. /// Rectangles. Returns null if there is no intersection.
  84. /// </remarks>
  85. public static Rect Intersect (Rect a, Rect b)
  86. {
  87. // MS.NET returns a non-empty rectangle if the two rectangles
  88. // touch each other
  89. if (!a.IntersectsWithInclusive (b))
  90. return Empty;
  91. return Rect.FromLTRB (
  92. Math.Max (a.Left, b.Left),
  93. Math.Max (a.Top, b.Top),
  94. Math.Min (a.Right, b.Right),
  95. Math.Min (a.Bottom, b.Bottom));
  96. }
  97. /// <summary>
  98. /// Intersect Method
  99. /// </summary>
  100. ///
  101. /// <remarks>
  102. /// Replaces the Rectangle with the intersection of itself
  103. /// and another Rectangle.
  104. /// </remarks>
  105. public void Intersect (Rect rect)
  106. {
  107. this = Rect.Intersect (this, rect);
  108. }
  109. /// <summary>
  110. /// Union Shared Method
  111. /// </summary>
  112. ///
  113. /// <remarks>
  114. /// Produces a new Rectangle from the union of 2 existing
  115. /// Rectangles.
  116. /// </remarks>
  117. public static Rect Union (Rect a, Rect b)
  118. {
  119. return FromLTRB (Math.Min (a.Left, b.Left),
  120. Math.Min (a.Top, b.Top),
  121. Math.Max (a.Right, b.Right),
  122. Math.Max (a.Bottom, b.Bottom));
  123. }
  124. /// <summary>
  125. /// Equality Operator
  126. /// </summary>
  127. ///
  128. /// <remarks>
  129. /// Compares two Rectangle objects. The return value is
  130. /// based on the equivalence of the Location and Size
  131. /// properties of the two Rectangles.
  132. /// </remarks>
  133. public static bool operator == (Rect left, Rect right)
  134. {
  135. return ((left.Location == right.Location) &&
  136. (left.Size == right.Size));
  137. }
  138. /// <summary>
  139. /// Inequality Operator
  140. /// </summary>
  141. ///
  142. /// <remarks>
  143. /// Compares two Rectangle objects. The return value is
  144. /// based on the equivalence of the Location and Size
  145. /// properties of the two Rectangles.
  146. /// </remarks>
  147. public static bool operator != (Rect left, Rect right)
  148. {
  149. return ((left.Location != right.Location) ||
  150. (left.Size != right.Size));
  151. }
  152. // -----------------------
  153. // Public Constructors
  154. // -----------------------
  155. /// <summary>
  156. /// Rectangle Constructor
  157. /// </summary>
  158. ///
  159. /// <remarks>
  160. /// Creates a Rectangle from Point and Size values.
  161. /// </remarks>
  162. public Rect (Point location, Size size)
  163. {
  164. X = location.X;
  165. Y = location.Y;
  166. Width = size.Width;
  167. Height = size.Height;
  168. }
  169. /// <summary>
  170. /// Rectangle Constructor
  171. /// </summary>
  172. ///
  173. /// <remarks>
  174. /// Creates a Rectangle from a specified x,y location and
  175. /// width and height values.
  176. /// </remarks>
  177. public Rect (int x, int y, int width, int height)
  178. {
  179. this.X = x;
  180. this.Y = y;
  181. this.Width = width;
  182. this.Height = height;
  183. }
  184. /// <summary>
  185. /// Bottom Property
  186. /// </summary>
  187. ///
  188. /// <remarks>
  189. /// The Y coordinate of the bottom edge of the Rectangle.
  190. /// Read only.
  191. /// </remarks>
  192. public int Bottom {
  193. get {
  194. return Y + Height;
  195. }
  196. }
  197. /// <summary>
  198. /// IsEmpty Property
  199. /// </summary>
  200. ///
  201. /// <remarks>
  202. /// Indicates if the width or height are zero. Read only.
  203. /// </remarks>
  204. public bool IsEmpty {
  205. get {
  206. return ((X == 0) && (Y == 0) && (Width == 0) && (Height == 0));
  207. }
  208. }
  209. /// <summary>
  210. /// Left Property
  211. /// </summary>
  212. ///
  213. /// <remarks>
  214. /// The X coordinate of the left edge of the Rectangle.
  215. /// Read only.
  216. /// </remarks>
  217. public int Left {
  218. get {
  219. return X;
  220. }
  221. }
  222. /// <summary>
  223. /// Location Property
  224. /// </summary>
  225. ///
  226. /// <remarks>
  227. /// The Location of the top-left corner of the Rectangle.
  228. /// </remarks>
  229. public Point Location {
  230. get {
  231. return new Point (X, Y);
  232. }
  233. set {
  234. X = value.X;
  235. Y = value.Y;
  236. }
  237. }
  238. /// <summary>
  239. /// Right Property
  240. /// </summary>
  241. ///
  242. /// <remarks>
  243. /// The X coordinate of the right edge of the Rectangle.
  244. /// Read only.
  245. /// </remarks>
  246. public int Right {
  247. get {
  248. return X + Width;
  249. }
  250. }
  251. /// <summary>
  252. /// Size Property
  253. /// </summary>
  254. ///
  255. /// <remarks>
  256. /// The Size of the Rectangle.
  257. /// </remarks>
  258. public Size Size {
  259. get {
  260. return new Size (Width, Height);
  261. }
  262. set {
  263. Width = value.Width;
  264. Height = value.Height;
  265. }
  266. }
  267. /// <summary>
  268. /// Top Property
  269. /// </summary>
  270. ///
  271. /// <remarks>
  272. /// The Y coordinate of the top edge of the Rectangle.
  273. /// Read only.
  274. /// </remarks>
  275. public int Top {
  276. get {
  277. return Y;
  278. }
  279. }
  280. /// <summary>
  281. /// Contains Method
  282. /// </summary>
  283. ///
  284. /// <remarks>
  285. /// Checks if an x,y coordinate lies within this Rectangle.
  286. /// </remarks>
  287. public bool Contains (int x, int y)
  288. {
  289. return ((x >= Left) && (x < Right) &&
  290. (y >= Top) && (y < Bottom));
  291. }
  292. /// <summary>
  293. /// Contains Method
  294. /// </summary>
  295. ///
  296. /// <remarks>
  297. /// Checks if a Point lies within this Rectangle.
  298. /// </remarks>
  299. public bool Contains (Point pt)
  300. {
  301. return Contains (pt.X, pt.Y);
  302. }
  303. /// <summary>
  304. /// Contains Method
  305. /// </summary>
  306. ///
  307. /// <remarks>
  308. /// Checks if a Rectangle lies entirely within this
  309. /// Rectangle.
  310. /// </remarks>
  311. public bool Contains (Rect rect)
  312. {
  313. return (rect == Intersect (this, rect));
  314. }
  315. /// <summary>
  316. /// Equals Method
  317. /// </summary>
  318. ///
  319. /// <remarks>
  320. /// Checks equivalence of this Rectangle and another object.
  321. /// </remarks>
  322. public override bool Equals (object obj)
  323. {
  324. if (!(obj is Rect))
  325. return false;
  326. return (this == (Rect) obj);
  327. }
  328. /// <summary>
  329. /// GetHashCode Method
  330. /// </summary>
  331. ///
  332. /// <remarks>
  333. /// Calculates a hashing value.
  334. /// </remarks>
  335. public override int GetHashCode ()
  336. {
  337. return (Height + Width) ^ X + Y;
  338. }
  339. /// <summary>
  340. /// IntersectsWith Method
  341. /// </summary>
  342. ///
  343. /// <remarks>
  344. /// Checks if a Rectangle intersects with this one.
  345. /// </remarks>
  346. public bool IntersectsWith (Rect rect)
  347. {
  348. return !((Left >= rect.Right) || (Right <= rect.Left) ||
  349. (Top >= rect.Bottom) || (Bottom <= rect.Top));
  350. }
  351. private bool IntersectsWithInclusive (Rect r)
  352. {
  353. return !((Left > r.Right) || (Right < r.Left) ||
  354. (Top > r.Bottom) || (Bottom < r.Top));
  355. }
  356. /// <summary>
  357. /// Offset Method
  358. /// </summary>
  359. ///
  360. /// <remarks>
  361. /// Moves the Rectangle a specified distance.
  362. /// </remarks>
  363. public void Offset (int x, int y)
  364. {
  365. this.X += x;
  366. this.Y += y;
  367. }
  368. /// <summary>
  369. /// Offset Method
  370. /// </summary>
  371. ///
  372. /// <remarks>
  373. /// Moves the Rectangle a specified distance.
  374. /// </remarks>
  375. public void Offset (Point pos)
  376. {
  377. X += pos.X;
  378. Y += pos.Y;
  379. }
  380. /// <summary>
  381. /// ToString Method
  382. /// </summary>
  383. ///
  384. /// <remarks>
  385. /// Formats the Rectangle as a string in (x,y,w,h) notation.
  386. /// </remarks>
  387. public override string ToString ()
  388. {
  389. return String.Format ("{{X={0},Y={1},Width={2},Height={3}}}",
  390. X, Y, Width, Height);
  391. }
  392. }
  393. }