Syntriax.Engine/Engine.Physics2D/Primitives/Circle.cs

116 lines
4.3 KiB
C#
Raw Normal View History

2024-02-01 12:32:58 +03:00
using System.Diagnostics;
using Syntriax.Engine.Core;
2024-01-24 19:21:53 +03:00
using Syntriax.Engine.Core.Abstract;
namespace Syntriax.Engine.Physics2D.Primitives;
2024-02-01 12:32:58 +03:00
/// <summary>
/// Represents a 2D circle.
/// </summary>
/// <param name="center">The center of the circle.</param>
/// <param name="radius">The radius of the circle.</param>
/// <remarks>
/// Initializes a new instance of the Circle struct with the specified center and radius.
/// </remarks>
[DebuggerDisplay("Center: {Center.ToString(),nq}, Radius: {Radius}")]
public readonly struct Circle(Vector2D center, float radius)
{
2024-02-01 12:32:58 +03:00
/// <summary>
/// The center of the circle.
/// </summary>
public readonly Vector2D Center = center;
2024-01-27 20:14:55 +03:00
2024-02-01 12:32:58 +03:00
/// <summary>
/// The radius of the <see cref="Circle"/>.
/// </summary>
public readonly float Radius = radius;
2024-02-01 12:32:58 +03:00
/// <summary>
/// Gets the squared radius of the <see cref="Circle"/>.
/// </summary>
public readonly float RadiusSquared => Radius * Radius;
2024-02-01 12:32:58 +03:00
/// <summary>
/// Gets the diameter of the <see cref="Circle"/>.
/// </summary>
public readonly float Diameter => 2f * Radius;
2024-02-01 12:32:58 +03:00
/// <summary>
/// A predefined unit <see cref="Circle"/> with a center at the origin and a radius of 1.
/// </summary>
public static readonly Circle UnitCircle = new(Vector2D.Zero, 1f);
/// <summary>
/// Sets the center of the <see cref="Circle"/>.
/// </summary>
2024-01-24 19:21:53 +03:00
public static Circle SetCenter(Circle circle, Vector2D center) => new(center, circle.Radius);
2024-02-01 12:32:58 +03:00
/// <summary>
/// Sets the radius of the <see cref="Circle"/>.
/// </summary>
2024-01-24 19:21:53 +03:00
public static Circle SetRadius(Circle circle, float radius) => new(circle.Center, radius);
2024-02-01 12:32:58 +03:00
/// <summary>
/// Displaces the <see cref="Circle"/> by the specified <see cref="Vector2D"/>.
/// </summary>
2024-01-24 19:21:53 +03:00
public static Circle Displace(Circle circle, Vector2D displaceVector) => new(circle.Center + displaceVector, circle.Radius);
2024-02-01 12:32:58 +03:00
/// <summary>
/// Projects the <see cref="Circle"/> onto the specified <see cref="Vector2D"/>.
/// </summary>
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-02-01 12:32:58 +03:00
/// <summary>
/// Transforms the <see cref="Circle"/> by the specified <see cref="ITransform"/>.
/// </summary>
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 / Vector2D.One.Magnitude));
2024-01-24 19:21:53 +03:00
2024-02-01 12:32:58 +03:00
/// <summary>
/// Checks if two <see cref="Circle"/>s are approximately equal.
/// </summary>
2024-01-23 19:14:23 +03:00
public static bool ApproximatelyEquals(Circle left, Circle right)
=> left.Center.ApproximatelyEquals(right.Center) && left.Radius.ApproximatelyEquals(right.Radius);
2024-01-23 19:14:23 +03:00
}
2024-02-01 12:32:58 +03:00
/// <summary>
/// Provides extension methods for the <see cref="Circle"/> struct.
/// </summary>
2024-01-23 19:14:23 +03:00
public static class CircleExtensions
{
2024-02-01 12:32:58 +03:00
/// <summary>
/// Sets the center of the <see cref="Circle"/>.
/// </summary>
2024-01-24 19:21:53 +03:00
public static Circle SetCenter(this Circle circle, Vector2D center) => Circle.SetCenter(circle, center);
2024-02-01 12:32:58 +03:00
/// <summary>
/// Sets the radius of the <see cref="Circle"/>.
/// </summary>
2024-01-24 19:21:53 +03:00
public static Circle SetRadius(this Circle circle, float radius) => Circle.SetRadius(circle, radius);
2024-02-01 12:32:58 +03:00
/// <summary>
/// Moves the <see cref="Circle"/> by the specified <see cref="Vector2D"/>.
/// </summary>
2024-01-24 19:21:53 +03:00
public static Circle Displace(this Circle circle, Vector2D displaceVector) => Circle.Displace(circle, displaceVector);
2024-02-01 12:32:58 +03:00
/// <summary>
/// Projects the <see cref="Circle"/> onto the specified <see cref="Vector2D"/>.
/// </summary>
2024-01-26 12:41:51 +03:00
public static Projection ToProjection(this Circle circle, Vector2D projectionVector) => Circle.Project(circle, projectionVector);
2024-02-01 12:32:58 +03:00
/// <summary>
/// Transforms the <see cref="Circle"/> by the specified <see cref="ITransform"/>.
/// </summary>
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-02-01 12:32:58 +03:00
/// <summary>
/// Checks if two <see cref="Circle"/>s are approximately equal.
/// </summary>
2024-01-23 19:14:23 +03:00
public static bool ApproximatelyEquals(this Circle left, Circle right) => Circle.ApproximatelyEquals(left, right);
}