From ea92bc8c07a33f5cd467159250bb684ae4fbbfa2 Mon Sep 17 00:00:00 2001 From: Syntriax Date: Mon, 22 Jan 2024 12:29:25 +0300 Subject: [PATCH] feat: Point2D Record --- Game/Physics2D/Primitives/Point2D.cs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 Game/Physics2D/Primitives/Point2D.cs diff --git a/Game/Physics2D/Primitives/Point2D.cs b/Game/Physics2D/Primitives/Point2D.cs new file mode 100644 index 0000000..d344a34 --- /dev/null +++ b/Game/Physics2D/Primitives/Point2D.cs @@ -0,0 +1,19 @@ +using System; + +namespace Syntriax.Engine.Physics2D.Primitives; + +public record Point2D(float X, float Y) +{ + public static Point2D operator +(Point2D left, Point2D right) => new(left.X + right.X, left.Y + right.Y); + public static Point2D operator -(Point2D left, Point2D right) => new(left.X - right.X, left.Y - right.Y); + public static Point2D operator *(Point2D point, float value) => new(point.X * value, point.Y * value); + public static Point2D operator /(Point2D point, float value) => new(point.X / value, point.Y / value); + + public static float Length(Point2D point) => MathF.Sqrt(LengthSqr(point)); + public static float LengthSqr(Point2D point) => point.X * point.X + point.Y * point.Y; + + public static Point2D Normalize(Point2D point) => point / Length(point); + public static float Cross(Point2D left, Point2D right) => left.X * right.Y - left.Y * right.X; + public static float Angle(Point2D left, Point2D right) => MathF.Acos(Dot(left, right) / (Length(left) * Length(right))); + public static float Dot(Point2D left, Point2D right) => left.X * right.X + left.Y * right.Y; +}