PointF.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. //
  2. // System.Drawing.PointF.cs
  3. //
  4. // Author:
  5. // Mike Kestner ([email protected])
  6. //
  7. // (C) 2001 Mike Kestner
  8. //
  9. using System;
  10. using System.Runtime.InteropServices;
  11. using System.ComponentModel;
  12. namespace System.Drawing {
  13. [Serializable]
  14. [ComVisible (true)]
  15. public struct PointF {
  16. // Private x and y coordinate fields.
  17. float cx, cy;
  18. // -----------------------
  19. // Public Shared Members
  20. // -----------------------
  21. /// <summary>
  22. /// Empty Shared Field
  23. /// </summary>
  24. ///
  25. /// <remarks>
  26. /// An uninitialized PointF Structure.
  27. /// </remarks>
  28. public static readonly PointF Empty;
  29. /// <summary>
  30. /// Addition Operator
  31. /// </summary>
  32. ///
  33. /// <remarks>
  34. /// Translates a PointF using the Width and Height
  35. /// properties of the given Size.
  36. /// </remarks>
  37. public static PointF operator + (PointF pt, Size sz)
  38. {
  39. return new PointF (pt.X + sz.Width, pt.Y + sz.Height);
  40. }
  41. /// <summary>
  42. /// Equality Operator
  43. /// </summary>
  44. ///
  45. /// <remarks>
  46. /// Compares two PointF 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 == (PointF pt_a, PointF pt_b)
  51. {
  52. return ((pt_a.X == pt_b.X) && (pt_a.Y == pt_b.Y));
  53. }
  54. /// <summary>
  55. /// Inequality Operator
  56. /// </summary>
  57. ///
  58. /// <remarks>
  59. /// Compares two PointF 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 != (PointF pt_a, PointF pt_b)
  64. {
  65. return ((pt_a.X != pt_b.X) || (pt_a.Y != pt_b.Y));
  66. }
  67. /// <summary>
  68. /// Subtraction Operator
  69. /// </summary>
  70. ///
  71. /// <remarks>
  72. /// Translates a PointF using the negation of the Width
  73. /// and Height properties of the given Size.
  74. /// </remarks>
  75. public static PointF operator - (PointF pt, Size sz)
  76. {
  77. return new PointF (pt.X - sz.Width, pt.Y - sz.Height);
  78. }
  79. // -----------------------
  80. // Public Constructor
  81. // -----------------------
  82. /// <summary>
  83. /// PointF Constructor
  84. /// </summary>
  85. ///
  86. /// <remarks>
  87. /// Creates a PointF from a specified x,y coordinate pair.
  88. /// </remarks>
  89. public PointF (float x, float y)
  90. {
  91. cx = x;
  92. cy = y;
  93. }
  94. // -----------------------
  95. // Public Instance Members
  96. // -----------------------
  97. /// <summary>
  98. /// IsEmpty Property
  99. /// </summary>
  100. ///
  101. /// <remarks>
  102. /// Indicates if both X and Y are zero.
  103. /// </remarks>
  104. [Browsable (false)]
  105. public bool IsEmpty {
  106. get {
  107. return ((cx == 0.0) && (cy == 0.0));
  108. }
  109. }
  110. /// <summary>
  111. /// X Property
  112. /// </summary>
  113. ///
  114. /// <remarks>
  115. /// The X coordinate of the PointF.
  116. /// </remarks>
  117. public float X {
  118. get {
  119. return cx;
  120. }
  121. set {
  122. cx = value;
  123. }
  124. }
  125. /// <summary>
  126. /// Y Property
  127. /// </summary>
  128. ///
  129. /// <remarks>
  130. /// The Y coordinate of the PointF.
  131. /// </remarks>
  132. public float Y {
  133. get {
  134. return cy;
  135. }
  136. set {
  137. cy = value;
  138. }
  139. }
  140. /// <summary>
  141. /// Equals Method
  142. /// </summary>
  143. ///
  144. /// <remarks>
  145. /// Checks equivalence of this PointF and another object.
  146. /// </remarks>
  147. public override bool Equals (object o)
  148. {
  149. if (!(o is PointF))
  150. return false;
  151. return (this == (PointF) o);
  152. }
  153. /// <summary>
  154. /// GetHashCode Method
  155. /// </summary>
  156. ///
  157. /// <remarks>
  158. /// Calculates a hashing value.
  159. /// </remarks>
  160. public override int GetHashCode ()
  161. {
  162. return (int) cx ^ (int) cy;
  163. }
  164. /// <summary>
  165. /// ToString Method
  166. /// </summary>
  167. ///
  168. /// <remarks>
  169. /// Formats the PointF as a string in coordinate notation.
  170. /// </remarks>
  171. public override string ToString ()
  172. {
  173. return String.Format ("{{X={0}, Y={1}}}", cx, cy);
  174. }
  175. }
  176. }