Point.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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.Globalization;
  11. using System.Text.Json.Serialization;
  12. namespace Terminal.Gui;
  13. /// <summary>Represents an ordered pair of integer x- and y-coordinates that defines a point in a two-dimensional plane.</summary>
  14. public partial struct Point
  15. {
  16. /// <summary>Gets or sets the x-coordinate of this Point.</summary>
  17. [JsonInclude]
  18. public int X;
  19. /// <summary>Gets or sets the y-coordinate of this Point.</summary>
  20. [JsonInclude]
  21. public int Y;
  22. // -----------------------
  23. // Public Shared Members
  24. // -----------------------
  25. /// <summary>Empty Shared Field</summary>
  26. /// <remarks>An uninitialized Point Structure.</remarks>
  27. public static readonly Point Empty;
  28. /// <summary>Addition Operator</summary>
  29. /// <remarks>Translates a Point using the Width and Height properties of the given <typeref>Size</typeref>.</remarks>
  30. public static Point operator + (Point pt, Size sz) { return new Point (pt.X + sz.Width, pt.Y + sz.Height); }
  31. /// <summary>Equality Operator</summary>
  32. /// <remarks>
  33. /// Compares two Point objects. The return value is based on the equivalence of the X and Y properties of the two
  34. /// points.
  35. /// </remarks>
  36. public static bool operator == (Point left, Point right) { return left.X == right.X && left.Y == right.Y; }
  37. /// <summary>Inequality Operator</summary>
  38. /// <remarks>
  39. /// Compares two Point objects. The return value is based on the equivalence of the X and Y properties of the two
  40. /// points.
  41. /// </remarks>
  42. public static bool operator != (Point left, Point right) { return left.X != right.X || left.Y != right.Y; }
  43. /// <summary>Subtraction Operator</summary>
  44. /// <remarks>Translates a Point using the negation of the Width and Height properties of the given Size.</remarks>
  45. public static Point operator - (Point pt, Size sz) { return new Point (pt.X - sz.Width, pt.Y - sz.Height); }
  46. /// <summary>Point to Size Conversion</summary>
  47. /// <remarks>Returns a Size based on the Coordinates of a given Point. Requires explicit cast.</remarks>
  48. public static explicit operator Size (Point p)
  49. {
  50. if (p.X < 0 || p.Y < 0)
  51. {
  52. throw new ArgumentException ("Either Width and Height must be greater or equal to 0.");
  53. }
  54. return new Size (p.X, p.Y);
  55. }
  56. // -----------------------
  57. // Public Constructors
  58. // -----------------------
  59. /// <summary>Point Constructor</summary>
  60. /// <remarks>Creates a Point from a Size value.</remarks>
  61. public Point (Size sz)
  62. {
  63. X = sz.Width;
  64. Y = sz.Height;
  65. }
  66. /// <summary>Point Constructor</summary>
  67. /// <remarks>Creates a Point from a specified x,y coordinate pair.</remarks>
  68. public Point (int x, int y)
  69. {
  70. X = x;
  71. Y = y;
  72. }
  73. // -----------------------
  74. // Public Instance Members
  75. // -----------------------
  76. /// <summary>IsEmpty Property</summary>
  77. /// <remarks>Indicates if both X and Y are zero.</remarks>
  78. [JsonIgnore]
  79. public bool IsEmpty => X == 0 && Y == 0;
  80. /// <summary>Equals Method</summary>
  81. /// <remarks>Checks equivalence of this Point and another object.</remarks>
  82. public override bool Equals (object obj)
  83. {
  84. if (!(obj is Point))
  85. {
  86. return false;
  87. }
  88. return this == (Point)obj;
  89. }
  90. /// <summary>GetHashCode Method</summary>
  91. /// <remarks>Calculates a hashing value.</remarks>
  92. public override int GetHashCode () { return X ^ Y; }
  93. /// <summary>Offset Method</summary>
  94. /// <remarks>Moves the Point a specified distance.</remarks>
  95. public void Offset (int dx, int dy)
  96. {
  97. X += dx;
  98. Y += dy;
  99. }
  100. /// <summary>ToString Method</summary>
  101. /// <remarks>Formats the Point as a string in coordinate notation.</remarks>
  102. public override string ToString ()
  103. {
  104. return string.Format (
  105. "{{X={0},Y={1}}}",
  106. X.ToString (CultureInfo.InvariantCulture),
  107. Y.ToString (CultureInfo.InvariantCulture)
  108. );
  109. }
  110. /// <summary>Adds the specified Size to the specified Point.</summary>
  111. /// <returns>The Point that is the result of the addition operation.</returns>
  112. /// <param name="pt">The Point to add.</param>
  113. /// <param name="sz">The Size to add.</param>
  114. public static Point Add (Point pt, Size sz) { return new Point (pt.X + sz.Width, pt.Y + sz.Height); }
  115. /// <summary>Translates this Point by the specified Point.</summary>
  116. /// <returns>The offset.</returns>
  117. /// <param name="p">The Point used offset this Point.</param>
  118. public void Offset (Point p) { Offset (p.X, p.Y); }
  119. /// <summary>Returns the result of subtracting specified Size from the specified Point.</summary>
  120. /// <returns>The Point that is the result of the subtraction operation.</returns>
  121. /// <param name="pt">The Point to be subtracted from.</param>
  122. /// <param name="sz">The Size to subtract from the Point.</param>
  123. public static Point Subtract (Point pt, Size sz) { return new Point (pt.X - sz.Width, pt.Y - sz.Height); }
  124. }