-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMyDrawingView.m
executable file
·607 lines (505 loc) · 16.3 KB
/
MyDrawingView.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
//
// MyDrawingView.m
// MyDrawingApp
//
// Created by joel johnson on 6/29/10.
// Copyright 2010 __MyCompanyName__. All rights reserved.
//
#import "MyDrawingView.h"
@implementation MyDrawingView
@synthesize currentColor;
@synthesize fillColor;
#define SQ_SIZE 5
#define MAX_MAZE_SIZE_X 205
#define MAX_MAZE_SIZE_Y 205
#define MOVE_LIST_SIZE (MAX_MAZE_SIZE_X * MAX_MAZE_SIZE_Y)
#define TOP 0
#define RIGHT 1
#define BOTTOM 2
#define LEFT 3
#define WALL_TOP 0x8000
#define WALL_RIGHT 0x4000
#define WALL_BOTTOM 0x2000
#define WALL_LEFT 0x1000
#define DOOR_IN_TOP 0x800
#define DOOR_IN_RIGHT 0x400
#define DOOR_IN_BOTTOM 0x200
#define DOOR_IN_LEFT 0x100
#define DOOR_IN_ANY 0xF00
#define DOOR_OUT_TOP 0x80
#define DOOR_OUT_RIGHT 0x40
#define DOOR_OUT_BOTTOM 0x20
#define DOOR_OUT_LEFT 0x10
#define CHECK_TOP 0x8880
#define CHECK_RIGHT 0x4440
#define CHECK_BOTTOM 0x2220
#define CHECK_LEFT 0x1110
#define START_SQUARE 0x2
#define END_SQUARE 0x1
#define SQ_SIZE_X 10
#define SQ_SIZE_Y 10
#define NUM_RANDOM 100
#define BORDERWIDTH 2
#define border_x (5)
#define border_y (5)
#define MIN_W 200
#define MIN_H 200
#define DEF_W 636
#define DEF_H 456
static unsigned short maze[MAX_MAZE_SIZE_X][MAX_MAZE_SIZE_Y];
static struct {
int x;
int y;
int dir;
} move_list[MOVE_LIST_SIZE], save_path[MOVE_LIST_SIZE], path[MOVE_LIST_SIZE];
static int maze_size_x, maze_size_y;
static int sqnum, cur_sq_x, cur_sq_y, path_length;
static int start_x, start_y, start_dir, end_x, end_y, end_dir;
int screen;
long background;
int reverse = 0;
int width = DEF_W, height = DEF_H ;
int x = 0, y = 0, restart = 0, stop = 1, state = 1;
int seed = 100;
void set_maze_sizes(int w, int h);
void initialize_maze ();
void create_maze();
int choose_door();
int backup();
void draw_maze_border();
void draw_wall();
void draw_solid_square(int i, int j, int dir, int color);
void enter_square(int n);
int get_random(int x);
void clear_screen();
void paint_maze();
void solve_maze();
static CGFunctionRef myGetFunction(CGColorSpaceRef myColorspace);
void PaintMyPattern(CGContextRef context, CGRect targetRect);
- (id)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
// Initialization code
}
return self;
}
- (id)initWithCoder:(NSCoder *)sourceCoder
{
if( (self = [super initWithCoder:sourceCoder] ) )
{
currentColor = [UIColor blueColor];
fillColor = [UIColor greenColor];
}
randomMain = [NSTimer scheduledTimerWithTimeInterval:(5.0) target:self selector:@selector(paint_maze) userInfo:nil repeats:YES];
return self;
}
- (void)drawRect:(CGRect)rect {
seed = (unsigned)time ( NULL );
CGRect screenRect = [[UIScreen mainScreen] bounds];
width = screenRect.size.width - 10;
height = screenRect.size.height - 25;
set_maze_sizes(width, height);
clear_screen();
initialize_maze();
draw_maze_border();
create_maze();
solve_maze();
}
-(void) paint_maze
{
[self setNeedsDisplay];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[randomMain invalidate];
}
- (void)dealloc {
[super dealloc];
}
int get_random(int x)
{
seed = seed * 1103515245 + 12345;
return (seed % ((unsigned int)RAND_MAX + 1)) % (x);
}
void set_maze_sizes(width, height)
{
maze_size_x = width / SQ_SIZE_X;
maze_size_y = height / SQ_SIZE_Y;
}
void clear_screen()
{
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);
CGContextFillRect(context, screenRect);
CGContextStrokePath(context);
}
void initialize_maze() { /* draw the surrounding wall and start/end squares */
register int i, j, wall;
/* initialize all squares */
for ( i=0; i<maze_size_x; i++) {
for ( j=0; j<maze_size_y; j++) {
maze[i][j] = 0;
}
}
/* top wall */
for ( i=0; i<maze_size_x; i++ ) {
maze[i][0] |= WALL_TOP;
}
/* right wall */
for ( j=0; j<maze_size_y; j++ ) {
maze[maze_size_x-1][j] |= WALL_RIGHT;
}
/* bottom wall */
for ( i=0; i<maze_size_x; i++ ) {
maze[i][maze_size_y-1] |= WALL_BOTTOM;
}
/* left wall */
for ( j=0; j<maze_size_y; j++ ) {
maze[0][j] |= WALL_LEFT;
}
/* set start square */
wall = get_random(4);
switch (wall) {
case 0:
i = get_random(maze_size_x);
j = 0;
break;
case 1:
i = maze_size_x - 1;
j = get_random(maze_size_y);
break;
case 2:
i = get_random(maze_size_x);
j = maze_size_y - 1;
break;
case 3:
i = 0;
j = get_random(maze_size_y);
break;
}
maze[i][j] |= START_SQUARE;
maze[i][j] |= ( DOOR_IN_TOP >> wall );
maze[i][j] &= ~( WALL_TOP >> wall );
cur_sq_x = i;
cur_sq_y = j;
start_x = i;
start_y = j;
start_dir = wall;
sqnum = 0;
/* set end square */
wall = (wall + 2)%4;
switch (wall) {
case 0:
i = get_random(maze_size_x);
j = 0;
break;
case 1:
i = maze_size_x - 1;
j = get_random(maze_size_y);
break;
case 2:
i = get_random(maze_size_x);
j = maze_size_y - 1;
break;
case 3:
i = 0;
j = get_random(maze_size_y);
break;
}
maze[i][j] |= END_SQUARE;
maze[i][j] |= ( DOOR_OUT_TOP >> wall );
maze[i][j] &= ~( WALL_TOP >> wall );
end_x = i;
end_y = j;
end_dir = wall;
}
void create_maze() /* create a maze layout given the intiialized maze */
{
register int i, newdoor;
do {
move_list[sqnum].x = cur_sq_x;
move_list[sqnum].y = cur_sq_y;
move_list[sqnum].dir = newdoor;
while ( ( newdoor = choose_door() ) == -1 ) { /* pick a door */
if ( backup() == -1 ) { /* no more doors ... backup */
return; /* done ... return */
}
}
/* mark the out door */
maze[cur_sq_x][cur_sq_y] |= ( DOOR_OUT_TOP >> newdoor );
switch (newdoor) {
case 0: cur_sq_y--;
break;
case 1: cur_sq_x++;
break;
case 2: cur_sq_y++;
break;
case 3: cur_sq_x--;
break;
}
sqnum++;
/* mark the in door */
maze[cur_sq_x][cur_sq_y] |= ( DOOR_IN_TOP >> ((newdoor+2)%4) );
/* if end square set path length and save path */
if ( maze[cur_sq_x][cur_sq_y] & END_SQUARE ) {
path_length = sqnum;
for ( i=0; i<path_length; i++) {
save_path[i].x = move_list[i].x;
save_path[i].y = move_list[i].y;
save_path[i].dir = move_list[i].dir;
}
}
} while (1);
}
int choose_door() /* pick a new path */
{
int candidates[3];
register int num_candidates;
num_candidates = 0;
topwall:
/* top wall */
if ( maze[cur_sq_x][cur_sq_y] & DOOR_IN_TOP )
goto rightwall;
if ( maze[cur_sq_x][cur_sq_y] & DOOR_OUT_TOP )
goto rightwall;
if ( maze[cur_sq_x][cur_sq_y] & WALL_TOP )
goto rightwall;
if ( maze[cur_sq_x][cur_sq_y - 1] & DOOR_IN_ANY ) {
maze[cur_sq_x][cur_sq_y] |= WALL_TOP;
maze[cur_sq_x][cur_sq_y - 1] |= WALL_BOTTOM;
draw_wall(cur_sq_x, cur_sq_y, 0);
goto rightwall;
}
candidates[num_candidates++] = 0;
rightwall:
/* right wall */
if ( maze[cur_sq_x][cur_sq_y] & DOOR_IN_RIGHT )
goto bottomwall;
if ( maze[cur_sq_x][cur_sq_y] & DOOR_OUT_RIGHT )
goto bottomwall;
if ( maze[cur_sq_x][cur_sq_y] & WALL_RIGHT )
goto bottomwall;
if ( maze[cur_sq_x + 1][cur_sq_y] & DOOR_IN_ANY ) {
maze[cur_sq_x][cur_sq_y] |= WALL_RIGHT;
maze[cur_sq_x + 1][cur_sq_y] |= WALL_LEFT;
draw_wall(cur_sq_x, cur_sq_y, 1);
goto bottomwall;
}
candidates[num_candidates++] = 1;
bottomwall:
/* bottom wall */
if ( maze[cur_sq_x][cur_sq_y] & DOOR_IN_BOTTOM )
goto leftwall;
if ( maze[cur_sq_x][cur_sq_y] & DOOR_OUT_BOTTOM )
goto leftwall;
if ( maze[cur_sq_x][cur_sq_y] & WALL_BOTTOM )
goto leftwall;
if ( maze[cur_sq_x][cur_sq_y + 1] & DOOR_IN_ANY ) {
maze[cur_sq_x][cur_sq_y] |= WALL_BOTTOM;
maze[cur_sq_x][cur_sq_y + 1] |= WALL_TOP;
draw_wall(cur_sq_x, cur_sq_y, 2);
goto leftwall;
}
candidates[num_candidates++] = 2;
leftwall:
/* left wall */
if ( maze[cur_sq_x][cur_sq_y] & DOOR_IN_LEFT )
goto donewall;
if ( maze[cur_sq_x][cur_sq_y] & DOOR_OUT_LEFT )
goto donewall;
if ( maze[cur_sq_x][cur_sq_y] & WALL_LEFT )
goto donewall;
if ( maze[cur_sq_x - 1][cur_sq_y] & DOOR_IN_ANY ) {
maze[cur_sq_x][cur_sq_y] |= WALL_LEFT;
maze[cur_sq_x - 1][cur_sq_y] |= WALL_RIGHT;
draw_wall(cur_sq_x, cur_sq_y, 3);
goto donewall;
}
candidates[num_candidates++] = 3;
donewall:
if (num_candidates == 0)
return ( -1 );
if (num_candidates == 1)
return ( candidates[0] );
return ( candidates[ get_random(num_candidates) ] );
}
int backup() /* back up a move */
{
sqnum--;
cur_sq_x = move_list[sqnum].x;
cur_sq_y = move_list[sqnum].y;
return ( sqnum );
}
void draw_maze_border() /* draw the maze outline */
{
register int i, j;
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);
for ( i=0; i<maze_size_x; i++) {
if ( maze[i][0] & WALL_TOP ) {
CGContextMoveToPoint(context,
border_x + SQ_SIZE_X * i,
border_y);
CGContextAddLineToPoint(context,
border_x + SQ_SIZE_X * (i+1),
border_y);
CGContextStrokePath(context);
}
if ((maze[i][maze_size_y - 1] & WALL_BOTTOM)) {
CGContextMoveToPoint(context,
border_x + SQ_SIZE_X * i,
border_y + SQ_SIZE_Y * (maze_size_y));
CGContextAddLineToPoint(context,
border_x + SQ_SIZE_X * (i+1),
border_y + SQ_SIZE_Y * (maze_size_y));
CGContextStrokePath(context);
}
}
for ( j=0; j<maze_size_y; j++) {
if ( maze[maze_size_x - 1][j] & WALL_RIGHT ) {
CGContextMoveToPoint(context,
border_x + SQ_SIZE_X * maze_size_x,
border_y + SQ_SIZE_Y * j);
CGContextAddLineToPoint(context,
border_x + SQ_SIZE_X * maze_size_x,
border_y + SQ_SIZE_Y * (j+1));
CGContextStrokePath(context);
}
if ( maze[0][j] & WALL_LEFT ) {
CGContextMoveToPoint(context,
border_x,
border_y + SQ_SIZE_Y * j);
CGContextAddLineToPoint(context,
border_x,
border_y + SQ_SIZE_Y * (j+1));
CGContextStrokePath(context);
}
}
draw_solid_square( start_x, start_y, start_dir, 1);
draw_solid_square( end_x, end_y, end_dir,1);
}
void draw_wall(i, j, dir) /* draw a single wall */
int i, j, dir;
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);
switch (dir) {
case 0:
CGContextMoveToPoint(context,
border_x + SQ_SIZE_X * i,
border_y + SQ_SIZE_Y * j);
CGContextAddLineToPoint(context,
border_x + SQ_SIZE_X * (i+1),
border_y + SQ_SIZE_Y * j);
break;
case 1:
CGContextMoveToPoint(context,
border_x + SQ_SIZE_X * (i+1),
border_y + SQ_SIZE_Y * j);
CGContextAddLineToPoint(context,
border_x + SQ_SIZE_X * (i+1),
border_y + SQ_SIZE_Y * (j+1));
break;
case 2:
CGContextMoveToPoint(context,
border_x + SQ_SIZE_X * i,
border_y + SQ_SIZE_Y * (j+1));
CGContextAddLineToPoint(context,
border_x + SQ_SIZE_X * (i+1),
border_y + SQ_SIZE_Y * (j+1));
break;
case 3:
CGContextMoveToPoint(context,
border_x + SQ_SIZE_X * i,
border_y + SQ_SIZE_Y * j);
CGContextAddLineToPoint(context,
border_x + SQ_SIZE_X * i,
border_y + SQ_SIZE_Y * (j+1));
break;
}
CGContextStrokePath(context);
}
void draw_solid_square(i, j, dir, color) /* draw a solid square in a square */
register int i, j, dir, color;
{
CGContextRef context = UIGraphicsGetCurrentContext();
if (color==1) {
CGContextSetFillColorWithColor(context, [UIColor blackColor].CGColor);
}
else {
CGContextSetFillColorWithColor(context, [UIColor grayColor].CGColor);
}
switch (dir) {
case 0: CGContextFillRect(context, CGRectMake(
border_x + 3 + SQ_SIZE_X * i,
border_y - 3 + SQ_SIZE_Y * j,
SQ_SIZE_X - 6, SQ_SIZE_Y));
break;
case 1: CGContextFillRect(context, CGRectMake(
border_x + 3 + SQ_SIZE_X * i,
border_y + 3 + SQ_SIZE_Y * j,
SQ_SIZE_X, SQ_SIZE_Y - 6));
break;
case 2: CGContextFillRect(context, CGRectMake(
border_x + 3 + SQ_SIZE_X * i,
border_y + 3 + SQ_SIZE_Y * j,
SQ_SIZE_X - 6, SQ_SIZE_Y));
break;
case 3: CGContextFillRect(context, CGRectMake(
border_x - 3 + SQ_SIZE_X * i,
border_y + 3 + SQ_SIZE_Y * j,
SQ_SIZE_X, SQ_SIZE_Y - 6));
break;
}
CGContextStrokePath(context);
}
void solve_maze() /* solve it with graphical feedback */
{
int i;
/* plug up the surrounding wall */
maze[start_x][start_y] |= (WALL_TOP >> start_dir);
maze[end_x][end_y] |= (WALL_TOP >> end_dir);
/* initialize search path */
i = 0;
path[i].x = end_x;
path[i].y = end_y;
path[i].dir = -1;
/* do it */
while (1) {
if ( ++path[i].dir >= 4 ) {
// draw_solid_square( (int)(path[i].x), (int)(path[i].y),(int)(path[i].dir),0);
i--;
draw_solid_square( (int)(path[i].x), (int)(path[i].y),(int)(path[i].dir),0);
}
else if ( ! (maze[path[i].x][path[i].y] &
(WALL_TOP >> path[i].dir)) &&
( (i == 0) || ( (path[i].dir !=
(int)(path[i-1].dir+2)%4) ) ) ) {
enter_square(i);
i++;
if ( maze[path[i].x][path[i].y] & START_SQUARE ) {
return;
}
}
}
}
void enter_square(n) /* move into a neighboring square */
int n;
{
draw_solid_square( (int)path[n].x, (int)path[n].y, (int)path[n].dir,1);
path[n+1].dir = -1;
switch (path[n].dir) {
case 0: path[n+1].x = path[n].x;
path[n+1].y = path[n].y - 1;
break;
case 1: path[n+1].x = path[n].x + 1;
path[n+1].y = path[n].y;
break;
case 2: path[n+1].x = path[n].x;
path[n+1].y = path[n].y + 1;
break;
case 3: path[n+1].x = path[n].x - 1;
path[n+1].y = path[n].y;
break;
}
}
@end