Math‎ > ‎Geometry‎ > ‎

### Lines

 Make it thin, make it redA 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 Lines2D Lines EquationsSlope-intercept formy = mx + b. m is slope and b is y interceptStandard formAx + Bx = dSigned 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 LinesClosest Point on a rayRay: p(t) = porg + tdd 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 = d·(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 + tdplane: p * n = dt = d - porg·n / d·nif d·n is 0 then ray is parallel to plane.Intersection of ray and circleIntersection of ray and sphere.Intersection of ray and AABB.Intersection of ray and triangle.Distance Point to Line Segmenthttp://www.randygaul.net/2014/07/23/distance-point-to-line-segment/