From 83f8555cdade71e9a0f3c803b235d1e428d4a673 Mon Sep 17 00:00:00 2001 From: Twinki Date: Tue, 6 Feb 2024 20:32:36 -0500 Subject: [PATCH] Simplify shapes --- .../Internal/Vector2Assertions.cs | 4 ++-- src/PolyZone/Extensions/VectorExtensions.cs | 15 +++++++----- src/PolyZone/Shapes/Circle.cs | 4 ++-- src/PolyZone/Shapes/Cuboid.cs | 3 ++- src/PolyZone/Shapes/Interfaces/ICircle.cs | 2 +- src/PolyZone/Shapes/Interfaces/ICuboid.cs | 2 +- src/PolyZone/Shapes/Interfaces/IPolygon.cs | 2 +- src/PolyZone/Shapes/Interfaces/IRectangle.cs | 2 +- src/PolyZone/Shapes/Interfaces/IShape2d.cs | 20 ++++++++++++++++ src/PolyZone/Shapes/Interfaces/IShape3d.cs | 20 ++++++++++++++++ .../Shapes/Interfaces/ISpatial2dShape.cs | 23 ------------------- .../Shapes/Interfaces/ISpatial3dShape.cs | 13 ----------- src/PolyZone/Shapes/Interfaces/ISphere.cs | 2 +- src/PolyZone/Shapes/Polygon.cs | 4 ++-- src/PolyZone/Shapes/Rectangle.cs | 4 ++-- src/PolyZone/Shapes/Sphere.cs | 8 ++++++- 16 files changed, 71 insertions(+), 57 deletions(-) create mode 100644 src/PolyZone/Shapes/Interfaces/IShape2d.cs create mode 100644 src/PolyZone/Shapes/Interfaces/IShape3d.cs delete mode 100644 src/PolyZone/Shapes/Interfaces/ISpatial2dShape.cs delete mode 100644 src/PolyZone/Shapes/Interfaces/ISpatial3dShape.cs diff --git a/src/PolyZone.Tests/Internal/Vector2Assertions.cs b/src/PolyZone.Tests/Internal/Vector2Assertions.cs index 5ab03b6..c2ab7ca 100644 --- a/src/PolyZone.Tests/Internal/Vector2Assertions.cs +++ b/src/PolyZone.Tests/Internal/Vector2Assertions.cs @@ -21,7 +21,7 @@ internal class Vector2Assertions(Vector2 instance) : ReferenceTypeAssertions "directory"; - internal AndConstraint BeInside(ISpatial2dShape shape, string because = "", params object[] becauseArgs) + internal AndConstraint BeInside(IShape2d shape, string because = "", params object[] becauseArgs) { Execute.Assertion .BecauseOf(because, becauseArgs) @@ -49,7 +49,7 @@ internal AndConstraint BeInsideOnlyOneOf(IEnumerable(this); } - internal AndConstraint BeOutside(ISpatial2dShape shape, string because = "", params object[] becauseArgs) + internal AndConstraint BeOutside(IShape2d shape, string because = "", params object[] becauseArgs) { Execute.Assertion .BecauseOf(because, becauseArgs) diff --git a/src/PolyZone/Extensions/VectorExtensions.cs b/src/PolyZone/Extensions/VectorExtensions.cs index 1465e6a..7a583de 100644 --- a/src/PolyZone/Extensions/VectorExtensions.cs +++ b/src/PolyZone/Extensions/VectorExtensions.cs @@ -5,12 +5,15 @@ namespace PolyZone.Extensions; public static class VectorExtensions { - /// - public static bool IsInside(this Vector2 vector2, in ISpatial2dShape shape) => shape.Contains(vector2); + /// + public static bool IsInside(this Vector2 vector2, in IShape2d shape) => shape.Contains(vector2); - /// - public static float DistanceTo(this Vector2 vector2, in ISpatial2dShape shape) => shape.DistanceFrom(vector2); + /// + public static float DistanceTo(this Vector2 vector2, in IShape2d shape) => shape.DistanceFrom(vector2); - /// - public static bool IsInside(this Vector3 vector2, in ISpatial3dShape shape) => shape.Contains(vector2); + /// + public static bool IsInside(this Vector3 vector2, in IShape3d shape) => shape.Contains(vector2); + + /// + public static float DistanceTo(this Vector3 vector3, in IShape3d shape) => shape.DistanceFrom(vector3); } diff --git a/src/PolyZone/Shapes/Circle.cs b/src/PolyZone/Shapes/Circle.cs index 14e50ea..20ee49c 100644 --- a/src/PolyZone/Shapes/Circle.cs +++ b/src/PolyZone/Shapes/Circle.cs @@ -24,7 +24,7 @@ public Circle(float centerX, float centerY, float radius) : this(new Vector2 { X } - /// + /// public bool Contains(in Vector2 point) { // Calculate the distance from the center of the circle to the given point @@ -34,7 +34,7 @@ public bool Contains(in Vector2 point) return distance <= Radius; } - /// + /// public float DistanceFrom(in Vector2 point) { // Calculate the distance from the center of the circle to the given point diff --git a/src/PolyZone/Shapes/Cuboid.cs b/src/PolyZone/Shapes/Cuboid.cs index 0449fdd..b13487f 100644 --- a/src/PolyZone/Shapes/Cuboid.cs +++ b/src/PolyZone/Shapes/Cuboid.cs @@ -155,7 +155,7 @@ public Cuboid(in Vector3[] corners) Center = new Vector3 { X = (MinX + MaxX) / 2, Y = (MinY + MaxY) / 2, Z = (MinZ + MaxZ) / 2 }; } - /// + /// public bool Contains(in Vector3 point) { // Check if the point is within the bounds defined by the corners of the cuboid @@ -165,6 +165,7 @@ public bool Contains(in Vector3 point) point.Z >= MinZ && point.Z <= MaxZ; } + /// public float DistanceFrom(in Vector3 point) { if (Contains(point)) diff --git a/src/PolyZone/Shapes/Interfaces/ICircle.cs b/src/PolyZone/Shapes/Interfaces/ICircle.cs index 22e8022..0b64525 100644 --- a/src/PolyZone/Shapes/Interfaces/ICircle.cs +++ b/src/PolyZone/Shapes/Interfaces/ICircle.cs @@ -3,4 +3,4 @@ /// /// A 2d circle /// -public interface ICircle : ISpatial2dShape; +public interface ICircle : IShape2d; diff --git a/src/PolyZone/Shapes/Interfaces/ICuboid.cs b/src/PolyZone/Shapes/Interfaces/ICuboid.cs index 77c1c12..e9e675f 100644 --- a/src/PolyZone/Shapes/Interfaces/ICuboid.cs +++ b/src/PolyZone/Shapes/Interfaces/ICuboid.cs @@ -3,4 +3,4 @@ /// /// A 3d cuboid shape /// -public interface ICuboid : ISpatial3dShape; +public interface ICuboid : IShape3d; diff --git a/src/PolyZone/Shapes/Interfaces/IPolygon.cs b/src/PolyZone/Shapes/Interfaces/IPolygon.cs index 5b7abd4..a192385 100644 --- a/src/PolyZone/Shapes/Interfaces/IPolygon.cs +++ b/src/PolyZone/Shapes/Interfaces/IPolygon.cs @@ -5,7 +5,7 @@ namespace PolyZone.Shapes.Interfaces; /// /// A 2d polygon /// -public interface IPolygon : ISpatial2dShape +public interface IPolygon : IShape2d { public IReadOnlyList Points { get; } } diff --git a/src/PolyZone/Shapes/Interfaces/IRectangle.cs b/src/PolyZone/Shapes/Interfaces/IRectangle.cs index 84eb51d..bbcce10 100644 --- a/src/PolyZone/Shapes/Interfaces/IRectangle.cs +++ b/src/PolyZone/Shapes/Interfaces/IRectangle.cs @@ -3,4 +3,4 @@ /// /// A 2d rectangle / box /// -public interface IRectangle : ISpatial2dShape; +public interface IRectangle : IShape2d; diff --git a/src/PolyZone/Shapes/Interfaces/IShape2d.cs b/src/PolyZone/Shapes/Interfaces/IShape2d.cs new file mode 100644 index 0000000..ff12dd2 --- /dev/null +++ b/src/PolyZone/Shapes/Interfaces/IShape2d.cs @@ -0,0 +1,20 @@ +using CitizenFX.Core; + +namespace PolyZone.Shapes.Interfaces; + +public interface IShape2d +{ + /// + /// Spatially tests a given position to determine if it lies within the shape + /// + /// A position + /// True if the position is inside the shape + bool Contains(in Vector2 point); + + /// + /// Calculates the float-accurate distance from a given position to the shape + /// + /// A position + /// The float-accurate distance from a to the shape + float DistanceFrom(in Vector2 point); +} diff --git a/src/PolyZone/Shapes/Interfaces/IShape3d.cs b/src/PolyZone/Shapes/Interfaces/IShape3d.cs new file mode 100644 index 0000000..c11bafc --- /dev/null +++ b/src/PolyZone/Shapes/Interfaces/IShape3d.cs @@ -0,0 +1,20 @@ +using CitizenFX.Core; + +namespace PolyZone.Shapes.Interfaces; + +public interface IShape3d +{ + /// + /// Spatially tests a given position to determine if it lies within the shape + /// + /// A position + /// True if the position is inside the shape + bool Contains(in Vector3 point); + + /// + /// Calculates the float-accurate distance from a given position to the shape + /// + /// A position + /// The float-accurate distance from a to the shape + float DistanceFrom(in Vector3 point); +} diff --git a/src/PolyZone/Shapes/Interfaces/ISpatial2dShape.cs b/src/PolyZone/Shapes/Interfaces/ISpatial2dShape.cs deleted file mode 100644 index 6d9387a..0000000 --- a/src/PolyZone/Shapes/Interfaces/ISpatial2dShape.cs +++ /dev/null @@ -1,23 +0,0 @@ -using CitizenFX.Core; - -namespace PolyZone.Shapes.Interfaces; - -/// -/// Generic interface for spatial 2d shapes -/// -public interface ISpatial2dShape -{ - /// - /// Spatially tests a given to determine if it lies within the shape - /// - /// , otherwise known as a 2d position - /// True if the is inside the shape - bool Contains(in Vector2 point); - - /// - /// Calculates the float-accurate distance from a to the shape - /// - /// , otherwise known as a 2d position - /// The float-accurate distance from a to the shape - float DistanceFrom(in Vector2 point); -} diff --git a/src/PolyZone/Shapes/Interfaces/ISpatial3dShape.cs b/src/PolyZone/Shapes/Interfaces/ISpatial3dShape.cs deleted file mode 100644 index 2f01ad9..0000000 --- a/src/PolyZone/Shapes/Interfaces/ISpatial3dShape.cs +++ /dev/null @@ -1,13 +0,0 @@ -using CitizenFX.Core; - -namespace PolyZone.Shapes.Interfaces; - -public interface ISpatial3dShape -{ - /// - /// Spatially tests a given to determine if it lies within the shape - /// - /// , otherwise known as a 2d position - /// True if the is inside the shape - bool Contains(in Vector3 point); -} diff --git a/src/PolyZone/Shapes/Interfaces/ISphere.cs b/src/PolyZone/Shapes/Interfaces/ISphere.cs index de7c705..b3d97e6 100644 --- a/src/PolyZone/Shapes/Interfaces/ISphere.cs +++ b/src/PolyZone/Shapes/Interfaces/ISphere.cs @@ -3,4 +3,4 @@ /// /// A 3d spherical shape /// -public interface ISphere : ISpatial3dShape; +public interface ISphere : IShape3d; diff --git a/src/PolyZone/Shapes/Polygon.cs b/src/PolyZone/Shapes/Polygon.cs index eec9706..c2da1c3 100644 --- a/src/PolyZone/Shapes/Polygon.cs +++ b/src/PolyZone/Shapes/Polygon.cs @@ -13,7 +13,7 @@ public class Polygon(IReadOnlyList points) : IPolygon public IReadOnlyList Points { get; } = points; - /// + /// public bool Contains(in Vector2 point) { if (_boundingBox is null) @@ -35,7 +35,7 @@ public bool Contains(in Vector2 point) return Contains(point, Points, _boundingBox); } - /// + /// public float DistanceFrom(in Vector2 point) => DistanceFrom(point, Points); // https://web.archive.org/web/20210225074947/http://geomalgorithms.com/a03-_inclusion.html diff --git a/src/PolyZone/Shapes/Rectangle.cs b/src/PolyZone/Shapes/Rectangle.cs index e4d6051..62bedcf 100644 --- a/src/PolyZone/Shapes/Rectangle.cs +++ b/src/PolyZone/Shapes/Rectangle.cs @@ -13,14 +13,14 @@ public class Rectangle(in Vector2 upperLeft, in Vector2 bottomRight) : IRectangl public Vector2 UpperLeft { get; } = upperLeft; public Vector2 BottomRight { get; } = bottomRight; - /// + /// public bool Contains(in Vector2 point) { return point.X >= UpperLeft.X && point.X <= BottomRight.X && point.Y >= UpperLeft.Y && point.Y <= BottomRight.Y; } - /// + /// public float DistanceFrom(in Vector2 point) { // Calculate the distance using Euclidean distance formula diff --git a/src/PolyZone/Shapes/Sphere.cs b/src/PolyZone/Shapes/Sphere.cs index 87610d6..3e5b2c9 100644 --- a/src/PolyZone/Shapes/Sphere.cs +++ b/src/PolyZone/Shapes/Sphere.cs @@ -13,7 +13,7 @@ public class Sphere(in Vector3 center, float radius) : ISphere public Vector3 Center { get; } = center; public float Radius { get; } = radius; - /// + /// public bool Contains(in Vector3 point) { // Calculate the distance from the center of the sphere to the given point @@ -22,4 +22,10 @@ public bool Contains(in Vector3 point) // Check if the distance is less than or equal to the radius return distance <= Radius; } + + /// + public float DistanceFrom(in Vector3 point) + { + throw new NotImplementedException(); + } }