Sign in Join
Wiki copytemporary

Geometry in Pascal/de

Deutsch · English · Français

Testen, ob ein Punkt in einem Polygon liegt

pascal
//  Die Funktion gibt den Wert True zurück, wenn der Punkt x,y innerhalb eines Polygons liegt.
//  Die Funktion gibt den Wert False zurück, wenn der Punkt x,y nicht in einem Polygon liegt.
//
//  Der Original C-Code: http://www.visibone.com/inpoly/inpoly.c.txt
//
//  Übersetzng von C nach Pascal: Felipe Monteiro de Carvalho
//
//  Lizenz: Public Domain

function IsPointInPolygon(AX, AY: Integer; APolygon: array of TPoint): Boolean;
var
  xnew, ynew,
  xold, yold,
  x1, y1,
  x2, y2:     Cardinal;
  i, npoints: Integer;
  inside:     Integer = 0;

begin
  Result := False;

  npoints := Length(APolygon);

  if (npoints < 3) then Exit;

  xold := APolygon[npoints-1].X;
  yold := APolygon[npoints-1].Y;

  for i := 0 to npoints - 1 do begin
    xnew := APolygon[i].X;
    ynew := APolygon[i].Y;

    if (xnew > xold) then begin
      x1 := xold;
      x2 := xnew;
      y1 := yold;
      y2 := ynew;
    end else begin
      x1 := xnew;
      x2 := xold;
      y1 := ynew;
      y2 := yold;
    end;

    if (((xnew < AX) = (AX <= xold)) and    // Die Ecke ist am linken Ende "offen"
       ((AY - y1) * (x2 - x1) < (y2 - y1) * (AX - x1))) then
    begin
      inside := not inside;
    end;

    xold := xnew;
    yold := ynew;

  end;

  Result := inside <> 0;
end;

Siehe auch


From the Free Pascal wiki, page "Geometry in Pascal/de", as it stood on 2020-08-22 after a change by Kai Burghardt, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's Licensing page. If you have concerns about this material, message the admin.

Revision 3, last changed by WikiArchive .

Edited on wiki.freepascal.org by 5 people, 2014–2020

From the official wiki's backup of 26 May 2024: who edited this page there, and when. Edits made there after that date were not recorded, and the earlier versions are not shown here; the main page says why.

WhenWhoWhat changed
OlafCreated page
FTurtle
MathiasTesten, ob ein Punkt in einem Polygon liegt
TrevFixed syntax highlighting (minor)
Kai Burghardtunify code style, translate heading (minor)

Talk about this page

Nobody has opened a bench for this page yet. Sign in to open one.