Link to home
Start Free TrialLog in
Avatar of BdLm
BdLmFlag for Germany

asked on

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;

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Geert G
Geert G
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
you would need an algorithm to dissect the polygon into triangles and then see if the point is in a triangle
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of BdLm

ASKER

@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 ?
Avatar of TKorhan
TKorhan

Yes, off couse. It is correct math solution...
not with a convex polygon ...
if it works with this shape, it should work with mostly all ...
convex.bmp
Avatar of BdLm

ASKER

btw,  whose solution is faster?   have use this function 10E5 times   .....