refactor: Line

This commit is contained in:
Syntriax 2024-01-23 19:15:09 +03:00
parent 5170dd0aea
commit dcda78b010
1 changed files with 51 additions and 52 deletions

View File

@ -9,30 +9,27 @@ namespace Syntriax.Engine.Physics2D.Primitives;
public record Line(Vector2D From, Vector2D To)
{
public Line Reversed => new(To, From);
public Vector2D Direction => Vector2D.Normalize(To - From);
public float Length => (From - To).Length();
public float LengthSquared => (From - To).LengthSquared();
public Vector2D Direction => From.FromTo(To).Normalize();
public float Length => From.FromTo(To).Length();
public float LengthSquared => From.FromTo(To).LengthSquared();
public LineEquation LineEquation
public static LineEquation GetLineEquation(Line line)
{
get
{
Vector2D slopeVector = To - From;
Vector2D slopeVector = line.From.FromTo(line.To);
float slope = slopeVector.Y / slopeVector.X;
float yOffset = From.Y - (slope * From.X);
float yOffset = line.From.Y - (slope * line.From.X);
return new LineEquation(slope, yOffset);
}
}
public bool Intersects(Vector2D point)
=> Resolve(point.X).ApproximatelyEquals(point);
public static bool Intersects(Line line, Vector2D point)
=> LineEquation.Resolve(GetLineEquation(line), point.X).ApproximatelyEquals(point.Y);
public float GetT(Vector2D point)
public static float GetT(Line line, Vector2D point)
{
float fromX = MathF.Abs(From.X);
float toX = MathF.Abs(To.X);
float fromX = MathF.Abs(line.From.X);
float toX = MathF.Abs(line.To.X);
float pointX = MathF.Abs(point.X);
float min = MathF.Min(fromX, toX);
@ -46,32 +43,32 @@ public record Line(Vector2D From, Vector2D To)
// 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))
if (!Lerp(line, t).ApproximatelyEquals(point))
return 1f - t;
return t;
}
public bool Exist(List<Vector2D> vertices)
public static bool Exist(Line line, List<Vector2D> vertices)
{
for (int i = 0; i < vertices.Count - 1; i++)
{
Vector2D vertexCurrent = vertices[i];
Vector2D vertexNext = vertices[i];
if (From == vertexCurrent && To == vertexNext) return true;
if (From == vertexNext && To == vertexCurrent) return true;
if (line.From == vertexCurrent && line.To == vertexNext) return true;
if (line.From == vertexNext && line.To == vertexCurrent) return true;
}
Vector2D vertexFirst = vertices[0];
Vector2D vertexLast = vertices[^1];
if (From == vertexFirst && To == vertexLast) return true;
if (From == vertexLast && To == vertexFirst) return true;
if (line.From == vertexFirst && line.To == vertexLast) return true;
if (line.From == vertexLast && line.To == vertexFirst) return true;
return false;
}
public float IntersectionParameterT(Line other)
public static float IntersectionParameterT(Line left, Line right)
{
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);
float numerator = (left.From.X - right.From.X) * (right.From.Y - right.To.Y) - (left.From.Y - right.From.Y) * (right.From.X - right.To.X);
float denominator = (left.From.X - left.To.X) * (right.From.Y - right.To.Y) - (left.From.Y - left.To.Y) * (right.From.X - right.To.X);
// Lines are parallel
if (denominator == 0)
@ -80,20 +77,17 @@ public record Line(Vector2D From, Vector2D To)
return numerator / denominator;
}
public Vector2D Lerp(float t)
public static Vector2D Lerp(Line line, float t)
=> new Vector2D(
From.X + (To.X - From.X) * t,
From.Y + (To.Y - From.Y) * t
line.From.X + (line.To.X - line.From.X) * t,
line.From.Y + (line.To.Y - line.From.Y) * t
);
public Vector2D Resolve(float x)
=> new Vector2D(x, LineEquation.Resolve(x));
public Vector2D ClosestPointTo(Vector2D point)
public static Vector2D ClosestPointTo(Line line, Vector2D point)
{
// Convert edge points to vectors
var edgeVector = new Vector2D(To.X - From.X, To.Y - From.Y);
var pointVector = new Vector2D(point.X - From.X, point.Y - From.Y);
var edgeVector = new Vector2D(line.To.X - line.From.X, line.To.Y - line.From.Y);
var pointVector = new Vector2D(point.X - line.From.X, point.Y - line.From.Y);
// 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);
@ -102,45 +96,50 @@ public record Line(Vector2D From, Vector2D To)
t = MathF.Max(0, MathF.Min(1, t));
// Calculate the closest point on the edge
float closestX = From.X + t * edgeVector.X;
float closestY = From.Y + t * edgeVector.Y;
float closestX = line.From.X + t * edgeVector.X;
float closestY = line.From.Y + t * edgeVector.Y;
return new Vector2D((float)closestX, (float)closestY);
}
public Vector2D IntersectionPoint(Line other)
=> Vector2D.Lerp(From, To, IntersectionParameterT(other));
public static Vector2D IntersectionPoint(Line left, Line right)
=> Vector2D.Lerp(left.From, left.To, IntersectionParameterT(left, right));
public bool Intersects(Line other)
public static bool Intersects(Line left, Line right)
{
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);
int o1 = PhysicsMath.Orientation(left.From, left.To, right.From);
int o2 = PhysicsMath.Orientation(left.From, left.To, right.To);
int o3 = PhysicsMath.Orientation(right.From, right.To, left.From);
int o4 = PhysicsMath.Orientation(right.From, right.To, left.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;
if (o1 == 0 && PhysicsMath.OnSegment(left.From, right.From, left.To)) return true;
if (o2 == 0 && PhysicsMath.OnSegment(left.From, right.To, left.To)) return true;
if (o3 == 0 && PhysicsMath.OnSegment(right.From, left.From, right.To)) return true;
if (o4 == 0 && PhysicsMath.OnSegment(right.From, left.To, right.To)) return true;
return false;
}
public bool Intersects(Line other, [NotNullWhen(returnValue: true)] out Vector2D? point)
public static bool Intersects(Line left, Line right, [NotNullWhen(returnValue: true)] out Vector2D? point)
{
point = null;
bool result = Intersects(other);
bool result = Intersects(left, right);
if (result)
point = IntersectionPoint(other);
point = IntersectionPoint(left, right);
return result;
}
public bool ApproximatelyEquals(Line other)
=> From.ApproximatelyEquals(other.From) && To.ApproximatelyEquals(other.To);
public static bool ApproximatelyEquals(Line left, Line right)
=> left.From.ApproximatelyEquals(right.From) && left.To.ApproximatelyEquals(right.To);
}
public static class LineExtensions
{
public static bool ApproximatelyEquals(this Line left, Line right) => Line.ApproximatelyEquals(left, right);
}