-
Notifications
You must be signed in to change notification settings - Fork 34
/
graphics.c
2365 lines (2090 loc) · 56.3 KB
/
graphics.c
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
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* graphics.c logo graphics module mak
*
* Copyright (C) 1993 by the Regents of the University of California
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#ifdef WIN32
#include <windows.h>
#endif /* WIN32 */
#define WANT_EVAL_REGS 1
#include <math.h>
#include "logo.h"
#include "eval.h"
/* #include "globals.h" has been moved further down */
#ifdef HAVE_WX
#include "wxGraphics.h"
#elif defined(mac)
#include "macterm.h"
#elif defined(WIN32)
#include "win32trm.h"
#elif defined(__RZTC__)
#include <fg.h>
#include "ztcterm.h"
#elif defined(x_window)
#include "xgraphics.h"
#elif defined(ibm)
#include "ibmterm.h"
#else
#include "nographics.h"
#endif /* end this whole big huge tree */
#include "globals.h"
#ifdef HAVE_WX
int drawToPrinter=0;
int turtlePosition_x=0;
int turtlePosition_y=0;
extern void wx_adjust_label_height();
#endif
#if defined(__RZTC__) && !defined(WIN32) /* sowings */
#define total_turtle_bottom_max (-(MaxY/2))
#else
#define total_turtle_bottom_max turtle_bottom_max
#endif
/* types of graphics moves that can be recorded */
#define FINISHED 0 /* must be zero */
#define LINEXY 1
#define MOVEXY 2
#define LABEL 3
#define SETPENVIS 4
#define SETPENMODE 5
#define SETPENCOLOR 6
#define SETPENSIZE 7
#define SETPENPATTERN 8
#define FILLERUP 9
#define ARC 10
#define SETPENRGB 11
#define NEXTBUFFER 12
#define STARTFILL 13
#define ENDFILL 14
#define COLORFILL 15
/* NOTE: See the files (macterm.c and macterm.h) or (ibmterm.c and ibmterm.h)
for examples of the functions and macros that this file assumes exist. */
#define One (sizeof(void *))
#define Two (2*One)
#define Three (3*One)
#define Four (4*One)
#define Big (sizeof(FLONUM))
#define PENMODE_PAINT 0
#define PENMODE_ERASE 1
#define PENMODE_REVERSE 2
int internal_penmode = PENMODE_PAINT;
int drawing_turtle = 0;
enum s_md screen_mode = SCREEN_TEXT;
mode_type current_mode = wrapmode;
FLONUM turtle_x = 0.0, turtle_y = 0.0, turtle_heading = 0.0;
FLONUM x_scale = 1.0, y_scale = 1.0;
BOOLEAN turtle_shown = FALSE, user_turtle_shown = TRUE;
int graphics_setup = 0;
FLONUM wanna_x = 0.0, wanna_y = 0.0;
BOOLEAN out_of_bounds = FALSE;
void setpos_bynumber(FLONUM, FLONUM);
void internal_hideturtle(void);
int max_palette_slot = 0;
char record_buffer[GR_SIZE];
char *record = 0;
FIXNUM record_index = 0;
int last_recorded = -1;
pen_info orig_pen;
BOOLEAN refresh_p = TRUE;
BOOLEAN doing_filled = FALSE;
/************************************************************/
double pfmod(double x, double y) {
double temp = fmod(x,y);
if (temp < 0) return temp+y;
return temp;
}
FLONUM cut_error(FLONUM n) {
n *= 1000000;
n = (n > 0 ? floor(n) : ceil(n));
n /= 1000000;
if (n == -0.0) n = 0.0;
return(n);
}
FIXNUM g_round(FLONUM n) {
n += (n < 0.0 ? -0.5 : 0.5);
if ((n < 0.0 ? -n : n) > MAXLOGOINT)
err_logo(TURTLE_OUT_OF_BOUNDS, NIL);
if (n < 0.0)
return((FIXNUM)ceil(n));
return((FIXNUM)floor(n));
}
/************************************************************/
void draw_turtle_helper(void);
void check_x_high(void);
void check_x_low(void);
void forward(FLONUM);
void draw_turtle(void) {
unsigned int r=0, g=0, b=0;
int old_color=pen_color;
int save_vis;
#ifdef HAVE_WX
// Support for XOR drawing in wxWidgets is currently platform
// dependent and likely to go away in the future.
// See: http://trac.wxwidgets.org/ticket/18050
//
// So, drawing the turtle is handled manually at the wx level.
return;
#endif
if (!turtle_shown) {
if (!graphics_setup) {
graphics_setup++;
turtle_shown = TRUE;
}
return;
}
drawing_turtle = 1;
turtle_shown = 0;
/*
get_palette(pen_color, &r, &g, &b);
if (r==0 && g==0 && b==0) {
set_pen_color(7);
}
*/
save_vis=pen_vis;
pen_vis = -1;
forward(-1);
draw_turtle_helper();
/* all that follows is for "turtle wrap" effect */
if ((turtle_y > turtle_top_max - turtle_height) &&
(current_mode == wrapmode)) {
turtle_y -= (screen_height + 1);
draw_turtle_helper();
check_x_high();
check_x_low();
turtle_y += (screen_height + 1);
}
if ((turtle_y < turtle_bottom_max + turtle_height) &&
(current_mode == wrapmode)) {
turtle_y += (screen_height + 1);
draw_turtle_helper();
check_x_high();
check_x_low();
turtle_y -= (screen_height + 1);
}
check_x_high();
check_x_low();
turtle_shown = 0;
set_pen_color(old_color);
forward(1);
set_pen_vis(save_vis);
turtle_shown = 1;
drawing_turtle = 0;
}
void check_x_high(void) {
if ((turtle_x > turtle_right_max - turtle_height) &&
(current_mode == wrapmode)) {
turtle_x -= (screen_width + 1);
draw_turtle_helper();
turtle_x += (screen_width + 1);
}
}
void check_x_low(void) {
if ((turtle_x < turtle_left_max + turtle_height) &&
(current_mode == wrapmode)) {
turtle_x += (screen_width + 1);
draw_turtle_helper();
turtle_x -= (screen_width + 1);
}
}
void draw_turtle_helper(void) {
pen_info saved_pen;
FLONUM real_heading;
int left_x, left_y, right_x, right_y, top_x, top_y;
#if 1 /* Evan Marshall Manning <[email protected]> */
double cos_real_heading, sin_real_heading;
FLONUM delta_x, delta_y;
#endif
prepare_to_draw;
prepare_to_draw_turtle;
save_pen(&saved_pen);
plain_xor_pen();
pen_vis = 0;
set_pen_width(1);
set_pen_height(1);
real_heading = -turtle_heading + 90.0;
cos_real_heading = cos((FLONUM)(real_heading*degrad));
sin_real_heading = sin((FLONUM)(real_heading*degrad));
delta_x = x_scale*(FLONUM)(sin_real_heading*turtle_half_bottom);
delta_y = y_scale*(FLONUM)(cos_real_heading*turtle_half_bottom);
left_x = g_round(turtle_x - delta_x);
left_y = g_round(turtle_y + delta_y);
right_x = g_round(turtle_x + delta_x);
right_y = g_round(turtle_y - delta_y);
top_x = g_round(turtle_x + x_scale*(FLONUM)(cos_real_heading*turtle_side));
top_y = g_round(turtle_y + y_scale*(FLONUM)(sin_real_heading*turtle_side));
/* move to right, draw to left, draw to top, draw to right */
move_to(screen_x_center + right_x, screen_y_center - right_y);
line_to(screen_x_center + left_x, screen_y_center - left_y);
line_to(screen_x_center + top_x, screen_y_center - top_y);
line_to(screen_x_center + right_x, screen_y_center - right_y);
restore_pen(&saved_pen);
done_drawing_turtle;
done_drawing;
}
/************************************************************/
void forward_helper(FLONUM);
BOOLEAN safe_to_save(void);
void save_line(void), save_move(void), save_vis(void), save_mode(void);
void save_color(void), save_size(void), save_pattern(void);
void save_string(char *, int);
void save_arc(FLONUM, FLONUM, FLONUM, FLONUM, FLONUM, FLONUM, FLONUM, FLONUM);
void right(FLONUM a) {
prepare_to_draw;
draw_turtle();
turtle_heading += a;
turtle_heading = pfmod(turtle_heading,360.0);
draw_turtle();
done_drawing;
}
NODE *numeric_arg(NODE *args) {
NODE *arg = car(args), *val;
val = cnv_node_to_numnode(arg);
while (val == UNBOUND && NOT_THROWING) {
setcar(args, err_logo(BAD_DATA, arg));
arg = car(args);
val = cnv_node_to_numnode(arg);
}
setcar(args,val);
return(val);
}
NODE *lright(NODE *arg) {
NODE *val;
FLONUM a;
val = numeric_arg(arg);
if (NOT_THROWING) {
if (nodetype(val) == INT)
a = (FLONUM)getint(val);
else
a = getfloat(val);
right(a);
}
return(UNBOUND);
}
NODE *lleft(NODE *arg) {
NODE *val;
FLONUM a;
val = numeric_arg(arg);
if (NOT_THROWING) {
if (nodetype(val) == INT)
a = (FLONUM)getint(val);
else
a = getfloat(val);
right(-a);
}
return(UNBOUND);
}
FLONUM wrap_right(FLONUM, FLONUM, FLONUM, FLONUM, FLONUM);
FLONUM wrap_left(FLONUM, FLONUM, FLONUM, FLONUM, FLONUM);
FLONUM wrap_up(FLONUM, FLONUM, FLONUM, FLONUM, FLONUM);
FLONUM wrap_down(FLONUM, FLONUM, FLONUM, FLONUM, FLONUM);
void forward(FLONUM d) {
// #ifndef WIN32
internal_hideturtle();
// #endif
prepare_to_draw;
draw_turtle();
forward_helper(d);
draw_turtle();
done_drawing;
wanna_x = turtle_x;
wanna_y = turtle_y;
out_of_bounds = FALSE;
}
void forward_helper(FLONUM d) {
FLONUM real_heading, dx, dy, x1, y1, x2, y2, newd = 0.0;
FIXNUM rx2, ry2;
wraploop:
if (newd != 0.0) d = newd;
real_heading = -turtle_heading + 90.0;
x1 = screen_x_coord;
y1 = screen_y_coord;
dx = (FLONUM)(cos((FLONUM)(real_heading*degrad))*d*x_scale);
dy = (FLONUM)(sin((FLONUM)(real_heading*degrad))*d*y_scale);
if ((dx < 0 && dx > -0.000001) || (dx > 0 && dx < 0.000001)) dx = 0;
if ((dy < 0 && dy > -0.000001) || (dy > 0 && dy < 0.000001)) dy = 0;
x2 = x1 + dx;
y2 = y1 - dy;
move_to(g_round(x1), g_round(y1));
save_move();
if (!drawing_turtle && check_throwing) return;
if (internal_penmode == PENMODE_REVERSE && pen_vis == 0 && d > 0.0) {
line_to(g_round(x1), g_round(y1)); /* flip the corner */
save_line();
}
rx2 = g_round(x2);
ry2 = g_round(y2);
if (!drawing_turtle && check_throwing) return;
if (current_mode == windowmode ||
(rx2 >= screen_left && rx2 <= screen_right &&
ry2 >= screen_top && ry2 <= screen_bottom)) {
turtle_x = turtle_x + dx;
turtle_y = turtle_y + dy;
line_to(rx2, ry2);
save_line();
} else {
if (rx2 > screen_right && g_round(x1) == screen_right) {
move_to(screen_left, g_round(y1));
turtle_x = turtle_left_max;
goto wraploop;
}
if (ry2 < screen_top && g_round(y1) == screen_top) {
move_to(g_round(x1), screen_bottom);
turtle_y = turtle_bottom_max;
goto wraploop;
}
if (rx2 < screen_left && g_round(x1) == screen_left) {
move_to(screen_right, g_round(y1));
turtle_x = turtle_right_max;
goto wraploop;
}
if (ry2 > screen_bottom && g_round(y1) == screen_bottom) {
move_to(g_round(x1), screen_top);
turtle_y = turtle_top_max;
goto wraploop;
}
if ((newd = wrap_right(d, x1, y1, x2, y2)) != 0.0) goto wraploop;
if ((newd = wrap_left(d, x1, y1, x2, y2)) != 0.0) goto wraploop;
if ((newd = wrap_up(d, x1, y1, x2, y2)) != 0.0) goto wraploop;
if ((newd = wrap_down(d, x1, y1, x2, y2)) != 0.0) goto wraploop;
}
if (internal_penmode == PENMODE_REVERSE && pen_vis == 0 && d < 0.0) {
line_to(g_round(screen_x_coord), g_round(screen_y_coord));
save_line();
}
}
FLONUM wrap_right(FLONUM d, FLONUM x1, FLONUM y1, FLONUM x2, FLONUM y2) {
FLONUM yi, newd;
FIXNUM ryi;
if (x2 > screen_right + 0.5) {
yi = ((y2 - y1)/(x2 - x1)) * (screen_right + 1 - x1) + y1;
ryi = g_round(yi);
if (ryi >= screen_top && ryi <= screen_bottom) {
line_to(screen_right, ryi);
save_line();
turtle_x = turtle_left_max;
turtle_y = screen_y_center - yi;
if (current_mode == wrapmode) {
newd = d * ((x2 - screen_right - 1)/(x2 - x1));
if (newd*d > 0) return(newd);
} else {
turtle_x = turtle_right_max;
err_logo(TURTLE_OUT_OF_BOUNDS, NIL);
}
}
}
return(0.0);
}
FLONUM wrap_left(FLONUM d, FLONUM x1, FLONUM y1, FLONUM x2, FLONUM y2) {
FLONUM yi, newd;
FIXNUM ryi;
if (x2 < screen_left - 0.5) {
yi = ((y1 - y2)/(x2 - x1)) * (x1 + 1 - screen_left) + y1;
ryi = g_round(yi);
if (ryi >= screen_top && ryi <= screen_bottom) {
line_to(screen_left, ryi);
save_line();
turtle_x = turtle_right_max;
turtle_y = screen_y_center - yi;
if (current_mode == wrapmode) {
newd = d * ((x2 + 1 - screen_left)/(x2 - x1));
if (newd*d > 0) return(newd);
} else {
turtle_x = turtle_left_max;
err_logo(TURTLE_OUT_OF_BOUNDS, NIL);
}
}
}
return(0.0);
}
FLONUM wrap_up(FLONUM d, FLONUM x1, FLONUM y1, FLONUM x2, FLONUM y2) {
FLONUM xi, newd;
FIXNUM rxi;
if (y2 < screen_top - 0.5) {
xi = ((x2 - x1)/(y1 - y2)) * (y1 + 1 - screen_top) + x1;
rxi = g_round(xi);
if (rxi >= screen_left && rxi <= screen_right) {
line_to(rxi, screen_top);
save_line();
turtle_x = xi - screen_x_center;
turtle_y = turtle_bottom_max;
if (current_mode == wrapmode) {
newd = d * ((y2 + 1 - screen_top)/(y2 - y1));
if (newd*d > 0) return(newd);
} else {
turtle_y = turtle_top_max;
err_logo(TURTLE_OUT_OF_BOUNDS, NIL);
}
} else if (rxi >= (screen_left-1) && rxi <= (screen_right+1)) {
rxi = (rxi > screen_right ? screen_right : screen_left);
line_to(rxi, screen_top);
save_line();
turtle_x = xi - screen_x_center;
turtle_y = turtle_bottom_max;
if (current_mode == wrapmode) {
newd = d * ((y2 + 1 - screen_top)/(y2 - y1));
if (newd*d > 0) return(newd);
} else {
turtle_y = turtle_top_max;
err_logo(TURTLE_OUT_OF_BOUNDS, NIL);
}
}
}
return(0.0);
}
FLONUM wrap_down(FLONUM d, FLONUM x1, FLONUM y1, FLONUM x2, FLONUM y2) {
FLONUM xi, newd;
FIXNUM rxi;
if (y2 > screen_bottom + 0.5) {
xi = ((x2 - x1)/(y2 - y1)) * (screen_bottom + 1 - y1) + x1;
rxi = g_round(xi);
if (rxi >= screen_left && rxi <= screen_right) {
line_to(rxi, screen_bottom);
save_line();
turtle_x = xi - screen_x_center;
turtle_y = turtle_top_max;
if (current_mode == wrapmode) {
newd = d * ((y2 - screen_bottom - 1)/(y2 - y1));
if (newd*d > 0) return(newd);
} else {
turtle_y = turtle_bottom_max;
err_logo(TURTLE_OUT_OF_BOUNDS, NIL);
}
} else if (rxi >= (screen_left-1) && rxi <= (screen_right+1)) {
rxi = (rxi > screen_right ? screen_right : screen_left);
line_to(rxi, screen_bottom);
save_line();
turtle_x = xi - screen_x_center;
turtle_y = turtle_top_max;
if (current_mode == wrapmode) {
newd = d * ((y2 - screen_bottom - 1)/(y2 - y1));
if (newd*d > 0) return(newd);
} else {
turtle_y = turtle_bottom_max;
err_logo(TURTLE_OUT_OF_BOUNDS, NIL);
}
}
}
return(0.0);
}
FLONUM get_number(NODE *arg) {
NODE *val = numeric_arg(arg);
if (NOT_THROWING) {
if (nodetype(val) == INT)
return (FLONUM)getint(val);
else
return getfloat(val);
} else return 0.0;
}
NODE *lforward(NODE *arg) {
FLONUM d = get_number(arg);
if (NOT_THROWING) {
forward(d);
}
return(UNBOUND);
}
NODE *lback(NODE *arg) {
FLONUM d = get_number(arg);
if (NOT_THROWING) {
forward(-d);
}
return(UNBOUND);
}
NODE *lshowturtle(NODE *args) {
if(!graphics_setup) graphics_setup++;
prepare_to_draw;
if (!turtle_shown) {
turtle_shown = TRUE;
draw_turtle();
}
done_drawing;
user_turtle_shown = TRUE;
return(UNBOUND);
}
void internal_hideturtle() {
if(!graphics_setup) graphics_setup++;
prepare_to_draw;
if (turtle_shown) {
draw_turtle();
turtle_shown = FALSE;
}
done_drawing;
}
NODE *lhideturtle(NODE *args) {
internal_hideturtle();
user_turtle_shown = FALSE;
return(UNBOUND);
}
void fix_turtle_shownness() {
if (graphics_setup
#if !defined(x_window) || defined(HAVE_WX)
&& screen_mode != SCREEN_TEXT
#endif
) {
if(user_turtle_shown)
(void)lshowturtle(NIL);
}
}
NODE *lshownp(NODE *args) {
return(user_turtle_shown ? TrueName() : FalseName());
}
NODE *lsetheading(NODE *arg) {
NODE *val;
val = numeric_arg(arg);
if (NOT_THROWING) {
prepare_to_draw;
draw_turtle();
if (nodetype(val) == INT)
turtle_heading = (FLONUM)getint(val);
else
turtle_heading = getfloat(val);
turtle_heading = pfmod(turtle_heading,360.0);
draw_turtle();
done_drawing;
}
return(UNBOUND);
}
NODE *lheading(NODE *args) {
return(make_floatnode(turtle_heading));
}
NODE *vec_arg_helper(NODE *args, BOOLEAN floatok, BOOLEAN three) {
NODE *arg = car(args), *val1, *val2, *val3 = NIL;
while (NOT_THROWING) {
if (arg != NIL &&
is_list(arg) &&
cdr(arg) != NIL &&
(three ? (cddr(arg) != NIL && cdr(cddr(arg)) == NIL) : cddr(arg) == NIL)) {
val1 = cnv_node_to_numnode(car(arg));
val2 = cnv_node_to_numnode(cadr(arg));
if (three) val3 = cnv_node_to_numnode(car(cddr(arg)));
if (val1 != UNBOUND && val2 != UNBOUND &&
(floatok || (nodetype(val1) == INT && getint(val1) >= 0 &&
nodetype(val2) == INT && getint(val2) >= 0 &&
(!three || (nodetype(val3) == INT && getint(val3) >= 0))))) {
setcar(arg, val1);
setcar(cdr(arg), val2);
if (three) setcar (cddr(arg), val3);
return(arg);
}
}
setcar(args, err_logo(BAD_DATA, arg));
arg = car(args);
}
return(UNBOUND);
}
NODE *vector_arg(NODE *args) {
return vec_arg_helper(args,TRUE,FALSE);
}
NODE *pos_int_vector_arg(NODE *args) {
return vec_arg_helper(args,FALSE,FALSE);
}
NODE *rgb_arg(NODE *args) {
return vec_arg_helper(args,TRUE,TRUE);
}
FLONUM towards_helper(FLONUM x, FLONUM y, FLONUM from_x, FLONUM from_y) {
FLONUM m, a, tx, ty;
tx = from_x/x_scale;
ty = from_y/y_scale;
if (x != tx || y != ty) {
if (x == tx)
a = (y < ty) ? -90 : 90;
else {
m = (y - ty)/(x - tx);
a = atan(m)/degrad;
if (x < tx) a = fmod(a + 180.0,360.0);
}
a = -(a - 90.0);
return (a < 0 ? 360.0+a : a);
}
return 0.0;
}
NODE *ltowards(NODE *args) {
NODE *xnode, *ynode = UNBOUND, *arg;
FLONUM x, y;
arg = vector_arg(args);
if (NOT_THROWING) {
xnode = car(arg);
ynode = cadr(arg);
x = ((nodetype(xnode) == FLOATT) ? getfloat(xnode) :
(FLONUM)getint(xnode));
y = ((nodetype(ynode) == FLOATT) ? getfloat(ynode) :
(FLONUM)getint(ynode));
return make_floatnode(towards_helper(x, y, turtle_x, turtle_y));
}
return(UNBOUND);
}
NODE *lpos(NODE *args) {
return(cons(make_floatnode(cut_error(turtle_x/x_scale)),
cons(make_floatnode(cut_error(turtle_y/y_scale)), NIL)));
}
NODE *lscrunch(NODE *args) {
return(cons(make_floatnode(x_scale), cons(make_floatnode(y_scale), NIL)));
}
NODE *lhome(NODE *args) {
prepare_to_draw;
out_of_bounds = FALSE;
setpos_bynumber((FLONUM)0.0, (FLONUM)0.0);
draw_turtle();
turtle_heading = 0.0;
draw_turtle();
done_drawing;
return(UNBOUND);
}
void cs_helper(int centerp) {
#if defined(x_window) && !defined(HAVE_WX)
clearing_screen++;
#endif
prepare_to_draw;
clear_screen;
#if defined(x_window) && !defined(HAVE_WX)
clearing_screen==0;
#endif
if (centerp) {
wanna_x = wanna_y = turtle_x = turtle_y = turtle_heading = 0.0;
out_of_bounds = FALSE;
move_to(screen_x_coord, screen_y_coord);
}
if (!graphics_setup) {
graphics_setup++;
turtle_shown = TRUE;
}
draw_turtle();
save_pen(&orig_pen);
p_info_x(orig_pen) = g_round(screen_x_coord);
p_info_y(orig_pen) = g_round(screen_y_coord);
(void)safe_to_save();
record = record_buffer;
record_index = One;
last_recorded = -1;
record[record_index] = FINISHED;
if (turtle_x != 0.0 || turtle_y != 0.0) save_move();
if (pen_vis != 0) save_vis();
if (internal_penmode != PENMODE_PAINT) save_mode();
if (pen_color != 7) save_color();
if (pen_width != 1 || pen_height != 1) save_size();
done_drawing;
}
NODE *lclearscreen(NODE *args) {
cs_helper(TRUE);
return(UNBOUND);
}
NODE *lclean(NODE *args) {
cs_helper(FALSE);
return(UNBOUND);
}
void setpos_commonpart(FLONUM target_x, FLONUM target_y) {
FLONUM scaled_x, scaled_y, tx, ty, save_heading;
BOOLEAN wrapping = FALSE;
if (NOT_THROWING) {
scaled_x = target_x * x_scale;
scaled_y = target_y * y_scale;
wrapping = scaled_x > turtle_right_max || scaled_x < turtle_left_max ||
scaled_y > turtle_top_max || scaled_y < turtle_bottom_max;
if (current_mode == fencemode && wrapping)
err_logo(TURTLE_OUT_OF_BOUNDS, NIL);
else if (current_mode == wrapmode && (wrapping || out_of_bounds)) {
save_heading = turtle_heading;
turtle_heading = towards_helper(target_x, target_y,
wanna_x, wanna_y);
tx = wanna_x/x_scale;
ty = wanna_y/y_scale;
forward_helper(hypot(target_x - tx, target_y - ty));
turtle_heading = save_heading;
wanna_x = scaled_x;
wanna_y = scaled_y;
out_of_bounds = wrapping;
}
else {
out_of_bounds = FALSE;
wanna_x = turtle_x = scaled_x;
wanna_y = turtle_y = scaled_y;
line_to(g_round(screen_x_coord),
g_round(screen_y_coord));
save_line();
}
draw_turtle();
done_drawing;
}
}
void setpos_bynumber(FLONUM target_x, FLONUM target_y) {
if (NOT_THROWING) {
prepare_to_draw;
draw_turtle();
move_to(g_round(screen_x_coord), g_round(screen_y_coord));
setpos_commonpart(target_x, target_y);
done_drawing;
}
}
void setpos_helper(NODE *xnode, NODE *ynode) {
FLONUM target_x, target_y;
if (NOT_THROWING) {
internal_hideturtle();
prepare_to_draw;
draw_turtle();
move_to(g_round(screen_x_coord), g_round(screen_y_coord));
target_x = ((xnode == NIL) ?
turtle_x/x_scale :
((nodetype(xnode) == FLOATT) ? getfloat(xnode) :
(FLONUM)getint(xnode)));
target_y = ((ynode == NIL) ?
turtle_y/y_scale :
((nodetype(ynode) == FLOATT) ? getfloat(ynode) :
(FLONUM)getint(ynode)));
setpos_commonpart(target_x, target_y);
done_drawing;
}
}
NODE *lsetpos(NODE *args) {
NODE *arg = vector_arg(args);
if (NOT_THROWING) {
setpos_helper(car(arg), cadr(arg));
}
return(UNBOUND);
}
NODE *lsetxy(NODE *args) {
NODE *xnode, *ynode;
xnode = numeric_arg(args);
ynode = numeric_arg(cdr(args));
if (NOT_THROWING) {
setpos_helper(xnode, ynode);
}
return(UNBOUND);
}
NODE *lsetx(NODE *args) {
NODE *xnode;
xnode = numeric_arg(args);
if (NOT_THROWING) {
setpos_helper(xnode, NIL);
}
return(UNBOUND);
}
NODE *lsety(NODE *args) {
NODE *ynode;
ynode = numeric_arg(args);
if (NOT_THROWING) {
setpos_helper(NIL, ynode);
}
return(UNBOUND);
}
NODE *lwrap(NODE *args) {
prepare_to_draw;
draw_turtle();
current_mode = wrapmode;
while (turtle_x > turtle_right_max) {
turtle_x -= screen_width;
}
while (turtle_x < turtle_left_max) {
turtle_x += screen_width;
}
while (turtle_y > turtle_top_max) {
turtle_y -= screen_height;
}
while (turtle_y < turtle_bottom_max) {
turtle_y += screen_height;
}
move_to(screen_x_coord, screen_y_coord);
draw_turtle();
done_drawing;
return(UNBOUND);
}
NODE *lfence(NODE *args) {
(void)lwrap(args); /* get turtle inside the fence */
prepare_to_draw;
draw_turtle();
current_mode = fencemode;
draw_turtle();
done_drawing;
return(UNBOUND);
}
NODE *lwindow(NODE *args) {
prepare_to_draw;
draw_turtle();
current_mode = windowmode;
draw_turtle();
done_drawing;
return(UNBOUND);
}
NODE *lturtlemode(NODE *args) {
switch (current_mode) {
case wrapmode: return(theName(Name_wrap));
case fencemode: return(theName(Name_fence));
case windowmode: return(theName(Name_window));
}
return(UNBOUND); /* Can't get here, but makes compiler happy */
}
NODE *lfill(NODE *args) {
prepare_to_draw;
draw_turtle();
logofill();
draw_turtle();
if (safe_to_save()) {
save_move();
last_recorded = record[record_index] = FILLERUP;
record_index += One;
record[record_index] = FINISHED;
}
done_drawing;
return(UNBOUND);
}
NODE *llabel(NODE *arg) {
char textbuf[300];
short theLength;
char *old_stringptr = print_stringptr;
int old_stringlen = print_stringlen;
print_stringptr = textbuf;
print_stringlen = 300;
ndprintf((FILE *)NULL,"%p",car(arg));
*print_stringptr = '\0';
print_stringptr = old_stringptr;
print_stringlen = old_stringlen;
if (NOT_THROWING) {
prepare_to_draw;
draw_turtle();
theLength = strlen(textbuf);
#ifdef mac
c_to_pascal_string(textbuf, theLength);
#endif
label(textbuf);
save_string(textbuf,theLength);
draw_turtle();
done_drawing;
}
return(UNBOUND);
}
NODE *ltextscreen(NODE *args) {
text_screen;
screen_mode = SCREEN_TEXT;
return(UNBOUND);
}
NODE *lsplitscreen(NODE *args) {
if (!graphics_setup) {
graphics_setup++;
// turtle_shown = TRUE;
}
split_screen;