gps - Javascript solution for user being inside or outside a rectangle -
i checking stackoverflow , many other pages find javascript (or jquery) solution tells me if user (with current gps location) inside or outside predefined rectangle on map. there many solutions circles , squares can't find solution rectangles. furthermore rectangle must not parallel f.e. equator. can have possible angle it. parameter of rectangle required make inside/outside calculation? knows javascript solution solve problem?
to know if point inside rectangle in 2d plan need:
- the position of 1 point of rectangle: x0, y0;
- the angle rectangle making horizontal axis: a;
- the length of first side: l1;
- the length of second side: l2.
a point (x,y) inside rectangle when:
- 0 >= (x-x0)*cos(a) - (y-y0)*sin(a) >= l1
- 0 >= (x-x0)*sin(a) + (y-y0)*cos(a) >= l2
or in javascript:
function isinrectangle(x, y, x0, y0, a, l1, l2) { var v1 = (x-x0)*math.cos(a) - (y-y0)*math.sin(a); var v2 = (x-x0)*math.sin(a) + (y-y0)*math.cos(a); return v1 >= 0 && v1 <= l1 && v2 >= 0 && v2 <= l2; }
to use it:
var p = {x: /*user longitude*/, y: /*user latitude*/}; var p0 = {x: /*rect longitude*/, y: /*rect latitude*/}; var = /*angle of rect in rad*/; var l1 = /*length of 1 side of rectangle*/ var l2 = /*length of second size*/ isinrectangle(p.x, p.y, p0.x, p0.y, a, l1, l2); // => return true if inside, false otherwise
Comments
Post a Comment