Q:

Javascript solution for user being inside or outside a rectangle

I was checking Stackoverflow and many other pages to find a Javascript (or JQuery) solution that tells me if a user (with its current GPS location) is inside or outside a predefined rectangle on a map. There are many solutions for circles and squares but I can't find a solution for rectangles. 
Furthermore the rectangle must not be parallel to f.e. the equator. It can have any possible angle to it. 
Which parameter of the rectangle are required to make the inside/outside calculation? Anyone knows a Javascript solution to solve this problem?

A:

To know if a point is inside a rectangle in a 2D plan you need:

the position of one point of the rectangle: X0, Y0;
the angle the rectangle is making with the horizontal axis: a;
the length of the first side: L1;
the length of the second side: L2.

See this drawing.
A point (X,Y) is inside the 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 a = /*angle of the rect in rad*/;
var l1 = /*length of one side of the rectangle*/
var l2 = /*length of the second size*/
isInRectangle(p.x, p.y, p0.x, p0.y, a, l1, l2); // => return true if inside, false otherwise

