Point.cs 4.3 KB

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