[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 586
  • Last Modified:

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

0
BdLm
Asked:
BdLm
  • 4
  • 2
  • 2
2 Solutions
 
Geert GruwezOracle 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 GruwezOracle 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 GruwezOracle dbaCommented:
not with a convex polygon ...
0
 
Geert GruwezOracle 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

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 4
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now