I have 4 coordinates of area: x1,y1 ... etc. And have one more position x0,y0.
How to check if my coordinate in selected area?
I have 4 coordinates of area: x1,y1 ... etc. And have one more position x0,y0.
How to check if my coordinate in selected area?
I will explain you how to check that (x0,y0) lies "below" the line through (x1,y1) and (x2,y2). Essentially, you want that the vector (x0-x1,y0-y1) points "to the right" of (x2-x1, y2-y1). This is equivalent to saying that the matrix
x0-x1 y0-y1
x2-x1 y2-y1
has a negative determinant. So your condition becomes
(x0-x1)(y2-y1) < (y0-y1)(x2-x1).
You get such a condition for any line bounding the area.
Let
A = {x1, y1}
B = {x2, y2}
C = {x3, x3}
D = {x4, x4}
First, make sure that points form a polynomial and are not in straight line. This can be done by comparing the direction(AB) != direction(AC) != direction(AD)
where AB, AC, AD are directional vectors.
To make sure that certain point P = {x0, y0}
lies within the polygon ABCD
, it is sufficient to check that sign(AC X AP) == sign(CD X CP) == sign(DB X DP) == sign(BA X BP)
.
AC: Directional vector A -> C
AP: Directional vector A -> P
.
. so on!
.
X: Cross product
sign: sign of cross product (+ or -)
It is only required to compare the sign of direction not the magnitude.