Wiki-KopieMirror

Dies ist ein Mirror des Wikis von pascal.chat. Bearbeiten, Verlauf und Anmeldung finden dort statt.

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.

Version 3, zuletzt geändert von WikiArchive .