Point in polygon test - spherical coords

This is a discussion on Point in polygon test - spherical coords within the C Programming forums, part of the General Programming Boards category; Hello there,
I have an algorithm that tests a point for being inside a polygon using the rays algorithm.
It ...

Point in polygon test - spherical coords

Hello there,
I have an algorithm that tests a point for being inside a polygon using the rays algorithm.

It works fine and i was able to make a small optimisation but i find it hard to adapt the code to work with spherical coordinates: x,y are degrees and x axis is actually a circle and x belongs to [-180*10^5; 180*10^5) range.

What i did to the algorithm is to substract 360*10^5 degrees if the value is more than 180*10^5, and add 360*10^5 if value is less than -180*10^5.

I tested the code with several polygons but some points fail.

The integer coordinates are with 5 grades of accuracy
and x in [-18000000; 18000000)
y in [-9000000; 9000000]

What i did to the algorithm is to substract 360*10^5 degrees if the value is more than 180*10^5, and add 360*10^5 if value is less than -180*10^5.

Here, I presume:

Code:

if(xi > X_MAX)
xi-=X_RANGE;
else if(xi < X_MIN)
xi+=X_RANGE;

That works reasonably well, unless xi is, say, over X_RANGE outside of the valid range. A fix for this would be to use while loops instead of if statements. However, the modulus operator will take care of the positive side for you. This

"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell