Point.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. return new Size (p.X, p.Y);
  99. }
  100. // -----------------------
  101. // Public Constructors
  102. // -----------------------
  103. /// <summary>
  104. /// Point Constructor
  105. /// </summary>
  106. ///
  107. /// <remarks>
  108. /// Creates a Point from a Size value.
  109. /// </remarks>
  110. public Point (Size sz)
  111. {
  112. X = sz.Width;
  113. Y = sz.Height;
  114. }
  115. /// <summary>
  116. /// Point Constructor
  117. /// </summary>
  118. ///
  119. /// <remarks>
  120. /// Creates a Point from a specified x,y coordinate pair.
  121. /// </remarks>
  122. public Point (int x, int y)
  123. {
  124. this.X = x;
  125. this.Y = y;
  126. }
  127. // -----------------------
  128. // Public Instance Members
  129. // -----------------------
  130. /// <summary>
  131. /// IsEmpty Property
  132. /// </summary>
  133. ///
  134. /// <remarks>
  135. /// Indicates if both X and Y are zero.
  136. /// </remarks>
  137. public bool IsEmpty {
  138. get {
  139. return ((X == 0) && (Y == 0));
  140. }
  141. }
  142. /// <summary>
  143. /// Equals Method
  144. /// </summary>
  145. ///
  146. /// <remarks>
  147. /// Checks equivalence of this Point and another object.
  148. /// </remarks>
  149. public override bool Equals (object obj)
  150. {
  151. if (!(obj is Point))
  152. return false;
  153. return (this == (Point) obj);
  154. }
  155. /// <summary>
  156. /// GetHashCode Method
  157. /// </summary>
  158. ///
  159. /// <remarks>
  160. /// Calculates a hashing value.
  161. /// </remarks>
  162. public override int GetHashCode ()
  163. {
  164. return X^Y;
  165. }
  166. /// <summary>
  167. /// Offset Method
  168. /// </summary>
  169. ///
  170. /// <remarks>
  171. /// Moves the Point a specified distance.
  172. /// </remarks>
  173. public void Offset (int dx, int dy)
  174. {
  175. X += dx;
  176. Y += dy;
  177. }
  178. /// <summary>
  179. /// ToString Method
  180. /// </summary>
  181. ///
  182. /// <remarks>
  183. /// Formats the Point as a string in coordinate notation.
  184. /// </remarks>
  185. public override string ToString ()
  186. {
  187. return string.Format ("{{X={0},Y={1}}}", X.ToString (CultureInfo.InvariantCulture),
  188. Y.ToString (CultureInfo.InvariantCulture));
  189. }
  190. /// <summary>
  191. /// Adds the specified Size to the specified Point.
  192. /// </summary>
  193. /// <returns>The Point that is the result of the addition operation.</returns>
  194. /// <param name="pt">The Point to add.</param>
  195. /// <param name="sz">The Size to add.</param>
  196. public static Point Add (Point pt, Size sz)
  197. {
  198. return new Point (pt.X + sz.Width, pt.Y + sz.Height);
  199. }
  200. /// <summary>
  201. /// Translates this Point by the specified Point.
  202. /// </summary>
  203. /// <returns>The offset.</returns>
  204. /// <param name="p">The Point used offset this Point.</param>
  205. public void Offset (Point p)
  206. {
  207. Offset (p.X, p.Y);
  208. }
  209. /// <summary>
  210. /// Returns the result of subtracting specified Size from the specified Point.
  211. /// </summary>
  212. /// <returns>The Point that is the result of the subtraction operation.</returns>
  213. /// <param name="pt">The Point to be subtracted from.</param>
  214. /// <param name="sz">The Size to subtract from the Point.</param>
  215. public static Point Subtract (Point pt, Size sz)
  216. {
  217. return new Point (pt.X - sz.Width, pt.Y - sz.Height);
  218. }
  219. }
  220. }