Assume a point P
and a convex polygon with n
vertices V_1
to V_n
(n > 2).
Sort the vertices of the polygon by their angle relative to a selected vertex, so that they are in clock-wise or counter-clockwise order. The edges of the polygon are then V_1 -> V_2, V_2 -> V_3, ..., V_(n-1) -> V_n, V_n -> V_1
.
Now, for every edge, check the value of the cross product (V_(i+1) - V_i) x (P - V_i)
. Now P
is inside the polygon iif all the values are >= 0 or all the values are <= 0.
There's a good tutorial on TopCoder for the more general problem where the polygon doesn't have to be convex. What they do is send a ray from the test point and check how many edges it intersects.
NOTE: The cross product used here is defined as (u1, u2) x (v1, v2) := u1*v2 - u2*v1
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…