123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace bbiwarg.Utility
- {
- class Vector2D
- {
- 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(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 cross(Vector2D v)
- {
- return X * v.Y - Y * v.X;
- }
- 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 Vector2D normalize()
- {
- return new Vector2D(X / Length, Y / Length);
- }
- 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 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);
- }
- }
- }
|