Vector2D.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 float X { get; private set; }
  12. public float Y { get; private set; }
  13. public int IntX { get { return (int)X; } }
  14. public int IntY { get { return (int)Y; } }
  15. public float Length { get { return (float)Math.Sqrt(X * X + Y * Y); } }
  16. public Vector2D(float x, float y)
  17. {
  18. X = x;
  19. Y = y;
  20. }
  21. public Vector2D(Point point)
  22. {
  23. X = point.X;
  24. Y = point.Y;
  25. }
  26. public Vector2D(PointF point)
  27. {
  28. X = point.X;
  29. Y = point.Y;
  30. }
  31. public float getDistanceTo(Vector2D point)
  32. {
  33. return (this - point).Length;
  34. }
  35. public float getAngleBetween(Vector2D vector)
  36. {
  37. return (float)Math.Acos(dotProduct(vector) / (Length * vector.Length));
  38. }
  39. public float dotProduct(Vector2D vector)
  40. {
  41. return X * vector.X + Y * vector.Y;
  42. }
  43. public float cross(Vector2D v)
  44. {
  45. return X * v.Y - Y * v.X;
  46. }
  47. public bool isInBox(Vector2D corner1, Vector2D corner2)
  48. {
  49. float minX = Math.Min(corner1.X, corner2.X);
  50. float maxX = Math.Max(corner1.X, corner2.X);
  51. float minY = Math.Min(corner1.Y, corner2.Y);
  52. float maxY = Math.Max(corner1.Y, corner2.Y);
  53. return (minX <= X && X <= maxX && minY <= Y && Y <= maxY);
  54. }
  55. public Vector2D normalize()
  56. {
  57. return new Vector2D(X / Length, Y / Length);
  58. }
  59. public override string ToString()
  60. {
  61. return "(" + X + ", " + Y + ")";
  62. }
  63. public static Vector2D operator *(float scalar, Vector2D vector)
  64. {
  65. return new Vector2D(scalar * vector.X, scalar * vector.Y);
  66. }
  67. public static Vector2D operator *(Vector2D vector, float scalar)
  68. {
  69. return new Vector2D(scalar * vector.X, scalar * vector.Y);
  70. }
  71. public static Vector2D operator /(Vector2D vector, float scalar)
  72. {
  73. return new Vector2D(vector.X / scalar, vector.Y / scalar);
  74. }
  75. public static Vector2D operator +(Vector2D vector1, Vector2D vector2)
  76. {
  77. return new Vector2D(vector1.X + vector2.X, vector1.Y + vector2.Y);
  78. }
  79. public static Vector2D operator -(Vector2D vector1, Vector2D vector2)
  80. {
  81. return new Vector2D(vector1.X - vector2.X, vector1.Y - vector2.Y);
  82. }
  83. public static Vector2D mean(List<Vector2D> vectors)
  84. {
  85. Vector2D meanVector = new Vector2D(0, 0);
  86. foreach (Vector2D vector in vectors)
  87. {
  88. meanVector += vector;
  89. }
  90. return meanVector / vectors.Count;
  91. }
  92. public static implicit operator PointF(Vector2D vec)
  93. {
  94. return new PointF(vec.X, vec.Y);
  95. }
  96. }
  97. }