Point Inside Polygon - Ray Method



Here is the most known method to test if point is inside polygon.

p polygon
n number of vertices in polygon
q point

RightHorizontalRayIntersection(q,u,v) tests if horizontal ray starting
from point q (in right direction) intersects a segment (u,v).

bool IsPointInsidePolygon( P *p,int n,P q ) {
int i,j,c=0;
for (i=0,j=n-1; i<n; j=i++) // Considering all segments (p[i],p[j])
if (RightHorizontalRayIntersection( q,p[i],p[j] )) c++;
return c%2;
}

There are some special cases.
1. Ray r intersects a segment in one of it's vertices.
2. Ray r is collinear to one of p's segments.
3. Point q is lying on one of p's segments.

I need a good method to avoid this special cases and/or other method to
solve this problem.

Thanks in advance,
Narek Saribekyan

.