Skip to content

Core.Data.LineF

Dennis Steffen edited this page Jan 4, 2026 · 1 revision

LineF Documentation

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.

Overview

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.

Key Properties

  • Start / End: The beginning and ending coordinates of the line segment as Vector2.
  • Length / LengthSquared: Returns the Euclidean distance between the start and end points. Use LengthSquared for 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 the Start and End vectors.

Core Methods

Linear Interpolation (Lerp)

Returns a point along the line based on a value between 0.0 (Start) and 1.0 (End).

public Vector2 Lerp(float position)

Closest Point

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)

Intersection Logic

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.

Use Cases

  1. Collision Detection: Checking if a fast-moving projectile (represented as a line) has passed through a wall or player boundary.
  2. 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.
  3. Pathfinding & Navigation: Finding the closest point on a navigation path to a player's current position to snap them back to a track.
  4. UI/Graphics: Defining edges for custom 2D shapes or UI borders.

Examples

1. Creating a Line and Finding its Length

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)

2. Checking for Intersection

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>
}

3. Finding the Closest Point to a Player

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 in Meatcorps.Engine.Core.Extensions for its intersection math.

Clone this wiki locally