feat: added Ray2D primitive
This commit is contained in:
		
							
								
								
									
										66
									
								
								Engine.Core/Primitives/Ray2D.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										66
									
								
								Engine.Core/Primitives/Ray2D.cs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,66 @@
 | 
			
		||||
namespace Syntriax.Engine.Core;
 | 
			
		||||
 | 
			
		||||
public readonly struct Ray2D(Vector2D Origin, Vector2D Direction)
 | 
			
		||||
{
 | 
			
		||||
    /// <summary>
 | 
			
		||||
    /// The starting point of the <see cref="Ray2D"/>.
 | 
			
		||||
    /// </summary>
 | 
			
		||||
    public readonly Vector2D Origin = Origin;
 | 
			
		||||
 | 
			
		||||
    /// <summary>
 | 
			
		||||
    /// The direction in which the <see cref="Ray2D"/> points. Should be a normalized vector.
 | 
			
		||||
    /// </summary>
 | 
			
		||||
    public readonly Vector2D Direction = Direction;
 | 
			
		||||
 | 
			
		||||
    /// <summary>
 | 
			
		||||
    /// Gets a <see cref="Ray2D"/> with the same origin but with the direction reversed.
 | 
			
		||||
    /// </summary>
 | 
			
		||||
    public readonly Ray2D Reversed => new(Origin, -Direction);
 | 
			
		||||
 | 
			
		||||
    public static implicit operator Ray2D(Line2D line) => new(line.From, line.From.FromTo(line.To).Normalized);
 | 
			
		||||
 | 
			
		||||
    /// <summary>
 | 
			
		||||
    /// Constructs a <see cref="Line2D"/> from a <see cref="Ray2D"/>, extending from its origin in the <see cref="Ray2D"/>'s direction for a given distance.
 | 
			
		||||
    /// </summary>
 | 
			
		||||
    /// <param name="ray">The source <see cref="Ray2D"/>.</param>
 | 
			
		||||
    /// <param name="distance">The length of the line segment to create from the <see cref="Ray2D"/>.</param>
 | 
			
		||||
    /// <returns>A <see cref="Line2D"/> representing the segment of the <see cref="Ray2D"/>.</returns>
 | 
			
		||||
    public static Line2D GetLine(Ray2D ray, float distance)
 | 
			
		||||
        => new(ray.Origin, ray.Origin + ray.Direction * distance);
 | 
			
		||||
 | 
			
		||||
    /// <summary>
 | 
			
		||||
    /// Evaluates the point on the <see cref="Ray2D"/> at a specified distance from its origin.
 | 
			
		||||
    /// </summary>
 | 
			
		||||
    /// <param name="ray">The <see cref="Ray2D"/> to evaluate.</param>
 | 
			
		||||
    /// <param name="distanceFromOrigin">The distance from the origin along the <see cref="Ray2D"/>'s direction.</param>
 | 
			
		||||
    /// <returns>A <see cref="Vector2D"/> representing the point at the given distance on the <see cref="Ray2D"/>.</returns>
 | 
			
		||||
    public static Vector2D Evaluate(Ray2D ray, float distanceFromOrigin)
 | 
			
		||||
        => ray.Origin + ray.Direction * distanceFromOrigin;
 | 
			
		||||
 | 
			
		||||
    /// <summary>
 | 
			
		||||
    /// Calculates the closest point on the <see cref="Ray2D"/> to the specified point.
 | 
			
		||||
    /// </summary>
 | 
			
		||||
    public static Vector2D ClosestPointTo(Ray2D ray, Vector2D point)
 | 
			
		||||
    {
 | 
			
		||||
        Vector2D originToPoint = ray.Origin.FromTo(point);
 | 
			
		||||
 | 
			
		||||
        float dot = ray.Direction.Dot(originToPoint);
 | 
			
		||||
 | 
			
		||||
        return ray.Origin + ray.Direction * dot;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/// <summary>
 | 
			
		||||
/// Provides extension methods for the <see cref="Ray2D"/> struct.
 | 
			
		||||
/// </summary>
 | 
			
		||||
public static class Ray2DExtensions
 | 
			
		||||
{
 | 
			
		||||
    /// <inheritdoc cref="Ray2D.GetLine(Ray2D, float) />
 | 
			
		||||
    public static Line2D ToLine(this Ray2D ray, float distance) => Ray2D.GetLine(ray, distance);
 | 
			
		||||
 | 
			
		||||
    /// <inheritdoc cref="Ray2D.Evaluate(Ray2D, float) />
 | 
			
		||||
    public static Vector2D Evaluate(this Ray2D ray, float distanceFromOrigin) => Ray2D.Evaluate(ray, distanceFromOrigin);
 | 
			
		||||
 | 
			
		||||
    /// <inheritdoc cref="Ray2D.ClosestPointTo(Ray2D, Vector2D) />
 | 
			
		||||
    public static Vector2D ClosestPointTo(this Ray2D ray, Vector2D point) => Ray2D.ClosestPointTo(ray, point);
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user