-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUPointInPolygone.pas
115 lines (101 loc) · 3.81 KB
/
UPointInPolygone.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
unit UPointInPolygone;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Buttons, ExtCtrls, Math, StdCtrls;
type
TForm1 = class(TForm)
Image1: TImage;
SpeedButton1: TSpeedButton;
SpeedButton2: TSpeedButton;
SpeedButton4: TSpeedButton;
SpeedButton5: TSpeedButton;
Label1: TLabel;
Label2: TLabel;
procedure SpeedButton1Click(Sender: TObject);
procedure Image1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure Image1DblClick(Sender: TObject);
procedure SpeedButton2Click(Sender: TObject);
procedure SpeedButton4Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure SpeedButton5Click(Sender: TObject);
private
{ Déclarations privées }
public
{ Déclarations publiques }
end;
var
Form1: TForm1;
polygon:array of tpoint; //out polygon
point:tpoint; //our point
clique:integer; //to draw the polygon (number of vertices
implementation
{$R *.dfm}
function intersect(p1,p2,s1,s2:tpoint):boolean;
var a1,b1,c1,a2,b2,c2:real;
begin
a1:=(p1.y-p2.y); b1:=-(p1.x-p2.x); c1:=-(a1*p1.x+b1*p1.y); //linear equation 1
a2:=(s1.y-s2.y); b2:=-(s1.x-s2.x); c2:=-(a2*s1.x+b2*s1.y); //linear equation 2
if ((sign(a1*s1.x+b1*s1.y+c1)*sign(a1*s2.x+b1*s2.y+c1)<0))and //if p1 and p2 are in opposite sides of the line carrying s1 and s2 and vis versa then the two lines intersect
((sign(a2*p1.x+b2*p1.y+c2)*sign(a2*p2.x+b2*p2.y+c2)<0)) then intersect:=true
else intersect:=false;
end;
procedure TForm1.SpeedButton1Click(Sender: TObject);
begin
SpeedButton1.Flat:=true;
end;
procedure TForm1.Image1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
if SpeedButton1.Flat then begin //add a point to our polygon
inc(clique);
if clique=1 then Image1.Canvas.MoveTo(X,Y);
SetLength(Polygon,clique);
Polygon[clique-1].X:=X;
Polygon[clique-1].Y:=Y;
Image1.Canvas.LineTo(X,Y);
end;
if SpeedButton4.Flat then begin //affect coordinates to our point
point.X:=X;
point.Y:=Y;
Image1.Canvas.Ellipse(X-3,Y-3,X+3,Y+3);
SpeedButton4.Flat:=false;
end;
end;
procedure TForm1.Image1DblClick(Sender: TObject); // DblClick means that we finished to record the polygon
begin
if SpeedButton1.Flat then begin
SpeedButton1.Flat:=false;
Image1.Canvas.LineTo(Polygon[0].X,Polygon[0].Y);
Label1.Caption:='Vertex = '+inttostr(clique);
clique:=0;
end;
end;
procedure TForm1.SpeedButton2Click(Sender: TObject);
var i,n,counter:integer;
infiniteP:tpoint;
begin
n:=length(polygon);
infiniteP.X:=Image1.Width;infiniteP.Y:=point.Y;
counter:=0;
for i:=0 to n-1 do
if intersect(point,infiniteP,polygon[i],polygon[(i+1)mod n]) then inc(counter);
if counter mod 2 = 0 then Label2.Caption:='The point is outside'
else Label2.Caption:='The point is inside';
end;
procedure TForm1.SpeedButton4Click(Sender: TObject);
begin
SpeedButton4.Flat:=true;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
Image1.Canvas.Create;
end;
procedure TForm1.SpeedButton5Click(Sender: TObject);
begin
Image1.Canvas.Rectangle(-1,-1,Image1.Width+1,Image1.Height+1); //draw a white rectangle to erase our image
Label1.Caption:='';
Label2.Caption:='';
end;
end.