9813

An Efficient Test for a Point to Be in a Convex Polygon

This Demonstration shows an efficient test for checking whether a point is in a convex polygon, indicated by a color change. Note that the test works correctly only for convex polygons.

SNAPSHOTS

  • [Snapshot]
  • [Snapshot]
  • [Snapshot]

DETAILS

This simple and efficient algorithm determines whether a point is located inside a convex polygon or not. Suppose the polygon has vertices.
First the polygon is translated by , so that becomes the new origin.
Next the angles of all pairs of adjacent vectors pointing from the origin to the vertices of the polygon are calculated.
If all the angles are in the range to (when the polygon is defined counterclockwise) or all in to (in the clockwise case), then the point is inside the convex polygon.
The angle calculation can be accomplished as follows:
,
where is the rotation matrix through .
The dot product with reverses the , coordinate values and adjusts one of their signs; it is used so that only arctan and the dot product are needed.
Fortunately we only need the Boolean information of the sign of the angle, so the necessary calculation simplifies to
. This further expands to .
Note: must be treated mod .
Further we can see that ; this is the area spanned by the two vectors, giving us the total signed area of the polygon: .
So the total costs for the test are just two additions (for the initial origin translation), two multiplications, one subtraction, and one "greater than zero" comparison for every vertex; finally an -fold equality comparison if all the signs of the angles are equal. No floating-point calculations are needed in the case of integer coordinates.
Additionally, the signed area of the polygon is calculated to show that both orientations of polygons are handled correctly.
This Demonstration was developed before fully understanding the Point in Triangle Demonstration. The slight difference is the use of the DotProduct (and perhaps a better documentation/explanation and a more compact implementation) in contrast to the Determinant used in Point in Triangle.
Similar algorithms exist which unnecessarily sum up all the mentioned angles (not only their signs), at the relative high costs of arctan and floating-point operations.
    • Share:

Embed Interactive Demonstration New!

Just copy and paste this snippet of JavaScript code into your website or blog to put the live Demonstration on your site. More details »

Files require Wolfram CDF Player or Mathematica.









 
RELATED RESOURCES
Mathematica »
The #1 tool for creating Demonstrations
and anything technical.
Wolfram|Alpha »
Explore anything with the first
computational knowledge engine.
MathWorld »
The web's most extensive
mathematics resource.
Course Assistant Apps »
An app for every course—
right in the palm of your hand.
Wolfram Blog »
Read our views on math,
science, and technology.
Computable Document Format »
The format that makes Demonstrations
(and any information) easy to share and
interact with.
STEM Initiative »
Programs & resources for
educators, schools & students.
Computerbasedmath.org »
Join the initiative for modernizing
math education.
Step-by-step Solutions »
Walk through homework problems one step at a time, with hints to help along the way.
Wolfram Problem Generator »
Unlimited random practice problems and answers with built-in Step-by-step solutions. Practice online or make a printable study sheet.
Wolfram Language »
Knowledge-based programming for everyone.
Powered by Wolfram Mathematica © 2014 Wolfram Demonstrations Project & Contributors  |  Terms of Use  |  Privacy Policy  |  RSS Give us your feedback
Note: To run this Demonstration you need Mathematica 7+ or the free Mathematica Player 7EX
Download or upgrade to Mathematica Player 7EX
I already have Mathematica Player or Mathematica 7+