forked from P33a/SimTwo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPathFinder.pas
108 lines (80 loc) · 2.08 KB
/
PathFinder.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
unit PathFinder;
{$MODE Delphi}
interface
uses windows, extctrls, sysutils, classes, Graphics, Math, AStar;
const
AStarVirgin = 0;
AStarObstacle = 1;
AStarClosed = 2;
AStarOpen = 3;
procedure ClearAStarMap;
procedure SetupAStarMap(xi, yi, xt, yt: integer; nEucliDistK: double);
procedure AddAStarObstacleCircle(xc, yc, r: double);
procedure AddAStarObstacleRect(xi, yi, xf, yf: double);
procedure CalcAStarPath;
function GetAStarPathPoint(i: integer): TPoint;
function GetAStarPathCount: integer;
var AStarMap: TAStarMap;
AStarActPath: TAStarPath;
implementation
procedure InitAStarMap;
begin
RecalcSqrtCache(AStarMap);
end;
procedure ClearAStarMap;
begin
AStarClear(AStarMap);
end;
procedure SetupAStarMap(xi, yi, xt, yt: integer; nEucliDistK: double);
begin
with AStarMap.InitialPoint do begin
x := xi;
y := yi;
end;
with AStarMap.TargetPoint do begin
x := xt;
y := yt;
end;
AStarMap.EucliDistK:= nEucliDistK;
end;
procedure AddAStarObstacleCircle(xc, yc, r: double);
var i, j: integer;
dx: double;
begin
for i:=0 to round(r) do begin
dx:=sqrt(r*r - i*i);
for j:= -round(dx)+1 to round(dx)-1 do begin
AStarMap.GridState[round(xc+j),round(yc+i)]:= AStarObstacle;
AStarMap.GridState[round(xc+j),round(yc-i)]:= AStarObstacle;
end;
end;
end;
procedure AddAStarObstacleRect(xi, yi, xf, yf: double);
var x, y: integer;
begin
for y := round(yi) to round(yf) do begin
for x := round(xi) to round(xf) do begin
AStarMap.GridState[x,y]:= AStarObstacle;
end;
end;
end;
procedure CalcAStarPath;
begin
AStarGo(AStarMap);
AStarActPath := AStarPath(AStarMap);
end;
function GetAStarPathPoint(i: integer): TPoint;
begin
if i >= AStarActPath.count then begin
i := AStarActPath.count-1;
end;
result.x := AStarActPath.Points[i].x;
result.y := AStarActPath.Points[i].y;
end;
function GetAStarPathCount: integer;
begin
result := AStarActPath.count;
end;
initialization
RecalcSqrtCache(AStarMap);
end.