-
Notifications
You must be signed in to change notification settings - Fork 49
/
pointOnLine.js
23 lines (21 loc) · 884 Bytes
/
pointOnLine.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import { lineLength } from "../lines/lineLength";
import { cross } from "../utils/crossProduct";
// See https://math.stackexchange.com/questions/274712/calculate-on-which-side-of-a-straight-line-is-a-given-point-located
function topPointFirst(line){
return line[1][1] > line[0][1] ? line : [line[1], line[0]];
}
export function pointLeftofLine(point, line){
const t = topPointFirst(line);
return cross(point, t[1], t[0]) < 0;
}
export function pointRightofLine(point, line){
const t = topPointFirst(line);
return cross(point, t[1], t[0]) > 0;
}
export function pointOnLine(point, line, epsilon = 0){
const l = lineLength(line);
return pointWithLine(point, line, epsilon) && lineLength([line[0], point]) <= l && lineLength([line[1], point]) <= l;
}
export function pointWithLine(point, line, epsilon = 0){
return Math.abs(cross(point, line[0], line[1])) <= epsilon;
}