From 0c3bf48d2c0b5fadef88f7af4d452ff160747d9e Mon Sep 17 00:00:00 2001 From: Syntriax Date: Wed, 24 Jan 2024 12:31:53 +0300 Subject: [PATCH] feat: Vector2D.Orientation --- Engine.Core/Vector2D.cs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/Engine.Core/Vector2D.cs b/Engine.Core/Vector2D.cs index 336fd4e..3f8292f 100644 --- a/Engine.Core/Vector2D.cs +++ b/Engine.Core/Vector2D.cs @@ -49,6 +49,23 @@ public record Vector2D(float X, float Y) 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; + /// + /// Finds the Orientation of 3 s + /// + /// 0 -> Collinear, 1 -> Clockwise, 2 -> Counterclockwise + public static int Orientation(Vector2D left, Vector2D middle, Vector2D right) + { + Vector2D leftToMiddle = left.FromTo(middle); + Vector2D middleToRight = middle.FromTo(right); + + float value = leftToMiddle.Y * middleToRight.X - + leftToMiddle.X * middleToRight.Y; + + if (value > 0) return 1; + if (value < 0) return 2; + return 0; + } + public static bool ApproximatelyEquals(Vector2D left, Vector2D right, float epsilon = float.Epsilon) => left.X.ApproximatelyEquals(right.X, epsilon) && left.Y.ApproximatelyEquals(right.Y, epsilon);