Vector2D.cs 5.6 KB

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