From 02cc897426b803d5af1441d96a8b12e1bef838c9 Mon Sep 17 00:00:00 2001 From: Oliver Booth Date: Wed, 29 Jun 2022 17:55:04 +0100 Subject: [PATCH] Add Point.IsOnLine (#64) --- CHANGELOG.md | 5 ++ X10D.Unity/src/Drawing/PointFExtensions.cs | 19 ++++++ X10D.Unity/src/Numerics/Vector2Extensions.cs | 52 ++++++++++++++ .../src/Numerics/Vector2IntExtensions.cs | 68 +++++++++++++++++++ X10D/src/Drawing/PointExtensions.cs | 62 +++++++++++++++++ X10D/src/Drawing/PointFExtensions.cs | 65 ++++++++++++++++++ X10D/src/Numerics/Vector2Extensions.cs | 66 ++++++++++++++++++ 7 files changed, 337 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6c2f5f0..03386e6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,8 @@ - X10D: Added `Circle`, `CircleF`, `Cuboid`, `Ellipse`, `EllipseF`, `Line3D`, `Line`, `LineF`, `Polygon`, `PolygonF`, `Polyhedron`, and `Sphere`, to complement System.Drawing structs such as `Point` and `Rectangle` - X10D: Added `DirectoryInfo.Clear([bool])` - X10D: Added `IList.RemoveRange(Range)` +- X10D: Added `Point.IsOnLine(LineF)`, `Point.IsOnLine(PointF, PointF)`, and `Point.IsOnLine(Vector2, Vector2)` +- X10D: Added `PointF.IsOnLine(LineF)`, `PointF.IsOnLine(PointF, PointF)`, and `PointF.IsOnLine(Vector2, Vector2)` - X10D: Added `Point.ToSize()` - X10D: Added `Point.ToSizeF()` - X10D: Added `Point.ToVector2()` @@ -17,6 +19,7 @@ - X10D: Added `Size.ToVector2()` - X10D: Added `Quaternion.Multiply(Vector3)` - this functions as an equivalent to Unity's `Quaternion * Vector3` operator - X10D: Added `Vector2.Deconstruct()` +- X10D: Added `Vector2.IsOnLine(LineF)`, `Vector2.IsOnLine(PointF, PointF)`, and `Vector2.IsOnLine(Vector2, Vector2)` - X10D: Added `Vector2.ToPointF()` - X10D: Added `Vector2.ToSizeF()` - X10D: Added `Vector3.Deconstruct()` @@ -39,6 +42,8 @@ - X10D.Unity: Added `Size.ToUnityVector2Int()` - X10D.Unity: Added `SizeF.ToUnityVector2()` - X10D.Unity: Added `Vector2.Deconstruct()` +- X10D.Unity: Added `Vector2.IsOnLine(LineF)`, `Vector2.IsOnLine(PointF, PointF)`, and `Vector2.IsOnLine(Vector2, Vector2)` +- X10D.Unity: Added `Vector2Int.IsOnLine(LineF)`, `Vector2Int.IsOnLine(PointF, PointF)`, `Vector2Int.IsOnLine(Vector2, Vector2)`, and `Vector2Int.IsOnLine(Vector2Int, Vector2Int)` - X10D.Unity: Added `Vector2.ToSystemPointF()` - X10D.Unity: Added `Vector2.ToSystemSizeF()` - X10D.Unity: Added `Vector2Int.Deconstruct()` diff --git a/X10D.Unity/src/Drawing/PointFExtensions.cs b/X10D.Unity/src/Drawing/PointFExtensions.cs index fa936ce..4940e46 100644 --- a/X10D.Unity/src/Drawing/PointFExtensions.cs +++ b/X10D.Unity/src/Drawing/PointFExtensions.cs @@ -2,6 +2,8 @@ using System.Drawing; using System.Runtime.CompilerServices; using UnityEngine; +using X10D.Drawing; +using X10D.Unity.Numerics; namespace X10D.Unity.Drawing; @@ -10,6 +12,23 @@ namespace X10D.Unity.Drawing; /// public static class PointFExtensions { + /// + /// Determines if the current lies on the specified . + /// + /// The point to check. + /// The starting point of the line. + /// The ending point of the line. + /// + /// if lies on the line defined by and + /// ; otherwise . + /// + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static bool IsOnLine(this PointF point, Vector2 start, Vector2 end) + { + return point.IsOnLine(start.ToSystemVector(), end.ToSystemVector()); + } + /// /// Converts the current to a . /// diff --git a/X10D.Unity/src/Numerics/Vector2Extensions.cs b/X10D.Unity/src/Numerics/Vector2Extensions.cs index 6ae262c..23ff4da 100644 --- a/X10D.Unity/src/Numerics/Vector2Extensions.cs +++ b/X10D.Unity/src/Numerics/Vector2Extensions.cs @@ -2,6 +2,8 @@ using System.Drawing; using System.Runtime.CompilerServices; using UnityEngine; +using X10D.Drawing; +using X10D.Numerics; namespace X10D.Unity.Numerics; @@ -22,6 +24,56 @@ public static class Vector2Extensions y = vector.y; } + /// + /// Determines if the current lies on the specified . + /// + /// The point to check. + /// The line on which the point may lie. + /// + /// if lies on the line defined by ; otherwise + /// . + /// + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static bool IsOnLine(this Vector2 point, LineF line) + { + return point.ToSystemVector().IsOnLine(line); + } + + /// + /// Determines if the current lies on the specified line. + /// + /// The point to check. + /// The starting point of the line. + /// The ending point of the line. + /// + /// if lies on the line defined by and + /// ; otherwise . + /// + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static bool IsOnLine(this Vector2 point, PointF start, PointF end) + { + return point.IsOnLine(new LineF(start, end)); + } + + /// + /// Determines if the current lies on the specified line. + /// + /// The point to check. + /// The starting point of the line. + /// The ending point of the line. + /// + /// if lies on the line defined by and + /// ; otherwise . + /// + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static bool IsOnLine(this Vector2 point, Vector2 start, Vector2 end) + { + return point.ToSystemVector().IsOnLine(start.ToSystemVector(), end.ToSystemVector()); + } + /// /// Converts the current into a . /// diff --git a/X10D.Unity/src/Numerics/Vector2IntExtensions.cs b/X10D.Unity/src/Numerics/Vector2IntExtensions.cs index fd551ae..babf2d9 100644 --- a/X10D.Unity/src/Numerics/Vector2IntExtensions.cs +++ b/X10D.Unity/src/Numerics/Vector2IntExtensions.cs @@ -2,6 +2,7 @@ using System.Drawing; using System.Runtime.CompilerServices; using UnityEngine; +using X10D.Drawing; namespace X10D.Unity.Numerics; @@ -22,6 +23,73 @@ public static class Vector2IntExtensions y = vector.y; } + /// + /// Determines if the current lies on the specified . + /// + /// The point to check. + /// The line on which the point may lie. + /// + /// if lies on the line defined by ; otherwise + /// . + /// + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static bool IsOnLine(this Vector2Int point, LineF line) + { + return point.ToSystemPoint().IsOnLine(line); + } + + /// + /// Determines if the current lies on the specified line. + /// + /// The point to check. + /// The starting point of the line. + /// The ending point of the line. + /// + /// if lies on the line defined by and + /// ; otherwise . + /// + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static bool IsOnLine(this Vector2Int point, PointF start, PointF end) + { + return point.IsOnLine(new LineF(start, end)); + } + + /// + /// Determines if the current lies on the specified line. + /// + /// The point to check. + /// The starting point of the line. + /// The ending point of the line. + /// + /// if lies on the line defined by and + /// ; otherwise . + /// + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static bool IsOnLine(this Vector2Int point, Vector2Int start, Vector2Int end) + { + return point.ToSystemPoint().IsOnLine(new LineF(start.ToSystemVector(), end.ToSystemVector())); + } + + /// + /// Determines if the current lies on the specified line. + /// + /// The point to check. + /// The starting point of the line. + /// The ending point of the line. + /// + /// if lies on the line defined by and + /// ; otherwise . + /// + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static bool IsOnLine(this Vector2Int point, Vector2 start, Vector2 end) + { + return point.ToSystemPoint().IsOnLine(new LineF(start.ToSystemVector(), end.ToSystemVector())); + } + /// /// Converts the current into a . /// diff --git a/X10D/src/Drawing/PointExtensions.cs b/X10D/src/Drawing/PointExtensions.cs index 50c708f..f81f0e1 100644 --- a/X10D/src/Drawing/PointExtensions.cs +++ b/X10D/src/Drawing/PointExtensions.cs @@ -10,6 +10,68 @@ namespace X10D.Drawing; /// public static class PointExtensions { + /// + /// Determines if the current lies on the specified . + /// + /// The point to check. + /// The line on which the point may lie. + /// + /// if lies on the line defined by ; otherwise + /// . + /// + [Pure] +#if NETSTANDARD2_1 + [MethodImpl(MethodImplOptions.AggressiveInlining)] +#else + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] +#endif + public static bool IsOnLine(this Point point, LineF line) + { + return ((PointF)point).IsOnLine(line); + } + + /// + /// Determines if the current lies on the specified line. + /// + /// The point to check. + /// The starting point of the line. + /// The ending point of the line. + /// + /// if lies on the line defined by and + /// ; otherwise . + /// + [Pure] +#if NETSTANDARD2_1 + [MethodImpl(MethodImplOptions.AggressiveInlining)] +#else + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] +#endif + public static bool IsOnLine(this Point point, PointF start, PointF end) + { + return point.IsOnLine(new LineF(start, end)); + } + + /// + /// Determines if the current lies on the specified line. + /// + /// The point to check. + /// The starting point of the line. + /// The ending point of the line. + /// + /// if lies on the line defined by and + /// ; otherwise . + /// + [Pure] +#if NETSTANDARD2_1 + [MethodImpl(MethodImplOptions.AggressiveInlining)] +#else + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] +#endif + public static bool IsOnLine(this Point point, Vector2 start, Vector2 end) + { + return point.IsOnLine(new LineF(start, end)); + } + /// /// Converts the current to a . /// diff --git a/X10D/src/Drawing/PointFExtensions.cs b/X10D/src/Drawing/PointFExtensions.cs index cb9d3ac..13864e4 100644 --- a/X10D/src/Drawing/PointFExtensions.cs +++ b/X10D/src/Drawing/PointFExtensions.cs @@ -10,6 +10,71 @@ namespace X10D.Drawing; /// public static class PointFExtensions { + /// + /// Determines if the current lies on the specified . + /// + /// The point to check. + /// The line on which the point may lie. + /// + /// if lies on the line defined by ; otherwise + /// . + /// + [Pure] +#if NETSTANDARD2_1 + [MethodImpl(MethodImplOptions.AggressiveInlining)] +#else + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] +#endif + public static bool IsOnLine(this PointF point, LineF line) + { + (float x1, float x2) = (line.Start.X, line.End.X); + (float y1, float y2) = (line.Start.Y, line.End.Y); + (float x, float y) = (point.X, point.Y); + return System.Math.Abs((y2 - y1) * (x - x2) - (y - y2) * (x2 - x1)) < float.Epsilon; + } + + /// + /// Determines if the current lies on the specified . + /// + /// The point to check. + /// The starting point of the line. + /// The ending point of the line. + /// + /// if lies on the line defined by and + /// ; otherwise . + /// + [Pure] +#if NETSTANDARD2_1 + [MethodImpl(MethodImplOptions.AggressiveInlining)] +#else + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] +#endif + public static bool IsOnLine(this PointF point, PointF start, PointF end) + { + return point.IsOnLine(new LineF(start, end)); + } + + /// + /// Determines if the current lies on the specified . + /// + /// The point to check. + /// The starting point of the line. + /// The ending point of the line. + /// + /// if lies on the line defined by and + /// ; otherwise . + /// + [Pure] +#if NETSTANDARD2_1 + [MethodImpl(MethodImplOptions.AggressiveInlining)] +#else + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] +#endif + public static bool IsOnLine(this PointF point, Vector2 start, Vector2 end) + { + return point.IsOnLine(new LineF(start, end)); + } + /// /// Converts the current to a . /// diff --git a/X10D/src/Numerics/Vector2Extensions.cs b/X10D/src/Numerics/Vector2Extensions.cs index 11533de..6ec710d 100644 --- a/X10D/src/Numerics/Vector2Extensions.cs +++ b/X10D/src/Numerics/Vector2Extensions.cs @@ -2,6 +2,7 @@ using System.Drawing; using System.Numerics; using System.Runtime.CompilerServices; +using X10D.Drawing; namespace X10D.Numerics; @@ -22,6 +23,71 @@ public static class Vector2Extensions y = vector.Y; } + /// + /// Determines if the current lies on the specified line. + /// + /// The point to check. + /// The line on which the point may lie. + /// + /// if lies on the line defined by ; otherwise + /// . + /// + [Pure] +#if NETSTANDARD2_1 + [MethodImpl(MethodImplOptions.AggressiveInlining)] +#else + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] +#endif + public static bool IsOnLine(this Vector2 point, LineF line) + { + (float x1, float x2) = (line.Start.X, line.End.X); + (float y1, float y2) = (line.Start.Y, line.End.Y); + (float x, float y) = (point.X, point.Y); + return System.Math.Abs((y2 - y1) * (x - x2) - (y - y2) * (x2 - x1)) < float.Epsilon; + } + + /// + /// Determines if the current lies on the specified line. + /// + /// The point to check. + /// The starting point of the line. + /// The ending point of the line. + /// + /// if lies on the line defined by and + /// ; otherwise . + /// + [Pure] +#if NETSTANDARD2_1 + [MethodImpl(MethodImplOptions.AggressiveInlining)] +#else + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] +#endif + public static bool IsOnLine(this Vector2 point, PointF start, PointF end) + { + return point.IsOnLine(new LineF(start, end)); + } + + /// + /// Determines if the current lies on the specified line. + /// + /// The point to check. + /// The starting point of the line. + /// The ending point of the line. + /// + /// if lies on the line defined by and + /// ; otherwise . + /// + [Pure] +#if NETSTANDARD2_1 + [MethodImpl(MethodImplOptions.AggressiveInlining)] +#else + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] +#endif + public static bool IsOnLine(this Vector2 point, Vector2 start, Vector2 end) + { + return point.IsOnLine(new LineF(start, end)); + } + /// /// Converts the current to a . ///