Is a point inside or outside a polygon?
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,
not working this implementation (scilab)
function [ inside ] = inpoly(polygon,xt,yt)
rows = size(polygon);
npoints = rows(1);
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)
if ((xnew < xt) == (xt <= xold) & (yt-y1)*(x2-x1) < (y2-y1)*(xt-x1) )