-
Notifications
You must be signed in to change notification settings - Fork 6
/
turtle.cpp
640 lines (586 loc) · 13.6 KB
/
turtle.cpp
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
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
#include "turtle.h"
#include <math.h>
#include <queue>
#include <memory>
#define BASE_STEP 1
using namespace ege;
typedef struct
{
double x;
double y;
double orient; //头指向角度
BOOL is_pen_down;
color_t pen_color;
int pen_size;
int pen_speed;
PIMAGE icon;
double icon_width;
double icon_height;
BOOL is_show; //是否显示海龟
} Turtle;
typedef struct
{
int width;
int height;
double scale;
int back_color;
BOOL rewind;
BOOL immediate;
PIMAGE world_image;
PIMAGE background_image;
PIMAGE default_background;
int origin_x;
int origin_y;
int frame_count;
} World;
static Turtle myturtle;
static World myworld;
static double d2a(double degree)
{
return degree*M_PI/180.0;
}
static PIMAGE screenImage;
static void displayWorld()
{
if (myworld.background_image != NULL) {
putimage(screenImage,0,0,myworld.background_image);
}
putimage_withalpha(screenImage,myworld.world_image,0,0);
if (myturtle.is_show)
{
putimage_rotatetransparent(screenImage,
myturtle.icon,
myturtle.x*myworld.scale,myturtle.y*myworld.scale,
0,0,
myturtle.icon_width,
myturtle.icon_height,
myturtle.icon_width/2,
myturtle.icon_height/2,
WHITE,
(myturtle.orient+90)/180*PI);
}
putimage(0,0,screenImage);
}
static void refreshWorld()
{
myworld.frame_count++;
myworld.frame_count%=myturtle.pen_speed;
if (myturtle.pen_speed>100)
{
int s=myturtle.pen_speed/100;
if (myworld.frame_count%s!=1)
{
return;
}
delay_ms(10);
}
else
{
delay_ms(1000/myturtle.pen_speed);
}
displayWorld();
}
static void prepareTurtleOriginIcon()
{
int width,height;
width=10;
height=10;
myturtle.icon=newimage(width,height);
myturtle.icon_width=width;
myturtle.icon_height=height;
setbkcolor(WHITE,myturtle.icon);
setfillcolor(BLACK,myturtle.icon);
cleardevice(myturtle.icon);
putpixel(4,0,LIGHTRED,myturtle.icon);
putpixel(5,0,LIGHTRED,myturtle.icon);
putpixel(3,1,LIGHTRED,myturtle.icon);
putpixel(4,1,LIGHTRED,myturtle.icon);
putpixel(5,1,LIGHTRED,myturtle.icon);
putpixel(6,1,LIGHTRED,myturtle.icon);
putpixel(4,2,LIGHTRED,myturtle.icon);
putpixel(5,2,LIGHTRED,myturtle.icon);
//画龟背轮廓
putpixel(4,3,RED, myturtle.icon);
putpixel(5,3,RED, myturtle.icon);
putpixel(3,4,RED, myturtle.icon);
putpixel(6,4,RED, myturtle.icon);
putpixel(2,5,RED, myturtle.icon);
putpixel(7,5,RED, myturtle.icon);
putpixel(2,6,RED, myturtle.icon);
putpixel(7,6,RED, myturtle.icon);
putpixel(3,7,RED, myturtle.icon);
putpixel(6,7,RED, myturtle.icon);
putpixel(4,8,RED, myturtle.icon);
putpixel(5,8,RED, myturtle.icon);
//填充龟背
putpixel(4,4,DARKGRAY, myturtle.icon);
putpixel(5,4,DARKGRAY, myturtle.icon);
putpixel(3,5,DARKGRAY, myturtle.icon);
putpixel(4,5,DARKGRAY, myturtle.icon);
putpixel(5,5,DARKGRAY, myturtle.icon);
putpixel(6,5,DARKGRAY, myturtle.icon);
putpixel(3,6,DARKGRAY, myturtle.icon);
putpixel(4,6,DARKGRAY, myturtle.icon);
putpixel(5,6,DARKGRAY, myturtle.icon);
putpixel(6,6,DARKGRAY, myturtle.icon);
putpixel(4,7,DARKGRAY, myturtle.icon);
putpixel(5,7,DARKGRAY, myturtle.icon);
//绘制四肢
putpixel(1,3,LIGHTRED,myturtle.icon);
putpixel(1,4,LIGHTRED,myturtle.icon);
putpixel(2,3,LIGHTRED,myturtle.icon);
putpixel(2,4,LIGHTRED,myturtle.icon);
putpixel(7,3,LIGHTRED,myturtle.icon);
putpixel(7,4,LIGHTRED,myturtle.icon);
putpixel(8,3,LIGHTRED,myturtle.icon);
putpixel(8,4,LIGHTRED,myturtle.icon);
putpixel(1,7,LIGHTRED,myturtle.icon);
putpixel(1,8,LIGHTRED,myturtle.icon);
putpixel(2,7,LIGHTRED,myturtle.icon);
putpixel(2,8,LIGHTRED,myturtle.icon);
putpixel(7,7,LIGHTRED,myturtle.icon);
putpixel(7,8,LIGHTRED,myturtle.icon);
putpixel(8,7,LIGHTRED,myturtle.icon);
putpixel(8,8,LIGHTRED,myturtle.icon);
//绘制尾巴
putpixel(4,9,LIGHTRED,myturtle.icon);
putpixel(5,9,LIGHTRED,myturtle.icon);
}
void initWorld(int width,int height,double scale)
{
ege::initgraph(width*scale,height*scale);
randomize();
myworld.width=width;
myworld.height=height;
myworld.back_color=WHITE;
myworld.rewind=TRUE;
myworld.immediate=FALSE;
myworld.scale=scale;
myworld.world_image=newimage(width*scale,height*scale);
myworld.frame_count=0;
myworld.origin_x=width/2;
myworld.origin_y=height/2;
setbkcolor(EGERGBA(0,0,0,0), myworld.world_image);
//setbkcolor(myworld.back_color, myworld.world_image);
setfillcolor(myturtle.pen_color, myworld.world_image);
setlinewidth(myturtle.pen_size, myworld.world_image);
cleardevice(myworld.world_image);
myworld.default_background=newimage(width*scale,height*scale);
setbkcolor(myworld.back_color, myworld.default_background);
cleardevice(myworld.default_background);
myworld.background_image = myworld.default_background;
myturtle.x=myworld.origin_x;
myturtle.y=myworld.origin_y;
myturtle.orient=-90;
myturtle.is_pen_down=TRUE;
myturtle.pen_color=BLACK;
myturtle.pen_size=1;
myturtle.pen_speed=100;
myturtle.icon=NULL;
myturtle.is_show=TRUE;
prepareTurtleOriginIcon();
setcolor(myturtle.pen_color,myworld.world_image);
screenImage=newimage(width*scale,height*scale);
setrendermode(RENDER_MANUAL);
refreshWorld();
}
void fd(double step)
{
forward(step);
}
void forward(double step)
{
double delta_x,delta_y;
double x,y,old_x,old_y;
delta_x=BASE_STEP*cos(d2a(myturtle.orient));
delta_y=BASE_STEP*sin(d2a(myturtle.orient));
if (step<0)
{
delta_x=-delta_x;
delta_y=-delta_y;
step=-step;
}
if (!myworld.immediate) {
old_x=x=myturtle.x;
old_y=y=myturtle.y;
for (int i=0; i<step; i++)
{
if ((i+1)<step)
{
x+=delta_x;
y+=delta_y;
}
else
{
x+=(step-i)*delta_x;
y+=(step-i)*delta_y;
}
if (myworld.rewind)
{
if (x<0)
{
x+=myworld.width;
old_x+=myworld.width;
}
else if (x>myworld.width)
{
x-=myworld.width;
old_x-=myworld.width;
}
if (y<0)
{
y+=myworld.height;
old_y+=myworld.height;
}
else if (y>myworld.height)
{
y-=myworld.height;
old_y-=myworld.height;
}
}
if (myturtle.is_pen_down)
{
ege_line(round(old_x*myworld.scale),round(old_y*myworld.scale),round(x*myworld.scale),round(y*myworld.scale),myworld.world_image);
}
old_x=x;
old_y=y;
myturtle.x=x;
myturtle.y=y;
refreshWorld();
}
} else {
x=myturtle.x+step*delta_x;
y=myturtle.y+step*delta_y;
if (myworld.rewind) {
while (x<0) {
x+=myworld.width;
}
while (x>myworld.width) {
x-=myworld.width;
}
while (y<0) {
y+=myworld.height;
}
while (y>myworld.height) {
y-=myworld.height;
}
}
if (myturtle.is_pen_down) {
ege_line(round(myturtle.x*myworld.scale),round(myturtle.y*myworld.scale),round(x*myworld.scale),round(y*myworld.scale),myworld.world_image);
}
myturtle.x=x;
myturtle.y=y;
}
refreshWorld();
}
void bk(double step)
{
backward(step);
}
void backward(double step)
{
forward(-step);
}
void lt(double degree)
{
leftTurn(degree);
}
void leftTurn(double degree)
{
double origin_angle=myturtle.orient;
if (!myworld.immediate)
{
if (degree>0)
{
for (int i=0; i<degree; i+=2)
{
myturtle.orient=origin_angle-i;
//prepareTurtleIcon();
refreshWorld();
}
}
else
{
for (int i=0; i<-degree; i+=2)
{
myturtle.orient=origin_angle+i;
//prepareTurtleIcon();
refreshWorld();
}
}
}
degree=degree-(int)degree/360*360;
myturtle.orient=origin_angle-degree;
if (myturtle.orient>360)
{
myturtle.orient-=360;
}
if (myturtle.orient<0)
{
myturtle.orient+=360;
}
//prepareTurtleIcon();
refreshWorld();
}
void rt(double degree)
{
rightTurn(degree);
}
void rightTurn(double degree)
{
lt(-degree);
}
void pd()
{
penDown();
}
void penDown()
{
myturtle.is_pen_down=TRUE;
}
void penUp()
{
myturtle.is_pen_down=FALSE;
}
void pu()
{
penUp();
}
void clearScreen()
{
cleardevice(myworld.world_image);
myturtle.x=myworld.origin_x;
myturtle.y=myworld.origin_y;
myturtle.orient=-90;
//prepareTurtleIcon();
refreshWorld();
}
void cs()
{
clearScreen();
}
void clear()
{
clearScreen();
}
void home()
{
int to_x,to_y;
to_x=myworld.origin_x;
to_y=myworld.origin_y;
if (myturtle.is_pen_down)
{
ege_line(myturtle.x*myworld.scale,myturtle.y*myworld.scale,to_x*myworld.scale,to_y*myworld.scale,myworld.world_image);
}
myturtle.x=to_x;
myturtle.y=to_y;
myturtle.orient=-90;
//prepareTurtleIcon();
refreshWorld();
}
void setPenSize(int size)
{
myturtle.pen_size=size;
setlinestyle(SOLID_LINE,0,size,myworld.world_image);
setlinewidth(size,myworld.world_image);
};
void setPenColor(color_t color)
{
myturtle.pen_color=color;
setcolor(color,myworld.world_image);
}
void setSpeed(int speed)
{
if (speed>=1)
{
myturtle.pen_speed=speed;
}
else
{
myturtle.pen_speed=1;
}
}
void setRewind(int isRewind)
{
myworld.rewind=isRewind;
}
void setImmediate(int isImmediate)
{
myworld.immediate=isImmediate;
}
void pause()
{
myworld.frame_count=0;
//prepareTurtleIcon();
displayWorld();
getch();
}
void show()
{
myturtle.is_show=TRUE;
refreshWorld();
}
void hide()
{
myturtle.is_show=FALSE;
refreshWorld();
}
void setXY(double x, double y)
{
myturtle.x=myworld.origin_x+x;
myturtle.y=myworld.origin_y-y;
refreshWorld();
}
double getX()
{
int cent_x;
cent_x=round(myworld.width/2);
return myturtle.x-cent_x;
}
double getY()
{
int cent_y;
cent_y=round(myworld.height/2);
return cent_y-myturtle.y;
}
void setAngle(double angle)
{
myturtle.orient=-angle;
//prepareTurtleIcon();
refreshWorld();
}
double getAngle()
{
return -myturtle.orient;
}
TurtleState getState()
{
TurtleState state;
state.x=getX();
state.y=getY();
state.angle=getAngle();
return state;
}
void setState(TurtleState state)
{
BOOL pd=myturtle.is_pen_down;
myturtle.is_pen_down=0;
setXY(state.x,state.y);
setAngle(state.angle);
myturtle.is_pen_down=pd;
}
void faceXY(double x,double y)
{
x=myworld.origin_x+x;
y=myworld.origin_y-y;
double delta_x=x-myturtle.x;
double delta_y=-(y-myturtle.y);
double angle=atan2(delta_y,delta_x)/M_PI*180;
turnTo(angle);
}
void turnTo(double angle)
{
double turn_angle;
turn_angle=-angle - myturtle.orient ;
while (turn_angle>180)
{
turn_angle=turn_angle-360;
}
while (turn_angle<-180)
{
turn_angle=360+turn_angle;
}
rt(turn_angle);
}
void gotoXY(double x, double y)
{
faceXY(x,y);
x=myworld.origin_x+x;
y=myworld.origin_y-y;
double delta_x=x-myturtle.x;
double delta_y=-(y-myturtle.y);
double step=sqrt(delta_x*delta_x+delta_y*delta_y);
fd(step);
myturtle.x=x;
myturtle.y=y;
//prepareTurtleIcon();
refreshWorld();
}
double randBetween(double start,double end)
{
return (end-start)*randomf()+start;
}
void setOrigin(int x, int y)
{
myworld.origin_x=myworld.origin_x+x;
myworld.origin_y=myworld.origin_y-y;
home();
clearScreen();
}
void setCaption(const char* title)
{
setcaption(title);
}
void setBackgroundImage(PIMAGE backImg)
{
if (backImg == NULL) {
myworld.background_image = myworld.default_background;
} else {
myworld.background_image = backImg;
}
}
int setBackgroundImage(const char* filename) {
ege::PIMAGE img = ege::newimage();
int result = ege::getimage(img,filename);
if (result!=ege::grOk)
return result;
setBackgroundImage(img);
return result;
}
int setBackgroundImage(const wchar_t* filename) {
ege::PIMAGE img = ege::newimage();
int result = ege::getimage(img,filename);
if (result!=ege::grOk)
return result;
setBackgroundImage(img);
return result;
}
void setBackgroundColor(ege::color_t color)
{
myworld.back_color = color;
setbkcolor(color,myworld.default_background);
}
void fill() {
int x = round(myturtle.x*myworld.scale);
int y = round(myturtle.y*myworld.scale);
int w = myworld.width * myworld.scale;
int h = myworld.height * myworld.scale;
color_t* buffer = getbuffer(myworld.world_image);
color_t c=buffer[y*w+x];
std::queue<int> points_x;
std::queue<int> points_y;
points_x.push(x);
points_y.push(y);
while (!points_x.empty()) {
int t_x = points_x.front();
int t_y = points_y.front();
points_x.pop();
points_y.pop();
if (t_x<0 || t_x >= w)
continue;
if (t_y<0 || t_y >= h)
continue;
if (buffer[t_y*w+t_x]!=c) {
continue;
}
buffer[t_y*w+t_x] = myturtle.pen_color;
points_x.push(t_x-1);
points_y.push(t_y);
points_x.push(t_x+1);
points_y.push(t_y);
points_x.push(t_x);
points_y.push(t_y-1);
points_x.push(t_x);
points_y.push(t_y+1);
}
}