All files / core/domain/utils/geometry isPointOnSegment.js

0% Statements 0/4
0% Branches 0/5
0% Functions 0/1
0% Lines 0/4

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30                                                           
import getDistance from "./getDistance";
 
/**
 * Check if a point is on a line segment.
 *
 * @param  {number} x       The X coordinate of the point to check.
 * @param  {number} y       The Y coordinate of the point to check.
 * @param  {number} x1      The X coordinate of the line start point.
 * @param  {number} y1      The Y coordinate of the line start point.
 * @param  {number} x2      The X coordinate of the line end point.
 * @param  {number} y2      The Y coordinate of the line end point.
 * @param  {number} epsilon The precision (consider the line thickness).
 * @return {boolean}        True if point is "close to" the line
 *                          segment, false otherwise.
 */
export default function isPointOnSegment(x, y, x1, y1, x2, y2, epsilon) {
  // http://stackoverflow.com/a/328122
  const crossProduct = Math.abs((y - y1) * (x2 - x1) - (x - x1) * (y2 - y1));
  const d = getDistance(x1, y1, x2, y2);
  const nCrossProduct = crossProduct / d; // normalized cross product
 
  return (
    nCrossProduct < epsilon &&
    Math.min(x1, x2) <= x &&
    x <= Math.max(x1, x2) &&
    Math.min(y1, y2) <= y &&
    y <= Math.max(y1, y2)
  );
}