-
Notifications
You must be signed in to change notification settings - Fork 1
/
byrne.mp
4263 lines (4027 loc) · 158 KB
/
byrne.mp
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
% byrne-euclid 0.2.5
% MetaPost library that implements most of the graphical
% features present in Oliver Byrne's version of Euclid's "Elements"
% Copyright 2024 Sergey Slyusarev
%
% 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 <http://www.gnu.org/licenses/>.
%
% Here we define some things of general interest
%
pi := 3.1415926;
radian := 180/pi;
lineWidth := 2pt;
lineWidthThin := 1pt;
lineWidthHair := 1/2pt;
pointMarkSize := 4pt;
pointLinesSize := 1/2cm;
defaultScaleFactor := 1;
angleSize := 1cm;
angleScale := 1;
globalRotation := 0;
magnitudeSize := 5/18cm;
magnitudeScale := 1;
magnitudeGap := 3/2lineWidth;
markLength := 3lineWidth;
rayExtension := 1/3cm;
textLabelScaleFactor := 2/5;
%
% Some symbolic constants for better readability
%
SOLID_LINE := 0;
DASHED_LINE := 1;
SOLID_SECTOR := 0;
ARC_SECTOR := 1;
DASHED_ARC_SECTOR := 2;
ALL_LABELS := 0;
OMIT_LABELS_AT_STRAIGHT_ANGLES := 1;
OMIT_FIRST_AND_LAST_LABEL := 2;
OMIT_FIRST_LABEL := 3;
OMIT_LAST_LABEL := 4;
REGULAR_WIDTH := 0;
THIN_WIDTH := 1;
HAIR_WIDTH := 2;
%
% Some common settings
%
boolean textLabels, ghostLines, autoRightAngles, omitDuplicateTextLabels, autoLabelingMode, mainPictureMode, omitSpaceRotation;
textLabels := false;
ghostLines := true;
autoRightAngles := false;
omitDuplicateTextLabels := false;
textLabelShift := lineWidth;
autoLabelingMode := false;
mainPictureMode := false;
omitSpaceRotation := false;
boolean compensateLineLabels, lineLabelsOnTop;
compensateLineLabels := true;
lineLabelsOnTop := true;
boolean solidAngleMode;
solidAngleMode := false;
boolean angleClockwiseMode, angleModeFallback;
angleClockwiseMode := false;
angleModeFallback := false;
numeric dpLength[];
dpLength0 := 2lineWidth;
dpLength1 := 2lineWidth;
dpLength2 := 3/2lineWidth;
vardef sin primary x = (sind(x*radian)) enddef;
vardef cos primary x = (cosd(x*radian)) enddef;
vardef arcsind primary x = angle((1+-+x,x)) enddef;
vardef arccosd primary x = angle((x,1+-+x)) enddef;
primarydef a xor b =
begingroup
if a:
b
else:
not b
fi
endgroup
enddef;
vardef pathToString (expr p) =
save outputString, endpoint, i;
string outputString;
if (length(p) > 0):
outputString :=
"(" & decimal(xpart(point 0 of p)) & ", " & decimal(ypart(point 0 of p)) & ")"
& ".. controls (" & decimal(xpart(postcontrol 0 of p)) & ", " & decimal(ypart(postcontrol 0 of p)) & ") and "
& "(" & decimal(xpart(precontrol 1 of p)) & ", " & decimal(ypart(precontrol 1 of p)) & ")";
if (cycle p):
endpoint := length(p) - 1;
else:
endpoint := length(p);
fi;
for i := 1 step 1 until endpoint:
outputString := outputString
& ".. (" & decimal(xpart(point i of p)) & ", " & decimal(ypart(point i of p)) & ")";
if ((i < length(p)) or (cycle p)):
outputString := outputString
& ".. controls (" & decimal(xpart(postcontrol i of p)) & ", " & decimal(ypart(postcontrol i of p)) & ") and "
& "(" & decimal(xpart(precontrol i + 1 of p)) & ", " & decimal(ypart(precontrol i + 1 of p)) & ")";
fi;
endfor;
if (cycle p):
outputString := outputString & " .. cycle";
fi;
else:
outputString :=
"(" & decimal(xpart(point 0 of p)) & ", " & decimal(ypart(point 0 of p)) & ")";
fi;
outputString
enddef;
color byred, byblue, byyellow, byblack, bygrey,
byredN, byblueN, byyellowN, byblackN, bygreyN,
outlineColor.byredN, outlineColor.byblueN, outlineColor.byyellowN, outlineColor.byblackN, outlineColor.bygreyN,
bytransparent, byDefaultAngleOptionalColor;
byred := (.85, .3, .1);
byblue := (.15, .35, .6);
byyellow := (.95, .7, 0.1);
byblack := black;
bygrey := (.8, .8, .8);
byredN := white;
byblueN := white;
byyellowN := white;
byblackN := white;
bygreyN := white;
outlineColor.byredN := byred;
outlineColor.byblueN := byblue;
outlineColor.byyellowN := byyellow;
outlineColor.byblackN := byblack;
outlineColor.bygreyN := bygrey;
bytransparent := 0.99white;
cmykcolor bylabelcolor;
bylabelcolor := (0, 0, 0, 1);
byDefaultAngleOptionalColor := white;
linecap := butt;
vardef typeOf (expr v) =
save determinedType;
string determinedType;
determinedType := "undefined";
if (boolean v):
determinedType := "boolean";
elseif (cmykcolor v):
determinedType := "cmykcolor";
elseif (color v):
determinedType := "color";
elseif (rgbcolor v):
determinedType := "rgbcolor";
elseif (numeric v):
determinedType := "numeric";
elseif (pair v):
determinedType := "pair";
elseif (path v):
determinedType := "path";
elseif (pen v):
determinedType := "pen";
elseif (picture v):
determinedType := "picture";
elseif (string v):
determinedType := "string";
elseif (transform v):
determinedType := "transform";
fi;
determinedType
enddef;
vardef sign@#(expr x) =
if str @# <> "mindZero":
if x > 0: 1 fi
if x < 0: -1 fi
if x = 0: 1 fi
else:
if x > 0: 1 fi
if x < 0: -1 fi
if x = 0: 0 fi
fi
enddef;
vardef colorLightness (expr col) =
save r, g, b;
if color col:
r := redpart(col);
g := greenpart(col);
b := bluepart(col);
elseif cmykcolor col:
r := (1 - cyanpart(col)) * (1 - blackpart(col));
g := (1 - magentapart(col)) * (1 - blackpart(col));
b := (1 - yellowpart(col)) * (1 - blackpart(col));
fi;
(0.2126*r) + (0.7152*g) + (0.0722*b) % from here https://en.wikipedia.org/wiki/Relative_luminance but it doesn't work this way; to be corrected
enddef;
vardef isWhite (expr col) =
if (colorLightness(col) = 1): % arbitrary value
true
else:
false
fi
enddef;
vardef isLight (expr col) =
if (colorLightness(col) > 0.95): % arbitrary value
true
else:
false
fi
enddef;
vardef defineColor@#(expr col) =
if (typeOf(col) = "color"):
color @#;
else:
cmykcolor @#;
fi;
@# := col;
enddef;
vardef selectOutlineColor@# =
if (typeOf(outlineColor.@#) = "color") or (typeOf(outlineColor.@#) = "cmykcolor"):
outlineColor.@#
else:
black
fi
enddef;
defineColor.oiBlack((0, 0, 0));
defineColor.oiOrange((0.9,0.6,0));
defineColor.oiSkyBlue((0.35,0.7,0.9));
defineColor.oiGreen((0,0.6,0.5));
defineColor.oiYellow((0.95,0.9,0.25));
defineColor.oiBlue((0,0.45,0.7));
defineColor.oiVermillion((0.8,0.4,0));
defineColor.oiPurple((0.8,0.6,0.7));
% CMYK variants
%defineColor.oiBlack((0,0,0,1));
%defineColor.oiOrange((0,0.5,1,0));
%defineColor.oiSkyBlue((0.8,0,0,0));
%defineColor.oiGreen((0.97,0,0.75,0));
%defineColor.oiYellow((0.1,0.05,0.9,0));
%defineColor.oiBlue((1,0.5,0,0));
%defineColor.oiVermillion((0,0.8,1,0));
%defineColor.oiPurple((0.1,0.7,0,0));
vardef cycleval (expr v, l) =
save rv;
numeric rv;
if (l > 0):
if ((v mod l) < 0):
rv := l - (v mod l);
else:
rv := (v mod l);
fi;
else:
rv := 0;
fi;
rv
enddef;
vardef angleOpticalScale (expr v) =
save va;
numeric va;
if (v > 60):
va := 60;
else:
va := v;
fi;
((va/60)**(-1/2))
enddef;
vardef distanceToLine (expr givenPoint, givenLine) =
save p;
pair p[];
p0 := point 0 of givenLine;
p1 := point 0 of reverse(givenLine);
p2 - givenPoint = whatever * ((p1-p0) rotated 90);
p2 = whatever[p0, p1];
p3 := p2;
arclength(givenPoint -- p3)
enddef;
vardef angleValue (expr a, b, c) =
save v;
numeric v;
v := angle(a-b) - angle(c-b);
if (v > 180): v := v - 360; fi;
if (v < -180): v := v + 360; fi;
v
enddef;
vardef lineThickness (expr th) =
if (th = 0):
lineWidth
elseif (th = 1):
lineWidthThin
elseif (th = 2):
lineWidthHair
else:
lineWidth
fi
enddef;
vardef byDashPattern (expr l, dp, endsType) =
save d, n, p;
picture p;
numeric d[], n;
n := ceiling(l/(1/3dpLength[dp]));
if (n mod 3) <> 2:
n := n - (n mod 3) + 2;
fi;
d1 := 2(l/n);
d2 := l/n;
p := dashpattern (on d1 off d2);
if (endsType = 1):
n := n - (n mod 3);
d1 := 2(l/n);
d2 := l/n;
p := dashpattern (on d1 off d2);
elseif (endsType = 2):
n := n - (n mod 3);
d1 := 2(l/n);
d2 := l/n;
p := dashpattern (off d2 on d1);
elseif (endsType = 3):
if (n mod 3) <> 1:
n := n - (n mod 3) + 1;
fi;
d1 := 2(l/n);
d2 := l/n;
p := dashpattern (off d2 on d1);
fi;
p
enddef;
def startTempScale (expr tmpScale) =
begingroup
save scaleFactor;
scaleFactor := tmpScale;
enddef;
def stopTempScale =
endgroup
enddef;
def startTempAngleScale (expr tmpAngleScale) =
begingroup
save angleScale;
angleScale := tmpAngleScale;
enddef;
def stopTempAngleScale =
endgroup
enddef;
def startGlobalRotation (expr ang) =
begingroup
save globalRotation;
globalRotation := ang;
enddef;
def stopGlobalRotation =
endgroup
enddef;
def startAutoLabeling =
begingroup
save autoLabelingMode;
boolean autoLabelingMode;
autoLabelingMode := true;
enddef;
def stopAutoLabeling =
endgroup
enddef;
def startAngleOppositeMode =
begingroup
save newAngleMode;
boolean oldAngleMode;
oldAngleMode := angleClockwiseMode;
save angleClockwiseMode;
boolean angleClockwiseMode;
angleClockwiseMode := not oldAngleMode;
enddef;
def stopAngleOppositeMode =
endgroup
enddef;
def startOffspringPictureMode =
begingroup
save uniqueTextLabels, omitDuplicateTextLabels;
string uniqueTextLabels;
boolean omitDuplicateTextLabels;
omitDuplicateTextLabels := true;
uniqueTextLabels := "";
enddef;
def stopOffspringPictureMode =
endgroup
enddef;
def startMainPictureMode =
begingroup
save mainPictureMode, ghostPicture;
boolean mainPictureMode;
picture ghostPicture;
ghostPicture := image();
mainPictureMode := true;
enddef;
def stopMainPictureMode =
endgroup
enddef;
picture ghostPicture;
ghostPicture := image();
%
% This section is devoted to lists
%
vardef appendList (suffix listName)(expr valueToAdd, whereToAdd, omitDuplicates) =
if (not string listName) or (unknown listName):
string listName;
listName := "";
fi;
if length(listName) = 0:
listName := valueToAdd;
else:
save valueExists;
boolean valueExists;
valueExists := false;
if omitDuplicates:
valueExists := isInList (valueToAdd, listName);
fi;
if not valueExists:
if (whereToAdd = 1):
listName := listName & ", " & valueToAdd;
else:
listName := valueToAdd & ", " & listName;
fi;
fi;
fi;
enddef;
vardef reverseList (expr listToReverse) =
save reversedList, rv;
string reversedList, rv;
reversedList := "";
forsuffixes i=scantokens(listToReverse):
appendList(reversedList, str i, 0, false);
endfor;
rv := reversedList;
rv
enddef;
vardef isInList (expr valueToLookFor)(suffix listName) =
save rv, i;
boolean rv;
rv := false;
if (not string listName) or (unknown listName):
string listName;
listName := "";
fi;
forsuffixes i=scantokens(listName):
if (str i = valueToLookFor):
rv := true;
fi;
endfor;
rv
enddef;
vardef setAttribute@#(expr attrGenus, attrSpecies, attrName, attrValue) =
save finName;
string finName;
if not string scantokens(attrGenus & "Synonym").scantokens(attrName):
finName := attrName;
else:
finName := scantokens(attrGenus & "Synonym").scantokens(attrName);
if (boolean scantokens(attrGenus & "SynonymPartial").scantokens(attrName)):
if scantokens(attrGenus & "SynonymPartial").scantokens(attrName):
finName := attrName;
fi;
fi;
fi;
if str @# = "":
scantokens(typeOf(attrValue)) scantokens(attrGenus & attrSpecies).scantokens(finName);
elseif (typeOf(scantokens(attrGenus & attrSpecies).scantokens(finName)0) <> typeOf(attrValue)):
scantokens(typeOf(attrValue)) scantokens(attrGenus & attrSpecies).scantokens(finName)[];
fi;
scantokens(attrGenus & attrSpecies).scantokens(finName)@# := attrValue;
enddef;
vardef getAttribute@#(expr attrGenus, attrSpecies, attrName) =
save finName;
string finName;
if not string scantokens(attrGenus & "Synonym").scantokens(attrName):
finName := attrName;
else:
finName := scantokens(attrGenus & "Synonym").scantokens(attrName);
if (boolean scantokens(attrGenus & "SynonymPartial").scantokens(attrName)):
if scantokens(attrGenus & "SynonymPartial").scantokens(attrName):
if known scantokens(attrGenus & attrSpecies).scantokens(attrName)@#:
finName := attrName;
fi;
fi;
fi;
fi;
scantokens(attrGenus & attrSpecies).scantokens(finName)@#
enddef;
vardef attributeExists (expr attrGenus, attrSpecies, attrName) =
save rv;
boolean rv;
if known scantokens(attrGenus & attrSpecies).scantokens(attrName):
rv := true;
else:
rv := false;
fi;
rv
enddef;
setAttribute("line", "Color", "noLine", white);
setAttribute("line", "Dashed", "noLine", 0);
setAttribute("line", "EndAName", "noLine", "noLine");
setAttribute("line", "EndBName", "noLine", "noLine");
setAttribute("line", "EndA", "noLine", inf);
setAttribute("line", "EndB", "noLine", inf);
setAttribute("line", "Synonym", "noLine", "noLine");
setAttribute("line", "Color", "dummyLine", bygrey);
setAttribute("line", "Dashed", "dummyLine", 0);
setAttribute("line", "Thin", "dummyLine", 1);
%
% Some general 3D stuff
%
color projectionAngle;
projectionAngle := (0, 0, 0);
color rotationAxisB.X, rotationAxisE.X, rotationAxisB.Y, rotationAxisE.Y, rotationAxisB.Z, rotationAxisE.Z;
string rotationAxisPart.X, rotationAxisPart.Y, rotationAxisPart.Z;
rotationAxisB.X := (0, 0, 0);
rotationAxisE.X := (1, 0, 0);
rotationAxisPart.X := "redpart";
rotationAxisB.Y := (0, 0, 0);
rotationAxisE.Y := (0, 1, 0);
rotationAxisPart.Y := "greenpart";
rotationAxisB.Z := (0, 0, 0);
rotationAxisE.Z := (0, 0, 1);
rotationAxisPart.Z := "bluepart";
primarydef somecolor colorrotated someangle =
rotateColor(somecolor, someangle)
enddef;
% Rotation order can be selected like this rotateColor.XYZ,
% or by setting defaultRotationOrder
string defaultRotationOrder;
defaultRotationOrder := "ZYX";
vardef rotateColor@#(expr somecolor, someangle) =
save rotationOrder, argumentString, rv;
string rotationOrder[], argumentString;
color rv;
argumentString := str @#;
if argumentString = "":
argumentString := defaultRotationOrder;
fi;
for i := 0 step 1 until length(argumentString) - 1:
rotationOrder[i] = substring (i, i + 1) of argumentString;
endfor;
rv := somecolor;
rv := byRotateAroundAxis(
rotationAxisB.scantokens(rotationOrder0),
rotationAxisE.scantokens(rotationOrder0),
scantokens(rotationAxisPart.scantokens(rotationOrder0))(someangle), rv);
rv := byRotateAroundAxis(
rotationAxisB.scantokens(rotationOrder1),
rotationAxisE.scantokens(rotationOrder1),
scantokens(rotationAxisPart.scantokens(rotationOrder1))(someangle), rv);
rv := byRotateAroundAxis(
rotationAxisB.scantokens(rotationOrder2),
rotationAxisE.scantokens(rotationOrder2),
scantokens(rotationAxisPart.scantokens(rotationOrder2))(someangle), rv);
rv
enddef;
primarydef colorone crossproduct colortwo =
begingroup
save xp, yp, zp;
numeric xp[], yp[], zp[];
xp1 := (redpart colorone)/cm;
yp1 := (greenpart colorone)/cm;
zp1 := (bluepart colorone)/cm;
xp2 := (redpart colortwo)/cm;
yp2 := (greenpart colortwo)/cm;
zp2 := (bluepart colortwo)/cm;
xp3 := ((yp1*zp2) - (zp1*yp2))*cm;
yp3 := ((zp1*xp2) - (xp1*zp2))*cm;
zp3 := ((xp1*yp2) - (yp1*xp2))*cm;
(xp3, yp3, zp3)
endgroup
enddef;
primarydef colorone dotprodXYZ colortwo =
begingroup
save xp, yp, zp;
numeric xp[], yp[], zp[];
xp1 := (redpart colorone);
yp1 := (greenpart colorone);
zp1 := (bluepart colorone);
xp2 := (redpart colortwo);
yp2 := (greenpart colortwo);
zp2 := (bluepart colortwo);
xp1*xp2 + yp1*yp2 + zp1*zp2
endgroup
enddef;
vardef isInPlane (expr colPoint, colPlaneA, colPlaneB, colPlaneC) =
save vecsToCompare, rv;
color vecsToCompare[];
boolean rv;
rv := false;
if (colPoint = colPlaneA)
or (colPoint = colPlaneB)
or (colPoint = colPlaneC):
rv := true;
else:
vecsToCompare1 := colPlaneA-colPlaneB;
vecsToCompare2 := colPlaneC-colPlaneB;
vecsToCompare3 := colPoint-colPlaneB;
fi;
if not rv:
if ((absXYZ(unitvectorXYZ(vecsToCompare1) - unitvectorXYZ(vecsToCompare3)) < 1/100)
or (absXYZ(unitvectorXYZ(vecsToCompare1) + unitvectorXYZ(vecsToCompare3)) < 1/100)
or (absXYZ(unitvectorXYZ(vecsToCompare2) - unitvectorXYZ(vecsToCompare3)) < 1/100)
or (absXYZ(unitvectorXYZ(vecsToCompare2) + unitvectorXYZ(vecsToCompare3)) < 1/100)):
rv := true;
else:
vecsToCompare4 := unitvectorXYZ(vecsToCompare1 crossproduct vecsToCompare2);
vecsToCompare5 := unitvectorXYZ(vecsToCompare3 crossproduct vecsToCompare2);
if ((absXYZ(vecsToCompare4 - vecsToCompare5) < 1/100)
or (absXYZ(vecsToCompare4 + vecsToCompare5) < 1/100)):
rv := true;
else:
rv := false;
fi;
fi;
fi;
rv
enddef;
vardef unitvectorXYZ (expr somecolor) =
save vecLength;
numeric vecLength;
vecLength := absXYZ(somecolor);
if (vecLength > 0):
somecolor/vecLength
else:
errmessage("Can't construct a unit vector from a zero-length vector");
fi
enddef;
vardef absXYZ (expr somecolor) =
(sqrt(
((redpart somecolor)/1cm)**2
+ ((greenpart somecolor)/1cm)**2
+ ((bluepart somecolor)/1cm)**2)*1cm)
enddef;
vardef lineAndPlaneIntersection (expr lineA, lineB, planeA, planeB, planeC) =
save planeNormal, planeOrigin, lineD;
color planeNormal, planeOrigin;
numeric lineD;
planeNormal := normalToPlane(planeA, planeB, planeC);
planeOrigin := planeB;
lineD := ((planeNormal/cm) dotprodXYZ ((planeOrigin - lineA)/cm))
/((planeNormal/cm) dotprodXYZ ((lineB - lineA)/cm));
lineA + ((lineB-lineA)*lineD)
enddef;
vardef normalToPlane (expr planeA, planeB, planeC) =
unitvectorXYZ((planeA-planeB) crossproduct (planeC-planeB))
enddef;
vardef spaceRotated (suffix somepath)(expr someangle) =
save rv, tv, spacepoint, op, rp, tp;
path rv, tv;
color spacepoint[];
pair op[], rp[], tp[];
if (typeOf(thirdDimension.somepath) <> "path"):
path thirdDimension.somepath;
thirdDimension.somepath := somepath scaled 0;
fi;
for i := 0 step 1 until length(somepath):
op0 := point i of somepath;
spacepoint0 :=
(xpart(op0), ypart(op0),
xpart(point i of thirdDimension.somepath)
) colorrotated someangle;
rp0 := (redpart(spacepoint0), greenpart(spacepoint0));
tp0 := (bluepart(spacepoint0), 0);
if (i = 0):
rv := rp0;
tv := tp0;
else:
op1 := postcontrol (i-1) of somepath;
spacepoint1 :=
(xpart(op1), ypart(op1),
xpart(postcontrol (i-1) of thirdDimension.somepath)
) colorrotated someangle;
rp1 := (redpart(spacepoint1), greenpart(spacepoint1));
tp1 := (bluepart(spacepoint1), 0);
op2 := precontrol i of somepath;
spacepoint2 :=
(xpart(op2), ypart(op2),
xpart(precontrol i of thirdDimension.somepath)
) colorrotated someangle;
rp2 := (redpart(spacepoint2), greenpart(spacepoint2));
tp2 := (bluepart(spacepoint2), 0);
rv := rv .. controls rp1 and rp2 ..
if ((i < length(somepath)) or (not cycle somepath)):
rp0
else:
cycle
fi;
tv := tv .. controls tp1 and tp2 ..
if ((i < length(somepath)) or (not cycle somepath)):
tp0
else:
cycle
fi;
fi;
endfor;
thirdDimension.somepath := tv;
rv
enddef;
vardef bySetProjection@#(expr xa, ya, za) =
projectionAngle := (xa, ya, za);
byRotatePoints@#(xa, ya, za, false)();
if str @# <> "":
defaultRotationOrder := str @#;
fi;
enddef;
vardef byPutPointsInSpace (text pL) =
byRotatePoints(0, 0, 0, true)(pL);
enddef;
% Gets a either a 2d or a 3d point and returns a 3d point
vardef byGetPointXYZ (suffix givenPoint) =
if (typeOf(givenPoint) = "color"):
givenPoint
elseif (typeOf(pointXYZ.givenPoint) = "color"):
pointXYZ.givenPoint
else:
errmessage("Point " & str givenPoint & " has no representation in space");
fi
enddef;
vardef byRotateAroundAxis (expr lineEndA, lineEndB, a, pointA) =
save vecA, vecB, rV, q;
color vecA, vecB, rV;
cmykcolor q[];
vecA := pointA - lineEndA;
vecB := unitvectorXYZ(lineEndB - lineEndA);
q1 := (0, redpart(vecA), greenpart(vecA), bluepart(vecA));
q2 := (cosd(a/2), sind(a/2)*redpart(vecB), sind(a/2)*greenpart(vecB), sind(a/2)*bluepart(vecB));
q3 := (cyanpart(q2), -magentapart(q2), -yellowpart(q2), -blackpart(q2));
q4 := (q2 hamiltonProduct q1) hamiltonProduct q3;
rV := (magentapart(q4), yellowpart(q4), blackpart(q4)) + lineEndA;
rV
enddef;
vardef byRotatePointsAroundAxis (suffix lineEndA, lineEndB)(expr a)(text pointsList) =
save lineEndAxyz, lineEndBxyz, pN;
color lineEndAxyz, lineEndBxyz;
if (typeOf(lineEndA) = "color"):
lineEndAxyz := lineEndA;
else:
byPutPointsInSpace(lineEndA);
lineEndAxyz := pointXYZ.lineEndA;
fi;
if (typeOf(lineEndB) = "color"):
lineEndBxyz := lineEndB;
else:
byPutPointsInSpace(lineEndB);
lineEndBxyz := pointXYZ.lineEndB;
fi;
forsuffixes pN := pointsList:
if (typeOf(pN) = "color"):
pN := byRotateAroundAxis(lineEndAxyz, lineEndBxyz, a, pN);
elseif (typeOf(pN) = "pair"):
byPutPointsInSpace(pN);
pointXYZ.pN := byRotateAroundAxis(lineEndAxyz, lineEndBxyz, a, pointXYZ.pN);
fi;
endfor;
enddef;
primarydef colorone hamiltonProduct colortwo =
begingroup
save a, b, c, d;
numeric a[], b[], c[], d[];
a1 := cyanpart(colorone);
b1 := magentapart(colorone);
c1 := yellowpart(colorone);
d1 := blackpart(colorone);
a2 := cyanpart(colortwo);
b2 := magentapart(colortwo);
c2 := yellowpart(colortwo);
d2 := blackpart(colortwo);
(
(a1*a2) - (b1*b2) - (c1*c2) - (d1*d2),
(a1*b2) + (b1*a2) + (c1*d2) - (d1*c2),
(a1*c2) - (b1*d2) + (c1*a2) + (d1*b2),
(a1*d2) + (b1*c2) - (c1*b2) + (d1*a2)
)
endgroup
enddef;
vardef isRightAngle (suffix a, b, c) =
save d, rv;
numeric d;
boolean rv;
if (attributeExists("point", "XYZ", str a)
and attributeExists("point", "XYZ", str b)
and attributeExists("point", "XYZ", str c)):
d := unitvectorXYZ(pointXYZ.a - pointXYZ.b) dotprodXYZ unitvectorXYZ(pointXYZ.c - pointXYZ.b);
else:
d := unitvector(a - b) dotprod unitvector(c - b);
fi;
if (abs(d) < 1/1000):
rv := true;
else:
rv := false;
fi;
rv
enddef;
%
% This section is dedicated to lines
%
vardef byLineRender (expr a, b, col, dp, th, c, d, ct, dt, dpt, s, sf) =
save p, la, lineItself, lineBleeding, clippingPath, cutAngle, linePerp, lineStyShift, currentLineWidth, clippingPathPoint, aS, bS;
picture p;
path lineItself, lineBleeding, clippingPath;
pair cutAngle[], linePerp, lineStyShift, clippingPathPoint[], aS, bS;
la := angle(b-a);
aS := a*sf;
bS := b*sf;
lineStyShift := (0, 0);
currentLineWidth := lineThickness(th);
cutAngle1 := unitvector(b-a) rotated -90;
if (abs(c-a) > 0):
if (abs(unitvector(c-a)-unitvector(a-b)) > 1/100):
if (ct = 0):
cutAngle1 := unitvector(1/2[unitvector(b-a), unitvector(c-a)]);
elseif (ct = 1):
cutAngle1 := unitvector(1[unitvector(b-a), unitvector(c-a)]);
fi;
fi;
fi;
cutAngle2 := unitvector(a-b) rotated 90;
if (abs(d-b) > 0):
if (abs(unitvector(d-b)-unitvector(b-a)) > 1/100):
if (dt = 0):
cutAngle2 := unitvector(1/2[unitvector(a-b), unitvector(d-b)]);
elseif (dt = 1):
cutAngle2 := unitvector(1[unitvector(a-b), unitvector(d-b)]);
fi;
fi;
fi;
if (sign(ypart(cutAngle2 rotated -la)) <> sign(ypart(cutAngle1 rotated -la))):
cutAngle2 := cutAngle2 rotated 180;
fi;
lineItself := (a--b) scaled sf;
linePerp := unitvector((point 0 of lineItself)-(point 1 of lineItself)) rotated 90;
lineBleeding :=
((point 0 of lineItself) shifted (unitvector((point 0 of lineItself) - (point 1 of lineItself)) scaled 2dpLength[dp])) --
lineItself -- ((point 1 of lineItself) shifted (unitvector((point 1 of lineItself) - (point 0 of lineItself)) scaled 2dpLength[dp]));
lineStyShift := (linePerp scaled (1/2currentLineWidth*s));
p := image(
pickup pencircle scaled currentLineWidth;
if (dp > 0):
draw (lineItself shifted lineStyShift) withcolor white;
draw (lineItself shifted lineStyShift) withcolor col
dashed byDashPattern(arclength(lineItself), dp, dpt);
if ((dpt = 0) or (dpt = 1)):
draw (subpath(0, 1) of lineBleeding shifted lineStyShift) withcolor col;
fi;
if ((dpt = 0) or (dpt = 2)):
draw (subpath(2, 3) of lineBleeding shifted lineStyShift) withcolor col;
fi;
else:
draw (lineBleeding shifted lineStyShift) withcolor col;
fi;
if (th < 0):
draw (lineBleeding shifted (lineStyShift + (linePerp scaled 1/4currentLineWidth))) withpen pencircle scaled 1/2currentLineWidth withcolor black;
fi;
);
clippingPathPoint1
= whatever[aS shifted (linePerp*1/2lineWidth) shifted lineStyShift,
bS shifted (linePerp*1/2lineWidth) shifted lineStyShift]
= whatever[aS, aS shifted cutAngle1];
clippingPathPoint2
= whatever[aS shifted (linePerp*-1/2lineWidth) shifted lineStyShift,
bS shifted (linePerp*-1/2lineWidth) shifted lineStyShift]
= whatever[aS, aS shifted cutAngle1];
clippingPathPoint3
= whatever[aS shifted (linePerp*-1/2lineWidth) shifted lineStyShift,
bS shifted (linePerp*-1/2lineWidth) shifted lineStyShift]
= whatever[bS, bS shifted cutAngle2];
clippingPathPoint4
= whatever[aS shifted (linePerp*1/2lineWidth) shifted lineStyShift,
bS shifted (linePerp*1/2lineWidth) shifted lineStyShift]
= whatever[bS, bS shifted cutAngle2];
clippingPath := clippingPathPoint1 -- clippingPathPoint2 -- clippingPathPoint3 -- clippingPathPoint4 -- cycle;
clip p to clippingPath;
if mainPictureMode and ghostLines:
save gP;
picture gP;
gP := ghostPicture;
ghostPicture := image(
draw gP;
begingroup;
save ghostLines;
boolean ghostLines;
ghostLines := false;
draw byLineRender (a, b, white, dp, 2, c, d, ct, dt, dpt, s, sf);
% draw byLineRender (a, b, col, dp, 2, c, d, ct, dt, dpt, s, sf);
endgroup;
);
fi;
p rotated globalRotation
enddef;
vardef byReturnLineLength (suffix a, b) =
if byIsPointInSpace(a, b):
absXYZ(pointXYZ.a - pointXYZ.b)
else:
abs(a-b)
fi
enddef;
vardef byLineDefine@#(suffix a, b)(expr col, dp, th) =
if str @# = "":
byLineDefine.scantokens(str a & str b)(a, b)(col, dp, th);
else:
save lA;
if mainPictureMode:
appendList(allLinesList, str @#, 1, true);
fi;
if byIsPointInSpace(a, b):
if (typeOf(a) = "pair") and (typeOf(b) = "pair"):
if (a <> b):
lA := angle(b-a);