Skip to content

Determine whether a point is inside an arbitrary polygon

Notifications You must be signed in to change notification settings

Yuxiang1995/isPointInPoly

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

isPointInPoly

A fast algorithm which can determine whether a point is inside an arbitrary polygon, this method can support convex and non-convex polygon. It costs O(n), n refers to the n-sided shape.

Input:

point: tuple(x, y) poly: [x1, y1, x2, y2, ..., xn, yn]

Output:

True: inside False: outside

Algorithm

  1. When a point is inside the polygon, the sum of the angles between it and the vertices of the polygon is 2π. As shown in Convex(a) and Non-Convex(a).

  2. While the point is outside the polygon, the sum of the angles is 0. As shown in Convex(b) and Non-Convex(b).

  3. It should be noted that due to the order, the included angle here has positive and negative. Cross product is employed to deal with this problem.

For example:

Convex: Image text

Non-Convex: Image text

About

Determine whether a point is inside an arbitrary polygon

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages