PointF.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. // See the LICENSE file in the project root for more information.
  4. // Copied from: https://github.com/dotnet/corefx/tree/master/src/System.Drawing.Primitives/src/System/Drawing
  5. using System;
  6. using System.ComponentModel;
  7. namespace Terminal.Gui {
  8. /// <summary>
  9. /// Represents an ordered pair of x and y coordinates that define a point in a two-dimensional plane.
  10. /// </summary>
  11. public struct PointF : IEquatable<PointF> {
  12. /// <summary>
  13. /// Creates a new instance of the <see cref='Terminal.Gui.PointF'/> class with member data left uninitialized.
  14. /// </summary>
  15. public static readonly PointF Empty;
  16. private float x; // Do not rename (binary serialization)
  17. private float y; // Do not rename (binary serialization)
  18. /// <summary>
  19. /// Initializes a new instance of the <see cref='Terminal.Gui.PointF'/> class with the specified coordinates.
  20. /// </summary>
  21. public PointF (float x, float y)
  22. {
  23. this.x = x;
  24. this.y = y;
  25. }
  26. /// <summary>
  27. /// Gets a value indicating whether this <see cref='Terminal.Gui.PointF'/> is empty.
  28. /// </summary>
  29. [Browsable (false)]
  30. public bool IsEmpty => x == 0f && y == 0f;
  31. /// <summary>
  32. /// Gets the x-coordinate of this <see cref='Terminal.Gui.PointF'/>.
  33. /// </summary>
  34. public float X {
  35. get => x;
  36. set => x = value;
  37. }
  38. /// <summary>
  39. /// Gets the y-coordinate of this <see cref='Terminal.Gui.PointF'/>.
  40. /// </summary>
  41. public float Y {
  42. get => y;
  43. set => y = value;
  44. }
  45. /// <summary>
  46. /// Translates a <see cref='Terminal.Gui.PointF'/> by a given <see cref='Terminal.Gui.Size'/> .
  47. /// </summary>
  48. public static PointF operator + (PointF pt, Size sz) => Add (pt, sz);
  49. /// <summary>
  50. /// Translates a <see cref='Terminal.Gui.PointF'/> by the negative of a given <see cref='Terminal.Gui.Size'/> .
  51. /// </summary>
  52. public static PointF operator - (PointF pt, Size sz) => Subtract (pt, sz);
  53. /// <summary>
  54. /// Translates a <see cref='Terminal.Gui.PointF'/> by a given <see cref='Terminal.Gui.SizeF'/> .
  55. /// </summary>
  56. public static PointF operator + (PointF pt, SizeF sz) => Add (pt, sz);
  57. /// <summary>
  58. /// Translates a <see cref='Terminal.Gui.PointF'/> by the negative of a given <see cref='Terminal.Gui.SizeF'/> .
  59. /// </summary>
  60. public static PointF operator - (PointF pt, SizeF sz) => Subtract (pt, sz);
  61. /// <summary>
  62. /// Compares two <see cref='Terminal.Gui.PointF'/> objects. The result specifies whether the values of the
  63. /// <see cref='Terminal.Gui.PointF.X'/> and <see cref='Terminal.Gui.PointF.Y'/> properties of the two
  64. /// <see cref='Terminal.Gui.PointF'/> objects are equal.
  65. /// </summary>
  66. public static bool operator == (PointF left, PointF right) => left.X == right.X && left.Y == right.Y;
  67. /// <summary>
  68. /// Compares two <see cref='Terminal.Gui.PointF'/> objects. The result specifies whether the values of the
  69. /// <see cref='Terminal.Gui.PointF.X'/> or <see cref='Terminal.Gui.PointF.Y'/> properties of the two
  70. /// <see cref='Terminal.Gui.PointF'/> objects are unequal.
  71. /// </summary>
  72. public static bool operator != (PointF left, PointF right) => !(left == right);
  73. /// <summary>
  74. /// Translates a <see cref='Terminal.Gui.PointF'/> by a given <see cref='Terminal.Gui.Size'/> .
  75. /// </summary>
  76. public static PointF Add (PointF pt, Size sz) => new PointF (pt.X + sz.Width, pt.Y + sz.Height);
  77. /// <summary>
  78. /// Translates a <see cref='Terminal.Gui.PointF'/> by the negative of a given <see cref='Terminal.Gui.Size'/> .
  79. /// </summary>
  80. public static PointF Subtract (PointF pt, Size sz) => new PointF (pt.X - sz.Width, pt.Y - sz.Height);
  81. /// <summary>
  82. /// Translates a <see cref='Terminal.Gui.PointF'/> by a given <see cref='Terminal.Gui.SizeF'/> .
  83. /// </summary>
  84. public static PointF Add (PointF pt, SizeF sz) => new PointF (pt.X + sz.Width, pt.Y + sz.Height);
  85. /// <summary>
  86. /// Translates a <see cref='Terminal.Gui.PointF'/> by the negative of a given <see cref='Terminal.Gui.SizeF'/> .
  87. /// </summary>
  88. public static PointF Subtract (PointF pt, SizeF sz) => new PointF (pt.X - sz.Width, pt.Y - sz.Height);
  89. /// <summary>
  90. /// Compares two <see cref='Terminal.Gui.PointF'/> objects. The result specifies whether the values of the
  91. /// <see cref='Terminal.Gui.PointF.X'/> and <see cref='Terminal.Gui.PointF.Y'/> properties of the two
  92. /// <see cref='Terminal.Gui.PointF'/> objects are equal.
  93. /// </summary>
  94. public override bool Equals (object obj) => obj is PointF && Equals ((PointF)obj);
  95. /// <summary>
  96. /// Compares two <see cref='Terminal.Gui.PointF'/> objects. The result specifies whether the values of the
  97. /// <see cref='Terminal.Gui.PointF.X'/> and <see cref='Terminal.Gui.PointF.Y'/> properties of the two
  98. /// <see cref='Terminal.Gui.PointF'/> objects are equal.
  99. /// </summary>
  100. public bool Equals (PointF other) => this == other;
  101. /// <summary>
  102. /// Generates a hashcode from the X and Y components
  103. /// </summary>
  104. /// <returns></returns>
  105. public override int GetHashCode ()
  106. {
  107. return X.GetHashCode() ^ Y.GetHashCode ();
  108. }
  109. /// <summary>
  110. /// Returns a string including the X and Y values
  111. /// </summary>
  112. /// <returns></returns>
  113. public override string ToString () => "{X=" + x.ToString () + ", Y=" + y.ToString () + "}";
  114. }
  115. }