Open Sphinxxxx opened 4 years ago
Consecutive lines/vectors turn left or right
/*
* https://algorithmtutor.com/Computational-Geometry/Determining-if-two-consecutive-segments-turn-left-or-right/
*
* Returns the cross product of vector p1p3 and p1p2
* - If p1p3 is clockwise from p1p2 it returns +ve value
* - If p1p3 is anti-clockwise from p1p2 it returns -ve value
* - If p1 p2 and p3 are collinear it returns 0
*/
function direction(p1, p2, p3) {
function subtract(pa, pb) {
return [pa.x - pb.x, pa.y - pb.y];
}
// Calculates the cross product of vector p1 and p2
// if p1 is clockwise from p2 wrt origin then it returns +ve value
// if p2 is anti-clockwise from p2 wrt origin then it returns -ve value
// if p1 p2 and origin are collinear then it returs 0
function cross_product(pa, pb) {
return pa[0] * pb[1] - pb[0] * pa[1];
}
return cross_product(subtract(p3, p1), subtract(p2, p1));
}