Point.cs 5.5 KB

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