2024-01-23 18:39:25 +03:00
|
|
|
using Syntriax.Engine.Core;
|
2024-01-24 19:21:53 +03:00
|
|
|
using Syntriax.Engine.Core.Abstract;
|
2024-01-23 18:39:25 +03:00
|
|
|
|
|
|
|
namespace Syntriax.Engine.Physics2D.Primitives;
|
|
|
|
|
2024-01-27 00:51:34 +03:00
|
|
|
[System.Diagnostics.DebuggerDisplay("Center: {Center.ToString(), nq}, Radius: {Radius}")]
|
2024-01-26 23:40:02 +03:00
|
|
|
public readonly struct Circle(Vector2D Center, float Radius)
|
2024-01-23 18:39:25 +03:00
|
|
|
{
|
2024-01-27 20:14:55 +03:00
|
|
|
public static readonly Circle UnitCircle = new(Vector2D.Zero, 1f);
|
|
|
|
|
2024-01-26 23:40:02 +03:00
|
|
|
public readonly Vector2D Center { get; init; } = Center;
|
|
|
|
public readonly float Radius { get; init; } = Radius;
|
|
|
|
|
|
|
|
public readonly float RadiusSquared => Radius * Radius;
|
|
|
|
public readonly float Diameter => 2f * Radius;
|
2024-01-23 18:39:25 +03:00
|
|
|
|
2024-01-24 19:21:53 +03:00
|
|
|
public static Circle SetCenter(Circle circle, Vector2D center) => new(center, circle.Radius);
|
|
|
|
public static Circle SetRadius(Circle circle, float radius) => new(circle.Center, radius);
|
|
|
|
|
|
|
|
public static Circle Displace(Circle circle, Vector2D displaceVector) => new(circle.Center + displaceVector, circle.Radius);
|
|
|
|
|
2024-01-26 12:41:51 +03:00
|
|
|
public static Projection Project(Circle circle, Vector2D projectionVector)
|
|
|
|
{
|
|
|
|
float projectedCenter = circle.Center.Dot(projectionVector);
|
|
|
|
return new(projectedCenter - circle.Radius, projectedCenter + circle.Radius);
|
|
|
|
}
|
|
|
|
|
2024-01-24 19:21:53 +03:00
|
|
|
public static Circle TransformCircle(ITransform transform, Circle circle)
|
|
|
|
=> new(transform.TransformVector2D(circle.Center), circle.Radius * transform.Scale.Magnitude);
|
|
|
|
|
2024-01-23 19:14:23 +03:00
|
|
|
public static bool ApproximatelyEquals(Circle left, Circle right)
|
2024-01-24 18:44:26 +03:00
|
|
|
=> left.Center.ApproximatelyEquals(right.Center) && left.Radius.ApproximatelyEquals(right.Radius);
|
2024-01-23 19:14:23 +03:00
|
|
|
}
|
2024-01-23 18:39:25 +03:00
|
|
|
|
2024-01-23 19:14:23 +03:00
|
|
|
public static class CircleExtensions
|
|
|
|
{
|
2024-01-24 19:21:53 +03:00
|
|
|
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);
|
|
|
|
|
2024-01-26 12:41:51 +03:00
|
|
|
public static Projection ToProjection(this Circle circle, Vector2D projectionVector) => Circle.Project(circle, projectionVector);
|
|
|
|
|
2024-01-25 18:37:51 +03:00
|
|
|
public static Circle TransformCircle(this ITransform transform, Circle circle) => Circle.TransformCircle(transform, circle);
|
2024-01-24 19:21:53 +03:00
|
|
|
|
2024-01-23 19:14:23 +03:00
|
|
|
public static bool ApproximatelyEquals(this Circle left, Circle right) => Circle.ApproximatelyEquals(left, right);
|
2024-01-23 18:39:25 +03:00
|
|
|
}
|