Is a point inside or outside a polygon?

Hi All,

My first post, any help greatly appreciated.

In need to calculate if a point is inside or outside a polygon.

The application is to calculate which oceanic polygon a ship is in, or conversly, which ships are within a certain polygon.

The vertices of the polygon will be in degrees lat and long, but this should translate fine to a simple 2D polygon on a cartesian diagram. The ship's position will also be in degrees lat/long, again should corresponde fine to cartesian coordinates.

I realise that it's actually a polygon on a sphere i should be calculating, but for this i'm happy to assume the edges are straight.

I've added a diagram to show what I mean. In the first case, the point P is inside the polygon, in the second case it's outside.

How do I determine which?? Thanks everyone,

Luke

http://www.fbs.uk.com/fbs.nsf/Images...%20Problem.gif

not working this implementation (scilab)

Code:

function [ inside ] = inpoly(polygon,xt,yt)

rows = size(polygon);

npoints = rows(1);

disp (npoints);

inside = 0;

xold = polygon(npoints,1);

yold = polygon(npoints,2);

for i = 1:1:npoints

xnew = polygon(i,1);

ynew = polygon(i,2);

if (xnew > xold)

x1=xold;

x2=xnew;

y1=yold;

y2=ynew;

else

x1=xnew;

x2=xold;

y1=ynew;

y2=yold;

end

if ((xnew < xt) == (xt <= xold) & (yt-y1)*(x2-x1) < (y2-y1)*(xt-x1) )

inside=~inside;

end

xold=xnew;

yold=ynew;

end

endfunction