The simFlight Network Forums

# Determining if the FS Aircraft lies Inside a Specific Poly

## Recommended Posts

I need to tell if the aircraft is inside a user definable polygon defined by a list of vertices (lat/lon values). The poly could be almost any shape. I guess this must have been solved already but I am scratching my head for a 'foolproof' method.

Any pointers would be much appreciated

##### Share on other sites

Taking each pair of polygon points in turn, calculate the signed angle between the vectors from the aircraft to the points and accumulate. If the absolute value of the result is greater than 180 degrees, the aircraft is inside the polygon, if it is less, it is outside.

George

##### Share on other sites

Hi George

Thank you very much :D :D Could you just clarify whay you mean by a signed angle - I presume it has to be positive. Also a quick sketch would seem to show that if the aircraft is inside the poly the sum of the angles should be 360, but that may just show I have mi-understood :)

EDIT. Hmm the 360 would not seem right if the poly is an L shape

##### Share on other sites

Hmm the 360 would not seem right if the poly is an L shape

Yes, I can assure you that it does work for any shaped polygon.

The signed angle means that all angles should be measured either clockwise or counterclockwise. Hint, use a vector cross-product.

George

##### Share on other sites

OK Thanks George I understand that now

##### Share on other sites

Here is an example. Taking pairs 1-2, 2-3 etc, the blue angles are positive and the red angles are negative. The sum should be zero, however with rounding errors I always use abs(sum) < 180.

George

##### Share on other sites

Thanks George - I understand how it works. I am not reading up on vector maths to code up the angle calculations. I was going to work out the bearing of each of the pairs from the aircraft and subtract the second from the first but that gets complicated around 360/0. Looks like using vectors and cross-products to calculate the angle is better as you suggest. I just digging up some information on that now - it is a long time since I did this kind of math :D

EDIT - OK I think I have coded it now I need to make a test rig.

##### Share on other sites

Hi folks,

I too have this problem but wasnt going to delve into it until I hit that part of my program. However, I have been reading up on vector cross products and from the advice I have been able to find it says that a VCP only applies to 3D vectors. Is this correct? Have I missed something?

Taking that further, how would this apply to a 3D problem. That is, determining if an aircraft is inside a block of airspace?

Simon

##### Share on other sites

All vectors are 3D. For 2D, set the third component to zero which is quite adequate for small distances. If the block of airspace is delimited by two heights, you simply check as above then check max/min height. Unless the heights are wildly different, then you need to use spherical projection.

George

##### Share on other sites

Ah of course. Yes I understand. Many thanks George.

Simon

##### Share on other sites

• 4 weeks later...

I had a similar problem, and the solution I use can be found here http://mathforum.org/library/drmath/view/54386.html.

I use the matrix method down the bottom of the page, it works great for simple polygons.

## Create an account

Register a new account