Engine-Pong/Game/Physics2D/Primitives/Line.cs

147 lines
4.9 KiB
C#
Raw Normal View History

2023-12-07 10:55:49 +03:00
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
2024-01-23 12:16:58 +03:00
using Syntriax.Engine.Core;
2023-12-07 10:55:49 +03:00
namespace Syntriax.Engine.Physics2D.Primitives;
2024-01-23 12:16:58 +03:00
public record Line(Vector2D From, Vector2D To)
2023-12-07 10:55:49 +03:00
{
2023-12-18 22:49:26 +03:00
public Line Reversed => new(To, From);
2024-01-23 12:16:58 +03:00
public Vector2D Direction => Vector2D.Normalize(To - From);
2023-12-07 10:55:49 +03:00
public float Length => (From - To).Length();
public float LengthSquared => (From - To).LengthSquared();
public LineEquation LineEquation
{
get
{
2024-01-23 12:16:58 +03:00
Vector2D slopeVector = To - From;
2023-12-07 10:55:49 +03:00
float slope = slopeVector.Y / slopeVector.X;
float yOffset = From.Y - (slope * From.X);
return new LineEquation(slope, yOffset);
}
}
2024-01-23 12:16:58 +03:00
public bool Intersects(Vector2D point)
2023-12-07 10:55:49 +03:00
=> Resolve(point.X).ApproximatelyEquals(point);
2024-01-23 12:16:58 +03:00
public float GetT(Vector2D point)
2023-12-07 10:55:49 +03:00
{
float fromX = MathF.Abs(From.X);
float toX = MathF.Abs(To.X);
float pointX = MathF.Abs(point.X);
float min = MathF.Min(fromX, toX);
float max = MathF.Max(fromX, toX) - min;
pointX -= min;
2023-12-18 22:49:26 +03:00
float t = pointX / max;
// FIXME
// I don't even know, apparently whatever I wrote up there doesn't take into account of the direction of the line
// Which... I can see how, but I am also not sure how I can make it take into account. Or actually I'm for some reason
// too unmotivated to find a solution. Future me, find a better way if possible, please.
if (!Lerp(t).ApproximatelyEquals(point))
return 1f - t;
return t;
2023-12-07 10:55:49 +03:00
}
2024-01-23 12:16:58 +03:00
public bool Exist(List<Vector2D> vertices)
2023-12-07 10:55:49 +03:00
{
for (int i = 0; i < vertices.Count - 1; i++)
{
2024-01-23 12:16:58 +03:00
Vector2D vertexCurrent = vertices[i];
Vector2D vertexNext = vertices[i];
2023-12-07 10:55:49 +03:00
if (From == vertexCurrent && To == vertexNext) return true;
if (From == vertexNext && To == vertexCurrent) return true;
}
2024-01-23 12:16:58 +03:00
Vector2D vertexFirst = vertices[0];
Vector2D vertexLast = vertices[^1];
2023-12-07 10:55:49 +03:00
if (From == vertexFirst && To == vertexLast) return true;
if (From == vertexLast && To == vertexFirst) return true;
return false;
}
public float IntersectionParameterT(Line other)
2023-12-18 22:49:26 +03:00
{
float numerator = (From.X - other.From.X) * (other.From.Y - other.To.Y) - (From.Y - other.From.Y) * (other.From.X - other.To.X);
float denominator = (From.X - To.X) * (other.From.Y - other.To.Y) - (From.Y - To.Y) * (other.From.X - other.To.X);
// Lines are parallel
if (denominator == 0)
return float.NaN;
return numerator / denominator;
}
2023-12-07 10:55:49 +03:00
2024-01-23 12:16:58 +03:00
public Vector2D Lerp(float t)
=> new Vector2D(
2023-12-18 22:49:26 +03:00
From.X + (To.X - From.X) * t,
From.Y + (To.Y - From.Y) * t
);
2023-12-07 10:55:49 +03:00
2024-01-23 12:16:58 +03:00
public Vector2D Resolve(float x)
=> new Vector2D(x, LineEquation.Resolve(x));
2023-12-07 10:55:49 +03:00
2024-01-23 12:16:58 +03:00
public Vector2D ClosestPointTo(Vector2D point)
2023-12-07 10:55:49 +03:00
{
// Convert edge points to vectors
2024-01-23 12:16:58 +03:00
var edgeVector = new Vector2D(To.X - From.X, To.Y - From.Y);
var pointVector = new Vector2D(point.X - From.X, point.Y - From.Y);
2023-12-07 10:55:49 +03:00
// Calculate the projection of pointVector onto edgeVector
float t = (pointVector.X * edgeVector.X + pointVector.Y * edgeVector.Y) / (edgeVector.X * edgeVector.X + edgeVector.Y * edgeVector.Y);
// Clamp t to the range [0, 1] to ensure the closest point is on the edge
2024-01-22 18:46:51 +03:00
t = MathF.Max(0, MathF.Min(1, t));
2023-12-07 10:55:49 +03:00
// Calculate the closest point on the edge
float closestX = From.X + t * edgeVector.X;
float closestY = From.Y + t * edgeVector.Y;
2024-01-23 12:16:58 +03:00
return new Vector2D((float)closestX, (float)closestY);
2023-12-07 10:55:49 +03:00
}
2024-01-23 12:16:58 +03:00
public Vector2D IntersectionPoint(Line other)
=> Vector2D.Lerp(From, To, IntersectionParameterT(other));
2023-12-07 10:55:49 +03:00
public bool Intersects(Line other)
{
int o1 = PhysicsMath.Orientation(From, To, other.From);
int o2 = PhysicsMath.Orientation(From, To, other.To);
int o3 = PhysicsMath.Orientation(other.From, other.To, From);
int o4 = PhysicsMath.Orientation(other.From, other.To, To);
if (o1 != o2 && o3 != o4)
return true;
if (o1 == 0 && PhysicsMath.OnSegment(From, other.From, To)) return true;
if (o2 == 0 && PhysicsMath.OnSegment(From, other.To, To)) return true;
if (o3 == 0 && PhysicsMath.OnSegment(other.From, From, other.To)) return true;
if (o4 == 0 && PhysicsMath.OnSegment(other.From, To, other.To)) return true;
return false;
}
2024-01-23 12:16:58 +03:00
public bool Intersects(Line other, [NotNullWhen(returnValue: true)] out Vector2D? point)
2023-12-07 10:55:49 +03:00
{
point = null;
bool result = Intersects(other);
if (result)
point = IntersectionPoint(other);
return result;
}
2023-12-07 11:14:18 +03:00
public bool ApproximatelyEquals(Line other)
=> From.ApproximatelyEquals(other.From) && To.ApproximatelyEquals(other.To);
2023-12-07 10:55:49 +03:00
}