javascript - Three.js - How to determine if a point is on a line? -
how find out if point (x,y,z) on line between pointa , pointb?
what boolean function this:
pointa // random three.vector3 pointb // random three.vector3 pointtocheck // random three.vector3 var isonline = three.pointonline(pointa, pointb, pointtocheck) if (isonline) { console.log('point on line'); }
here image visualization:
cross product of 2 vectors can solve problem.
function ispointonline (pointa, pointb, pointtocheck) { var c = new three.vector3(); c.crossvectors(pointa.clone().sub(pointtocheck), pointb.clone().sub(pointtocheck)); return !c.length(); } three.ispointonlineandbetweenpoints = function (pointa, pointb, pointtocheck) { if (!ispointonline(pointa, pointb, pointtocheck)) { return false; } var dx = pointb.x - pointa.x; var dy = pointb.y - pointa.y; // if line more horizontal vertical: if (math.abs(dx) >= math.abs(dy)) { if (dx > 0) { return pointa.x <= pointtocheck.x && pointtocheck.x <= pointb.x; } else { return pointb.x <= pointtocheck.x && pointtocheck.x <= pointa.x; } } else { if (dy > 0 ) { return pointa.y <= pointtocheck.y && pointtocheck.y <= pointb.y; } else { return pointb.y <= pointtocheck.y && pointtocheck.y <= pointa.y; } } }
a call:
three.ispointonlineandbetweenpoints(new three.vector3(1, 0, 0), new three.vector3(2, 0, 0), new three.vector3(2, 0, 0));
use following function if wanna know whether point on line or not:
ispointonline(new three.vector3(1, 0, 0), new three.vector3(2, 0, 0), new three.vector3(2, 0, 0));
Comments
Post a Comment