Rect.cs 9.0 KB

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