# calcualte if a Point is over a Polygon area

can somebody provide an formula for calculation  if a point ( record of 2 real) is over a polygon (defined by an array of ral values)

below is the code I using now, failure is a divison by 0

``````function IsPointInPolygon(p : FPoint; poly : TFPolygon) : Boolean;
var i,j : integer;
Begin
result := false;
j := High(poly);
For i := Low(poly) to High(poly) do begin
if (( ((poly[i].y <= p.y) and (p.y < poly[j].y)) or ((poly[j].y <= p.y) and (p.y < poly[i].y)) ) and
(p.x < ((poly[j].x - poly[i].x) * (p.y - poly[i].y) / (poly[j].y - poly[i].y) + poly[i].x))) then result := not result;
j := i
end;
end;
``````
LVL 8
###### Who is Participating?

Oracle dbaCommented:
create a region from the area and then use PtInRegion
this only works for integer values
(if you do real x 1000 you could it very accurate ...)

what is the type of FPoint and TFPolygon ?
it would need converting to TPoint and array of TPoint if not compatible

``````function IsPointInPolygon(p : FPoint; poly : TFPolygon) : Boolean;
var i,j : integer;
aRegion: HRGN;
begin
result := false;
aRegion := CreatePolygonRgn(poly, High(Poly) - Low(Poly)+1, WINDING);
if aRegion > 0 then
try
Result := PtInRegion(aRegion, P.X, P.Y);
finally
DeleteObject(aRegion);
end;
end;
``````
0

Oracle dbaCommented:
you would need an algorithm to dissect the polygon into triangles and then see if the point is in a triangle
0

Commented:
function IsPointInPolygon(p : FPoint; poly : TFPolygon) : Boolean;
var i,j : integer;
Begin
result := false;
j := High(poly);
For i := Low(poly) to High(poly) do begin
if (poly[j].y - poly[i].y) + poly[i].x) <> 0 then
if (( ((poly[i].y <= p.y) and (p.y < poly[j].y)) or ((poly[j].y <= p.y) and (p.y < poly[i].y)) ) and
(p.x < ((poly[j].x - poly[i].x) * (p.y - poly[i].y) / (poly[j].y - poly[i].y) + poly[i].x))) then result := not result;
j := i
end;
end;
0

Author Commented:
@Geert  I habe real number and values are from 100 ....1E-9, hard to scale

@TKorhan  I did the same code as a workaround by myself, is it still the creect math solution ?
0

Commented:
Yes, off couse. It is correct math solution...
0

Oracle dbaCommented:
not with a convex polygon ...
0

Oracle dbaCommented:
if it works with this shape, it should work with mostly all ...
convex.bmp
0

Author Commented:
btw,  whose solution is faster?   have use this function 10E5 times   .....
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.