Vector2D.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace bbiwarg.Utility
  8. {
  9. public class Vector2D
  10. {
  11. public static Vector2D Zero { get { return new Vector2D(0, 0); } }
  12. public float X { get; private set; }
  13. public float Y { get; private set; }
  14. public int IntX { get { return (int)X; } }
  15. public int IntY { get { return (int)Y; } }
  16. public float Length { get { counter++; return (float)Math.Sqrt(X * X + Y * Y); } }
  17. public static int counter = 0;
  18. public Vector2D(float x, float y)
  19. {
  20. X = x;
  21. Y = y;
  22. }
  23. public Vector2D(Vector2D vec)
  24. {
  25. X = vec.X;
  26. Y = vec.Y;
  27. }
  28. public Vector2D(Point point)
  29. {
  30. X = point.X;
  31. Y = point.Y;
  32. }
  33. public Vector2D(PointF point)
  34. {
  35. X = point.X;
  36. Y = point.Y;
  37. }
  38. public float getDistanceTo(Vector2D point)
  39. {
  40. return (this - point).Length;
  41. }
  42. public float getAngleBetween(Vector2D vector)
  43. {
  44. return (float)Math.Acos(dotProduct(vector) / (Length * vector.Length));
  45. }
  46. public bool isInOppositeDirection(Vector2D vector)
  47. {
  48. return (getAngleBetween(vector) > Math.PI / 2);
  49. }
  50. public float dotProduct(Vector2D vector)
  51. {
  52. return X * vector.X + Y * vector.Y;
  53. }
  54. public float crossProduct(Vector2D v)
  55. {
  56. return X * v.Y - Y * v.X;
  57. }
  58. public Vector2D scale(Vector2D v)
  59. {
  60. return new Vector2D(X * v.X, Y * v.Y);
  61. }
  62. public bool isInBox(Vector2D corner1, Vector2D corner2)
  63. {
  64. float minX = Math.Min(corner1.X, corner2.X);
  65. float maxX = Math.Max(corner1.X, corner2.X);
  66. float minY = Math.Min(corner1.Y, corner2.Y);
  67. float maxY = Math.Max(corner1.Y, corner2.Y);
  68. return (minX <= X && X <= maxX && minY <= Y && Y <= maxY);
  69. }
  70. public bool isInBound()
  71. {
  72. return isInBound(Vector2D.Zero, Parameters.ImageMaxPixel);
  73. }
  74. public bool isInBound(Vector2D topLeft, Vector2D bottomRight)
  75. {
  76. return (X >= topLeft.X && X <= bottomRight.X && Y >= topLeft.Y && Y <= bottomRight.Y);
  77. }
  78. public Vector2D moveInBound(Vector2D inBoundDirection)
  79. {
  80. return moveInBound(Vector2D.Zero, Parameters.ImageMaxPixel, inBoundDirection);
  81. }
  82. public Vector2D moveInBound(Vector2D topLeft, Vector2D bottomRight, Vector2D inBoundDirection)
  83. {
  84. Vector2D position = new Vector2D(X, Y);
  85. while (!position.isInBound(topLeft, bottomRight))
  86. position += inBoundDirection;
  87. return position;
  88. }
  89. public Vector2D moveWithinBound(Vector2D direction, float factor) {
  90. Vector2D newPosition = this + factor * direction;
  91. if (!newPosition.isInBound())
  92. newPosition = newPosition.moveInBound(direction.getInverse());
  93. return newPosition;
  94. }
  95. public Vector2D normalize()
  96. {
  97. float length = Length;
  98. return new Vector2D(X / length, Y / length);
  99. }
  100. public Vector2D getOrthogonal(bool side = true)
  101. {
  102. if (side)
  103. return new Vector2D(Y, -X);
  104. else
  105. return new Vector2D(-Y, X);
  106. }
  107. public Vector2D getInverse()
  108. {
  109. return new Vector2D(-X, -Y);
  110. }
  111. public Vector2D getAbsolute()
  112. {
  113. return new Vector2D(Math.Abs(X), Math.Abs(Y));
  114. }
  115. public Vector2D copy()
  116. {
  117. return new Vector2D(X, Y);
  118. }
  119. public override string ToString()
  120. {
  121. return "(" + X + "|" + Y + ")";
  122. }
  123. public static Vector2D operator *(float scalar, Vector2D vector)
  124. {
  125. return new Vector2D(scalar * vector.X, scalar * vector.Y);
  126. }
  127. public static Vector2D operator *(Vector2D vector, float scalar)
  128. {
  129. return new Vector2D(scalar * vector.X, scalar * vector.Y);
  130. }
  131. public static Vector2D operator /(Vector2D vector, float scalar)
  132. {
  133. return new Vector2D(vector.X / scalar, vector.Y / scalar);
  134. }
  135. public static Vector2D operator /(Vector2D vector1, Vector2D vector2)
  136. {
  137. return new Vector2D(vector1.X / vector2.X, vector1.Y / vector2.Y);
  138. }
  139. public static Vector2D operator +(Vector2D vector1, Vector2D vector2)
  140. {
  141. return new Vector2D(vector1.X + vector2.X, vector1.Y + vector2.Y);
  142. }
  143. public static Vector2D operator -(Vector2D vector1, Vector2D vector2)
  144. {
  145. return new Vector2D(vector1.X - vector2.X, vector1.Y - vector2.Y);
  146. }
  147. public static Vector2D mean(List<Vector2D> vectors)
  148. {
  149. Vector2D sumVector = Vector2D.sum(vectors);
  150. return sumVector / vectors.Count;
  151. }
  152. public static Vector2D sum(List<Vector2D> vectors)
  153. {
  154. Vector2D sumVector = new Vector2D(0, 0);
  155. foreach (Vector2D vector in vectors)
  156. sumVector += vector;
  157. return sumVector;
  158. }
  159. public static implicit operator PointF(Vector2D vec)
  160. {
  161. return new PointF(vec.X, vec.Y);
  162. }
  163. public static implicit operator Point(Vector2D vec)
  164. {
  165. return new Point(vec.IntX, vec.IntY);
  166. }
  167. }
  168. }