Point.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. //
  2. // System.Drawing.Point.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. using System.Globalization;
  12. namespace Terminal.Gui
  13. {
  14. /// <summary>
  15. /// Represents an ordered pair of integer x- and y-coordinates that defines a point in a two-dimensional plane.
  16. /// </summary>
  17. public struct Point
  18. {
  19. /// <summary>
  20. /// Gets or sets the x-coordinate of this Point.
  21. /// </summary>
  22. [System.Text.Json.Serialization.JsonInclude]
  23. public int X;
  24. /// <summary>
  25. /// Gets or sets the y-coordinate of this Point.
  26. /// </summary>
  27. [System.Text.Json.Serialization.JsonInclude]
  28. public int Y;
  29. // -----------------------
  30. // Public Shared Members
  31. // -----------------------
  32. /// <summary>
  33. /// Empty Shared Field
  34. /// </summary>
  35. ///
  36. /// <remarks>
  37. /// An uninitialized Point Structure.
  38. /// </remarks>
  39. public static readonly Point Empty;
  40. /// <summary>
  41. /// Addition Operator
  42. /// </summary>
  43. ///
  44. /// <remarks>
  45. /// Translates a Point using the Width and Height
  46. /// properties of the given <typeref>Size</typeref>.
  47. /// </remarks>
  48. public static Point operator + (Point pt, Size sz)
  49. {
  50. return new Point (pt.X + sz.Width, pt.Y + sz.Height);
  51. }
  52. /// <summary>
  53. /// Equality Operator
  54. /// </summary>
  55. ///
  56. /// <remarks>
  57. /// Compares two Point objects. The return value is
  58. /// based on the equivalence of the X and Y properties
  59. /// of the two points.
  60. /// </remarks>
  61. public static bool operator == (Point left, Point right)
  62. {
  63. return ((left.X == right.X) && (left.Y == right.Y));
  64. }
  65. /// <summary>
  66. /// Inequality Operator
  67. /// </summary>
  68. ///
  69. /// <remarks>
  70. /// Compares two Point objects. The return value is
  71. /// based on the equivalence of the X and Y properties
  72. /// of the two points.
  73. /// </remarks>
  74. public static bool operator != (Point left, Point right)
  75. {
  76. return ((left.X != right.X) || (left.Y != right.Y));
  77. }
  78. /// <summary>
  79. /// Subtraction Operator
  80. /// </summary>
  81. ///
  82. /// <remarks>
  83. /// Translates a Point using the negation of the Width
  84. /// and Height properties of the given Size.
  85. /// </remarks>
  86. public static Point operator - (Point pt, Size sz)
  87. {
  88. return new Point (pt.X - sz.Width, pt.Y - sz.Height);
  89. }
  90. /// <summary>
  91. /// Point to Size Conversion
  92. /// </summary>
  93. ///
  94. /// <remarks>
  95. /// Returns a Size based on the Coordinates of a given
  96. /// Point. Requires explicit cast.
  97. /// </remarks>
  98. public static explicit operator Size (Point p)
  99. {
  100. if (p.X < 0 || p.Y < 0)
  101. throw new ArgumentException ("Either Width and Height must be greater or equal to 0.");
  102. return new Size (p.X, p.Y);
  103. }
  104. // -----------------------
  105. // Public Constructors
  106. // -----------------------
  107. /// <summary>
  108. /// Point Constructor
  109. /// </summary>
  110. ///
  111. /// <remarks>
  112. /// Creates a Point from a Size value.
  113. /// </remarks>
  114. public Point (Size sz)
  115. {
  116. X = sz.Width;
  117. Y = sz.Height;
  118. }
  119. /// <summary>
  120. /// Point Constructor
  121. /// </summary>
  122. ///
  123. /// <remarks>
  124. /// Creates a Point from a specified x,y coordinate pair.
  125. /// </remarks>
  126. public Point (int x, int y)
  127. {
  128. this.X = x;
  129. this.Y = y;
  130. }
  131. // -----------------------
  132. // Public Instance Members
  133. // -----------------------
  134. /// <summary>
  135. /// IsEmpty Property
  136. /// </summary>
  137. ///
  138. /// <remarks>
  139. /// Indicates if both X and Y are zero.
  140. /// </remarks>
  141. [System.Text.Json.Serialization.JsonIgnore]
  142. public bool IsEmpty {
  143. get {
  144. return ((X == 0) && (Y == 0));
  145. }
  146. }
  147. /// <summary>
  148. /// Equals Method
  149. /// </summary>
  150. ///
  151. /// <remarks>
  152. /// Checks equivalence of this Point and another object.
  153. /// </remarks>
  154. public override bool Equals (object obj)
  155. {
  156. if (!(obj is Point))
  157. return false;
  158. return (this == (Point) obj);
  159. }
  160. /// <summary>
  161. /// GetHashCode Method
  162. /// </summary>
  163. ///
  164. /// <remarks>
  165. /// Calculates a hashing value.
  166. /// </remarks>
  167. public override int GetHashCode ()
  168. {
  169. return X^Y;
  170. }
  171. /// <summary>
  172. /// Offset Method
  173. /// </summary>
  174. ///
  175. /// <remarks>
  176. /// Moves the Point a specified distance.
  177. /// </remarks>
  178. public void Offset (int dx, int dy)
  179. {
  180. X += dx;
  181. Y += dy;
  182. }
  183. /// <summary>
  184. /// ToString Method
  185. /// </summary>
  186. ///
  187. /// <remarks>
  188. /// Formats the Point as a string in coordinate notation.
  189. /// </remarks>
  190. public override string ToString ()
  191. {
  192. return string.Format ("{{X={0},Y={1}}}", X.ToString (CultureInfo.InvariantCulture),
  193. Y.ToString (CultureInfo.InvariantCulture));
  194. }
  195. /// <summary>
  196. /// Adds the specified Size to the specified Point.
  197. /// </summary>
  198. /// <returns>The Point that is the result of the addition operation.</returns>
  199. /// <param name="pt">The Point to add.</param>
  200. /// <param name="sz">The Size to add.</param>
  201. public static Point Add (Point pt, Size sz)
  202. {
  203. return new Point (pt.X + sz.Width, pt.Y + sz.Height);
  204. }
  205. /// <summary>
  206. /// Translates this Point by the specified Point.
  207. /// </summary>
  208. /// <returns>The offset.</returns>
  209. /// <param name="p">The Point used offset this Point.</param>
  210. public void Offset (Point p)
  211. {
  212. Offset (p.X, p.Y);
  213. }
  214. /// <summary>
  215. /// Returns the result of subtracting specified Size from the specified Point.
  216. /// </summary>
  217. /// <returns>The Point that is the result of the subtraction operation.</returns>
  218. /// <param name="pt">The Point to be subtracted from.</param>
  219. /// <param name="sz">The Size to subtract from the Point.</param>
  220. public static Point Subtract (Point pt, Size sz)
  221. {
  222. return new Point (pt.X - sz.Width, pt.Y - sz.Height);
  223. }
  224. }
  225. }