109 lines
		
	
	
		
			5.3 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
			
		
		
	
	
			109 lines
		
	
	
		
			5.3 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
using System;
 | 
						|
 | 
						|
namespace Engine.Core;
 | 
						|
 | 
						|
/// <summary>
 | 
						|
/// Represents an infinite ray in 2D space.
 | 
						|
/// </summary>
 | 
						|
/// <param name="Origin">The <see cref="Vector2D"/> in 2D space where the ray starts from.</param>
 | 
						|
/// <param name="Direction">Normalized <see cref="Vector2D"/> indicating the ray's is direction.</param> 
 | 
						|
public readonly struct Ray2D(Vector2D Origin, Vector2D Direction) : IEquatable<Ray2D>
 | 
						|
{
 | 
						|
    /// <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 bool operator ==(Ray2D left, Ray2D right) => left.Origin == right.Origin && left.Direction == right.Direction;
 | 
						|
    public static bool operator !=(Ray2D left, Ray2D right) => left.Origin != right.Origin || left.Direction != right.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>
 | 
						|
    /// Checks if two <see cref="Ray2D"/>s are approximately equal within a specified epsilon range.
 | 
						|
    /// </summary>
 | 
						|
    /// <param name="left">The first <see cref="Ray2D"/>.</param>
 | 
						|
    /// <param name="right">The second <see cref="Ray2D"/>.</param>
 | 
						|
    /// <param name="epsilon">The epsilon range.</param>
 | 
						|
    /// <returns><see cref="true"/> if the <see cref="Ray2D"/>s are approximately equal; otherwise, <see cref="false"/>.</returns>
 | 
						|
    public static bool ApproximatelyEquals(Ray2D left, Ray2D right, float epsilon = float.Epsilon)
 | 
						|
        => left.Origin.ApproximatelyEquals(right.Origin, epsilon) && left.Direction.ApproximatelyEquals(right.Direction, epsilon);
 | 
						|
 | 
						|
    /// <summary>
 | 
						|
    /// Determines whether the specified object is equal to the current <see cref="Ray2D"/>.
 | 
						|
    /// </summary>
 | 
						|
    /// <param name="obj">The object to compare with the current <see cref="Ray2D"/>.</param>
 | 
						|
    /// <returns><see cref="true"/> if the specified object is equal to the current <see cref="Ray2D"/>; otherwise, <see cref="false"/>.</returns>
 | 
						|
    public override bool Equals(object? obj) => obj is Ray2D ray2D && this == ray2D;
 | 
						|
    public bool Equals(Ray2D other) => this == other;
 | 
						|
 | 
						|
    /// <summary>
 | 
						|
    /// Generates a hash code for the <see cref="Ray2D"/>.
 | 
						|
    /// </summary>
 | 
						|
    /// <returns>A hash code for the <see cref="Ray2D"/>.</returns>
 | 
						|
    public override int GetHashCode() => System.HashCode.Combine(Origin, Direction);
 | 
						|
 | 
						|
    /// <summary>
 | 
						|
    /// Converts the <see cref="Ray2D"/> to its string representation.
 | 
						|
    /// </summary>
 | 
						|
    /// <returns>A string representation of the <see cref="Ray2D"/>.</returns>
 | 
						|
    public override string ToString() => $"{nameof(Ray2D)}({Origin}, {Direction})";
 | 
						|
}
 | 
						|
 | 
						|
/// <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);
 | 
						|
 | 
						|
    /// <inheritdoc cref="Ray2D.ApproximatelyEquals(Ray2D, Ray2D, float)" />
 | 
						|
    public static bool ApproximatelyEquals(this Ray2D left, Ray2D right, float epsilon = float.Epsilon) => Ray2D.ApproximatelyEquals(left, right, epsilon);
 | 
						|
}
 |