Point inside a convex polygon

1.6k Views Asked by At

I'd like to know when a point is contained in a convex $n-$polygon.I find one question here, but it's different from my problem.

2

There are 2 best solutions below

0
On BEST ANSWER

Let $A_1A_2...A_n$ be our convex polygon, $A_{n+1}\equiv A_1$ and $P$ be a point in the plain of this polygon.

Now, if $$\sum_{k=1}^n\measuredangle A_kPA_{k+1}=360^{\circ}$$ then $P$ placed inside our polygon.

If $$\sum_{k=1}^n\measuredangle A_kPA_{k+1}\neq360^{\circ}$$ then this is not so.

1
On

There's a rather easy way: Let's assume your point is $P_0=(x_0,y_0)$, and your polygon is given as a sequence of points $P_i=(x_i,y_i)$ for $i=1,\ldots,n$, and let's assume $P_{n+1}=P_1$. First, you determine the indices $i$ where $P_i, P_{i+1}$ lie on different sides of the horizontal line through $P_0$, i.e. $(y_i-y_0)(y_0-y_{i+1})\ge0$. There must be two such indices. Then, you determine the abscissas of the intersection of $P_iP_{i+1}$ with $y=y_0$. If $x_0$ lies between them, your point is inside the polygon, in all other cases (meaning that there aren't two intersection points, or $x_0$ isn't between them), it's outside.