lib/geometry.js
function getRayLineSegmentIntersectState([ rayOrigin, rayPoint ], [ lineBegin, lineEnd ]) {
	// Find unit deviations for ray and point
	let dRay = vectorSubtract(rayPoint, rayOrigin);
	let dLine = vectorSubtract(lineEnd, lineBegin);

	if (vectorCross(dRay, dLine) === 0) {
		if (vectorCross(vectorSubtract(lineBegin, rayOrigin), dRay) === 0) {
			// Lines are parallel and collinear
			// Find out if any part of the ray intersects with the line segment
			let t1 = vectorDot(vectorSubtract(lineEnd, rayOrigin), dRay) / vectorDot(dRay, dRay);
			if (t1 >= 0) {
				return OVERLAP;
			} else {
				return UNINTERSECT;
			}
		} else {
			// Lines are parallel and not collinear
			return UNINTERSECT;
		}
	} else {
		// Find solution to parametrized line intersect equations
		let tRay = vectorCross(vectorSubtract(lineBegin, rayOrigin), dLine) / vectorCross(dRay, dLine);
		let tLine = vectorCross(vectorSubtract(rayOrigin, lineBegin), dRay) / vectorCross(dLine, dRay);
		if (tRay > 0 && tLine > 0 && tLine < 1) {
			return INTERSECT;
		} else if (tRay > 0 && (tLine === 0 || tLine === 1)) {
			// Intersects at an endpoint of the line
			return TANGENT;
		} else {
			return UNINTERSECT;
		}
	}
}
function getRayLineSegmentIntersectState([ rayOrigin, rayPoint ], [ lineBegin, lineEnd ]) {
	// Find unit deviations for ray and point
	let dRay = vectorSubtract(rayPoint, rayOrigin);
	let dLine = vectorSubtract(lineEnd, lineBegin);

	if (vectorCross(dRay, dLine) === 0) {
		if (vectorCross(vectorSubtract(lineBegin, rayOrigin), dRay) === 0) {
			// Lines are parallel and collinear
			// Find out if any part of the ray intersects with the line segment
			let t1 = vectorDot(vectorSubtract(lineEnd, rayOrigin), dRay) / vectorDot(dRay, dRay);
			if (t1 >= 0) {
				return OVERLAP;
			} else {
				return UNINTERSECT;
			}
		} else {
			// Lines are parallel and not collinear
			return UNINTERSECT;
		}
	} else {
		// Find solution to parametrized line intersect equations
		let tRay = vectorCross(vectorSubtract(lineBegin, rayOrigin), dLine) / vectorCross(dRay, dLine);
		let tLine = vectorCross(vectorSubtract(rayOrigin, lineBegin), dRay) / vectorCross(dLine, dRay);
		if (tRay > 0 && tLine > 0 && tLine < 1) {
			return INTERSECT;
		} else if (tRay > 0 && (tLine === 0 || tLine === 1)) {
			// Intersects at an endpoint of the line
			return TANGENT;
		} else {
			return UNINTERSECT;
		}
	}
}
function getRayLineSegmentIntersectState ( [ rayOrigin , rayPoint ] , [ lineBegin , lineEnd ] ) { // Find unit deviations for ray and point let dRay = vectorSubtract ( rayPoint , rayOrigin ) ; let dLine = vectorSubtract ( lineEnd , lineBegin ) ; if ( vectorCross ( dRay , dLine ) === 0 ) { if ( vectorCross ( vectorSubtract ( lineBegin , rayOrigin ) , dRay ) === 0 ) { // Lines are parallel and collinear // Find out if any part of the ray intersects with the line segment let t1 = vectorDot ( vectorSubtract ( lineEnd , rayOrigin ) , dRay ) / vectorDot ( dRay , dRay ) ; if ( t1 >= 0 ) { return OVERLAP ; } else { return UNINTERSECT ; } } else { // Lines are parallel and not collinear return UNINTERSECT ; } } else { // Find solution to parametrized line intersect equations let tRay = vectorCross ( vectorSubtract ( lineBegin , rayOrigin ) , dLine ) / vectorCross ( dRay , dLine ) ; let tLine = vectorCross ( vectorSubtract ( rayOrigin , lineBegin ) , dRay ) / vectorCross ( dLine , dRay ) ; if ( tRay > 0 && tLine > 0 && tLine < 1 ) { return INTERSECT ; } else if ( tRay > 0 && ( tLine === 0 || tLine === 1 ) ) { // Intersects at an endpoint of the line return TANGENT ; } else { return UNINTERSECT ; } } }
get the state of ray in relationship to a line segment

@method getRayLineSegmentIntersectState
@param {Number[][]} [ point, point2 ] - `point` is the begin point of the ray. `point2` is a point in ray
@param {Number[][]} [ lineBegin, lineEnd ] - coordinates of begin and end point of line segment
@return {String} - return the state string. Can only be one of: '~intersect', 'intersect', 'overlap' and 'tangent'
get the state of ray in relationship to a line segment