Point.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. // -----------------------------------------------------------------------
  2. // <copyright file="Point.cs" company="">
  3. // Triangle.NET code by Christian Woltering, http://triangle.codeplex.com/
  4. // </copyright>
  5. // -----------------------------------------------------------------------
  6. namespace UnityEngine.U2D.Animation.TriangleNet
  7. .Geometry
  8. {
  9. using System;
  10. using System.Diagnostics;
  11. /// <summary>
  12. /// Represents a 2D point.
  13. /// </summary>
  14. #if USE_Z
  15. [DebuggerDisplay("ID {ID} [{X}, {Y}, {Z}]")]
  16. #else
  17. [DebuggerDisplay("ID {ID} [{X}, {Y}]")]
  18. #endif
  19. internal class Point : IComparable<Point>, IEquatable<Point>
  20. {
  21. internal int id;
  22. internal int label;
  23. internal double x;
  24. internal double y;
  25. #if USE_Z
  26. internal double z;
  27. #endif
  28. public Point()
  29. : this(0.0, 0.0, 0)
  30. {
  31. }
  32. public Point(double x, double y)
  33. : this(x, y, 0)
  34. {
  35. }
  36. public Point(double x, double y, int label)
  37. {
  38. this.x = x;
  39. this.y = y;
  40. this.label = label;
  41. }
  42. #region Public properties
  43. /// <summary>
  44. /// Gets or sets the vertex id.
  45. /// </summary>
  46. public int ID
  47. {
  48. get { return this.id; }
  49. set { this.id = value; }
  50. }
  51. /// <summary>
  52. /// Gets or sets the vertex x coordinate.
  53. /// </summary>
  54. public double X
  55. {
  56. get { return this.x; }
  57. set { this.x = value; }
  58. }
  59. /// <summary>
  60. /// Gets or sets the vertex y coordinate.
  61. /// </summary>
  62. public double Y
  63. {
  64. get { return this.y; }
  65. set { this.y = value; }
  66. }
  67. #if USE_Z
  68. /// <summary>
  69. /// Gets or sets the vertex z coordinate.
  70. /// </summary>
  71. public double Z
  72. {
  73. get { return this.z; }
  74. set { this.z = value; }
  75. }
  76. #endif
  77. /// <summary>
  78. /// Gets or sets a general-purpose label.
  79. /// </summary>
  80. /// <remarks>
  81. /// This is used for the vertex boundary mark.
  82. /// </remarks>
  83. public int Label
  84. {
  85. get { return this.label; }
  86. set { this.label = value; }
  87. }
  88. #endregion
  89. #region Operator overloading / overriding Equals
  90. // Compare "Guidelines for Overriding Equals() and Operator =="
  91. // http://msdn.microsoft.com/en-us/library/ms173147.aspx
  92. public static bool operator==(Point a, Point b)
  93. {
  94. // If both are null, or both are same instance, return true.
  95. if (Object.ReferenceEquals(a, b))
  96. {
  97. return true;
  98. }
  99. // If one is null, but not both, return false.
  100. if (((object)a == null) || ((object)b == null))
  101. {
  102. return false;
  103. }
  104. return a.Equals(b);
  105. }
  106. public static bool operator!=(Point a, Point b)
  107. {
  108. return !(a == b);
  109. }
  110. public override bool Equals(object obj)
  111. {
  112. // If parameter is null return false.
  113. if (obj == null)
  114. {
  115. return false;
  116. }
  117. Point p = obj as Point;
  118. if ((object)p == null)
  119. {
  120. return false;
  121. }
  122. return (x == p.x) && (y == p.y);
  123. }
  124. public bool Equals(Point p)
  125. {
  126. // If vertex is null return false.
  127. if ((object)p == null)
  128. {
  129. return false;
  130. }
  131. // Return true if the fields match:
  132. return (x == p.x) && (y == p.y);
  133. }
  134. #endregion
  135. public int CompareTo(Point other)
  136. {
  137. if (x == other.x && y == other.y)
  138. {
  139. return 0;
  140. }
  141. return (x < other.x || (x == other.x && y < other.y)) ? -1 : 1;
  142. }
  143. public override int GetHashCode()
  144. {
  145. int hash = 19;
  146. hash = hash * 31 + x.GetHashCode();
  147. hash = hash * 31 + y.GetHashCode();
  148. return hash;
  149. }
  150. }
  151. }