# Board index » delphi » Polygon Operations

## Polygon Operations

2006-08-13 09:26:06 PM
delphi55
i need a routine that will say if a point is within
a polyg on or not. also i need something that will
tell me if a line is intersecting the polygon. any
ideas on how to do this in delphi? is there a routine
available?
-Tara

## Re:Polygon Operations

Hello,
Try the FastGEO Library www.partow.net/projects/fastgeo/index.html
?"Tara Schmitt" <XXXX@XXXXX.COM>έγραψε στ?μήνυμα
##### Quote

i need a routine that will say if a point is within
a polyg on or not. also i need something that will
tell me if a line is intersecting the polygon. any
ideas on how to do this in delphi? is there a routine
available?

-Tara

## Re:Polygon Operations

Here's how to check if a point is in a polygon:
function PtInPoly(x, y: integer; APoly: TPoints): boolean;
var
i, j : integer;
count: integer;
begin
Result := false;
count := Length(APoly);
for i := 0 to count - 1 do
begin
j := (i + 1) mod count;
if ((((APoly[i].Y <= y) and (y < APoly[j].Y)) or ((APoly[j].Y <= y) and
(y < APoly[i].Y))) and
(x < (APoly[j].X - APoly[i].X) * (y - APoly[i].Y)/(APoly[j].Y -
APoly[i].Y) + APoly[i].X)) then
Result := not Result;
end;
end;
And for the other one (just from the top of mind - not tested and can be
optimized):
function CalcLine(LineStart, LineEnd, OtherPt: TPoint): integer;
begin
Result := (OtherPt.Y - LineStart.Y) * (LineEnd.X - LineStart.X) -
(LineEnd.Y - LineStart.Y) * (OtherPt.X - LineStart.X);
end;
function LinesIntersect(Line1Start, Line1End, Line2Start, Line2End):
boolean;
begin
Result := (CalcLine(Line1Start, Line1End, Line2Start) *
CalcLine(Line1Start, Line1End, Line2End) < 0);
end;
function LineIntersectsWithPoly(poly: array of TPoint; LineStart, LineEnd:
TPoint): boolean;
var
i, len: integer;
begin
len := Length(poly);
Result := false;
if len = 0 then Exit;
SetLength(poly, len + 1);
poly[len] := poly[0];
for i := 0 to len - 1 do
if LinesIntersect(poly[i], poly[i + 1], LineStart, LineEnd) then
begin
Result := true;
Exit;
end;
end;
Hope this works :)
Regards
Marko

## Re:Polygon Operations

I'm sorry, it seems I forgot something - TPoints = array of TPoint;
Marko

## Re:Polygon Operations

hi peter,
thanx for that great tip! something like fastgeo was
exactly what i was looking for its soo simple to use.
-Tara
"Peter S." <XXXX@XXXXX.COM>writes:
##### Quote
Hello,

Try the FastGEO Library www.partow.net/projects/fastgeo/index.html

?"Tara Schmitt" <XXXX@XXXXX.COM>έγραψε στ?μήνυμα
news:44df286e\$XXXX@XXXXX.COM...
>
>i need a routine that will say if a point is within
>a polyg on or not. also i need something that will
>tell me if a line is intersecting the polygon. any
>ideas on how to do this in delphi? is there a routine
>available?
>
>
>-Tara
>
>