Find Point in polygon PHP

后端 未结 8 865
半阙折子戏
半阙折子戏 2020-11-30 19:34

i have a typical question with the Geometric datatype of mysql, polygon.

I have the polygon data, in the form of an array of latitudes and longitudes, ex:

         


        
8条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-30 20:25

    Here's a possible algorithm.

    1. Define a new coordinate system with your point of interest at the center.
    2. In your new coordinate system, convert all of your polygon vertices into polar coordinates.
    3. Traverse the polygon, keeping track of the net change in angle, ∆θ. Always use the smallest possible value for each change in angle.
    4. If, once you've traversed the polygon, your total ∆θ is 0, then you're outside the polygon. On the other hand, if it's is ±2π, then you're inside.
    5. If, by chance ∆θ>2π or ∆θ<-2π, that means you have a polygon that doubles back on itself.

    Writing the code is left as an exercise. :)

提交回复
热议问题