Engine-Pong/Game/Physics2D/Primitives/Vector2D.cs

31 lines
1.7 KiB
C#
Raw Normal View History

2024-01-22 12:30:08 +03:00
using System;
namespace Syntriax.Engine.Physics2D.Primitives;
public record Vector2D(float X, float Y)
{
2024-01-22 12:55:20 +03:00
public readonly static Vector2D Up = new(0f, 1f);
public readonly static Vector2D Down = new(0f, -1f);
public readonly static Vector2D Left = new(-1f, 0f);
public readonly static Vector2D Right = new(1f, 0f);
public readonly static Vector2D One = new(1f, 1f);
2024-01-22 12:30:08 +03:00
public static Vector2D operator +(Vector2D left, Vector2D right) => new(left.X + right.X, left.Y + right.Y);
public static Vector2D operator -(Vector2D left, Vector2D right) => new(left.X - right.X, left.Y - right.Y);
public static Vector2D operator *(Vector2D vector, float value) => new(vector.X * value, vector.Y * value);
public static Vector2D operator /(Vector2D vector, float value) => new(vector.X / value, vector.Y / value);
2024-01-22 12:30:08 +03:00
public static float Length(Vector2D vector) => MathF.Sqrt(LengthSquared(vector));
public static float LengthSquared(Vector2D vector) => vector.X * vector.X + vector.Y * vector.Y;
2024-01-22 12:30:08 +03:00
2024-01-22 12:48:12 +03:00
public static float Distance(Vector2D from, Vector2D to) => Length(FromTo(from, to));
public static Vector2D Normalize(Vector2D vector) => vector / Length(vector);
2024-01-22 12:41:48 +03:00
public static Vector2D FromTo(Vector2D from, Vector2D to) => to - from;
2024-01-22 12:53:22 +03:00
public static Vector2D Scale(Vector2D vector, Vector2D scale) => new(vector.X * scale.X, vector.Y * scale.Y);
2024-01-22 12:41:48 +03:00
2024-01-22 12:30:08 +03:00
public static float Cross(Vector2D left, Vector2D right) => left.X * right.Y - left.Y * right.X;
public static float Angle(Vector2D left, Vector2D right) => MathF.Acos(Dot(left, right) / (Length(left) * Length(right)));
public static float Dot(Vector2D left, Vector2D right) => left.X * right.X + left.Y * right.Y;
}