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

LVL 8
BdLmAsked:
Who is Participating?
 
Geert GOracle 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;

Open in new window

0
 
Geert GOracle dbaCommented:
you would need an algorithm to dissect the polygon into triangles and then see if the point is in a triangle
0
 
TKorhanCommented:
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
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
BdLmAuthor 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
 
TKorhanCommented:
Yes, off couse. It is correct math solution...
0
 
Geert GOracle dbaCommented:
not with a convex polygon ...
0
 
Geert GOracle dbaCommented:
if it works with this shape, it should work with mostly all ...
convex.bmp
0
 
BdLmAuthor 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.

All Courses

From novice to tech pro — start learning today.