2
0

Rect.cs 9.8 KB

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