Rect.cs 9.4 KB

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