index.js
function rayVsLineSegment(ray, segment) {
  var result = lineIntersect.checkIntersection(
    ray.start.x, ray.start.y, ray.end.x, ray.end.y,
    segment.start.x, segment.start.y, segment.end.x, segment.end.y
  );

  // definitely no intersection
  if (result.type == 'none' || result.type == 'parallel') return null;

  // single intersection point
  if (result.type == 'intersecting') return result.point;

  // colinear, so now check if ray/segment overlap
  if (segmentContainsPoint(segment, ray.start)) {
    return ray.start;
  } else {
    // return segment endpoint that is
    //   - within ray
    //   - closest to ray start
    var rayStart = new Vec2(ray.start);
    var endpointsInRay = segmentEndpointsInRay(ray, segment);
    return rayStart.nearest(endpointsInRay);
  }
}
function rayVsLineSegment(ray, segment) {
  var result = lineIntersect.checkIntersection(
    ray.start.x, ray.start.y, ray.end.x, ray.end.y,
    segment.start.x, segment.start.y, segment.end.x, segment.end.y
  );

  // definitely no intersection
  if (result.type == 'none' || result.type == 'parallel') return null;

  // single intersection point
  if (result.type == 'intersecting') return result.point;

  // colinear, so now check if ray/segment overlap
  if (segmentContainsPoint(segment, ray.start)) {
    return ray.start;
  } else {
    // return segment endpoint that is
    //   - within ray
    //   - closest to ray start
    var rayStart = new Vec2(ray.start);
    var endpointsInRay = segmentEndpointsInRay(ray, segment);
    return rayStart.nearest(endpointsInRay);
  }
}
function rayVsLineSegment ( ray , segment ) { var result = lineIntersect . checkIntersection ( ray . start . x , ray . start . y , ray . end . x , ray . end . y , segment . start . x , segment . start . y , segment . end . x , segment . end . y ) ; // definitely no intersection if ( result . type == 'none' || result . type == 'parallel' ) return null ; // single intersection point if ( result . type == 'intersecting' ) return result . point ; // colinear, so now check if ray/segment overlap if ( segmentContainsPoint ( segment , ray . start ) ) { return ray . start ; } else { // return segment endpoint that is //   - within ray //   - closest to ray start var rayStart = new Vec2 ( ray . start ) ; var endpointsInRay = segmentEndpointsInRay ( ray , segment ) ; return rayStart . nearest ( endpointsInRay ) ; } }
Finds where a ray hits a line segment, if at all.

@param {object} ray - Object that looks like
{
start: {x: number, y: number},
end: {x: number, y: number}
}

@param {object} segment - Object that looks like
{
start: {x: number, y: number},
end: {x: number, y: number}
}

@return {object} point (x/y) where ray hits segment or null if it doesn't hit
Finds where a ray hits a line segment if at all .