Vector2D.cs 13 KB

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