Vector2D.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. 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 float dotProduct(Vector2D vector)
  46. {
  47. return X * vector.X + Y * vector.Y;
  48. }
  49. public float cross(Vector2D v)
  50. {
  51. return X * v.Y - Y * v.X;
  52. }
  53. public bool isInBox(Vector2D corner1, Vector2D corner2)
  54. {
  55. float minX = Math.Min(corner1.X, corner2.X);
  56. float maxX = Math.Max(corner1.X, corner2.X);
  57. float minY = Math.Min(corner1.Y, corner2.Y);
  58. float maxY = Math.Max(corner1.Y, corner2.Y);
  59. return (minX <= X && X <= maxX && minY <= Y && Y <= maxY);
  60. }
  61. public Vector2D normalize()
  62. {
  63. float length = Length;
  64. return new Vector2D(X / length, Y / length);
  65. }
  66. public Vector2D normalizeComponentOne() {
  67. if (Math.Abs(X) > Math.Abs(Y))
  68. return new Vector2D(1, Y / X);
  69. else
  70. return new Vector2D(X / Y, 1);
  71. }
  72. public bool isWithin(float minX, float minY, float maxX, float maxY)
  73. {
  74. return (X >= minX && Y >= minY && X <= maxX && Y <= maxY);
  75. }
  76. public Vector2D moveInBound(float minX, float minY, float maxX, float maxY)
  77. {
  78. float x = Math.Min(maxX, Math.Max(minX, X));
  79. float y = Math.Min(maxY, Math.Max(minY, Y));
  80. return new Vector2D(x, y);
  81. }
  82. public bool isOnBound(float minX, float minY, float maxX, float maxY) {
  83. return (IntX == minX || IntX == maxX || IntY == minY || IntY == maxY);
  84. }
  85. public Vector2D getOrthogonal(bool side)
  86. {
  87. if (side)
  88. return new Vector2D(Y, -X);
  89. else
  90. return new Vector2D(-Y, X);
  91. }
  92. public Vector2D getInverse()
  93. {
  94. return new Vector2D(-X, -Y);
  95. }
  96. public override string ToString()
  97. {
  98. return "(" + X + "|" + Y + ")";
  99. }
  100. public static Vector2D operator *(float scalar, Vector2D vector)
  101. {
  102. return new Vector2D(scalar * vector.X, scalar * vector.Y);
  103. }
  104. public static Vector2D operator *(Vector2D vector, float scalar)
  105. {
  106. return new Vector2D(scalar * vector.X, scalar * vector.Y);
  107. }
  108. public static Vector2D operator /(Vector2D vector, float scalar)
  109. {
  110. return new Vector2D(vector.X / scalar, vector.Y / scalar);
  111. }
  112. public static Vector2D operator +(Vector2D vector1, Vector2D vector2)
  113. {
  114. return new Vector2D(vector1.X + vector2.X, vector1.Y + vector2.Y);
  115. }
  116. public static Vector2D operator -(Vector2D vector1, Vector2D vector2)
  117. {
  118. return new Vector2D(vector1.X - vector2.X, vector1.Y - vector2.Y);
  119. }
  120. public static Vector2D mean(List<Vector2D> vectors)
  121. {
  122. Vector2D meanVector = new Vector2D(0, 0);
  123. foreach (Vector2D vector in vectors)
  124. {
  125. meanVector += vector;
  126. }
  127. return meanVector / vectors.Count;
  128. }
  129. public static implicit operator PointF(Vector2D vec)
  130. {
  131. return new PointF(vec.X, vec.Y);
  132. }
  133. public static implicit operator Point(Vector2D vec)
  134. {
  135. return new Point(vec.IntX, vec.IntY);
  136. }
  137. }
  138. }