using System.Diagnostics; using Syntriax.Engine.Core; using Syntriax.Engine.Core.Abstract; namespace Syntriax.Engine.Physics2D.Primitives; /// /// Represents a 2D circle. /// /// The center of the circle. /// The radius of the circle. /// /// Initializes a new instance of the Circle 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 Projection 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 TransformCircle(ITransform transform, Circle circle) => new(transform.TransformVector2D(circle.Center), circle.Radius * (transform.Scale.Magnitude / Vector2D.One.Magnitude)); /// /// Checks if two s are approximately equal. /// public static bool ApproximatelyEquals(Circle left, Circle right) => left.Center.ApproximatelyEquals(right.Center) && left.Radius.ApproximatelyEquals(right.Radius); } /// /// Provides extension methods for the struct. /// public static class CircleExtensions { /// /// Sets the center of the . /// public static Circle SetCenter(this Circle circle, Vector2D center) => Circle.SetCenter(circle, center); /// /// Sets the radius of the . /// public static Circle SetRadius(this Circle circle, float radius) => Circle.SetRadius(circle, radius); /// /// Moves the by the specified . /// public static Circle Displace(this Circle circle, Vector2D displaceVector) => Circle.Displace(circle, displaceVector); /// /// Projects the onto the specified . /// public static Projection ToProjection(this Circle circle, Vector2D projectionVector) => Circle.Project(circle, projectionVector); /// /// Transforms the by the specified . /// public static Circle TransformCircle(this ITransform transform, Circle circle) => Circle.TransformCircle(transform, circle); /// /// Checks if two s are approximately equal. /// public static bool ApproximatelyEquals(this Circle left, Circle right) => Circle.ApproximatelyEquals(left, right); }