using System.Diagnostics; using Syntriax.Engine.Core.Abstract; namespace Syntriax.Engine.Core; /// /// Represents a 2D circle. /// /// The center of the circle. /// The radius of the circle. /// /// Initializes a new instance of the struct with the specified center and radius. /// [DebuggerDisplay("Center: {Center.ToString(),nq}, Radius: {Radius}")] public readonly struct Circle(Vector2D center, float radius) { /// /// The center of the circle. /// public readonly Vector2D Center = center; /// /// The radius of the . /// public readonly float Radius = radius; /// /// Gets the squared radius of the . /// public readonly float RadiusSquared => Radius * Radius; /// /// Gets the diameter of the . /// public readonly float Diameter => 2f * Radius; /// /// A predefined unit with a center at the origin and a radius of 1. /// public static readonly Circle UnitCircle = new(Vector2D.Zero, 1f); /// /// Sets the center of the . /// public static Circle SetCenter(Circle circle, Vector2D center) => new(center, circle.Radius); /// /// Sets the radius of the . /// public static Circle SetRadius(Circle circle, float radius) => new(circle.Center, radius); /// /// Displaces the by the specified . /// public static Circle Displace(Circle circle, Vector2D displaceVector) => new(circle.Center + displaceVector, circle.Radius); /// /// Projects the onto the specified . /// public static Projection1D Project(Circle circle, Vector2D projectionVector) { float projectedCenter = circle.Center.Dot(projectionVector); return new(projectedCenter - circle.Radius, projectedCenter + circle.Radius); } /// /// Transforms the by the specified . /// public static Circle Transform(ITransform2D transform, Circle circle) => new(transform.Transform(circle.Center), circle.Radius * (transform.Scale.Magnitude / Vector2D.One.Magnitude)); /// /// Checks if two s are approximately equal. /// /// The first . /// The second . /// The epsilon range. /// if the s are approximately equal; otherwise, . public static bool ApproximatelyEquals(Circle left, Circle right, float epsilon = float.Epsilon) => left.Center.ApproximatelyEquals(right.Center, epsilon) && left.Radius.ApproximatelyEquals(right.Radius, epsilon); } /// /// Provides extension methods for the struct. /// public static class CircleExtensions { /// public static Circle SetCenter(this Circle circle, Vector2D center) => Circle.SetCenter(circle, center); /// public static Circle SetRadius(this Circle circle, float radius) => Circle.SetRadius(circle, radius); /// public static Circle Displace(this Circle circle, Vector2D displaceVector) => Circle.Displace(circle, displaceVector); /// public static Projection1D ToProjection(this Circle circle, Vector2D projectionVector) => Circle.Project(circle, projectionVector); /// public static Circle Transform(this ITransform2D transform, Circle circle) => Circle.Transform(transform, circle); /// public static Circle Transform(this Circle circle, ITransform2D transform) => Circle.Transform(transform, circle); /// public static bool ApproximatelyEquals(this Circle left, Circle right, float epsilon = float.Epsilon) => Circle.ApproximatelyEquals(left, right, epsilon); }