-
Notifications
You must be signed in to change notification settings - Fork 0
Core.Data.LineF
The LineF struct is a core data type in the Meatcorps.Engine.Core.Data namespace designed to represent and manipulate 2D line segments using floating-point coordinates. It leverages System.Numerics.Vector2 for efficient mathematical operations.
A LineF consists of two points: a Start and an End. It provides a variety of utility properties and methods for geometric calculations, such as finding the length, interpolating points along the line, and performing intersection tests.
-
Start/End: The beginning and ending coordinates of the line segment asVector2. -
Length/LengthSquared: Returns the Euclidean distance between the start and end points. UseLengthSquaredfor performance-sensitive comparisons to avoid square root calculations. -
DirectionStartNormalized/DirectionEndNormalized: Returns a unit vector (length of 1) pointing from the start to the end, or vice versa. -
RadiusStart/RadiusEnd: Returns the angle of the line in radians. -
Dot: Calculates the dot product between theStartandEndvectors.
Returns a point along the line based on a value between 0.0 (Start) and 1.0 (End).
public Vector2 Lerp(float position)Finds the point on the line segment (or the infinite line) closest to a given external point.
-
clamped = true: Returns a point strictly within the segment boundaries. -
clamped = false: Returns a point on the infinite line defined by the segment.
public Vector2 ClosestPoint(Vector2 point, bool clamped)The struct provides methods to determine if two line segments intersect and can optionally return the exact intersection coordinates.
-
Simple check:
bool IsIntersecting(LineF other) -
Detailed check: Returns the intersection point and the interpolation values (
lerp) for both lines.
- Collision Detection: Checking if a fast-moving projectile (represented as a line) has passed through a wall or player boundary.
- AI Sightlines: Determining if an NPC has a clear line of sight to a target by checking if the line between them intersects any environmental obstacles.
- Pathfinding & Navigation: Finding the closest point on a navigation path to a player's current position to snap them back to a track.
- UI/Graphics: Defining edges for custom 2D shapes or UI borders.
using System.Numerics;
using Meatcorps.Engine.Core.Data;
// Create a line from (0,0) to (10,10)
var line = new LineF(new Vector2(0, 0), new Vector2(10, 10));
float length = line.Length; // Approx 14.14
Vector2 midPoint = line.Lerp(0.5f); // Returns (5, 5)var lineA = new LineF(0, 0, 10, 10);
var lineB = new LineF(0, 10, 10, 0);
if (lineA.IsIntersecting(ref lineB, out Vector2 intersection))
{
Console.WriteLine($"Lines intersect at: {intersection}");
// Output: Lines intersect at: <5, 5>
}var wall = new LineF(0, 5, 20, 5);
var playerPos = new Vector2(10, 8);
// Find where the player would "hit" the wall
Vector2 pointOnWall = wall.ClosestPoint(playerPos, true);
// Returns (10, 5)Note: This struct relies on extension methods (like
.Cross()and.Between01()) usually found inMeatcorps.Engine.Core.Extensionsfor its intersection math.