134 lines
		
	
	
		
			6.0 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
			
		
		
	
	
			134 lines
		
	
	
		
			6.0 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
using System;
 | 
						|
using System.Diagnostics;
 | 
						|
 | 
						|
namespace Engine.Core;
 | 
						|
 | 
						|
/// <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 <see cref="Circle"/> struct with the specified center and radius.
 | 
						|
/// </remarks>
 | 
						|
[DebuggerDisplay("Center: {Center.ToString(),nq}, Radius: {Radius}")]
 | 
						|
public readonly struct Circle(Vector2D center, float radius) : IEquatable<Circle>
 | 
						|
{
 | 
						|
    /// <summary>
 | 
						|
    /// The center of the <see cref="Circle"/>.
 | 
						|
    /// </summary>
 | 
						|
    public readonly Vector2D Center = center;
 | 
						|
 | 
						|
    /// <summary>
 | 
						|
    /// The radius of the <see cref="Circle"/>.
 | 
						|
    /// </summary>
 | 
						|
    public readonly float Radius = radius;
 | 
						|
 | 
						|
    /// <summary>
 | 
						|
    /// Gets the squared radius of the <see cref="Circle"/>.
 | 
						|
    /// </summary>
 | 
						|
    public readonly float RadiusSquared => Radius * Radius;
 | 
						|
 | 
						|
    /// <summary>
 | 
						|
    /// Gets the diameter of the <see cref="Circle"/>.
 | 
						|
    /// </summary>
 | 
						|
    public readonly float Diameter => 2f * Radius;
 | 
						|
 | 
						|
    /// <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);
 | 
						|
 | 
						|
    public static bool operator ==(Circle left, Circle right) => left.Center == right.Center && left.Radius == right.Radius;
 | 
						|
    public static bool operator !=(Circle left, Circle right) => left.Center != right.Center || left.Radius != right.Radius;
 | 
						|
 | 
						|
    public static implicit operator Circle(Sphere3D sphere) => new(sphere.Center, sphere.Radius);
 | 
						|
 | 
						|
    /// <summary>
 | 
						|
    /// Sets the center of the <see cref="Circle"/>.
 | 
						|
    /// </summary>
 | 
						|
    public static Circle SetCenter(Circle circle, Vector2D center) => new(center, circle.Radius);
 | 
						|
 | 
						|
    /// <summary>
 | 
						|
    /// Sets the radius of the <see cref="Circle"/>.
 | 
						|
    /// </summary>
 | 
						|
    public static Circle SetRadius(Circle circle, float radius) => new(circle.Center, radius);
 | 
						|
 | 
						|
    /// <summary>
 | 
						|
    /// Displaces the <see cref="Circle"/> by the specified <see cref="Vector2D"/>.
 | 
						|
    /// </summary>
 | 
						|
    public static Circle Displace(Circle circle, Vector2D displaceVector) => new(circle.Center + displaceVector, circle.Radius);
 | 
						|
 | 
						|
    /// <summary>
 | 
						|
    /// Projects the <see cref="Circle"/> onto the specified <see cref="Vector2D"/>.
 | 
						|
    /// </summary>
 | 
						|
    public static Projection1D Project(Circle circle, Vector2D projectionVector)
 | 
						|
    {
 | 
						|
        float projectedCenter = circle.Center.Dot(projectionVector);
 | 
						|
        return new(projectedCenter - circle.Radius, projectedCenter + circle.Radius);
 | 
						|
    }
 | 
						|
 | 
						|
    /// <summary>
 | 
						|
    /// Transforms the <see cref="Circle"/> by the specified <see cref="ITransform2D"/>.
 | 
						|
    /// </summary>
 | 
						|
    public static Circle Transform(ITransform2D transform, Circle circle)
 | 
						|
        => new(transform.Transform(circle.Center), circle.Radius * (transform.Scale.Magnitude / Vector2D.One.Magnitude));
 | 
						|
 | 
						|
    /// <summary>
 | 
						|
    /// Checks if two <see cref="Circle"/>s are approximately equal.
 | 
						|
    /// </summary>
 | 
						|
    /// <param name="left">The first <see cref="Circle"/>.</param>
 | 
						|
    /// <param name="right">The second <see cref="Circle"/>.</param>
 | 
						|
    /// <param name="epsilon">The epsilon range.</param>
 | 
						|
    /// <returns><see cref="true"/> if the <see cref="Circle"/>s are approximately equal; otherwise, <see cref="false"/>.</returns>
 | 
						|
    public static bool ApproximatelyEquals(Circle left, Circle right, float epsilon = float.Epsilon)
 | 
						|
        => left.Center.ApproximatelyEquals(right.Center, epsilon) && left.Radius.ApproximatelyEquals(right.Radius, epsilon);
 | 
						|
 | 
						|
    /// <summary>
 | 
						|
    /// Determines whether the specified object is equal to the current <see cref="Circle"/>.
 | 
						|
    /// </summary>
 | 
						|
    /// <param name="obj">The object to compare with the current <see cref="Circle"/>.</param>
 | 
						|
    /// <returns><see cref="true"/> if the specified object is equal to the current <see cref="Circle"/>; otherwise, <see cref="false"/>.</returns>
 | 
						|
    public override bool Equals(object? obj) => obj is Circle circle && this == circle;
 | 
						|
    public bool Equals(Circle other) => this == other;
 | 
						|
 | 
						|
    /// <summary>
 | 
						|
    /// Generates a hash code for the <see cref="Circle"/>.
 | 
						|
    /// </summary>
 | 
						|
    /// <returns>A hash code for the <see cref="Circle"/>.</returns>
 | 
						|
    public override int GetHashCode() => System.HashCode.Combine(Center, Radius);
 | 
						|
 | 
						|
    /// <summary>
 | 
						|
    /// Converts the <see cref="Circle"/> to its string representation.
 | 
						|
    /// </summary>
 | 
						|
    /// <returns>A string representation of the <see cref="Circle"/>.</returns>
 | 
						|
    public override string ToString() => $"{nameof(Circle)}({Center}, {Radius})";
 | 
						|
}
 | 
						|
 | 
						|
