123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace bbiwarg.Utility
- {
- class Vector2D
- {
- public float x;
- public float y;
- public Vector2D(float x, float y) {
- this.x = x;
- this.y = y;
- }
- public float getLength() {
- return (float) Math.Sqrt(x * x + y * y);
- }
- public float getDistanceTo(Vector2D vector) {
- float xDiff = x - vector.x;
- float yDiff = y - vector.y;
- return (float)Math.Sqrt(xDiff * xDiff + yDiff * yDiff);
- }
- public float getMinAngleBetween(Vector2D vector) {
- float angle = getAngleBetween(vector);
- if (angle > Math.PI/2)
- return (float) (Math.PI - angle);
- else
- return angle;
- }
- public float getAngleBetween(Vector2D vector) {
- return (float)Math.Acos(dotProduct(vector) / (getLength() * vector.getLength()));
- }
- public float dotProduct(Vector2D vector) {
- return x * vector.x + y * vector.y;
- }
- public static Vector2D projectToLine(Vector2D point, Vector2D direction, Vector2D pointOnLine) {
- float px = point.x, py = point.y, dx = direction.x, dy = direction.y, ox = pointOnLine.x, oy = pointOnLine.y;
- float diffx = px - ox;
- float diffy = py - oy;
- float diff_d = (diffx * dx + diffy * dy);
- float d_d = (dx * dx + dy * dy);
- float q = diff_d / d_d;
- float newX = ox + q * dx;
- float newY = oy + q * dy;
- return new Vector2D(newX, newY);
- }
- public static Vector2D operator *(float scalar, Vector2D vector)
- {
- return new Vector2D(scalar * vector.x, scalar * vector.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);
- }
- }
- }
|