Board index » delphi » Polygon Operations
Tara Schmitt
Delphi Developer 
Tara Schmitt
Delphi Developer 
Polygon Operations20060813 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 
Peter S.
Delphi Developer 
20060813 11:34:31 PM
Re:Polygon Operations
Hello,
Try the FastGEO Library www.partow.net/projects/fastgeo/index.html ?"Tara Schmitt" <XXXX@XXXXX.COM>έγραψε στ?μήνυμα Quote

Marko Binic
Delphi Developer 
20060814 03:50:20 AM
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 
Marko Binic
Delphi Developer 
20060814 03:52:23 AM
Re:Polygon Operations
I'm sorry, it seems I forgot something  TPoints = array of TPoint;
Marko 
Tara Schmitt
Delphi Developer 
20060818 07:51:32 PM
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: QuoteHello, 