From f9613a243dcc9dec1095ecc2628b46c6dd3921b3 Mon Sep 17 00:00:00 2001 From: Twinki Date: Sun, 14 Jan 2024 19:33:57 -0500 Subject: [PATCH] Sphere --- src/PolyZone.Tests/Shapes/CircleTests.cs | 2 +- src/PolyZone/Shapes/Interfaces/ISphere.cs | 6 ++++++ src/PolyZone/Shapes/Sphere.cs | 26 +++++++++++++++++++++++ 3 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 src/PolyZone/Shapes/Interfaces/ISphere.cs create mode 100644 src/PolyZone/Shapes/Sphere.cs diff --git a/src/PolyZone.Tests/Shapes/CircleTests.cs b/src/PolyZone.Tests/Shapes/CircleTests.cs index dd4706f..4719be2 100644 --- a/src/PolyZone.Tests/Shapes/CircleTests.cs +++ b/src/PolyZone.Tests/Shapes/CircleTests.cs @@ -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[] { diff --git a/src/PolyZone/Shapes/Interfaces/ISphere.cs b/src/PolyZone/Shapes/Interfaces/ISphere.cs new file mode 100644 index 0000000..de7c705 --- /dev/null +++ b/src/PolyZone/Shapes/Interfaces/ISphere.cs @@ -0,0 +1,6 @@ +namespace PolyZone.Shapes.Interfaces; + +/// +/// A 3d spherical shape +/// +public interface ISphere : ISpatial3dShape; diff --git a/src/PolyZone/Shapes/Sphere.cs b/src/PolyZone/Shapes/Sphere.cs new file mode 100644 index 0000000..8fe6474 --- /dev/null +++ b/src/PolyZone/Shapes/Sphere.cs @@ -0,0 +1,26 @@ +using CitizenFX.Core; +using PolyZone.Shapes.Interfaces; + +namespace PolyZone.Shapes; + +/// +/// A 3d sphere constructed from a center 3d point and a radius +/// +/// Center of the sphere +/// Radius of the sphere +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 + 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; + + } +}