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 bool isInOppositeDirection(Vector2D vector) { return (getAngleBetween(vector) > Math.PI / 2); } 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(ImageSize imageSize) { return isInBound(Vector2D.Zero, imageSize.MaxPixel); } public bool isInBound(Vector2D topLeft, Vector2D bottomRight) { return (X >= topLeft.X && X <= bottomRight.X && Y >= topLeft.Y && Y <= bottomRight.Y); } public Vector2D moveInBound(ImageSize imageSize, Vector2D inBoundDirection) { return moveInBound(Vector2D.Zero, imageSize.MaxPixel, inBoundDirection); } public Vector2D moveInBound(Vector2D topLeft, Vector2D bottomRight, Vector2D inBoundDirection) { Vector2D position = new Vector2D(X, Y); while (!position.isInBound(topLeft, bottomRight)) position += inBoundDirection; return position; } public Vector2D moveWithinBound(ImageSize imageSize, Vector2D direction, float factor) { Vector2D newPosition = this + factor * direction; if (!newPosition.isInBound(imageSize)) newPosition = newPosition.moveInBound(imageSize, direction.getInverse()); return newPosition; } 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 Vector2D copy() { return new Vector2D(X, 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 vectors) { Vector2D sumVector = Vector2D.sum(vectors); return sumVector / vectors.Count; } public static Vector2D sum(List vectors) { Vector2D sumVector = new Vector2D(0, 0); foreach (Vector2D vector in vectors) sumVector += vector; return sumVector; } 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); } } }