Skip to content

Commit

Permalink
Sphere
Browse files Browse the repository at this point in the history
  • Loading branch information
Twinki14 committed Jan 15, 2024
1 parent 07f1813 commit f9613a2
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/PolyZone.Tests/Shapes/CircleTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class CircleTests
[Fact]
public void CircleA()
{
var circle = new Circle(-7.7f, -3.55f, 5f);
var circle = new Circle(new Vector2 { X = -7.7f, Y = -3.55f }, 5f);

var insidePoints = new[]
{
Expand Down
6 changes: 6 additions & 0 deletions src/PolyZone/Shapes/Interfaces/ISphere.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace PolyZone.Shapes.Interfaces;

/// <summary>
/// A 3d spherical shape
/// </summary>
public interface ISphere : ISpatial3dShape;
26 changes: 26 additions & 0 deletions src/PolyZone/Shapes/Sphere.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using CitizenFX.Core;
using PolyZone.Shapes.Interfaces;

namespace PolyZone.Shapes;

/// <summary>
/// A 3d sphere constructed from a center 3d point and a radius
/// </summary>
/// <param name="center">Center of the sphere</param>
/// <param name="radius">Radius of the sphere</param>
public class Sphere(in Vector3 center, float radius) : ISphere
{
public Vector3 Center { get; } = center;
public float Radius { get; } = radius;

/// <inheritdoc cref="ISpatial3dShape.Contains"/>
public bool Contains(in Vector3 point)
{
// Calculate the distance from the center of the sphere to the given point
var distance = (float) Math.Sqrt(Math.Pow(point.X - Center.X, 2) + Math.Pow(point.Y - Center.Y, 2) + Math.Pow(point.Z - Center.Z, 2));

// Check if the distance is less than or equal to the radius
return distance <= Radius;

}
}

0 comments on commit f9613a2

Please sign in to comment.