123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace bbiwarg.Utility
- {
- public class Vector2D
- {
- public static Vector2D Zero { get { return new Vector2D(0, 0); } }
- public float X { get; private set; }
- public float Y { get; private set; }
- public int IntX { get { return (int)X; } }
- public int IntY { get { return (int)Y; } }
- public float Length { get { return (float)Math.Sqrt(X * X + Y * Y); } }
- public Vector2D(float x, float y)
- {
- X = x;
- Y = y;
- }
- public Vector2D(Vector2D vec)
- {
- X = vec.X;
- Y = vec.Y;
- }
- public Vector2D(Point point)
- {
- X = point.X;
- Y = point.Y;
- }
- public Vector2D(PointF point)
- {
- X = point.X;
- Y = point.Y;
- }
- public float getDistanceTo(Vector2D point)
- {
- return (this - point).Length;
- }
- public float getAngleBetween(Vector2D vector)
- {
- return (float)Math.Acos(dotProduct(vector) / (Length * vector.Length));
- }
- public float dotProduct(Vector2D vector)
- {
- return X * vector.X + Y * vector.Y;
- }
- public float crossProduct(Vector2D v)
- {
- return X * v.Y - Y * v.X;
- }
- public Vector2D scale(Vector2D v) {
- return new Vector2D(X * v.X, Y * v.Y);
- }
- public bool isInBox(Vector2D corner1, Vector2D corner2)
- {
- float minX = Math.Min(corner1.X, corner2.X);
- float maxX = Math.Max(corner1.X, corner2.X);
- float minY = Math.Min(corner1.Y, corner2.Y);
- float maxY = Math.Max(corner1.Y, corner2.Y);
- return (minX <= X && X <= maxX && minY <= Y && Y <= maxY);
- }
- public bool isInBound(Vector2D topLeft, Vector2D bottomRight) {
- return (X >= topLeft.X && X <= bottomRight.X && Y >= topLeft.Y && Y <= bottomRight.Y);
- }
- public Vector2D moveInBound(Vector2D topLeft, Vector2D bottomRight, Vector2D inBoundDirection) {
- Vector2D pos = new Vector2D(X, Y);
- while (!pos.isInBound(topLeft, bottomRight)) {
- pos += inBoundDirection;
- }
- return pos;
- }
- public Vector2D normalize()
- {
- float length = Length;
- return new Vector2D(X / length, Y / length);
- }
- public Vector2D getOrthogonal(bool side=true)
- {
- if (side)
- return new Vector2D(Y, -X);
- else
- return new Vector2D(-Y, X);
- }
- public Vector2D getInverse()
- {
- return new Vector2D(-X, -Y);
- }
- public Vector2D getAbsolute() {
- return new Vector2D(Math.Abs(X), Math.Abs(Y));
- }
- public override string ToString()
- {
- return "(" + X + "|" + Y + ")";
- }
- public static Vector2D operator *(float scalar, Vector2D vector)
- {
- return new Vector2D(scalar * vector.X, scalar * vector.Y);
- }
- public static Vector2D operator *(Vector2D vector, float scalar)
- {
- return new Vector2D(scalar * vector.X, scalar * vector.Y);
- }
- public static Vector2D operator /(Vector2D vector, float scalar)
- {
- return new Vector2D(vector.X / scalar, vector.Y / scalar);
- }
- public static Vector2D operator /(Vector2D vector1, Vector2D vector2)
- {
- return new Vector2D(vector1.X / vector2.X, vector1.Y / vector2.Y);
- }
- public static Vector2D operator +(Vector2D vector1, Vector2D vector2)
- {
- return new Vector2D(vector1.X + vector2.X, vector1.Y + vector2.Y);
- }
- public static Vector2D operator -(Vector2D vector1, Vector2D vector2)
- {
- return new Vector2D(vector1.X - vector2.X, vector1.Y - vector2.Y);
- }
- public static Vector2D mean(List<Vector2D> vectors)
- {
- Vector2D meanVector = new Vector2D(0, 0);
- foreach (Vector2D vector in vectors)
- {
- meanVector += vector;
- }
- return meanVector / vectors.Count;
- }
- public static implicit operator PointF(Vector2D vec)
- {
- return new PointF(vec.X, vec.Y);
- }
- public static implicit operator Point(Vector2D vec)
- {
- return new Point(vec.IntX, vec.IntY);
- }
- }
- }
|