Sign in Join
Wiki copytemporary

Geometry in Pascal: what changed

Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.

Title: Geometry in Pascal

OldNewLine
1+## Checking if a point is inside a polygon (integer version)
2+
3+``` pascal
4+// The function will return True if the point x,y is inside the polygon, or
5+// False if it is not.
6+//
7+// Original C code: http://www.visibone.com/inpoly/inpoly.c.txt
8+//
9+// Translation from C by Felipe Monteiro de Carvalho
10+//
11+// License: Public Domain
12+function IsPointInPolygon(AX, AY: Integer; APolygon: array of TPoint): Boolean;
13+var
14+ xnew, ynew: Cardinal;
15+ xold,yold: Cardinal;
16+ x1,y1: Cardinal;
17+ x2,y2: Cardinal;
18+ i, npoints: Integer;
19+ inside: Integer = 0;
20+begin
21+ Result := False;
22+ npoints := Length(APolygon);
23+ if (npoints < 3) then Exit;
24+ xold := APolygon[npoints-1].X;
25+ yold := APolygon[npoints-1].Y;
26+ for i := 0 to npoints - 1 do
27+ begin
28+ xnew := APolygon[i].X;
29+ ynew := APolygon[i].Y;
30+ if (xnew > xold) then
31+ begin
32+ x1:=xold;
33+ x2:=xnew;
34+ y1:=yold;
35+ y2:=ynew;
36+ end
37+ else
38+ begin
39+ x1:=xnew;
40+ x2:=xold;
41+ y1:=ynew;
42+ y2:=yold;
43+ end;
44+ if (((xnew < AX) = (AX <= xold)) // edge "open" at left end
45+ and ((AY-y1)*(x2-x1) < (y2-y1)*(AX-x1))) then
46+ begin
47+ inside := not inside;
48+ end;
49+ xold:=xnew;
50+ yold:=ynew;
51+ end;
52+ Result := inside <> 0;
53+end;
54+```
55+
56+## Checking if a point is inside a polygon, version 2
57+
58+Code from [German forum](https://www.lazarusforum.de/viewtopic.php?p=118119#p118119), by winni.
59+
60+``` pascal
61+function PointInPoly(p : TPointF; const poly : array of TPointF) : Boolean;
62+var
63+ i,k : integer;
64+Begin
65+ result := false;
66+ k := High(poly);
67+ For i := 0 to high(poly) do begin
68+ if (
69+ ( ((poly[i].y <= p.y) and (p.y < poly[k].y)) or ((poly[k].y <= p.y) and (p.y < poly[i].y)) ) and
70+ (p.x < ((poly[k].x - poly[i].x) * (p.y - poly[i].y) / (poly[k].y - poly[i].y) + poly[i].x) )
71+ ) then result := not result;
72+ k := i
73+ end;
74+end;
75+```
76+
77+## See also
78+
79+- [A Point about Polygons](http://www.visibone.com/inpoly/)
80+
81+---
82+*From the Free Pascal wiki, page "Geometry in Pascal", as it stood on 2022-03-09 after a change by Alextp, 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](/wiki-contact).*

Back to history