I have this matrix
/// as if the create a rectangle
int [][] loc = {
{5, 15},//(x1, y1)
{5, 30}, // (x1, y2)
{20, 15},// (x2, y1)
{20, 30}, // (x2,
A point (x, y) is inside a rectangle (x1,y1) - (x2, y2) if
(x1 <= x <= x2) and (y1 <= y <= y2)
Your code should look like this (this actually is C code, but JavaScript shouldn't be much different):
x1 = loc[0][0];
x2 = loc[2][0];
y1 = loc[0][1];
y2 = loc[2][1];
for (int i = 0; i < num_points; i++) {
if ((x1 <= point[i][0]) && (point[i][0] <= x2) &&
(y1 <= point[i][1]) && (point[i][1] <= y2)) {
// This point is inside the rectangle - insert code here
} else {
// This point is not inside the rectangle - insert code here
}
}
Note that this will only work if (x1 <= x2) and (y1 <= y2), so you might perhaps make sure by using this instead the first four lines above:
x1 = Math.Min(loc[0][0], loc[2][0]);
x2 = Math.Max(loc[0][0], loc[2][0]);
y1 = Math.Min(loc[0][1], loc[2][1]);
y2 = Math.Max(loc[0][1], loc[2][1]);