Mega Code Archive

 
Categories / Delphi / Examples
 

Point in polygon

How to check if point X,Y is within a polygon. Point in polygon Apparently this solution involves checking how many times you cross a line to pass from point X,Y to the outer edge of the screen. I honestly have never tried reading the code properly to try to understand the concept, I just know that it seems to work ! function PtInPoly(const Points: Array of TPoint; X,Y: Integer): Boolean; var Count, I, J : Integer; begin Result := False; Count := Length(Points); J := Count-1; for I := 0 to Count-1 do begin if ((Points[I].Y <=Y) and (Y < Points[J].Y)) or ((Points[J].Y <=Y) and (Y < Points[I].Y)) then begin if (x < (Points[j].X - Points[i].X) * (y - Points[i].Y) / (Points[j].Y - Points[i].Y) + Points[i].X) then Result := not Result; end; J := I; end; end;