DVector2.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. using UnityEngine;
  2. using M = System.Math;
  3. // ReSharper disable InconsistentNaming
  4. namespace ARLocation
  5. {
  6. [System.Serializable]
  7. public struct DVector2
  8. {
  9. public double x;
  10. public double y;
  11. /// <summary>
  12. /// Gets the magnitude of the vector.
  13. /// </summary>
  14. /// <value>The magnitude.</value>
  15. public double magnitude
  16. {
  17. get
  18. {
  19. return M.Sqrt(x * x + y * y);
  20. }
  21. }
  22. /// <summary>
  23. /// Gets the normalized version of this vector.
  24. /// </summary>
  25. /// <value>The normalized.</value>
  26. public DVector2 normalized
  27. {
  28. get
  29. {
  30. var m = magnitude;
  31. if (m < 0.00000001)
  32. {
  33. return new DVector2();
  34. }
  35. return new DVector2(x, y) / magnitude;
  36. }
  37. }
  38. public DVector2 Clone()
  39. {
  40. return new DVector2(x, y);
  41. }
  42. /// <summary>
  43. /// Initializes a new instance of the <see cref="T:DVector2"/> struct.
  44. /// </summary>
  45. /// <param name="x">The x coordinate.</param>
  46. /// <param name="y">The y coordinate.</param>
  47. public DVector2(double x = 0.0, double y = 0.0)
  48. {
  49. this.x = x;
  50. this.y = y;
  51. }
  52. /// <summary>
  53. /// Converts to a Vector2.
  54. /// </summary>
  55. /// <returns>The vector2.</returns>
  56. public Vector2 toVector2()
  57. {
  58. return new Vector2((float)x, (float)y);
  59. }
  60. /// <summary>
  61. /// Equals the specified v and e.
  62. /// </summary>
  63. /// <returns>The equals.</returns>
  64. /// <param name="v">V.</param>
  65. /// <param name="e">E.</param>
  66. public bool Equals(DVector2 v, double e = 0.00005)
  67. {
  68. return (M.Abs(x - v.x) <= e) && (M.Abs(y - v.y) <= e);
  69. }
  70. /// <summary>
  71. /// Normalize this instance.
  72. /// </summary>
  73. public void Normalize()
  74. {
  75. double m = magnitude;
  76. x /= m;
  77. y /= m;
  78. }
  79. /// <summary>
  80. /// Set the specified x and y.
  81. /// </summary>
  82. /// <param name="newX"></param>
  83. /// <param name="newY"></param>
  84. public void Set(double newX = 0.0, double newY = 0.0)
  85. {
  86. x = newX;
  87. y = newY;
  88. }
  89. /// <summary>
  90. /// Returns a <see cref="T:System.String"/> that represents the current <see cref="T:DVector2"/>.
  91. /// </summary>
  92. /// <returns>A <see cref="T:System.String"/> that represents the current <see cref="T:DVector2"/>.</returns>
  93. override public string ToString()
  94. {
  95. return "(" + x + ", " + y + ")";
  96. }
  97. /// <summary>
  98. /// Dot the specified a and b.
  99. /// </summary>
  100. /// <returns>The dot.</returns>
  101. /// <param name="a">The alpha component.</param>
  102. /// <param name="b">The blue component.</param>
  103. public static double Dot(DVector2 a, DVector2 b)
  104. {
  105. return a.x * b.x + a.y * b.y;
  106. }
  107. /// <summary>
  108. /// Distance the specified a and b.
  109. /// </summary>
  110. /// <returns>The distance.</returns>
  111. /// <param name="a">The alpha component.</param>
  112. /// <param name="b">The blue component.</param>
  113. public static double Distance(DVector2 a, DVector2 b)
  114. {
  115. return M.Sqrt(a.x * b.x + a.y * b.y);
  116. }
  117. /// <summary>
  118. /// Lerp the specified a, b and t.
  119. /// </summary>
  120. /// <returns>The lerp.</returns>
  121. /// <param name="a">The alpha component.</param>
  122. /// <param name="b">The blue component.</param>
  123. /// <param name="t">T.</param>
  124. public static DVector2 Lerp(DVector2 a, DVector2 b, double t)
  125. {
  126. double s = M.Max(0, M.Min(t, 1));
  127. return a * (1 - s) + b * s;
  128. }
  129. /// <summary>
  130. /// Computes the product of <c>a</c> and <c>b</c>, yielding a new <see cref="T:DVector2"/>.
  131. /// </summary>
  132. /// <param name="a">The <see cref="DVector2"/> to multiply.</param>
  133. /// <param name="b">The <see cref="double"/> to multiply.</param>
  134. /// <returns>The <see cref="T:DVector2"/> that is the <c>a</c> * <c>b</c>.</returns>
  135. public static DVector2 operator *(DVector2 a, double b)
  136. {
  137. return new DVector2(
  138. b * a.x,
  139. b * a.y
  140. );
  141. }
  142. /// <summary>
  143. /// Computes the division of <c>a</c> and <c>b</c>, yielding a new <see cref="T:DVector2"/>.
  144. /// </summary>
  145. /// <param name="a">The <see cref="DVector2"/> to divide (the divident).</param>
  146. /// <param name="b">The <see cref="double"/> to divide (the divisor).</param>
  147. /// <returns>The <see cref="T:DVector2"/> that is the <c>a</c> / <c>b</c>.</returns>
  148. public static DVector2 operator /(DVector2 a, double b)
  149. {
  150. return new DVector2(
  151. a.x / b,
  152. a.y / b
  153. );
  154. }
  155. /// <summary>
  156. /// Adds a <see cref="DVector2"/> to a <see cref="DVector2"/>, yielding a new <see cref="T:DVector2"/>.
  157. /// </summary>
  158. /// <param name="a">The first <see cref="DVector2"/> to add.</param>
  159. /// <param name="b">The second <see cref="DVector2"/> to add.</param>
  160. /// <returns>The <see cref="T:DVector2"/> that is the sum of the values of <c>a</c> and <c>b</c>.</returns>
  161. public static DVector2 operator +(DVector2 a, DVector2 b)
  162. {
  163. return new DVector2(
  164. a.x + b.x,
  165. a.y + b.y
  166. );
  167. }
  168. /// <summary>
  169. /// Subtracts a <see cref="DVector2"/> from a <see cref="DVector2"/>, yielding a new <see cref="T:DVector2"/>.
  170. /// </summary>
  171. /// <param name="a">The <see cref="DVector2"/> to subtract from (the minuend).</param>
  172. /// <param name="b">The <see cref="DVector2"/> to subtract (the subtrahend).</param>
  173. /// <returns>The <see cref="T:DVector2"/> that is the <c>a</c> minus <c>b</c>.</returns>
  174. public static DVector2 operator -(DVector2 a, DVector2 b)
  175. {
  176. return new DVector2(
  177. a.x - b.x,
  178. a.y - b.y
  179. );
  180. }
  181. }
  182. }