Skip to content

Commit

Permalink
Simplify shapes
Browse files Browse the repository at this point in the history
  • Loading branch information
Twinki14 committed Feb 7, 2024
1 parent ac4549a commit 83f8555
Show file tree
Hide file tree
Showing 16 changed files with 71 additions and 57 deletions.
4 changes: 2 additions & 2 deletions src/PolyZone.Tests/Internal/Vector2Assertions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ internal class Vector2Assertions(Vector2 instance) : ReferenceTypeAssertions<Vec
private readonly Vector2 _instance = instance;
protected override string Identifier => "directory";

internal AndConstraint<Vector2Assertions> BeInside(ISpatial2dShape shape, string because = "", params object[] becauseArgs)
internal AndConstraint<Vector2Assertions> BeInside(IShape2d shape, string because = "", params object[] becauseArgs)
{
Execute.Assertion
.BecauseOf(because, becauseArgs)
Expand Down Expand Up @@ -49,7 +49,7 @@ internal AndConstraint<Vector2Assertions> BeInsideOnlyOneOf(IEnumerable<IPolygon
return new AndConstraint<Vector2Assertions>(this);
}

internal AndConstraint<Vector2Assertions> BeOutside(ISpatial2dShape shape, string because = "", params object[] becauseArgs)
internal AndConstraint<Vector2Assertions> BeOutside(IShape2d shape, string because = "", params object[] becauseArgs)
{
Execute.Assertion
.BecauseOf(because, becauseArgs)
Expand Down
15 changes: 9 additions & 6 deletions src/PolyZone/Extensions/VectorExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@ namespace PolyZone.Extensions;

public static class VectorExtensions
{
/// <inheritdoc cref="ISpatial2dShape.Contains"/>
public static bool IsInside(this Vector2 vector2, in ISpatial2dShape shape) => shape.Contains(vector2);
/// <inheritdoc cref="IShape2d.Contains"/>
public static bool IsInside(this Vector2 vector2, in IShape2d shape) => shape.Contains(vector2);

/// <inheritdoc cref="ISpatial2dShape.DistanceFrom"/>
public static float DistanceTo(this Vector2 vector2, in ISpatial2dShape shape) => shape.DistanceFrom(vector2);
/// <inheritdoc cref="IShape2d.DistanceFrom"/>
public static float DistanceTo(this Vector2 vector2, in IShape2d shape) => shape.DistanceFrom(vector2);

/// <inheritdoc cref="ISpatial3dShape.Contains"/>
public static bool IsInside(this Vector3 vector2, in ISpatial3dShape shape) => shape.Contains(vector2);
/// <inheritdoc cref="IShape3d.Contains"/>
public static bool IsInside(this Vector3 vector2, in IShape3d shape) => shape.Contains(vector2);

/// <inheritdoc cref="IShape3d.DistanceFrom"/>
public static float DistanceTo(this Vector3 vector3, in IShape3d shape) => shape.DistanceFrom(vector3);
}
4 changes: 2 additions & 2 deletions src/PolyZone/Shapes/Circle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public Circle(float centerX, float centerY, float radius) : this(new Vector2 { X

}

/// <inheritdoc cref="ISpatial2dShape.Contains"/>
/// <inheritdoc />
public bool Contains(in Vector2 point)
{
// Calculate the distance from the center of the circle to the given point
Expand All @@ -34,7 +34,7 @@ public bool Contains(in Vector2 point)
return distance <= Radius;
}

/// <inheritdoc cref="ISpatial2dShape.DistanceFrom"/>
/// <inheritdoc />
public float DistanceFrom(in Vector2 point)
{
// Calculate the distance from the center of the circle to the given point
Expand Down
3 changes: 2 additions & 1 deletion src/PolyZone/Shapes/Cuboid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public Cuboid(in Vector3[] corners)
Center = new Vector3 { X = (MinX + MaxX) / 2, Y = (MinY + MaxY) / 2, Z = (MinZ + MaxZ) / 2 };
}

/// <inheritdoc cref="ISpatial3dShape.Contains"/>
/// <inheritdoc />
public bool Contains(in Vector3 point)
{
// Check if the point is within the bounds defined by the corners of the cuboid
Expand All @@ -165,6 +165,7 @@ public bool Contains(in Vector3 point)
point.Z >= MinZ && point.Z <= MaxZ;
}

/// <inheritdoc />
public float DistanceFrom(in Vector3 point)
{
if (Contains(point))
Expand Down
2 changes: 1 addition & 1 deletion src/PolyZone/Shapes/Interfaces/ICircle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
/// <summary>
/// A 2d circle
/// </summary>
public interface ICircle : ISpatial2dShape;
public interface ICircle : IShape2d;
2 changes: 1 addition & 1 deletion src/PolyZone/Shapes/Interfaces/ICuboid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
/// <summary>
/// A 3d cuboid shape
/// </summary>
public interface ICuboid : ISpatial3dShape;
public interface ICuboid : IShape3d;
2 changes: 1 addition & 1 deletion src/PolyZone/Shapes/Interfaces/IPolygon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace PolyZone.Shapes.Interfaces;
/// <summary>
/// A 2d polygon
/// </summary>
public interface IPolygon : ISpatial2dShape
public interface IPolygon : IShape2d
{
public IReadOnlyList<Vector2> Points { get; }
}
2 changes: 1 addition & 1 deletion src/PolyZone/Shapes/Interfaces/IRectangle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
/// <summary>
/// A 2d rectangle / box
/// </summary>
public interface IRectangle : ISpatial2dShape;
public interface IRectangle : IShape2d;
20 changes: 20 additions & 0 deletions src/PolyZone/Shapes/Interfaces/IShape2d.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using CitizenFX.Core;

namespace PolyZone.Shapes.Interfaces;

public interface IShape2d
{
/// <summary>
/// Spatially tests a given <see cref="Vector2"/> position to determine if it lies within the shape
/// </summary>
/// <param name="point">A <see cref="Vector2"/> position</param>
/// <returns>True if the <see cref="Vector2"/> position is inside the shape</returns>
bool Contains(in Vector2 point);

/// <summary>
/// Calculates the float-accurate distance from a given <see cref="Vector2"/> position to the shape
/// </summary>
/// <param name="point">A <see cref="Vector2"/> position</param>
/// <returns>The float-accurate distance from a <see cref="Vector2"/> to the shape</returns>
float DistanceFrom(in Vector2 point);
}
20 changes: 20 additions & 0 deletions src/PolyZone/Shapes/Interfaces/IShape3d.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using CitizenFX.Core;

namespace PolyZone.Shapes.Interfaces;

public interface IShape3d
{
/// <summary>
/// Spatially tests a given <see cref="Vector3"/> position to determine if it lies within the shape
/// </summary>
/// <param name="point">A <see cref="Vector3"/> position</param>
/// <returns>True if the <see cref="Vector3"/> position is inside the shape</returns>
bool Contains(in Vector3 point);

/// <summary>
/// Calculates the float-accurate distance from a given <see cref="Vector3"/> position to the shape
/// </summary>
/// <param name="point">A <see cref="Vector3"/> position</param>
/// <returns>The float-accurate distance from a <see cref="Vector3"/> to the shape</returns>
float DistanceFrom(in Vector3 point);
}
23 changes: 0 additions & 23 deletions src/PolyZone/Shapes/Interfaces/ISpatial2dShape.cs

This file was deleted.

13 changes: 0 additions & 13 deletions src/PolyZone/Shapes/Interfaces/ISpatial3dShape.cs

This file was deleted.

2 changes: 1 addition & 1 deletion src/PolyZone/Shapes/Interfaces/ISphere.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
/// <summary>
/// A 3d spherical shape
/// </summary>
public interface ISphere : ISpatial3dShape;
public interface ISphere : IShape3d;
4 changes: 2 additions & 2 deletions src/PolyZone/Shapes/Polygon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class Polygon(IReadOnlyList<Vector2> points) : IPolygon

public IReadOnlyList<Vector2> Points { get; } = points;

/// <inheritdoc cref="ISpatial2dShape.Contains"/>
/// <inheritdoc />
public bool Contains(in Vector2 point)
{
if (_boundingBox is null)
Expand All @@ -35,7 +35,7 @@ public bool Contains(in Vector2 point)
return Contains(point, Points, _boundingBox);
}

/// <inheritdoc cref="ISpatial2dShape.DistanceFrom"/>
/// <inheritdoc />
public float DistanceFrom(in Vector2 point) => DistanceFrom(point, Points);

// https://web.archive.org/web/20210225074947/http://geomalgorithms.com/a03-_inclusion.html
Expand Down
4 changes: 2 additions & 2 deletions src/PolyZone/Shapes/Rectangle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ public class Rectangle(in Vector2 upperLeft, in Vector2 bottomRight) : IRectangl
public Vector2 UpperLeft { get; } = upperLeft;
public Vector2 BottomRight { get; } = bottomRight;

/// <inheritdoc cref="ISpatial2dShape.Contains"/>
/// <inheritdoc />
public bool Contains(in Vector2 point)
{
return point.X >= UpperLeft.X && point.X <= BottomRight.X &&
point.Y >= UpperLeft.Y && point.Y <= BottomRight.Y;
}

/// <inheritdoc cref="ISpatial2dShape.DistanceFrom"/>
/// <inheritdoc />
public float DistanceFrom(in Vector2 point)
{
// Calculate the distance using Euclidean distance formula
Expand Down
8 changes: 7 additions & 1 deletion src/PolyZone/Shapes/Sphere.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class Sphere(in Vector3 center, float radius) : ISphere
public Vector3 Center { get; } = center;
public float Radius { get; } = radius;

/// <inheritdoc cref="ISpatial3dShape.Contains"/>
/// <inheritdoc />
public bool Contains(in Vector3 point)
{
// Calculate the distance from the center of the sphere to the given point
Expand All @@ -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;
}

/// <inheritdoc />
public float DistanceFrom(in Vector3 point)
{
throw new NotImplementedException();
}
}

0 comments on commit 83f8555

Please sign in to comment.