-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAStar.m
182 lines (163 loc) · 4.98 KB
/
AStar.m
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
//
// AStar.m
// AStar
//
// Created by Mihails Tumkins on 11/29/11.
// Copyright (c) 2011 __MyCompanyName__. All rights reserved.
//
#import "AStar.h"
@implementation AStar
@synthesize open;
@synthesize closed;
@synthesize path;
@synthesize grid;
@synthesize straightCost;
@synthesize diagonalCost;
@synthesize heuristicType;
-(AStar *)initWithGrid:(AStarGrid *)g {
self = [super init];
if (self) {
self.grid = g;
self.diagonalCost = AS_COST_DIAGONAL;
self.straightCost = AS_COST_STRAIGHT;
self.heuristicType = EUCLIDIAN;
self.open = [[NSMutableArray alloc] init];
self.closed = [[NSMutableArray alloc] init];
self.path = [[NSMutableArray alloc] init];
}
return self;
}
-(BOOL)findPath {
if (!grid.startNode || !grid.endNode) {
return NO;
}
[open removeAllObjects];
[closed removeAllObjects];
[path removeAllObjects];
grid.startNode.g = 0;
grid.startNode.h = [self getHeruistic:grid.startNode];
grid.startNode.f = grid.startNode.g + grid.startNode.h;
return [self search];
}
-(BOOL)search {
AStarNode * node = grid.startNode;
while (![node isEqual:grid.endNode]) {
int startX = fmax(0, node.position.x - 1);
int endX = fmin(grid.numCols - 1, node.position.x + 1);
int startY = fmax(0, node.position.y - 1);
int endY = fmin(grid.numRows - 1, node.position.y + 1);
for (int i = startX; i <= endX; i++) {
for (int j = startY; j <= endY; j++) {
AStarNode * test = [grid getNodeAtPosition:CGPointMake(i, j)];
if ([test isEqual:node]) {
continue;
}
if (!test.walkable) {
continue;
}
if (AS_DIAGONAL_ALLOWED == 0) {
if (test.position.x != node.position.x && test.position.y != node.position.y) {
continue;
}
}
double cost = straightCost;
if(!((node.position.x == test.position.x) || (node.position.y == test.position.y))){
cost = diagonalCost;
}
double g = node.g + cost * test.cost;
double h = [self getHeruistic:test];
double f = g + h;
if ([self isOpen:test] || [self isClosed:test]) {
if(test.f > f)
{
test.f = f;
test.g = g;
test.h = h;
test.parentNode = node;
}
} else {
test.f = f;
test.g = g;
test.h = h;
test.parentNode = node;
[open addObject:test];
}
}
}
[closed addObject:node];
if ([open count] == 0) {
return NO;
}
[open sortUsingSelector:@selector(compareF:)];
node = [open objectAtIndex:0];
[open removeObjectAtIndex:0];
}
[self buildPath];
return YES;
}
-(NSMutableArray *)buildPath {
[path removeAllObjects];
AStarNode * node = grid.endNode;
while (![node isEqual:grid.startNode]) {
[path addObject:node];
node = node.parentNode;
}
return path;
}
-(double)getHeruistic:(AStarNode *)node {
switch (heuristicType) {
case DIAGONAL:
return [self diagonal:node];
break;
case MANHATTAN:
return [self manhattan:node];
break;
case EUCLIDIAN:
return [self euclidian:node];
break;
default:
return [self diagonal:node];
break;
}
}
-(double)manhattan:(AStarNode *)node {
return fabs(node.position.x - grid.endNode.position.x) * straightCost + fabs(node.position.y - grid.endNode.position.y) * straightCost;
}
-(double)euclidian:(AStarNode *)node {
double dx, dy;
dx = node.position.x - grid.endNode.position.x;
dy = node.position.y - grid.endNode.position.y;
return sqrt(dx * dx + dy * dy);
}
-(double)diagonal:(AStarNode *)node {
double dx, dy, diag, straight;
dx = fabs(node.position.x - grid.endNode.position.x);
dy = fabs(node.position.y - grid.endNode.position.y);
diag = fmin(dx, dy);
straight = dx + dy;
return diagonalCost * diag + straightCost * (straight - 2 * diag);
}
-(BOOL)isOpen:(AStarNode *)node {
for (AStarNode *n in open) {
if ([n isEqual:node]) {
return YES;
}
}
return NO;
}
-(BOOL)isClosed:(AStarNode *)node {
for (AStarNode *n in closed) {
if ([n isEqual:node]) {
return YES;
}
}
return NO;
}
-(void) dealloc {
[open release];
[closed release];
[path release];
[grid release];
[super dealloc];
}
@end