# Lines

Make it thin, make it red

• A line goes to infinity in both directions.
• A line segment a portion of a line and have two endpoints.
• A ray is a directed line segment. It has a start and then extends infinitely in one direction.
• Parametric representation of rays. p(t) = p0 + td. t goes from 0 to 1.

2D Lines

2D Lines Equations

Slope-intercept form

• y = mx + b.
• m is slope and b is y intercept

Standard form

• Ax + Bx = d

Signed distance to a 2D line

``   float Line2D_SignedDistance(Vector2 start, Vector2 end, Vector2 point)``
``   {``
``      Vector2 d = (end - start).normalized;``
``      return Cross2D(point, d) + Cross2D(d, start);``
``   }``

Closest point to a 2D Line

``   Vector2 Line2D_ClosestPoint(Vector2 start, Vector2 end, Vector2 point)``
``   {``
``      Vector2 d = (end - start).normalized;``
``      float length = (end - start).magnitude;``
``      float t = Vector2.Dot(d, point - start);``
``      return start + d * t;``
``   }``

Intersection point of two 2D Lines

``   bool Line2D_Intersect(Vector2 a_start, Vector2 a_end, Vector2 b_start, Vector2 b_end, out Vector2 intersect)``
``   {``
``      intersect = Vector3.zero;``
``      Vector2 a_unit = (a_end - a_start).normalized;``
``      Vector2 b_unit = (b_end - b_start).normalized;``
``      float denominator = Cross2D(a_unit, b_unit);``
``      if (Mathf.Abs(denominator) < Mathf.Epsilon)``
``         return false;``
``      float numerator = Cross2D(b_start - a_start, b_unit);``
``      float t = numerator / denominator;``
``      intersect = a_start + t * a_unit;``
``      return true;``
``   }``

3D Lines

Closest Point on a ray

• Ray: p(t) = porg + td
• d is unit vector and t goes from to l ( length of ray ).
• q is point we wish to check with.
• t for closest point on line is then is then t = (q - porg )
• If t<0 or t > l then closest point is outside ray.

Intersection of two lines in 2D and 3D.

Intersection of ray and plane.

• ray: p(t) = porg + td
• plane: p * n = d
• t = d - porg·n / d·n
• if d·n is 0 then ray is parallel to plane.

Intersection of ray and circle

Intersection of ray and sphere.

Intersection of ray and AABB.

Intersection of ray and triangle.

Distance Point to Line Segment

http://www.randygaul.net/2014/07/23/distance-point-to-line-segment/