/// <summary>
 | 
						|
/// Provides extension methods for the <see cref="Circle"/> struct.
 | 
						|
/// </summary>
 | 
						|
public static class CircleExtensions
 | 
						|
{
 | 
						|
    /// <inheritdoc cref="Circle.SetCenter(Circle, Vector2D)" />
 | 
						|
    public static Circle SetCenter(this Circle circle, Vector2D center) => Circle.SetCenter(circle, center);
 | 
						|
 | 
						|
    /// <inheritdoc cref="Circle.SetRadius(Circle, float)" />
 | 
						|
    public static Circle SetRadius(this Circle circle, float radius) => Circle.SetRadius(circle, radius);
 | 
						|
 | 
						|
    /// <inheritdoc cref="Circle.Displace(Circle, Vector2D)" />
 | 
						|
    public static Circle Displace(this Circle circle, Vector2D displaceVector) => Circle.Displace(circle, displaceVector);
 | 
						|
 | 
						|
    /// <inheritdoc cref="Circle.Project(Circle, Vector2D)" />
 | 
						|
    public static Projection1D ProjectTo(this Circle circle, Vector2D projectionVector) => Circle.Project(circle, projectionVector);
 | 
						|
 | 
						|
    /// <inheritdoc cref="Circle.Transform(ITransform2D, Circle)" />
 | 
						|
    public static Circle Transform(this ITransform2D transform, Circle circle) => Circle.Transform(transform, circle);
 | 
						|
 | 
						|
    /// <inheritdoc cref="Circle.Transform(ITransform2D, Circle)" />
 | 
						|
    public static Circle Transform(this Circle circle, ITransform2D transform) => Circle.Transform(transform, circle);
 | 
						|
 | 
						|
    /// <inheritdoc cref="Circle.ApproximatelyEquals(Circle, Circle, float)" />
 | 
						|
    public static bool ApproximatelyEquals(this Circle left, Circle right, float epsilon = float.Epsilon) => Circle.ApproximatelyEquals(left, right, epsilon);
 | 
						|
}
 |