PointF.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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.ComponentModel;
  6. namespace Terminal.Gui;
  7. /// <summary>Represents an ordered pair of x and y coordinates that define a point in a two-dimensional plane.</summary>
  8. public struct PointF : IEquatable<PointF>
  9. {
  10. /// <summary>Creates a new instance of the <see cref='Terminal.Gui.PointF'/> class with member data left uninitialized.</summary>
  11. public static readonly PointF Empty;
  12. /// <summary>Initializes a new instance of the <see cref='Terminal.Gui.PointF'/> class with the specified coordinates.</summary>
  13. public PointF (float x, float y)
  14. {
  15. X = x;
  16. Y = y;
  17. }
  18. /// <summary>Gets a value indicating whether this <see cref='Terminal.Gui.PointF'/> is empty.</summary>
  19. [Browsable (false)]
  20. public bool IsEmpty => X == 0f && Y == 0f;
  21. /// <summary>Gets the x-coordinate of this <see cref='Terminal.Gui.PointF'/>.</summary>
  22. public float X { get; set; }
  23. /// <summary>Gets the y-coordinate of this <see cref='Terminal.Gui.PointF'/>.</summary>
  24. public float Y { get; set; }
  25. /// <summary>Translates a <see cref='Terminal.Gui.PointF'/> by a given <see cref='Terminal.Gui.Size'/> .</summary>
  26. public static PointF operator + (PointF pt, Size sz) { return Add (pt, sz); }
  27. /// <summary>Translates a <see cref='Terminal.Gui.PointF'/> by the negative of a given <see cref='Terminal.Gui.Size'/> .</summary>
  28. public static PointF operator - (PointF pt, Size sz) { return Subtract (pt, sz); }
  29. /// <summary>Translates a <see cref='Terminal.Gui.PointF'/> by a given <see cref='Terminal.Gui.SizeF'/> .</summary>
  30. public static PointF operator + (PointF pt, SizeF sz) { return Add (pt, sz); }
  31. /// <summary>Translates a <see cref='Terminal.Gui.PointF'/> by the negative of a given <see cref='Terminal.Gui.SizeF'/> .</summary>
  32. public static PointF operator - (PointF pt, SizeF sz) { return Subtract (pt, sz); }
  33. /// <summary>
  34. /// Compares two <see cref='Terminal.Gui.PointF'/> objects. The result specifies whether the values of the
  35. /// <see cref='Terminal.Gui.PointF.X'/> and <see cref='Terminal.Gui.PointF.Y'/> properties of the two
  36. /// <see cref='Terminal.Gui.PointF'/> objects are equal.
  37. /// </summary>
  38. public static bool operator == (PointF left, PointF right) { return left.X == right.X && left.Y == right.Y; }
  39. /// <summary>
  40. /// Compares two <see cref='Terminal.Gui.PointF'/> objects. The result specifies whether the values of the
  41. /// <see cref='Terminal.Gui.PointF.X'/> or <see cref='Terminal.Gui.PointF.Y'/> properties of the two
  42. /// <see cref='Terminal.Gui.PointF'/> objects are unequal.
  43. /// </summary>
  44. public static bool operator != (PointF left, PointF right) { return !(left == right); }
  45. /// <summary>Translates a <see cref='Terminal.Gui.PointF'/> by a given <see cref='Terminal.Gui.Size'/> .</summary>
  46. public static PointF Add (PointF pt, Size sz) { return new PointF (pt.X + sz.Width, pt.Y + sz.Height); }
  47. /// <summary>Translates a <see cref='Terminal.Gui.PointF'/> by the negative of a given <see cref='Terminal.Gui.Size'/> .</summary>
  48. public static PointF Subtract (PointF pt, Size sz) { return new PointF (pt.X - sz.Width, pt.Y - sz.Height); }
  49. /// <summary>Translates a <see cref='Terminal.Gui.PointF'/> by a given <see cref='Terminal.Gui.SizeF'/> .</summary>
  50. public static PointF Add (PointF pt, SizeF sz) { return new PointF (pt.X + sz.Width, pt.Y + sz.Height); }
  51. /// <summary>Translates a <see cref='Terminal.Gui.PointF'/> by the negative of a given <see cref='Terminal.Gui.SizeF'/> .</summary>
  52. public static PointF Subtract (PointF pt, SizeF sz) { return new PointF (pt.X - sz.Width, pt.Y - sz.Height); }
  53. /// <summary>
  54. /// Compares two <see cref='Terminal.Gui.PointF'/> objects. The result specifies whether the values of the
  55. /// <see cref='Terminal.Gui.PointF.X'/> and <see cref='Terminal.Gui.PointF.Y'/> properties of the two
  56. /// <see cref='Terminal.Gui.PointF'/> objects are equal.
  57. /// </summary>
  58. public override bool Equals (object obj) { return obj is PointF && Equals ((PointF)obj); }
  59. /// <summary>
  60. /// Compares two <see cref='Terminal.Gui.PointF'/> objects. The result specifies whether the values of the
  61. /// <see cref='Terminal.Gui.PointF.X'/> and <see cref='Terminal.Gui.PointF.Y'/> properties of the two
  62. /// <see cref='Terminal.Gui.PointF'/> objects are equal.
  63. /// </summary>
  64. public bool Equals (PointF other) { return this == other; }
  65. /// <summary>Generates a hashcode from the X and Y components</summary>
  66. /// <returns></returns>
  67. public override int GetHashCode () { return X.GetHashCode () ^ Y.GetHashCode (); }
  68. /// <summary>Returns a string including the X and Y values</summary>
  69. /// <returns></returns>
  70. public override string ToString () { return "{X=" + X + ", Y=" + Y + "}"; }
  71. }