canvas - How find if a point is in a wedge of a other point javascript -
i have canvas , draw 2 points. have draw 2 straight lines each point. must check whether point u sees point v , vice versa.
to know, necessary see if point v in wedge of point u , vice versa:
-here v wedge of u, v sees u u don't see v
if 2 points in wedge of each 1 segment drawn between 2 points.
how can kind of wedge in canvas javascript?
cross product of 2 vectors.
using cross product of line (x1,y1, x2,y2) ax vector (x2-x1, y2-y1) , vector line start point x,y (x - x1, y - y1) give number negative if point left of line, 0 if point along line, , positive if point right of line.
thus point in view of v has to right of v's left arm , left of v's right arm.
a v described follows
const v = { lx : ?, // point on left v ly : ?, x : ?, // point of v y : ?, rx : ?, ry : ?, }
the following function return true if v1
can see v2
v1
, v2
objects described above.
function cansee(v1,v2){ const lx = v1.lx - v1.x; // vector center left line end const ly = v1.ly - v1.y; const cx = v2.x - v1.x; // vector center v2's center const cy = v2.y - v1.y; const rx = v1.rx - v1.x; // vector center right line end const ry = v1.ry - v1.y; // cross product of left , right arms other v's center const lc = lx * cy - ly * cx; const rc = rx * cy - ry * cx; return (lc > 0 && rc < 0); // return true if v2 center right of // left arm , left of arm. // else return false; }
using function can find out if both can see each other follows.
if(cansee(v1, v2) && cansee(v2, v1)){ } // both can see each other else { } //one or none can see other
it important points on correct sides. left , right defined when standing @ line start , looking towards line end
Comments
Post a Comment