Vector2D.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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. /// <summary>
  10. /// Class with represents a vector or a point in 2 dimensional space.
  11. /// </summary>
  12. public class Vector2D
  13. {
  14. /// <summary>
  15. /// length of the vector, is initialized with -1 and will only be calculated if needed and only once
  16. /// </summary>
  17. private float length = -1;
  18. /// <summary>
  19. /// the null vector or origin
  20. /// </summary>
  21. public static Vector2D Zero { get { return new Vector2D(0, 0); } }
  22. /// <summary>
  23. /// X (first) component
  24. /// </summary>
  25. public float X { get; private set; }
  26. /// <summary>
  27. /// Y (second) component
  28. /// </summary>
  29. public float Y { get; private set; }
  30. /// <summary>
  31. /// X component as integer
  32. /// </summary>
  33. public int IntX { get { return (int)X; } }
  34. /// <summary>
  35. /// Y component as integer
  36. /// </summary>
  37. public int IntY { get { return (int)Y; } }
  38. /// <summary>
  39. /// length of the vector, computed in euclidean distance (2. norm)
  40. /// </summary>
  41. public float Length { get { if (length == -1) length = (float)Math.Sqrt(X * X + Y * Y); return length; } }
  42. /// <summary>
  43. /// Standard constructor which sets the components.
  44. /// </summary>
  45. /// <param name="x">first component</param>
  46. /// <param name="y">second component</param>
  47. public Vector2D(float x, float y)
  48. {
  49. X = x;
  50. Y = y;
  51. }
  52. /// <summary>
  53. /// Constructor to create a Vector2D from a Point .
  54. /// </summary>
  55. /// <param name="point">a Point</param>
  56. public Vector2D(Point point)
  57. {
  58. X = point.X;
  59. Y = point.Y;
  60. }
  61. /// <summary>
  62. /// Constructor to create a Vector2D from a PointF.
  63. /// </summary>
  64. /// <param name="point">a PointF</param>
  65. public Vector2D(PointF point)
  66. {
  67. X = point.X;
  68. Y = point.Y;
  69. }
  70. /// <summary>
  71. /// Computes the euclidean distance between the point this vector describes and another point described by a Vector.
  72. /// </summary>
  73. /// <param name="point">a Point</param>
  74. /// <returns>euclidean distance</returns>
  75. public float getDistanceTo(Vector2D point)
  76. {
  77. return (this - point).Length;
  78. }
  79. /// <summary>
  80. /// Computes the angle between this vector and another vector in radians.
  81. /// </summary>
  82. /// <param name="vector">a vector</param>
  83. /// <returns>angle in radians</returns>
  84. public float getAngleBetween(Vector2D vector)
  85. {
  86. return (float)Math.Acos(dotProduct(vector) / (Length * vector.Length));
  87. }
  88. /// <summary>
  89. /// Computes whether this vector and another vector point in opposite directions, meaning the smallest angle is between 90° and 180°.
  90. /// </summary>
  91. /// <param name="vector">a vector</param>
  92. /// <returns>true iff the vectors point in opposite directions</returns>
  93. public bool isInOppositeDirection(Vector2D vector)
  94. {
  95. return (getAngleBetween(vector) > Math.PI / 2);
  96. }
  97. /// <summary>
  98. /// Computes the dot product of this vector and another vector.
  99. /// </summary>
  100. /// <param name="vector">the other vector</param>
  101. /// <returns>the dot product</returns>
  102. public float dotProduct(Vector2D vector)
  103. {
  104. return X * vector.X + Y * vector.Y;
  105. }
  106. /// <summary>
  107. /// Computes the cross product (determinant) of this vector and another vector.
  108. /// </summary>
  109. /// <param name="v">the other vector</param>
  110. /// <returns>cross product of this and v</returns>
  111. public float crossProduct(Vector2D v)
  112. {
  113. return X * v.Y - Y * v.X;
  114. }
  115. /// <summary>
  116. /// Multiplies this vector component-by-component with another vector.
  117. /// </summary>
  118. /// <param name="v">the other vector</param>
  119. /// <returns>the component-by-component multiplied vector</returns>
  120. public Vector2D scale(Vector2D v)
  121. {
  122. return new Vector2D(X * v.X, Y * v.Y);
  123. }
  124. /// <summary>
  125. /// Determines whether this point (vector) is inside a given box or not.
  126. /// </summary>
  127. /// <param name="corner1">first corner of the box</param>
  128. /// <param name="corner2">second corner of the box</param>
  129. /// <returns>true iff point is inside the box</returns>
  130. public bool isInBox(Vector2D corner1, Vector2D corner2)
  131. {
  132. float minX = Math.Min(corner1.X, corner2.X);
  133. float maxX = Math.Max(corner1.X, corner2.X);
  134. float minY = Math.Min(corner1.Y, corner2.Y);
  135. float maxY = Math.Max(corner1.Y, corner2.Y);
  136. return (minX <= X && X <= maxX && minY <= Y && Y <= maxY);
  137. }
  138. /// <summary>
  139. /// Determines whether a this point is inside the boundaries of a given image or not.
  140. /// </summary>
  141. /// <param name="imageSize">a imageSize object</param>
  142. /// <returns>true iff point is inside the image boundaries</returns>
  143. public bool isInBound(ImageSize imageSize)
  144. {
  145. return isInBound(Vector2D.Zero, imageSize.MaxPixel);
  146. }
  147. /// <summary>
  148. /// Determines whether a point is inside a box. iff bottomRight is higher or more left than topLeft the result is false.
  149. /// </summary>
  150. /// <param name="topLeft">top left corner of the box</param>
  151. /// <param name="bottomRight">bottom right corner of the box</param>
  152. /// <returns>true iff point is in box</returns>
  153. public bool isInBound(Vector2D topLeft, Vector2D bottomRight)
  154. {
  155. return (X >= topLeft.X && X <= bottomRight.X && Y >= topLeft.Y && Y <= bottomRight.Y);
  156. }
  157. /// <summary>
  158. /// Moves this vector along the direction vector factor times inside the imageSize, this point won't leave the image.
  159. /// </summary>
  160. /// <param name="imageSize">the imagesize</param>
  161. /// <param name="direction">the move direction</param>
  162. /// <param name="factor">the move factor</param>
  163. /// <returns>a point inside the image</returns>
  164. public Vector2D moveWithinBound(ImageSize imageSize, Vector2D direction, float factor)
  165. {
  166. Vector2D newPosition = this + factor * direction;
  167. if (!newPosition.isInBound(imageSize))
  168. {
  169. Vector2D inverseDirection = direction.getInverse().normalize();
  170. while (!newPosition.isInBound(imageSize))
  171. {
  172. newPosition += inverseDirection;
  173. }
  174. }
  175. return newPosition;
  176. }
  177. /// <summary>
  178. /// Normalizes this vector with the euclidean norm (2. norm).
  179. /// </summary>
  180. /// <returns>normalized vector</returns>
  181. public Vector2D normalize()
  182. {
  183. return new Vector2D(X / Length, Y / Length);
  184. }
  185. /// <summary>
  186. /// Computes a orthogonal vector of this vector, if side is true the X component will be switched, else the Y component.
  187. /// </summary>
  188. /// <param name="side">which vector</param>
  189. /// <returns>a orthogonal vector</returns>
  190. public Vector2D getOrthogonal(bool side = true)
  191. {
  192. if (side)
  193. return new Vector2D(Y, -X);
  194. else
  195. return new Vector2D(-Y, X);
  196. }
  197. /// <summary>
  198. /// Computes the inverse vector of this vector.
  199. /// </summary>
  200. /// <returns>inverse vector</returns>
  201. public Vector2D getInverse()
  202. {
  203. return new Vector2D(-X, -Y);
  204. }
  205. /// <summary>
  206. /// Computes the absolute vector of this vector.
  207. /// </summary>
  208. /// <returns>absolute vector</returns>
  209. public Vector2D getAbsolute()
  210. {
  211. return new Vector2D(Math.Abs(X), Math.Abs(Y));
  212. }
  213. /// <summary>
  214. /// Copies this vector (clone).
  215. /// </summary>
  216. /// <returns>this vector as new vector</returns>
  217. public Vector2D copy()
  218. {
  219. return new Vector2D(X, Y);
  220. }
  221. /// <summary>
  222. /// Creates a description of this vector.
  223. /// </summary>
  224. /// <returns>a string describing this vector</returns>
  225. public override string ToString()
  226. {
  227. return "(" + X + "|" + Y + ")";
  228. }
  229. /// <summary>
  230. /// Multiplies this vector component-by-component with a scalar value.
  231. /// </summary>
  232. /// <param name="scalar">multiplier</param>
  233. /// <param name="vector">multiplicant</param>
  234. /// <returns>multiplied vector (product)</returns>
  235. public static Vector2D operator *(float scalar, Vector2D vector)
  236. {
  237. return new Vector2D(scalar * vector.X, scalar * vector.Y);
  238. }
  239. /// <summary>
  240. /// Multiplies this vector component-by-component with a scalar value.
  241. /// </summary>
  242. /// <param name="vector">multiplicant</param>
  243. /// <param name="scalar">multiplier</param>
  244. /// <returns>multiplied vector (product)</returns>
  245. public static Vector2D operator *(Vector2D vector, float scalar)
  246. {
  247. return new Vector2D(scalar * vector.X, scalar * vector.Y);
  248. }
  249. /// <summary>
  250. /// Divides this vector with a scalar value.
  251. /// </summary>
  252. /// <param name="vector">this vector</param>
  253. /// <param name="scalar">the value</param>
  254. /// <returns>the divided vector</returns>
  255. public static Vector2D operator /(Vector2D vector, float scalar)
  256. {
  257. return new Vector2D(vector.X / scalar, vector.Y / scalar);
  258. }
  259. /// <summary>
  260. /// Divides on vector with another vector component-by-component.
  261. /// </summary>
  262. /// <param name="vector1">the dividend</param>
  263. /// <param name="vector2">the divisor</param>
  264. /// <returns>the component divided vector</returns>
  265. public static Vector2D operator /(Vector2D vector1, Vector2D vector2)
  266. {
  267. return new Vector2D(vector1.X / vector2.X, vector1.Y / vector2.Y);
  268. }
  269. /// <summary>
  270. /// Adds two vectors (component-by-component).
  271. /// </summary>
  272. /// <param name="vector1">first addend</param>
  273. /// <param name="vector2">second addend</param>
  274. /// <returns>sum of the vectors</returns>
  275. public static Vector2D operator +(Vector2D vector1, Vector2D vector2)
  276. {
  277. return new Vector2D(vector1.X + vector2.X, vector1.Y + vector2.Y);
  278. }
  279. /// <summary>
  280. /// Subtracts two vectors (component-by-component).
  281. /// </summary>
  282. /// <param name="vector1">the minuend</param>
  283. /// <param name="vector2">the subtrahend</param>
  284. /// <returns>the difference of the two vectors</returns>
  285. public static Vector2D operator -(Vector2D vector1, Vector2D vector2)
  286. {
  287. return new Vector2D(vector1.X - vector2.X, vector1.Y - vector2.Y);
  288. }
  289. /// <summary>
  290. /// Computes the mean of two vectors.
  291. /// </summary>
  292. /// <param name="vectors">second vector</param>
  293. /// <returns>the mean vector</returns>
  294. public static Vector2D mean(List<Vector2D> vectors)
  295. {
  296. Vector2D sumVector = Vector2D.sum(vectors);
  297. return sumVector / vectors.Count;
  298. }
  299. /// <summary>
  300. /// Sums a list of vectors (component-by-component).
  301. /// </summary>
  302. /// <param name="vectors">a list of vectors</param>
  303. /// <returns>vector of sumed components</returns>
  304. public static Vector2D sum(List<Vector2D> vectors)
  305. {
  306. Vector2D sumVector = new Vector2D(0, 0);
  307. foreach (Vector2D vector in vectors)
  308. sumVector += vector;
  309. return sumVector;
  310. }
  311. /// <summary>
  312. /// Casts a Vector2D to PointF.
  313. /// </summary>
  314. /// <param name="vec">a vector</param>
  315. /// <returns>a PointF</returns>
  316. public static implicit operator PointF(Vector2D vec)
  317. {
  318. return new PointF(vec.X, vec.Y);
  319. }
  320. /// <summary>
  321. /// Casts a Vector2D to Point.
  322. /// </summary>
  323. /// <param name="vec">a vector</param>
  324. /// <returns>a Point</returns>
  325. public static implicit operator Point(Vector2D vec)
  326. {
  327. return new Point(vec.IntX, vec.IntY);
  328. }
  329. }
  330. }