Debian Codegraph @

texmf-dist/metapost/byrne/byrne.mp

raw
1 % byrne-euclid 0.2.5
2 % MetaPost library that implements most of the graphical
3 % features present in Oliver Byrne's version of Euclid's "Elements"
4 % Copyright 2025 Sergey Slyusarev
5 %
6 % This program is free software: you can redistribute it and/or modify
7 % it under the terms of the GNU General Public License as published by
8 % the Free Software Foundation, either version 3 of the License, or
9 % (at your option) any later version.
10 %
11 % This program is distributed in the hope that it will be useful,
12 % but WITHOUT ANY WARRANTY; without even the implied warranty of
13 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 % GNU General Public License for more details.
15 %
16 % You should have received a copy of the GNU General Public License
17 % along with this program. If not, see <http://www.gnu.org/licenses/>.
18
19 %
20 % Here we define some things of general interest
21 %
22
23 pi := 3.1415926;
24 radian := 180/pi;
25 lineWidth := 2pt;
26 lineWidthThin := 1pt;
27 lineWidthHair := 1/2pt;
28 pointMarkSize := 4pt;
29 pointLinesSize := 1/2cm;
30 defaultScaleFactor := 1;
31 angleSize := 1cm;
32 angleScale := 1;
33 globalRotation := 0;
34 magnitudeSize := 5/18cm;
35 magnitudeScale := 1;
36 magnitudeGap := 3/2lineWidth;
37 markLength := 3lineWidth;
38 rayExtension := 1/3cm;
39 textLabelScaleFactor := 2/5;
40
41 %
42 % Some symbolic constants for better readability
43 %
44
45 SOLID_LINE := 0;
46 DASHED_LINE := 1;
47
48 SOLID_SECTOR := 0;
49 ARC_SECTOR := 1;
50 DASHED_ARC_SECTOR := 2;
51
52 ALL_LABELS := 0;
53 OMIT_LABELS_AT_STRAIGHT_ANGLES := 1;
54 OMIT_FIRST_AND_LAST_LABEL := 2;
55 OMIT_FIRST_LABEL := 3;
56 OMIT_LAST_LABEL := 4;
57
58 REGULAR_WIDTH := 0;
59 THIN_WIDTH := 1;
60 HAIR_WIDTH := 2;
61
62 %
63 % Some common settings
64 %
65
66 boolean textLabels, ghostLines, autoRightAngles, omitDuplicateTextLabels, autoLabelingMode, mainPictureMode, omitSpaceRotation;
67
68 textLabels := false;
69 ghostLines := true;
70 autoRightAngles := false;
71 omitDuplicateTextLabels := false;
72 textLabelShift := lineWidth;
73 autoLabelingMode := false;
74 mainPictureMode := false;
75 omitSpaceRotation := false;
76
77 boolean compensateLineLabels, lineLabelsOnTop;
78
79 compensateLineLabels := true;
80 lineLabelsOnTop := true;
81
82 boolean solidAngleMode;
83
84 solidAngleMode := false;
85
86 boolean angleClockwiseMode, angleModeFallback;
87
88 angleClockwiseMode := false;
89 angleModeFallback := false;
90
91 numeric dpLength[];
92 dpLength0 := 2lineWidth;
93 dpLength1 := 2lineWidth;
94 dpLength2 := 3/2lineWidth;
95
96 vardef sin primary x = (sind(x*radian)) enddef;
97
98 vardef cos primary x = (cosd(x*radian)) enddef;
99
100 vardef arcsind primary x = angle((1+-+x,x)) enddef;
101
102 vardef arccosd primary x = angle((x,1+-+x)) enddef;
103
104 primarydef a xor b =
105 begingroup
106 if a:
107 b
108 else:
109 not b
110 fi
111 endgroup
112 enddef;
113
114 vardef pathToString (expr p) =
115 save outputString, endpoint, i;
116 string outputString;
117 if (length(p) > 0):
118 outputString :=
119 "(" & decimal(xpart(point 0 of p)) & ", " & decimal(ypart(point 0 of p)) & ")"
120 & ".. controls (" & decimal(xpart(postcontrol 0 of p)) & ", " & decimal(ypart(postcontrol 0 of p)) & ") and "
121 & "(" & decimal(xpart(precontrol 1 of p)) & ", " & decimal(ypart(precontrol 1 of p)) & ")";
122 if (cycle p):
123 endpoint := length(p) - 1;
124 else:
125 endpoint := length(p);
126 fi;
127 for i := 1 step 1 until endpoint:
128 outputString := outputString
129 & ".. (" & decimal(xpart(point i of p)) & ", " & decimal(ypart(point i of p)) & ")";
130 if ((i < length(p)) or (cycle p)):
131 outputString := outputString
132 & ".. controls (" & decimal(xpart(postcontrol i of p)) & ", " & decimal(ypart(postcontrol i of p)) & ") and "
133 & "(" & decimal(xpart(precontrol i + 1 of p)) & ", " & decimal(ypart(precontrol i + 1 of p)) & ")";
134 fi;
135 endfor;
136 if (cycle p):
137 outputString := outputString & " .. cycle";
138 fi;
139 else:
140 outputString :=
141 "(" & decimal(xpart(point 0 of p)) & ", " & decimal(ypart(point 0 of p)) & ")";
142 fi;
143 outputString
144 enddef;
145
146 color byred, byblue, byyellow, byblack, bygrey,
147 byredN, byblueN, byyellowN, byblackN, bygreyN,
148 outlineColor.byredN, outlineColor.byblueN, outlineColor.byyellowN, outlineColor.byblackN, outlineColor.bygreyN,
149 bytransparent, byDefaultAngleOptionalColor;
150 byred := (.85, .3, .1);
151 byblue := (.15, .35, .6);
152 byyellow := (.95, .7, 0.1);
153 byblack := black;
154 bygrey := (.8, .8, .8);
155 byredN := white;
156 byblueN := white;
157 byyellowN := white;
158 byblackN := white;
159 bygreyN := white;
160 outlineColor.byredN := byred;
161 outlineColor.byblueN := byblue;
162 outlineColor.byyellowN := byyellow;
163 outlineColor.byblackN := byblack;
164 outlineColor.bygreyN := bygrey;
165 bytransparent := 0.99white;
166
167 cmykcolor bylabelcolor;
168 bylabelcolor := (0, 0, 0, 1);
169
170 byDefaultAngleOptionalColor := white;
171
172 linecap := butt;
173
174 vardef typeOf (expr v) =
175 save determinedType;
176 string determinedType;
177 determinedType := "undefined";
178 if (boolean v):
179 determinedType := "boolean";
180 elseif (cmykcolor v):
181 determinedType := "cmykcolor";
182 elseif (color v):
183 determinedType := "color";
184 elseif (rgbcolor v):
185 determinedType := "rgbcolor";
186 elseif (numeric v):
187 determinedType := "numeric";
188 elseif (pair v):
189 determinedType := "pair";
190 elseif (path v):
191 determinedType := "path";
192 elseif (pen v):
193 determinedType := "pen";
194 elseif (picture v):
195 determinedType := "picture";
196 elseif (string v):
197 determinedType := "string";
198 elseif (transform v):
199 determinedType := "transform";
200 fi;
201 determinedType
202 enddef;
203
204 vardef sign@#(expr x) =
205 if str @# <> "mindZero":
206 if x > 0: 1 fi
207 if x < 0: -1 fi
208 if x = 0: 1 fi
209 else:
210 if x > 0: 1 fi
211 if x < 0: -1 fi
212 if x = 0: 0 fi
213 fi
214 enddef;
215
216 vardef colorLightness (expr col) =
217 save r, g, b;
218 if color col:
219 r := redpart(col);
220 g := greenpart(col);
221 b := bluepart(col);
222 elseif cmykcolor col:
223 r := (1 - cyanpart(col)) * (1 - blackpart(col));
224 g := (1 - magentapart(col)) * (1 - blackpart(col));
225 b := (1 - yellowpart(col)) * (1 - blackpart(col));
226 fi;
227 (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
228 enddef;
229
230 vardef isWhite (expr col) =
231 if (colorLightness(col) = 1): % arbitrary value
232 true
233 else:
234 false
235 fi
236 enddef;
237
238 vardef isLight (expr col) =
239 if (colorLightness(col) > 0.95): % arbitrary value
240 true
241 else:
242 false
243 fi
244 enddef;
245
246 vardef defineColor@#(expr col) =
247 if (typeOf(col) = "color"):
248 color @#;
249 else:
250 cmykcolor @#;
251 fi;
252 @# := col;
253 enddef;
254
255 vardef selectOutlineColor@# =
256 if (typeOf(outlineColor.@#) = "color") or (typeOf(outlineColor.@#) = "cmykcolor"):
257 outlineColor.@#
258 else:
259 black
260 fi
261 enddef;
262
263 defineColor.oiBlack((0, 0, 0));
264 defineColor.oiOrange((0.9,0.6,0));
265 defineColor.oiSkyBlue((0.35,0.7,0.9));
266 defineColor.oiGreen((0,0.6,0.5));
267 defineColor.oiYellow((0.95,0.9,0.25));
268 defineColor.oiBlue((0,0.45,0.7));
269 defineColor.oiVermillion((0.8,0.4,0));
270 defineColor.oiPurple((0.8,0.6,0.7));
271
272 % CMYK variants
273 %defineColor.oiBlack((0,0,0,1));
274 %defineColor.oiOrange((0,0.5,1,0));
275 %defineColor.oiSkyBlue((0.8,0,0,0));
276 %defineColor.oiGreen((0.97,0,0.75,0));
277 %defineColor.oiYellow((0.1,0.05,0.9,0));
278 %defineColor.oiBlue((1,0.5,0,0));
279 %defineColor.oiVermillion((0,0.8,1,0));
280 %defineColor.oiPurple((0.1,0.7,0,0));
281
282 vardef cycleval (expr v, l) =
283 save rv;
284 numeric rv;
285 if (l > 0):
286 if ((v mod l) < 0):
287 rv := l - (v mod l);
288 else:
289 rv := (v mod l);
290 fi;
291 else:
292 rv := 0;
293 fi;
294 rv
295 enddef;
296
297 vardef angleOpticalScale (expr v) =
298 save va;
299 numeric va;
300 if (v > 60):
301 va := 60;
302 else:
303 va := v;
304 fi;
305 ((va/60)**(-1/2))
306 enddef;
307
308 vardef distanceToLine (expr givenPoint, givenLine) =
309 save p;
310 pair p[];
311 p0 := point 0 of givenLine;
312 p1 := point 0 of reverse(givenLine);
313 p2 - givenPoint = whatever * ((p1-p0) rotated 90);
314 p2 = whatever[p0, p1];
315 p3 := p2;
316 arclength(givenPoint -- p3)
317 enddef;
318
319 vardef angleValue (expr a, b, c) =
320 save v;
321 numeric v;
322 v := angle(a-b) - angle(c-b);
323 if (v > 180): v := v - 360; fi;
324 if (v < -180): v := v + 360; fi;
325 v
326 enddef;
327
328 vardef lineThickness (expr th) =
329 if (th = 0):
330 lineWidth
331 elseif (th = 1):
332 lineWidthThin
333 elseif (th = 2):
334 lineWidthHair
335 else:
336 lineWidth
337 fi
338 enddef;
339
340 vardef byDashPattern (expr l, dp, endsType) =
341 save d, n, p;
342 picture p;
343 numeric d[], n;
344 n := ceiling(l/(1/3dpLength[dp]));
345 if (n mod 3) <> 2:
346 n := n - (n mod 3) + 2;
347 fi;
348 d1 := 2(l/n);
349 d2 := l/n;
350 p := dashpattern (on d1 off d2);
351 if (endsType = 1):
352 n := n - (n mod 3);
353 d1 := 2(l/n);
354 d2 := l/n;
355 p := dashpattern (on d1 off d2);
356 elseif (endsType = 2):
357 n := n - (n mod 3);
358 d1 := 2(l/n);
359 d2 := l/n;
360 p := dashpattern (off d2 on d1);
361 elseif (endsType = 3):
362 if (n mod 3) <> 1:
363 n := n - (n mod 3) + 1;
364 fi;
365 d1 := 2(l/n);
366 d2 := l/n;
367 p := dashpattern (off d2 on d1);
368 fi;
369 p
370 enddef;
371
372 def startTempScale (expr tmpScale) =
373 begingroup
374 save scaleFactor;
375 scaleFactor := tmpScale;
376 enddef;
377
378 def stopTempScale =
379 endgroup
380 enddef;
381
382 def startTempAngleScale (expr tmpAngleScale) =
383 begingroup
384 save angleScale;
385 angleScale := tmpAngleScale;
386 enddef;
387
388 def stopTempAngleScale =
389 endgroup
390 enddef;
391
392 def startGlobalRotation (expr ang) =
393 begingroup
394 save globalRotation;
395 globalRotation := ang;
396 enddef;
397
398 def stopGlobalRotation =
399 endgroup
400 enddef;
401
402 def startAutoLabeling =
403 begingroup
404 save autoLabelingMode;
405 boolean autoLabelingMode;
406 autoLabelingMode := true;
407 enddef;
408
409 def stopAutoLabeling =
410 endgroup
411 enddef;
412
413 def startAngleOppositeMode =
414 begingroup
415 save newAngleMode;
416 boolean oldAngleMode;
417 oldAngleMode := angleClockwiseMode;
418 save angleClockwiseMode;
419 boolean angleClockwiseMode;
420 angleClockwiseMode := not oldAngleMode;
421 enddef;
422
423 def stopAngleOppositeMode =
424 endgroup
425 enddef;
426
427 def startOffspringPictureMode =
428 begingroup
429 save uniqueTextLabels, omitDuplicateTextLabels;
430 string uniqueTextLabels;
431 boolean omitDuplicateTextLabels;
432 omitDuplicateTextLabels := true;
433 uniqueTextLabels := "";
434 enddef;
435
436 def stopOffspringPictureMode =
437 endgroup
438 enddef;
439
440 def startMainPictureMode =
441 begingroup
442 save mainPictureMode, ghostPicture;
443 boolean mainPictureMode;
444 picture ghostPicture;
445 ghostPicture := image();
446 mainPictureMode := true;
447 enddef;
448
449 def stopMainPictureMode =
450 endgroup
451 enddef;
452
453 picture ghostPicture;
454 ghostPicture := image();
455
456 %
457 % This section is devoted to lists
458 %
459
460 vardef appendList (suffix listName)(expr valueToAdd, whereToAdd, omitDuplicates) =
461 if (not string listName) or (unknown listName):
462 string listName;
463 listName := "";
464 fi;
465 if length(listName) = 0:
466 listName := valueToAdd;
467 else:
468 save valueExists;
469 boolean valueExists;
470 valueExists := false;
471 if omitDuplicates:
472 valueExists := isInList (valueToAdd, listName);
473 fi;
474 if not valueExists:
475 if (whereToAdd = 1):
476 listName := listName & ", " & valueToAdd;
477 else:
478 listName := valueToAdd & ", " & listName;
479 fi;
480 fi;
481 fi;
482 enddef;
483
484 vardef reverseList (expr listToReverse) =
485 save reversedList, rv;
486 string reversedList, rv;
487 reversedList := "";
488 forsuffixes i=scantokens(listToReverse):
489 appendList(reversedList, str i, 0, false);
490 endfor;
491 rv := reversedList;
492 rv
493 enddef;
494
495 vardef isInList (expr valueToLookFor)(suffix listName) =
496 save rv, i;
497 boolean rv;
498 rv := false;
499 if (not string listName) or (unknown listName):
500 string listName;
501 listName := "";
502 fi;
503 forsuffixes i=scantokens(listName):
504 if (str i = valueToLookFor):
505 rv := true;
506 fi;
507 endfor;
508 rv
509 enddef;
510
511 vardef setAttribute@#(expr attrGenus, attrSpecies, attrName, attrValue) =
512 save finName;
513 string finName;
514 if not string scantokens(attrGenus & "Synonym").scantokens(attrName):
515 finName := attrName;
516 else:
517 finName := scantokens(attrGenus & "Synonym").scantokens(attrName);
518 if (boolean scantokens(attrGenus & "SynonymPartial").scantokens(attrName)):
519 if scantokens(attrGenus & "SynonymPartial").scantokens(attrName):
520 finName := attrName;
521 fi;
522 fi;
523 fi;
524 if str @# = "":
525 scantokens(typeOf(attrValue)) scantokens(attrGenus & attrSpecies).scantokens(finName);
526 elseif (typeOf(scantokens(attrGenus & attrSpecies).scantokens(finName)0) <> typeOf(attrValue)):
527 scantokens(typeOf(attrValue)) scantokens(attrGenus & attrSpecies).scantokens(finName)[];
528 fi;
529 scantokens(attrGenus & attrSpecies).scantokens(finName)@# := attrValue;
530 enddef;
531
532 vardef getAttribute@#(expr attrGenus, attrSpecies, attrName) =
533 save finName;
534 string finName;
535 if not string scantokens(attrGenus & "Synonym").scantokens(attrName):
536 finName := attrName;
537 else:
538 finName := scantokens(attrGenus & "Synonym").scantokens(attrName);
539 if (boolean scantokens(attrGenus & "SynonymPartial").scantokens(attrName)):
540 if scantokens(attrGenus & "SynonymPartial").scantokens(attrName):
541 if known scantokens(attrGenus & attrSpecies).scantokens(attrName)@#:
542 finName := attrName;
543 fi;
544 fi;
545 fi;
546 fi;
547 scantokens(attrGenus & attrSpecies).scantokens(finName)@#
548 enddef;
549
550 vardef attributeExists (expr attrGenus, attrSpecies, attrName) =
551 save rv;
552 boolean rv;
553 if known scantokens(attrGenus & attrSpecies).scantokens(attrName):
554 rv := true;
555 else:
556 rv := false;
557 fi;
558 rv
559 enddef;
560
561 setAttribute("line", "Color", "noLine", white);
562 setAttribute("line", "Dashed", "noLine", 0);
563 setAttribute("line", "EndAName", "noLine", "noLine");
564 setAttribute("line", "EndBName", "noLine", "noLine");
565 setAttribute("line", "EndA", "noLine", inf);
566 setAttribute("line", "EndB", "noLine", inf);
567 setAttribute("line", "Synonym", "noLine", "noLine");
568
569 setAttribute("line", "Color", "dummyLine", bygrey);
570 setAttribute("line", "Dashed", "dummyLine", 0);
571 setAttribute("line", "Thin", "dummyLine", 1);
572
573 %
574 % Some general 3D stuff
575 %
576
577 color projectionAngle;
578 projectionAngle := (0, 0, 0);
579
580 color rotationAxisB.X, rotationAxisE.X, rotationAxisB.Y, rotationAxisE.Y, rotationAxisB.Z, rotationAxisE.Z;
581 string rotationAxisPart.X, rotationAxisPart.Y, rotationAxisPart.Z;
582 rotationAxisB.X := (0, 0, 0);
583 rotationAxisE.X := (1, 0, 0);
584 rotationAxisPart.X := "redpart";
585 rotationAxisB.Y := (0, 0, 0);
586 rotationAxisE.Y := (0, 1, 0);
587 rotationAxisPart.Y := "greenpart";
588 rotationAxisB.Z := (0, 0, 0);
589 rotationAxisE.Z := (0, 0, 1);
590 rotationAxisPart.Z := "bluepart";
591
592 primarydef somecolor colorrotated someangle =
593 rotateColor(somecolor, someangle)
594 enddef;
595
596 % Rotation order can be selected like this rotateColor.XYZ,
597 % or by setting defaultRotationOrder
598
599 string defaultRotationOrder;
600 defaultRotationOrder := "ZYX";
601
602 vardef rotateColor@#(expr somecolor, someangle) =
603 save rotationOrder, argumentString, rv;
604 string rotationOrder[], argumentString;
605 color rv;
606 argumentString := str @#;
607 if argumentString = "":
608 argumentString := defaultRotationOrder;
609 fi;
610 for i := 0 step 1 until length(argumentString) - 1:
611 rotationOrder[i] = substring (i, i + 1) of argumentString;
612 endfor;
613 rv := somecolor;
614 rv := byRotateAroundAxis(
615 rotationAxisB.scantokens(rotationOrder0),
616 rotationAxisE.scantokens(rotationOrder0),
617 scantokens(rotationAxisPart.scantokens(rotationOrder0))(someangle), rv);
618 rv := byRotateAroundAxis(
619 rotationAxisB.scantokens(rotationOrder1),
620 rotationAxisE.scantokens(rotationOrder1),
621 scantokens(rotationAxisPart.scantokens(rotationOrder1))(someangle), rv);
622 rv := byRotateAroundAxis(
623 rotationAxisB.scantokens(rotationOrder2),
624 rotationAxisE.scantokens(rotationOrder2),
625 scantokens(rotationAxisPart.scantokens(rotationOrder2))(someangle), rv);
626 rv
627 enddef;
628
629 primarydef colorone crossproduct colortwo =
630 begingroup
631 save xp, yp, zp;
632 numeric xp[], yp[], zp[];
633 xp1 := (redpart colorone)/cm;
634 yp1 := (greenpart colorone)/cm;
635 zp1 := (bluepart colorone)/cm;
636 xp2 := (redpart colortwo)/cm;
637 yp2 := (greenpart colortwo)/cm;
638 zp2 := (bluepart colortwo)/cm;
639 xp3 := ((yp1*zp2) - (zp1*yp2))*cm;
640 yp3 := ((zp1*xp2) - (xp1*zp2))*cm;
641 zp3 := ((xp1*yp2) - (yp1*xp2))*cm;
642 (xp3, yp3, zp3)
643 endgroup
644 enddef;
645
646 primarydef colorone dotprodXYZ colortwo =
647 begingroup
648 save xp, yp, zp;
649 numeric xp[], yp[], zp[];
650 xp1 := (redpart colorone);
651 yp1 := (greenpart colorone);
652 zp1 := (bluepart colorone);
653 xp2 := (redpart colortwo);
654 yp2 := (greenpart colortwo);
655 zp2 := (bluepart colortwo);
656 xp1*xp2 + yp1*yp2 + zp1*zp2
657 endgroup
658 enddef;
659
660 vardef isInPlane (expr colPoint, colPlaneA, colPlaneB, colPlaneC) =
661 save vecsToCompare, rv;
662 color vecsToCompare[];
663 boolean rv;
664 rv := false;
665 if (colPoint = colPlaneA)
666 or (colPoint = colPlaneB)
667 or (colPoint = colPlaneC):
668 rv := true;
669 else:
670 vecsToCompare1 := colPlaneA-colPlaneB;
671 vecsToCompare2 := colPlaneC-colPlaneB;
672 vecsToCompare3 := colPoint-colPlaneB;
673 fi;
674 if not rv:
675 if ((absXYZ(unitvectorXYZ(vecsToCompare1) - unitvectorXYZ(vecsToCompare3)) < 1/100)
676 or (absXYZ(unitvectorXYZ(vecsToCompare1) + unitvectorXYZ(vecsToCompare3)) < 1/100)
677 or (absXYZ(unitvectorXYZ(vecsToCompare2) - unitvectorXYZ(vecsToCompare3)) < 1/100)
678 or (absXYZ(unitvectorXYZ(vecsToCompare2) + unitvectorXYZ(vecsToCompare3)) < 1/100)):
679 rv := true;
680 else:
681 vecsToCompare4 := unitvectorXYZ(vecsToCompare1 crossproduct vecsToCompare2);
682 vecsToCompare5 := unitvectorXYZ(vecsToCompare3 crossproduct vecsToCompare2);
683 if ((absXYZ(vecsToCompare4 - vecsToCompare5) < 1/100)
684 or (absXYZ(vecsToCompare4 + vecsToCompare5) < 1/100)):
685 rv := true;
686 else:
687 rv := false;
688 fi;
689 fi;
690 fi;
691 rv
692 enddef;
693
694 vardef unitvectorXYZ (expr somecolor) =
695 save vecLength;
696 numeric vecLength;
697 vecLength := absXYZ(somecolor);
698 if (vecLength > 0):
699 somecolor/vecLength
700 else:
701 errmessage("Can't construct a unit vector from a zero-length vector");
702 fi
703 enddef;
704
705 vardef absXYZ (expr somecolor) =
706 (sqrt(
707 ((redpart somecolor)/1cm)**2
708 + ((greenpart somecolor)/1cm)**2
709 + ((bluepart somecolor)/1cm)**2)*1cm)
710 enddef;
711
712 vardef lineAndPlaneIntersection (expr lineA, lineB, planeA, planeB, planeC) =
713 save planeNormal, planeOrigin, lineD;
714 color planeNormal, planeOrigin;
715 numeric lineD;
716 planeNormal := normalToPlane(planeA, planeB, planeC);
717 planeOrigin := planeB;
718 lineD := ((planeNormal/cm) dotprodXYZ ((planeOrigin - lineA)/cm))
719 /((planeNormal/cm) dotprodXYZ ((lineB - lineA)/cm));
720 lineA + ((lineB-lineA)*lineD)
721 enddef;
722
723 vardef normalToPlane (expr planeA, planeB, planeC) =
724 unitvectorXYZ((planeA-planeB) crossproduct (planeC-planeB))
725 enddef;
726
727 vardef spaceRotated (suffix somepath)(expr someangle) =
728 save rv, tv, spacepoint, op, rp, tp;
729 path rv, tv;
730 color spacepoint[];
731 pair op[], rp[], tp[];
732 if (typeOf(thirdDimension.somepath) <> "path"):
733 path thirdDimension.somepath;
734 thirdDimension.somepath := somepath scaled 0;
735 fi;
736 for i := 0 step 1 until length(somepath):
737 op0 := point i of somepath;
738 spacepoint0 :=
739 (xpart(op0), ypart(op0),
740 xpart(point i of thirdDimension.somepath)
741 ) colorrotated someangle;
742 rp0 := (redpart(spacepoint0), greenpart(spacepoint0));
743 tp0 := (bluepart(spacepoint0), 0);
744 if (i = 0):
745 rv := rp0;
746 tv := tp0;
747 else:
748 op1 := postcontrol (i-1) of somepath;
749 spacepoint1 :=
750 (xpart(op1), ypart(op1),
751 xpart(postcontrol (i-1) of thirdDimension.somepath)
752 ) colorrotated someangle;
753 rp1 := (redpart(spacepoint1), greenpart(spacepoint1));
754 tp1 := (bluepart(spacepoint1), 0);
755 op2 := precontrol i of somepath;
756 spacepoint2 :=
757 (xpart(op2), ypart(op2),
758 xpart(precontrol i of thirdDimension.somepath)
759 ) colorrotated someangle;
760 rp2 := (redpart(spacepoint2), greenpart(spacepoint2));
761 tp2 := (bluepart(spacepoint2), 0);
762 rv := rv .. controls rp1 and rp2 ..
763 if ((i < length(somepath)) or (not cycle somepath)):
764 rp0
765 else:
766 cycle
767 fi;
768 tv := tv .. controls tp1 and tp2 ..
769 if ((i < length(somepath)) or (not cycle somepath)):
770 tp0
771 else:
772 cycle
773 fi;
774 fi;
775 endfor;
776 thirdDimension.somepath := tv;
777 rv
778 enddef;
779
780 vardef bySetProjection@#(expr xa, ya, za) =
781 projectionAngle := (xa, ya, za);
782 byRotatePoints@#(xa, ya, za, false)();
783 if str @# <> "":
784 defaultRotationOrder := str @#;
785 fi;
786 enddef;
787
788 vardef byPutPointsInSpace (text pL) =
789 byRotatePoints(0, 0, 0, true)(pL);
790 enddef;
791
792 % Gets a either a 2d or a 3d point and returns a 3d point
793
794 vardef byGetPointXYZ (suffix givenPoint) =
795 if (typeOf(givenPoint) = "color"):
796 givenPoint
797 elseif (typeOf(pointXYZ.givenPoint) = "color"):
798 pointXYZ.givenPoint
799 else:
800 errmessage("Point " & str givenPoint & " has no representation in space");
801 fi
802 enddef;
803
804 vardef byRotateAroundAxis (expr lineEndA, lineEndB, a, pointA) =
805 save vecA, vecB, rV, q;
806 color vecA, vecB, rV;
807 cmykcolor q[];
808 vecA := pointA - lineEndA;
809 vecB := unitvectorXYZ(lineEndB - lineEndA);
810 q1 := (0, redpart(vecA), greenpart(vecA), bluepart(vecA));
811 q2 := (cosd(a/2), sind(a/2)*redpart(vecB), sind(a/2)*greenpart(vecB), sind(a/2)*bluepart(vecB));
812 q3 := (cyanpart(q2), -magentapart(q2), -yellowpart(q2), -blackpart(q2));
813 q4 := (q2 hamiltonProduct q1) hamiltonProduct q3;
814 rV := (magentapart(q4), yellowpart(q4), blackpart(q4)) + lineEndA;
815 rV
816 enddef;
817
818 vardef byRotatePointsAroundAxis (suffix lineEndA, lineEndB)(expr a)(text pointsList) =
819 save lineEndAxyz, lineEndBxyz, pN;
820 color lineEndAxyz, lineEndBxyz;
821 if (typeOf(lineEndA) = "color"):
822 lineEndAxyz := lineEndA;
823 else:
824 byPutPointsInSpace(lineEndA);
825 lineEndAxyz := pointXYZ.lineEndA;
826 fi;
827 if (typeOf(lineEndB) = "color"):
828 lineEndBxyz := lineEndB;
829 else:
830 byPutPointsInSpace(lineEndB);
831 lineEndBxyz := pointXYZ.lineEndB;
832 fi;
833 forsuffixes pN := pointsList:
834 if (typeOf(pN) = "color"):
835 pN := byRotateAroundAxis(lineEndAxyz, lineEndBxyz, a, pN);
836 elseif (typeOf(pN) = "pair"):
837 byPutPointsInSpace(pN);
838 pointXYZ.pN := byRotateAroundAxis(lineEndAxyz, lineEndBxyz, a, pointXYZ.pN);
839 fi;
840 endfor;
841 enddef;
842
843 primarydef colorone hamiltonProduct colortwo =
844 begingroup
845 save a, b, c, d;
846 numeric a[], b[], c[], d[];
847 a1 := cyanpart(colorone);
848 b1 := magentapart(colorone);
849 c1 := yellowpart(colorone);
850 d1 := blackpart(colorone);
851 a2 := cyanpart(colortwo);
852 b2 := magentapart(colortwo);
853 c2 := yellowpart(colortwo);
854 d2 := blackpart(colortwo);
855 (
856 (a1*a2) - (b1*b2) - (c1*c2) - (d1*d2),
857 (a1*b2) + (b1*a2) + (c1*d2) - (d1*c2),
858 (a1*c2) - (b1*d2) + (c1*a2) + (d1*b2),
859 (a1*d2) + (b1*c2) - (c1*b2) + (d1*a2)
860 )
861 endgroup
862 enddef;
863
864 vardef isRightAngle (suffix a, b, c) =
865 save d, rv;
866 numeric d;
867 boolean rv;
868 if (attributeExists("point", "XYZ", str a)
869 and attributeExists("point", "XYZ", str b)
870 and attributeExists("point", "XYZ", str c)):
871 d := unitvectorXYZ(pointXYZ.a - pointXYZ.b) dotprodXYZ unitvectorXYZ(pointXYZ.c - pointXYZ.b);
872 else:
873 d := unitvector(a - b) dotprod unitvector(c - b);
874 fi;
875 if (abs(d) < 1/1000):
876 rv := true;
877 else:
878 rv := false;
879 fi;
880 rv
881 enddef;
882
883 %
884 % This section is dedicated to lines
885 %
886
887 vardef byLineRender (expr a, b, col, dp, th, c, d, ct, dt, dpt, s, sf) =
888 save p, la, lineItself, lineBleeding, clippingPath, cutAngle, linePerp, lineStyShift, currentLineWidth, clippingPathPoint, aS, bS;
889 picture p;
890 path lineItself, lineBleeding, clippingPath;
891 pair cutAngle[], linePerp, lineStyShift, clippingPathPoint[], aS, bS;
892 la := angle(b-a);
893 aS := a*sf;
894 bS := b*sf;
895 lineStyShift := (0, 0);
896 currentLineWidth := lineThickness(th);
897 cutAngle1 := unitvector(b-a) rotated -90;
898 if (abs(c-a) > 0):
899 if (abs(unitvector(c-a)-unitvector(a-b)) > 1/100):
900 if (ct = 0):
901 cutAngle1 := unitvector(1/2[unitvector(b-a), unitvector(c-a)]);
902 elseif (ct = 1):
903 cutAngle1 := unitvector(1[unitvector(b-a), unitvector(c-a)]);
904 fi;
905 fi;
906 fi;
907 cutAngle2 := unitvector(a-b) rotated 90;
908 if (abs(d-b) > 0):
909 if (abs(unitvector(d-b)-unitvector(b-a)) > 1/100):
910 if (dt = 0):
911 cutAngle2 := unitvector(1/2[unitvector(a-b), unitvector(d-b)]);
912 elseif (dt = 1):
913 cutAngle2 := unitvector(1[unitvector(a-b), unitvector(d-b)]);
914 fi;
915 fi;
916 fi;
917 if (sign(ypart(cutAngle2 rotated -la)) <> sign(ypart(cutAngle1 rotated -la))):
918 cutAngle2 := cutAngle2 rotated 180;
919 fi;
920 lineItself := (a--b) scaled sf;
921 linePerp := unitvector((point 0 of lineItself)-(point 1 of lineItself)) rotated 90;
922 lineBleeding :=
923 ((point 0 of lineItself) shifted (unitvector((point 0 of lineItself) - (point 1 of lineItself)) scaled 2dpLength[dp])) --
924 lineItself -- ((point 1 of lineItself) shifted (unitvector((point 1 of lineItself) - (point 0 of lineItself)) scaled 2dpLength[dp]));
925 lineStyShift := (linePerp scaled (1/2currentLineWidth*s));
926 p := image(
927 pickup pencircle scaled currentLineWidth;
928 if (dp > 0):
929 draw (lineItself shifted lineStyShift) withcolor white;
930 draw (lineItself shifted lineStyShift) withcolor col
931 dashed byDashPattern(arclength(lineItself), dp, dpt);
932 if ((dpt = 0) or (dpt = 1)):
933 draw (subpath(0, 1) of lineBleeding shifted lineStyShift) withcolor col;
934 fi;
935 if ((dpt = 0) or (dpt = 2)):
936 draw (subpath(2, 3) of lineBleeding shifted lineStyShift) withcolor col;
937 fi;
938 else:
939 draw (lineBleeding shifted lineStyShift) withcolor col;
940 fi;
941 if (th < 0):
942 draw (lineBleeding shifted (lineStyShift + (linePerp scaled 1/4currentLineWidth))) withpen pencircle scaled 1/2currentLineWidth withcolor black;
943 fi;
944 );
945 clippingPathPoint1
946 = whatever[aS shifted (linePerp*1/2lineWidth) shifted lineStyShift,
947 bS shifted (linePerp*1/2lineWidth) shifted lineStyShift]
948 = whatever[aS, aS shifted cutAngle1];
949 clippingPathPoint2
950 = whatever[aS shifted (linePerp*-1/2lineWidth) shifted lineStyShift,
951 bS shifted (linePerp*-1/2lineWidth) shifted lineStyShift]
952 = whatever[aS, aS shifted cutAngle1];
953 clippingPathPoint3
954 = whatever[aS shifted (linePerp*-1/2lineWidth) shifted lineStyShift,
955 bS shifted (linePerp*-1/2lineWidth) shifted lineStyShift]
956 = whatever[bS, bS shifted cutAngle2];
957 clippingPathPoint4
958 = whatever[aS shifted (linePerp*1/2lineWidth) shifted lineStyShift,
959 bS shifted (linePerp*1/2lineWidth) shifted lineStyShift]
960 = whatever[bS, bS shifted cutAngle2];
961 clippingPath := clippingPathPoint1 -- clippingPathPoint2 -- clippingPathPoint3 -- clippingPathPoint4 -- cycle;
962 clip p to clippingPath;
963 if mainPictureMode and ghostLines:
964 save gP;
965 picture gP;
966 gP := ghostPicture;
967 ghostPicture := image(
968 draw gP;
969 begingroup;
970 save ghostLines;
971 boolean ghostLines;
972 ghostLines := false;
973 draw byLineRender (a, b, white, dp, 2, c, d, ct, dt, dpt, s, sf);
974 % draw byLineRender (a, b, col, dp, 2, c, d, ct, dt, dpt, s, sf);
975 endgroup;
976 );
977 fi;
978 p rotated globalRotation
979 enddef;
980
981 vardef byReturnLineLength (suffix a, b) =
982 if byIsPointInSpace(a, b):
983 absXYZ(pointXYZ.a - pointXYZ.b)
984 else:
985 abs(a-b)
986 fi
987 enddef;
988
989 vardef byLineDefine@#(suffix a, b)(expr col, dp, th) =
990 if str @# = "":
991 byLineDefine.scantokens(str a & str b)(a, b)(col, dp, th);
992 else:
993 save lA;
994 if mainPictureMode:
995 appendList(allLinesList, str @#, 1, true);
996 fi;
997 if byIsPointInSpace(a, b):
998 if (typeOf(a) = "pair") and (typeOf(b) = "pair"):
999 if (a <> b):
1000 lA := angle(b-a);
1001 else:
1002 lA := 0;
1003 fi;
1004 else:
1005 lA := 0;
1006 fi;
1007 else:
1008 if (a <> b):
1009 lA := angle(b-a);
1010 else:
1011 lA := 0;
1012 fi;
1013 fi;
1014 setAttribute("line", "Angle", str @#, lA);
1015 setAttribute("line", "Color", str @#, col);
1016 setAttribute("line", "Dashed", str @#, dp);
1017 setAttribute("line", "Thin", str @#, th);
1018 setAttribute("line", "EndA", str @#, a);
1019 setAttribute("line", "EndB", str @#, b);
1020 setAttribute("line", "Shift", str @#, 0);
1021 setAttribute("line", "DirA", str @#, a);
1022 setAttribute("line", "DirB", str @#, b);
1023 setAttribute("line", "EndAType", str @#, 0);
1024 setAttribute("line", "EndBType", str @#, 0);
1025 setAttribute("line", "EndAName", str @#, str a);
1026 setAttribute("line", "EndBName", str @#, str b);
1027 setAttribute("line", "DirAName", str @#, str a);
1028 setAttribute("line", "DirBName", str @#, str b);
1029 setAttribute("line", "UseLineLabel", str @#, false);
1030 setAttribute("line", "Label", str @#, str @#);
1031 setAttribute("line", "Synonym", str @#, str @#);
1032 fi;
1033 enddef;
1034
1035 vardef byLineStylize (suffix c, d)(expr ct, dt, s) (suffix lineName) =
1036 setAttribute("line", "Shift", str lineName, s);
1037 setAttribute("line", "DirA", str lineName, c);
1038 setAttribute("line", "DirB", str lineName, d);
1039 setAttribute("line", "DirAName", str lineName, str c);
1040 setAttribute("line", "DirBName", str lineName, str d);
1041 setAttribute("line", "EndAType", str lineName, ct);
1042 setAttribute("line", "EndBType", str lineName, dt);
1043 enddef;
1044
1045 vardef byLine@#(suffix a, b)(expr col, dp, th) =
1046 if str @# = "":
1047 byLine.scantokens(str a & str b)(a, b)(col, dp, th)
1048 else:
1049 byLineDefine@#(a, b, col, dp, th);
1050 byNamedLineFull(a, b, 0, 0, 0, 0)(@#)
1051 fi
1052 enddef;
1053
1054 vardef byLineFull@#(suffix a, b)(expr col, dp, th)(suffix c, d)(expr ct, dt, s) =
1055 if str @# = "":
1056 byLineFull.scantokens(str a & str b)(a, b)(col, dp, th)(c, d)(ct, dt, s)
1057 else:
1058 byLineDefine@#(a, b) (col, dp, th);
1059 byLineStylize (c, d, ct, dt, s)(@#);
1060 byNamedLine(@#)
1061 fi
1062 enddef;
1063
1064 % This one's behaviour is slightly off, since it affects line label type
1065
1066 vardef byLineWithName (suffix a, b)(expr col, dp, th)(suffix lineLabel) =
1067 byLineDefine.lineLabel(a, b, col, dp, th);
1068 setAttribute("line", "UseLineLabel", str lineLabel, true);
1069 byNamedLineFull(a, b, 0, 0, 0, 0)(lineLabel)
1070 enddef;
1071
1072 vardef byNamedLineFull (expr c, d, ct, dt, dpt, s) (suffix lineName) =
1073 if (mainPictureMode):
1074 appendList(pointLinesList.scantokens(getAttribute("line", "EndAName", str lineName)), str lineName, 1, true);
1075 appendList(pointLinesList.scantokens(getAttribute("line", "EndBName", str lineName)), str lineName, 1, true);
1076 fi;
1077 byLineRender(
1078 scantokens(getAttribute("line", "EndAName", str lineName)),
1079 scantokens(getAttribute("line", "EndBName", str lineName)),
1080 getAttribute("line", "Color", str lineName),
1081 getAttribute("line", "Dashed", str lineName),
1082 getAttribute("line", "Thin", str lineName),
1083 c, d, ct, dt, dpt, s, scaleFactor)
1084 enddef;
1085
1086 vardef byNamedLine (text linesList) =
1087 image(
1088 forsuffixes lN=linesList:
1089 draw byNamedLineFull(
1090 scantokens(getAttribute("line", "DirAName", str lN)),
1091 scantokens(getAttribute("line", "DirBName", str lN)),
1092 getAttribute("line", "EndAType", str lN),
1093 getAttribute("line", "EndBType", str lN),
1094 0,
1095 getAttribute("line", "Shift", str lN))(lN);
1096 endfor;
1097 )
1098 enddef;
1099
1100 vardef byNamedLineIgnoreEndTypes (text linesList) =
1101 image(
1102 forsuffixes lN=linesList:
1103 draw byNamedLineFull(
1104 scantokens(getAttribute("line", "EndAName", str lN)),
1105 scantokens(getAttribute("line", "EndBName", str lN)),
1106 0,
1107 0,
1108 0,
1109 0)(lN);
1110 endfor;
1111 )
1112 enddef;
1113
1114 vardef byNamedLineSeq (expr s)(text linesListRaw) =
1115 save lAname, lBname, lShift, lName, i, j, k, l, m, n, c, d, prp, dpt, dashStreak, pointNames, linesList;
1116 string lAname[], lBname[], lName[], c, d, pointNames, linesList;
1117 numeric i, j, k, l, m, n, prp, lShift[], dpt, dashStreak;
1118 linesList := byExpandLines(linesListRaw);
1119 pointNames := "";
1120 image(
1121 i := -1;
1122 dashStreak := 0;
1123 forsuffixes lN=scantokens(linesList):
1124 i := i + 1;
1125 lAname[i] := getAttribute("line", "EndAName", str lN);
1126 lBname[i] := getAttribute("line", "EndBName", str lN);
1127 lShift[i] := getAttribute("line", "Shift", str lN);
1128 lName[i] := str lN;
1129 endfor;
1130 if (i = 1):
1131 i := i + 1;
1132 lAname[i] := "noLine";
1133 lBname[i] := "noLine";
1134 lName[i] := "noLine";
1135 fi;
1136 if (i > 0):
1137 for j := 0 step 1 until i:
1138 k := cycleval (j - 1, i + 1);
1139 l := cycleval (j + 1, i + 1);
1140 m := 0;
1141 n := 0;
1142 if (lName[k] = "noLine"):
1143 k := cycleval (j - 2, i + 1);
1144 m := 1;
1145 fi;
1146 if (lName[l] = "noLine"):
1147 l := cycleval (j + 2, i + 1);
1148 n := 1;
1149 fi;
1150 prp := 0;
1151 if (lName[j] <> "noLine"):
1152 c := lAname[j];
1153 d := lBname[j];
1154 %if (lName[k] <> lName[l]):
1155 if (((lAname[j] = lAname[k]) or (lAname[j] = lBname[k])) and (m = 0)) or
1156 %and (not ((m <> 0) and (lName[k] = lName[l])))) or
1157 (((lBname[j] = lAname[l]) or (lBname[j] = lBname[l])) and (n = 0)):
1158 %and (not ((n <> 0) and (lName[k] = lName[l])))):
1159 if (lAname[j] = lAname[k]):
1160 if (m = 0): c := lBname[k]; fi;
1161 elseif (lAname[j] = lBname[k]):
1162 if (m = 0): c := lAname[k]; fi;
1163 else:
1164 m := 2;
1165 fi;
1166 if (lBname[j] = lAname[l]):
1167 if (n = 0): d := lBname[l]; fi;
1168 elseif (lBname[j] = lBname[l]):
1169 if (n = 0): d := lAname[l]; fi;
1170 else:
1171 n := 2;
1172 fi;
1173 prp := 1;
1174 else:
1175 if (lAname[j] = lAname[l]):
1176 if (n = 0): c := lBname[l]; fi;
1177 elseif (lAname[j] = lBname[l]):
1178 if (n = 0): c := lAname[l]; fi;
1179 else:
1180 n := 2;
1181 fi;
1182 if (lBname[j] = lAname[k]):
1183 if (m = 0): d := lBname[k]; fi;
1184 elseif (lBname[j] = lBname[k]):
1185 if (m = 0): d := lAname[k]; fi;
1186 else:
1187 m := 2;
1188 fi;
1189 prp := -1;
1190 fi;
1191 %fi;
1192 if (prp = 1):
1193 pointNames := pointNames & lAname[j] & ", ";
1194 if (n = 2):
1195 pointNames := pointNames & lBname[j] & ", noPoint, ";
1196 fi;
1197 elseif (prp = -1):
1198 pointNames := pointNames & lBname[j] & ", ";
1199 if (n = 2):
1200 pointNames := pointNames & lAname[j] & ", noPoint, ";
1201 fi;
1202 fi;
1203 lShift[j] := s*prp;
1204 dpt := 0;
1205 if (c <> lAname[j]):
1206 if abs(unitvector(scantokens(c) - scantokens(lAname[j])) - unitvector(scantokens(lAname[j]) - scantokens(lBname[j]))) < 1/100:
1207 dpt := dpt + 2;
1208 fi;
1209 fi;
1210 if (d <> lBname[j]):
1211 if abs(unitvector(scantokens(d) - scantokens(lBname[j])) - unitvector(scantokens(lBname[j]) - scantokens(lAname[j]))) < 1/100:
1212 dpt := dpt + 1;
1213 fi;
1214 fi;
1215 if (dashStreak > 0):
1216 if (prp = 1): dpt := dpt - 2; fi;
1217 if (prp = -1): dpt := dpt - 1; fi;
1218 fi;
1219 if (getAttribute("line", "Dashed", lName[j]) > 0)
1220 and (((prp = 1) and ((dpt <> 0) and (dpt <> 2)))
1221 or ((prp = -1) and ((dpt <> 0) and (dpt <> 1)))):
1222 dashStreak := dashStreak + 1;
1223 else:
1224 dashStreak := 0;
1225 fi;
1226 draw byNamedLineFull(scantokens(c), scantokens(d), 0, 0, dpt, -s*sign(prp))(scantokens(lName[j]));
1227 else:
1228 prp := -2;
1229 fi;
1230 endfor;
1231 if (prp = 1) and (n <> 2):
1232 pointNames := pointNames & lBname[i];
1233 elseif (prp = -1) and (n <> 2):
1234 pointNames := pointNames & lAname[i];
1235 else:
1236 pointNames := substring (0, length(pointNames)-2) of pointNames;
1237 fi;
1238 if (textLabels and autoLabelingMode):
1239 draw byLabelsOnPolygon(scantokens pointNames)(OMIT_LABELS_AT_STRAIGHT_ANGLES, s);
1240 fi;
1241 else:
1242 forsuffixes lN=scantokens(linesList):
1243 setAttribute("line", "Shift", str lN, s);
1244 draw byNamedLineFull(
1245 scantokens(getAttribute("line", "EndAName", str lN)),
1246 scantokens(getAttribute("line", "EndBName", str lN)),
1247 0, 0, 0, -s)(lN);
1248 if (textLabels and autoLabelingMode):
1249 draw byLabelsOnPolygon(scantokens (getAttribute("line", "EndAName", str lN) & ", " & getAttribute("line", "EndBName", str lN)))(OMIT_LABELS_AT_STRAIGHT_ANGLES, s);
1250 fi;
1251 endfor;
1252 fi;
1253 )
1254 enddef;
1255
1256 vardef byNamedLineSeqByPoints(expr s, cyc)(text pointsList) =
1257 save constLinesList, lastPoint, firstPoint, i;
1258 string constLinesList, lastPoint, firstPoint;
1259 numeric i;
1260 constLinesList := "";
1261 lastPoint := "";
1262 i := 0;
1263 forsuffixes pN=pointsList:
1264 i := i + 1;
1265 if (lastPoint <> ""):
1266 if (str pN <> "noPoint"):
1267 if (i > 2):
1268 constLinesList := constLinesList & ", ";
1269 fi;
1270 constLinesList := constLinesList & lastPoint & str pN;
1271 fi;
1272 else:
1273 firstPoint := str pN;
1274 fi;
1275 lastPoint := str pN;
1276 endfor;
1277 if (cyc) and (i <> 2) and (lastPoint <> "noPoint"):
1278 constLinesList := constLinesList & ", " & lastPoint & firstPoint;
1279 fi;
1280 byNamedLineSeq(s)(scantokens(constLinesList))
1281 enddef;
1282
1283 vardef byMarkLine(expr pos, col)(suffix lineName) =
1284 setAttribute("mark", "Position", str lineName, pos);
1285 setAttribute("mark", "Color", str lineName, col);
1286 byNamedMarkLine(lineName)
1287 enddef;
1288
1289 vardef byNamedMarkLine(suffix lineName) =
1290 save p, q;
1291 pair q[];
1292 picture p;
1293 p := image(
1294 q0 := getAttribute("mark", "Position", str lineName)[
1295 scantokens(getAttribute("line", "EndAName", str lineName)),
1296 scantokens(getAttribute("line", "EndBName", str lineName))
1297 ];
1298 q1 := q0 shifted (dir(getAttribute("line", "Angle", str lineName) + 90) scaled 1/2markLength);
1299 q2 := q0 shifted (dir(getAttribute("line", "Angle", str lineName) - 90) scaled 1/2markLength);
1300 q3 := (dir(getAttribute("line", "Angle", str lineName) - 90) scaled (1/2lineWidth*getAttribute("line", "Shift", str lineName)));
1301 draw (q1--q2) shifted q3 withpen pencircle scaled lineWidthThin withcolor getAttribute("mark", "Color", str lineName);
1302 );
1303 p rotated globalRotation
1304 enddef;
1305
1306 vardef byFindLinePointedThere (expr a, b) =
1307 save endA, endB, endAC, endBC, endAA, endBA, chosenLine, lL;
1308 string chosenLine;
1309 pair endA, endB, endAC, endBC, endAA, endBA;
1310 numeric lL;
1311 endA := scantokens(a);
1312 endB := b;
1313 if known pointLinesList.scantokens(a):
1314 forsuffixes lL=scantokens(pointLinesList.scantokens(a)):
1315 endAC := scantokens(getAttribute("line", "EndAName", str lL));
1316 endBC := scantokens(getAttribute("line", "EndBName", str lL));
1317 if (endAC = endA):
1318 endAA := endAC;
1319 endBA := endBC;
1320 elseif (endBC = endA):
1321 endAA := endBC;
1322 endBA := endAC;
1323 fi;
1324 if ((abs(angle(endBA - endAA) - angle(endB - endA)) mod 360) < 1):
1325 chosenLine := str lL;
1326 fi;
1327 endfor;
1328 fi;
1329 if unknown chosenLine:
1330 chosenLine := "";
1331 fi;
1332 chosenLine
1333 enddef;
1334
1335 vardef byConstructCompoundLine (expr lineName) =
1336 save lineStart, lEnd, newLine, lEndA, lEndB, compoundLineString, i, j, cPointA, cPointB;
1337 string lineStart, lEnd, newLine, lEndA, lEndB, compoundLineString, cPointA, cPointB;
1338 numeric i, j;
1339 j := 0;
1340 if (length(lineName) = 2):
1341 lineStart := substring (0, 1) of lineName;
1342 lEnd := substring (1, 2) of lineName;
1343 else:
1344 for i := 1 step 1 until length(lineName):
1345 cPointA := substring (0, i) of lineName;
1346 cPointB := substring (i, length(lineName)) of lineName;
1347 if known scantokens(cPointA): % for some reason 'and' doesn't work here
1348 if known scantokens(cPointB):
1349 j := j + 1;
1350 lineStart := cPointA;
1351 lEnd := cPointB;
1352 fi;
1353 fi;
1354 endfor;
1355 fi;
1356 if unknown lineStart:
1357 errmessage("Can't find points for line: " & lineName);
1358 fi;
1359 if j > 1:
1360 errmessage("Ambiguous line name: " & lineName);
1361 fi;
1362 i := 0;
1363 forever:
1364 i := i + 1;
1365 newLine := byFindLinePointedThere(lineStart, scantokens(lEnd));
1366 if (newLine = ""):
1367 errmessage("There's no line from " & lineStart & " to " & lEnd);
1368 fi;
1369 appendList (compoundLineString, newLine, 1, false);
1370 lEndA := getAttribute("line", "EndAName", newLine);
1371 lEndB := getAttribute("line", "EndBName", newLine);
1372 if (lEndA = lineStart):
1373 lineStart := lEndB;
1374 elseif (lEndB = lineStart):
1375 lineStart := lEndA;
1376 fi;
1377 exitif (i > 10);
1378 exitif (lEndA = lEnd);
1379 exitif (lEndB = lEnd);
1380 endfor;
1381 compoundLineString
1382 enddef;
1383
1384 vardef byExpandLines (text linesList) =
1385 save lineString, compoundLineString;
1386 string lineString, compoundLineString;
1387 forsuffixes lN=linesList:
1388 if attributeExists("line", "Synonym", str lN):
1389 appendList(lineString, str lN, 1, false);
1390 else:
1391 appendList(lineString, byConstructCompoundLine(str lN), 1, false);
1392 fi;
1393 endfor;
1394 lineString
1395 enddef;
1396
1397 vardef byNamedCompoundLine (expr cu, pw)(text linesList) =
1398 byNamedCompoundLineRender(cu, pw, 0)(scantokens(byExpandLines(linesList)))
1399 enddef;
1400
1401 vardef byNamedCompoundRay (expr cu, pw)(text linesList) =
1402 byNamedCompoundLineRender(cu, pw, 1)(scantokens(byExpandLines(linesList)))
1403 enddef;
1404
1405 vardef byNamedCompoundIndLine (expr cu, pw)(text linesList) =
1406 byNamedCompoundLineRender(cu, pw, 2)(scantokens(byExpandLines(linesList)))
1407 enddef;
1408
1409 vardef byNamedCompoundLineRender (expr cu, pw, sty)(text linesList) =
1410 save correctedLength, currentPosition, a, b, sc, totalLength, pwL, lineNames, endNames, i, j, angleCheck, dpt, dashStreak, lineNotStraight, lineNotContinuous, globalRotation, extensionNames, textLabelImage, labelAng;
1411 numeric correctedLength, currentPosition[], sc, totalLength, pwL, i, j, angleCheck[], dpt, dashStreak, labelAng;
1412 globalRotation := 0;
1413 string lineNames[], endNames[], extensionNames[];
1414 pair a, b;
1415 picture textLabelImage[];
1416 boolean lineNotStraight, lineNotContinuous;
1417 i := 0;
1418 totalLength := 0;
1419 lineNotStraight := false;
1420 lineNotContinuous := false;
1421 forsuffixes lN=linesList:
1422 lineNames[i] := getAttribute("line", "EndAName", str lN);
1423 lineNames[i+1] := getAttribute("line", "EndBName", str lN);
1424 if (i > 0):
1425 if (lineNames[i] <> lineNames[i-2])
1426 and (lineNames[i] <> lineNames[i-1])
1427 and (lineNames[i+1] <> lineNames[i-2])
1428 and (lineNames[i+1] <> lineNames[i-1]):
1429 lineNotContinuous := true;
1430 fi;
1431 fi;
1432 i := i + 2;
1433 angleCheck[i/2] := abs(cosd(getAttribute("line", "Angle", str lN)));
1434 if (abs(angleCheck[i/2] - angleCheck[1]) > 1/100):
1435 lineNotStraight := true;
1436 fi;
1437 totalLength := totalLength
1438 + byReturnLineLength(
1439 scantokens(getAttribute("line", "EndAName", str lN)),
1440 scantokens(getAttribute("line", "EndBName", str lN)));
1441 endfor;
1442 if (lineNotStraight):
1443 message("The line is not straight");
1444 fi;
1445 if (lineNotContinuous):
1446 errmessage("The line is not continuous");
1447 fi;
1448 if (pw <> 0):
1449 sc := cu**(1 - (1/pw));
1450 pwL := 1/pw;
1451 else:
1452 sc := cu/(totalLength*scaleFactor);
1453 pwL := 1;
1454 fi;
1455 currentPosition0 := 0;
1456 image(
1457 j := 0;
1458 dashStreak := 0;
1459 forsuffixes lN=linesList:
1460 correctedLength := (byReturnLineLength(
1461 scantokens(getAttribute("line", "EndAName", str lN)),
1462 scantokens(getAttribute("line", "EndBName", str lN))
1463 )**pwL)*sc;
1464 currentPosition1 := currentPosition0 + correctedLength;
1465 dpt := 0;
1466 if (j > 0):
1467 dpt := dpt + 2;
1468 fi;
1469 if (j < i/2 - 1):
1470 dpt := dpt + 1;
1471 fi;
1472 if (dashStreak > 0):
1473 dpt := dpt - 2;
1474 fi;
1475 if (getAttribute("line", "Dashed", str lN) > 0):
1476 dashStreak := dashStreak + 1;
1477 else:
1478 dashStreak := 0;
1479 fi;
1480 a := (currentPosition0, 0);
1481 b := (currentPosition1, 0);
1482 draw byLineRender(
1483 a, b,
1484 getAttribute("line", "Color", str lN),
1485 getAttribute("line", "Dashed", str lN),
1486 getAttribute("line", "Thin", str lN),
1487 a, b, 0, 0, dpt, 0, scaleFactor);
1488 currentPosition0 := currentPosition1;
1489 if (i > 2):
1490 if (j = 0):
1491 extensionNames3 := str lN;
1492 if (getAttribute("line", "EndAName", str lN) = lineNames[2])
1493 or (getAttribute("line", "EndAName", str lN) = lineNames[3]):
1494 endNames[0] := getAttribute("line", "EndBName", str lN);
1495 elseif (getAttribute("line", "EndBName", str lN) = lineNames[2])
1496 or (getAttribute("line", "EndBName", str lN) = lineNames[3]):
1497 endNames[0] := getAttribute("line", "EndAName", str lN);
1498 fi;
1499 fi;
1500 if (j = (i-2)/2):
1501 extensionNames2 := str lN;
1502 if (getAttribute("line", "EndAName", str lN) = lineNames[i-3])
1503 or (getAttribute("line", "EndAName", str lN) = lineNames[i-4]):
1504 endNames[1] := getAttribute("line", "EndBName", str lN);
1505 elseif (getAttribute("line", "EndBName", str lN) = lineNames[i-3])
1506 or (getAttribute("line", "EndBName", str lN) = lineNames[i-4]):
1507 endNames[1] := getAttribute("line", "EndAName", str lN);
1508 fi;
1509 fi;
1510 else:
1511 endNames[0] := getAttribute("line", "EndAName", str lN);
1512 endNames[1] := getAttribute("line", "EndBName", str lN);
1513 if (getAttribute("line", "UseLineLabel", str lN)):
1514 endNames[2] := str lN;
1515 fi;
1516 fi;
1517 j := j + 1;
1518 endfor;
1519 extensionNames0 := byFindLinePointedThere(endNames1, 2[scantokens(endNames0), scantokens(endNames1)]);
1520 extensionNames1 := byFindLinePointedThere(endNames0, 2[scantokens(endNames1), scantokens(endNames0)]);
1521 if extensionNames0 = "":
1522 extensionNames0 := "dummyLine";
1523 else:
1524 extensionNames2 := extensionNames0;
1525 fi;
1526 if extensionNames1 = "":
1527 extensionNames1 := "dummyLine";
1528 else:
1529 extensionNames3 := extensionNames1;
1530 fi;
1531 dpt := 0;
1532 if (sty > 0):
1533 if (xpart(b) > 0):
1534 dpt := 1;
1535 fi;
1536 draw byLineRender(b, b shifted (rayExtension/scaleFactor, 0),
1537 getAttribute("line", "Color", extensionNames0),
1538 getAttribute("line", "Dashed", extensionNames0),
1539 getAttribute("line", "Thin", extensionNames2),
1540 b, b shifted (rayExtension/scaleFactor, 0), 0, 0, dpt, 0, scaleFactor);
1541 fi;
1542 if (sty > 1):
1543 dpt := 1;
1544 draw byLineRender((0, 0), (0, 0) shifted (-rayExtension/scaleFactor, 0),
1545 getAttribute("line", "Color", extensionNames1),
1546 getAttribute("line", "Dashed", extensionNames1),
1547 getAttribute("line", "Thin", extensionNames3),
1548 (0, 0), (0, 0) shifted (-rayExtension/scaleFactor, 0), 0, 0, dpt, 0, scaleFactor);
1549 fi;
1550 if (textLabels and autoLabelingMode):
1551 if (lineLabelsOnTop = true):
1552 labelAng := 90;
1553 else:
1554 labelAng := -90;
1555 fi;
1556 if unknown endNames[2]:
1557 textLabelImage1 := byTextLabel.r(pointLabel)(endNames[0], (0, 0), labelAng, textLabelShift);
1558 textLabelImage2 := byTextLabel.l(pointLabel)(endNames[1], b, labelAng, textLabelShift);
1559 draw textLabelImage1;
1560 draw textLabelImage2;
1561 if (compensateLineLabels = true):
1562 % these are only needed to make the image symmetrical in order to have
1563 % the line with and without labels at the same level in text.
1564 draw (textLabelImage1 yscaled -1) withcolor white;
1565 draw (textLabelImage2 yscaled -1) withcolor white;
1566 fi;
1567 else:
1568 textLabelImage1 := byTextLabel(lineLabel)(endNames[2], 1/2[(0, 0), b], labelAng, textLabelShift);
1569 draw textLabelImage1;
1570 if (compensateLineLabels = true):
1571 draw (textLabelImage1 yscaled -1) withcolor white;
1572 fi;
1573 fi;
1574 fi;
1575 )
1576 enddef;
1577
1578 vardef generateLineSynonyms =
1579 if string allLinesList:
1580 save candidateName, originalName;
1581 string candidateName[], originalName;
1582 forsuffixes i=scantokens(allLinesList):
1583 originalName := str i;
1584 candidateName1 := getAttribute("line", "EndAName", originalName)
1585 & getAttribute("line", "EndBName", originalName);
1586 candidateName2 := getAttribute("line", "EndBName", originalName)
1587 & getAttribute("line", "EndAName", originalName);
1588 if not string getAttribute("line", "Synonym", candidateName1):
1589 setAttribute("line", "Synonym", candidateName1, originalName);
1590 fi;
1591 if not string getAttribute("line", "Synonym", candidateName2):
1592 setAttribute("line", "SynonymPartial", candidateName2, true);
1593 setAttribute("line", "Synonym", candidateName2, originalName);
1594 setAttribute("line", "EndA", candidateName2, getAttribute("line", "EndB", originalName));
1595 setAttribute("line", "EndB", candidateName2, getAttribute("line", "EndA", originalName));
1596 setAttribute("line", "EndAType", candidateName2, getAttribute("line", "EndBType", originalName));
1597 setAttribute("line", "EndBType", candidateName2, getAttribute("line", "EndAType", originalName));
1598 setAttribute("line", "EndAName", candidateName2, getAttribute("line", "EndBName", originalName));
1599 setAttribute("line", "EndBName", candidateName2, getAttribute("line", "EndAName", originalName));
1600 setAttribute("line", "DirA", candidateName2, getAttribute("line", "DirB", originalName));
1601 setAttribute("line", "DirB", candidateName2, getAttribute("line", "DirA", originalName));
1602 setAttribute("line", "DirAName", candidateName2, getAttribute("line", "DirBName", originalName));
1603 setAttribute("line", "DirBName", candidateName2, getAttribute("line", "DirAName", originalName));
1604 setAttribute("line", "Angle", candidateName2, 180 + getAttribute("line", "Angle", originalName));
1605 fi;
1606 endfor;
1607 fi;
1608 enddef;
1609
1610 %
1611 % Points
1612 %
1613
1614 vardef byPointLabelDefine (suffix pointName)(expr t) =
1615 setAttribute("point", "Label", str pointName, t);
1616 enddef;
1617
1618 vardef byPointLabelRemove (text pointsList) =
1619 forsuffixes i=pointsList:
1620 byPointLabelDefine(i, "");
1621 endfor;
1622 enddef;
1623
1624 vardef byPointMarkDefine (suffix pointName)(expr col, sty) =
1625 setAttribute("point", "Color", str pointName, col);
1626 setAttribute("point", "Style", str pointName, sty);
1627 enddef;
1628
1629 vardef byPointXYZDefine@#(expr ox, oy, oz) =
1630 if str @# = "":
1631 errmessage("byPointXYZDefine needs a name (byPointXYZDefine.somename...)");
1632 else:
1633 if mainPictureMode:
1634 appendList(allPointsList, str @#, 1, true);
1635 fi;
1636 setAttribute("point", "XYZ", str @#, (ox, oy, oz));
1637 pair @#;
1638 color pointXYZ.@#;
1639 @# := (ox, oy);
1640 pointXYZ.@# := (ox, oy, oz);
1641 fi;
1642 enddef;
1643
1644 vardef byPointXYZEmpty (text pointNames) =
1645 forsuffixes pN = pointNames:
1646 if mainPictureMode:
1647 appendList(allPointsList, str pN, 1, true);
1648 fi;
1649 color pointXYZ.pN;
1650 endfor;
1651 enddef;
1652
1653 vardef byIsPointInSpace (text pointNames) =
1654 save pN;
1655 (true forsuffixes pN = pointNames:
1656 and (typeOf(pointXYZ.pN) = "color")
1657 endfor)
1658 enddef;
1659
1660 vardef projectPointIfInSpace(expr pointValue) =
1661 save rv, projPoint;
1662 pair rv;
1663 if (typeOf(pointValue) = "color"):
1664 color projPoint;
1665 projPoint := pointValue colorrotated projectionAngle;
1666 rv := (redpart(projPoint), greenpart(projPoint));
1667 else:
1668 rv := pointValue;
1669 fi;
1670 rv
1671 enddef;
1672
1673 % in order to automatically project points they need to be added
1674 % to allPointsList list.
1675 vardef byPair (text pointsList) =
1676 save i;
1677 pair pointsList;
1678 forsuffixes i := pointsList:
1679 appendList(allPointsList, str i, 1, true);
1680 numeric pointXYZ.i;
1681 endfor;
1682 enddef;
1683
1684 % @# here stands for rotation order, e. g. byRotatePoints.XYZ
1685
1686 vardef byRotatePoints@#(expr xa, ya, za, toRotate)(text pointsList) =
1687 save oxyz, pointsToProcess;
1688 color oxyz;
1689 string pointsToProcess;
1690 forsuffixes i=pointsList:
1691 appendList(pointsToProcess, str i, 1, true);
1692 endfor;
1693 if (length(pointsToProcess) = 0):
1694 pointsToProcess := allPointsList;
1695 fi;
1696 forsuffixes i=scantokens(pointsToProcess):
1697 if (known i) or (known pointXYZ.i):
1698 if attributeExists("point", "XYZ", str i):
1699 oxyz := getAttribute("point", "XYZ", str i);
1700 elseif typeOf(i) = "pair":
1701 byPointXYZDefine.i(xpart i, ypart i, 0);
1702 oxyz := getAttribute("point", "XYZ", str i);
1703 else:
1704 errmessage("Point " & str i & " doesn't exist");
1705 fi;
1706 oxyz := rotateColor@#(oxyz, (xa, ya, za));
1707 pair i;
1708 i := (redpart(oxyz), greenpart(oxyz));
1709 if toRotate:
1710 setAttribute("point", "XYZ", str i, oxyz);
1711 fi;
1712 fi;
1713 endfor;
1714 enddef;
1715
1716 vardef byPointMarkRender (expr p, col, sty) =
1717 image(
1718 if (sty = 0):
1719 fill ((fullcircle scaled pointMarkSize) shifted (p scaled scaleFactor)) withcolor col;
1720 elseif (sty = 1):
1721 fill ((fullcircle scaled pointMarkSize) shifted (p scaled scaleFactor)) withcolor white;
1722 draw ((fullcircle scaled pointMarkSize) shifted (p scaled scaleFactor)) withcolor col withpen pencircle scaled lineWidthHair;
1723 fi;
1724 )
1725 enddef;
1726
1727 vardef byNamedPointMark (text pointsList) =
1728 image(
1729 forsuffixes pN=pointsList:
1730 if (attributeExists("point", "Color", str pN)):
1731 appendList(pL, str pN, 1, false);
1732 draw byPointMarkRender(pN, getAttribute("point", "Color", str pN), getAttribute("point", "Style", str pN)) rotated globalRotation;
1733 if (autoLabelingMode and textLabels):
1734 draw byTextLabel(pointLabel)(str pN, pN, 90, 1/2pointMarkSize + textLabelShift);
1735 fi;
1736 fi;
1737 endfor;
1738 )
1739 enddef;
1740
1741 vardef byPointMark (suffix pointName)(expr col, sty) =
1742 image(
1743 byPointMarkDefine(pointName)(col, sty);
1744 draw byNamedPointMark(pointName);
1745 )
1746 enddef;
1747
1748 vardef byNamedPointLines (suffix pointName)(expr omitLines) =
1749 save linesList, lineNames, pointNames, distantEnd, distantEnds, n, no, distL, maxDist, maxDistEndA, maxDistEndB, a, angleL, minAngle, maxAngle, minAngleEndB, maxAngleEndA, maxAngleEndB, safeLine;
1750 string linesList, lineNames[], pointNames[];
1751 pair distantEnd, distantEnds[];
1752 numeric n, no, distL, maxDist, maxDistEndA, maxDistEndB, a[], angleL, minAngle, maxAngle, minAngleEndB, maxAngleEndA, maxAngleEndB, safeLine;
1753 picture labelImage, linesImage;
1754 boolean omitLine[];
1755 if known pointLinesList.pointName:
1756 linesList := pointLinesList.pointName;
1757 n := 0;
1758 no := 0;
1759 maxDist := 0;
1760 maxAngle := 0;
1761 minAngle := 360;
1762 forsuffixes i = scantokens(linesList):
1763 n := n + 1;
1764 lineNames[n] := str i;
1765 omitLine[n] := false;
1766 if (length(omitLines) > 0):
1767 forsuffixes j = scantokens(omitLines):
1768 if ((str i) = (str j)):
1769 omitLine[n] := true;
1770 no := no + 1;
1771 fi;
1772 endfor;
1773 fi;
1774 if (not omitLine[n]):
1775 safeLine := n;
1776 fi;
1777 if (getAttribute("line", "EndAName", str i) = str pointName):
1778 distantEnds[n] :=
1779 scantokens(getAttribute("line", "EndAName", str i))
1780 + (unitvector(scantokens(getAttribute("line", "EndBName", str i))
1781 - scantokens(getAttribute("line", "EndAName", str i))) scaled (pointLinesSize/scaleFactor)
1782 );
1783 pointNames[n] := getAttribute("line", "EndBName", str i);
1784 else:
1785 distantEnds[n] :=
1786 scantokens(getAttribute("line", "EndBName", str i))
1787 + (unitvector(scantokens(getAttribute("line", "EndAName", str i))
1788 - scantokens(getAttribute("line", "EndBName", str i))) scaled (pointLinesSize/scaleFactor)
1789 );
1790 pointNames[n] := getAttribute("line", "EndAName", str i);
1791 fi;
1792 endfor;
1793 if (n - no > 1):
1794 for i := 1 step 1 until n:
1795 if (not omitLine[i]):
1796 minAngle := 360;
1797 for j := 1 step 1 until n:
1798 if (not omitLine[j]):
1799 distL := abs(distantEnds[i] - distantEnds[j]);
1800 if (distL > maxDist):
1801 maxDist := distL;
1802 maxDistEndA := i;
1803 maxDistEndB := j;
1804 fi;
1805 a1 := angle(distantEnds[i] - pointName);
1806 a2 := angle(distantEnds[j] - pointName);
1807 angleL := a2 - a1;
1808 if (angleL < 0): angleL := angleL + 360; fi;
1809 if (minAngle > angleL) and (angleL <> 0):
1810 minAngle := angleL;
1811 minAngleEndA := j;
1812 fi;
1813 fi;
1814 endfor;
1815 if (maxAngle <= minAngle + 1):
1816 maxAngle := minAngle;
1817 maxAngleEndA := minAngleEndA;
1818 maxAngleEndB := i;
1819 fi;
1820 fi;
1821 endfor;
1822 linesImage := image(
1823 for i := 1 step 1 until n:
1824 if (i <> maxDistEndA) and (i <> maxDistEndB) and (not omitLine[i]):
1825 draw byLineRender(pointName, distantEnds[i],
1826 getAttribute("line", "Color", lineNames[i]), getAttribute("line", "Dashed", lineNames[i]), getAttribute("line", "Thin", lineNames[i]),
1827 pointName, distantEnds[i], 0, 0, 0, 0, scaleFactor);
1828 fi;
1829 endfor;
1830 draw byLineRender(pointName, distantEnds[maxDistEndA],
1831 getAttribute("line", "Color", lineNames[maxDistEndA]),
1832 getAttribute("line", "Dashed", lineNames[maxDistEndA]),
1833 getAttribute("line", "Thin", lineNames[maxDistEndA]),
1834 distantEnds[maxDistEndB], distantEnds[maxDistEndA], 0, 0, 2, 0, scaleFactor);
1835 draw byLineRender(pointName, distantEnds[maxDistEndB],
1836 getAttribute("line", "Color", lineNames[maxDistEndB]),
1837 getAttribute("line", "Dashed", lineNames[maxDistEndB]),
1838 getAttribute("line", "Thin", lineNames[maxDistEndB]),
1839 distantEnds[maxDistEndA], distantEnds[maxDistEndB], 0, 0, 2, 0, scaleFactor);
1840 );
1841 labelImage := image(
1842 if (textLabels):
1843 draw byLabelsOnPolygon(scantokens(pointNames[maxAngleEndA]),pointName,scantokens(pointNames[maxAngleEndB]))(OMIT_FIRST_LABEL + OMIT_LAST_LABEL, 0);
1844 fi;
1845 );
1846 else:
1847 linesImage := image(
1848 draw byLineRender(pointName, distantEnds[safeLine],
1849 getAttribute("line", "Color", lineNames[safeLine]),
1850 getAttribute("line", "Dashed", lineNames[safeLine]),
1851 getAttribute("line", "Thin", lineNames[safeLine]),
1852 pointName, distantEnds[safeLine], 0, 0, 0, 0, scaleFactor);
1853 );
1854 labelImage := image(
1855 if (textLabels):
1856 draw byLabelLineEnd (pointName, distantEnds[safeLine])(0);
1857 fi;
1858 );
1859 fi;
1860 else:
1861 labelImage := image();
1862 linesImage := image();
1863 fi;
1864 image(
1865 draw labelImage;
1866 % this thing is intended to make the overall picture symmetrical
1867 % when all the lines are horizontal.
1868 draw labelImage rotatedabout(pointName scaled scaleFactor, 180) withcolor white;
1869 draw linesImage;
1870 )
1871 enddef;
1872
1873 %
1874 % Arcs and circles
1875 %
1876
1877 vardef byCirclePathGenerate (expr sr, pv) =
1878 save circPath, xp, xang, yang;
1879 path circPath;
1880 color pvt;
1881 numeric xang, yang;
1882 pair xp;
1883 circPath := fullcircle;
1884 if (not omitSpaceRotation):
1885 xp := (bluepart(pv), greenpart(pv));
1886 xang := angle(xp);
1887 pvt := (redpart(pv), ypart(xp rotated -xang), xpart(xp rotated -xang));
1888 yang := angle((bluepart(pvt), redpart(pvt)));
1889 circPath := spaceRotated(circPath)((-xang, yang, 0)); % plane vector to angles conversion should eventually go somewhere
1890 circPath := spaceRotated(circPath)(sr);
1891 circPath := spaceRotated(circPath)(projectionAngle);
1892 fi;
1893 circPath
1894 enddef;
1895
1896 vardef byArcRender (expr o, b, e, r, sr, pv, col, dp, th, dpt, s, et) =
1897 save p, q, m, t, currentLineWidth, srCircle;
1898 path p[], srCircle;
1899 picture q;
1900 pair t[];
1901 numeric m[];
1902 numeric currentLineWidth;
1903 save thirdDimension;
1904 image(
1905 srCircle := byCirclePathGenerate(sr, pv);
1906 currentLineWidth := lineThickness(th);
1907 p0 := ((subpath (b, e) of srCircle) scaled (r*scaleFactor + 2lineWidth*s + lineWidth)) shifted (o*scaleFactor);
1908 p1 := ((subpath (b, e) of srCircle) scaled (r*scaleFactor)) shifted (o*scaleFactor);
1909 p2 := (srCircle scaled (r*scaleFactor + 2lineWidth*s + lineWidth)) shifted (o*scaleFactor);
1910 p3 := ((subpath (b, e) of srCircle) scaled (r*scaleFactor + 2lineWidth*s)) shifted (o*scaleFactor);
1911 p4 := ((subpath (b + sign(b-e)*1/16, b) of srCircle) scaled (r*scaleFactor + 2lineWidth*s)) shifted (o*scaleFactor);
1912 p5 := ((subpath (e, e + sign(e-b)*1/16) of srCircle) scaled (r*scaleFactor + 2lineWidth*s)) shifted (o*scaleFactor);
1913 q := image(
1914 draw p3 withpen pencircle scaled currentLineWidth withcolor white;
1915 draw p3 withpen pencircle scaled currentLineWidth withcolor col if (dp > 0): dashed byDashPattern(arclength(p3), dp, dpt) fi;
1916 draw p4 withpen pencircle scaled currentLineWidth withcolor col;
1917 draw p5 withpen pencircle scaled currentLineWidth withcolor col;
1918 );
1919 if mainPictureMode and ghostLines:
1920 save gP;
1921 picture gP;
1922 gP := ghostPicture;
1923 ghostPicture := image(
1924 draw gP;
1925 draw p3 withcolor white withpen pencircle scaled lineWidthHair;
1926 );
1927 fi;
1928 if (et = 0):
1929 p6 := p0--(o*scaleFactor)--cycle;
1930 else:
1931 t0 := point 0 of p1;
1932 t1 := point 0 of reverse(p1);
1933 t2 := 1/2[t0, t1];
1934 m0 := xpart (p2 intersectiontimes (t2 -- 2[t2, t0]));
1935 m1 := xpart (p2 intersectiontimes (t2 -- 2[t2, t1]));
1936 for i := -(8*3) step 8 until (8*3):
1937 if (abs(b - m0) > abs(b - (m0 + i))):
1938 m0 := m0 + i;
1939 fi;
1940 if (abs(e - m1) > abs(e - (m1 + i))):
1941 m1 := m1 + i;
1942 fi;
1943 endfor;
1944 p6 := (subpath(m0, m1) of p2) -- cycle;
1945 fi;
1946 clip q to p6;
1947 draw q;
1948 ) rotated globalRotation
1949 enddef;
1950
1951 vardef byArcDefineBE@#(suffix o)(expr b, e, r, col, dp, th, s, et) =
1952 if str @# = "":
1953 byArcDefineBE.scantokens(str o)(b, e, r, col, dp, th, s, et);
1954 else:
1955 setAttribute("arc", "Color", str @#, col);
1956 setAttribute("arc", "Dashed", str @#, dp);
1957 setAttribute("arc", "Thin", str @#, th);
1958 setAttribute("arc", "Shift", str @#, s);
1959 setAttribute("arc", "Diameter", str @#, 2r);
1960 setAttribute("arc", "Center", str @#, o);
1961 setAttribute("arc", "CenterName", str @#, str o);
1962 setAttribute("arc", "BName", str @#, "");
1963 setAttribute("arc", "EName", str @#, "");
1964 setAttribute("arc", "Begin", str @#, b);
1965 setAttribute("arc", "End", str @#, e);
1966 setAttribute("arc", "EndType", str @#, et);
1967 setAttribute("arc", "Angle", str @#, angle((point b of fullcircle) - (point e of fullcircle)));
1968 setAttribute("arc", "SpaceRotation", str @#, (0,0,0));
1969 setAttribute("arc", "PlaneVector", str @#, (0,0,1));
1970 setAttribute("arc", "Synonym", str @#, str @#);
1971 fi;
1972 enddef;
1973
1974 vardef byArcBE@#(suffix o)(expr b, e, r, col, dp, th, s, et) =
1975 if str @# = "":
1976 byArcBE.scantokens(str o)(o)(b, e, r, col, dp, th, s, et)
1977 else:
1978 byArcDefineBE.@#(o, b, e, r, col, dp, th, s, et);
1979 byNamedArcExact(@#)
1980 fi
1981 enddef;
1982
1983 vardef byArcDefine@#(suffix o, pb, pe)(expr r, col, dp, th, s, et) =
1984 if str @# = "":
1985 byArcDefine.scantokens(str pb & str o & str pe)(o, pb, pe)(r, col, dp, th, s, et);
1986 else:
1987 save b, e;
1988 numeric b, e;
1989 b := xpart(((fullcircle scaled 2r) shifted o) intersectiontimes (o -- 2[o, pb]));
1990 e := xpart(((fullcircle scaled 2r) shifted o) intersectiontimes (o -- 2[o, pe]));
1991 if (b > e):
1992 b := b - 8;
1993 fi;
1994 byArcDefineBE.@#(o, b, e, r, col, dp, th, s, et);
1995 setAttribute("arc", "BName", str @#, str pb);
1996 setAttribute("arc", "EName", str @#, str pe);
1997 fi;
1998 enddef;
1999
2000 vardef byArc@#(suffix o, pb, pe)(expr r, col, dp, th, s, et) =
2001 if str @# = "":
2002 byArc.scantokens(str pb & str o & str pe)(o, pb, pe)(r, col, dp, th, s, et)
2003 else:
2004 byArcDefine.@#(o, pb, pe, r, col, dp, th, s, et);
2005 byNamedArcExact(@#)
2006 fi
2007 enddef;
2008
2009 vardef byNamedArcSeq (expr s)(text arcslist) =
2010 save i, j, arcC, arcD, dpt, dashStreak;
2011 numeric i, j, arcD, dpt[], dashStreak;
2012 string arcC;
2013 dashStreak := 0;
2014 image(
2015 i := 0;
2016 forsuffixes aNi=arcslist:
2017 j := 0;
2018 dpt[i] := 0;
2019 if getAttribute("arc", "Dashed", str aNi) > 0:
2020 forsuffixes aNj=arcslist:
2021 if (i <> j)
2022 and (getAttribute("arc", "CenterName", str aNi) = getAttribute("arc", "CenterName", str aNj))
2023 and (getAttribute("arc", "Diameter", str aNi) = getAttribute("arc", "Diameter", str aNj)):
2024 if (abs(
2025 (point getAttribute("arc", "Begin", str aNi) of fullcircle) -
2026 (point getAttribute("arc", "End", str aNj) of fullcircle)
2027 ) < 1/100):
2028 dpt[i] := dpt[i] + 2;
2029 fi;
2030 if (abs(
2031 (point getAttribute("arc", "End", str aNi) of fullcircle) -
2032 (point getAttribute("arc", "Begin", str aNj) of fullcircle)
2033 ) < 1/100):
2034 if getAttribute("arc", "Dashed", str aNj) = 0:
2035 dpt[i] := dpt[i] + 1;
2036 fi;
2037 fi;
2038 fi;
2039 j := j + 1;
2040 endfor;
2041 fi;
2042 i := i + 1;
2043 endfor;
2044 i := 0;
2045 forsuffixes aN=arcslist:
2046 draw byArcRender(
2047 scantokens(getAttribute("arc", "CenterName", str aN)),
2048 getAttribute("arc", "Begin", str aN),
2049 getAttribute("arc", "End", str aN),
2050 getAttribute("arc", "Diameter", str aN),
2051 getAttribute("arc", "SpaceRotation", str aN),
2052 getAttribute("arc", "PlaneVector", str aN),
2053 getAttribute("arc", "Color", str aN),
2054 getAttribute("arc", "Dashed", str aN),
2055 getAttribute("arc", "Thin", str aN),
2056 dpt[i],
2057 s,
2058 getAttribute("arc", "EndType", str aN));
2059 i := i + 1;
2060 endfor;
2061 if (autoLabelingMode):
2062 draw byNamedArcLabel(arcslist);
2063 fi;
2064 )
2065 enddef;
2066
2067 vardef byNamedArcExact (text arcslist) =
2068 image(
2069 forsuffixes aN=arcslist:
2070 draw byArcRender(
2071 scantokens(getAttribute("arc", "CenterName", str aN)),
2072 getAttribute("arc", "Begin", str aN),
2073 getAttribute("arc", "End", str aN),
2074 getAttribute("arc", "Diameter", str aN),
2075 getAttribute("arc", "SpaceRotation", str aN),
2076 getAttribute("arc", "PlaneVector", str aN),
2077 getAttribute("arc", "Color", str aN),
2078 getAttribute("arc", "Dashed", str aN),
2079 getAttribute("arc", "Thin", str aN),
2080 0,
2081 getAttribute("arc", "Shift", str aN),
2082 getAttribute("arc", "EndType", str aN));
2083 endfor;
2084 if (autoLabelingMode):
2085 draw byNamedArcLabel(arcslist);
2086 fi;
2087 )
2088 enddef;
2089
2090 vardef byNamedArc (text arcslist) =
2091 image(
2092 forsuffixes aN=arcslist:
2093 draw byArcRender(
2094 scantokens(getAttribute("arc", "CenterName", str aN)),
2095 getAttribute("arc", "Begin", str aN),
2096 getAttribute("arc", "End", str aN),
2097 getAttribute("arc", "Diameter", str aN),
2098 getAttribute("arc", "SpaceRotation", str aN),
2099 getAttribute("arc", "PlaneVector", str aN),
2100 getAttribute("arc", "Color", str aN),
2101 getAttribute("arc", "Dashed", str aN),
2102 getAttribute("arc", "Thin", str aN), 0, 0, 0);
2103 endfor;
2104 if (autoLabelingMode):
2105 draw byNamedArcLabel(arcslist);
2106 fi;
2107 )
2108 enddef;
2109
2110 vardef byNamedArcLabel (text arcslist) =
2111 save pointsListB, pointsListE, pointsListO, pointsListD, labelPos, labelVec, labelAng, i, j, k, allArcsDefined, beginFree, endFree, pointsListCrv;
2112 string pointsListB[], pointsListE[], pointsListO[];
2113 numeric i, j, k, pointsListD[], labelAng;
2114 boolean allArcsDefined, beginFree, endFree;
2115 color labelVec;
2116 pair labelPos;
2117 path pointsListCrv[];
2118 image(
2119 if (textLabels):
2120 i := -1;
2121 allArcsDefined := true;
2122 forsuffixes aN=arcslist:
2123 i := i + 1;
2124 pointsListB[i] := getAttribute("arc", "BName", str aN);
2125 pointsListE[i] := getAttribute("arc", "EName", str aN);
2126 pointsListO[i] := getAttribute("arc", "CenterName", str aN);
2127 pointsListR[i] := getAttribute("arc", "Diameter", str aN);
2128 begingroup
2129 save thirdDimension;
2130 pointsListCrv[i] := subpath
2131 (getAttribute("arc", "Begin", str aN), getAttribute("arc", "End", str aN))
2132 of byCirclePathGenerate (getAttribute("arc", "SpaceRotation", str aN), getAttribute("arc", "PlaneVector", str aN));
2133 endgroup;
2134 if (getAttribute("arc", "BName", str aN) = "")
2135 or (getAttribute("arc", "EName", str aN) = ""):
2136 allArcsDefined := false;
2137 fi;
2138 endfor;
2139 if allArcsDefined:
2140 for j := 0 step 1 until i:
2141 beginFree := true;
2142 endFree := true;
2143 for k := 0 step 1 until i:
2144 if (j <> k):
2145 if (pointsListB[j] = pointsListE[k]):
2146 beginFree := false;
2147 fi;
2148 if (pointsListE[j] = pointsListB[k]):
2149 endFree := false;
2150 fi;
2151 fi;
2152 endfor;
2153 if beginFree:
2154 labelAng := angle(direction 0 of pointsListCrv[j]) + 180;
2155 labelPos := ((point 0 of pointsListCrv[j]) * pointsListR[j]) + scantokens(pointsListO[j]);
2156 draw byTextLabel(pointLabel)(
2157 pointsListB[j],
2158 labelPos,
2159 labelAng,
2160 textLabelShift);
2161 fi;
2162 if endFree:
2163 labelAng := angle(direction 0 of reverse(pointsListCrv[j])) + 180;
2164 labelPos := ((point 0 of reverse(pointsListCrv[j])) * pointsListR[j]) + scantokens(pointsListO[j]);
2165 draw byTextLabel(pointLabel)(
2166 pointsListE[j],
2167 labelPos,
2168 labelAng,
2169 textLabelShift);
2170 fi;
2171 endfor;
2172 fi;
2173 fi;
2174 )
2175 enddef;
2176
2177 vardef byCircleDefineFree@#(expr o, r, col, dp, th, s) =
2178 if str @# = "":
2179 byCircleDefineFree.aCircle(o, r, col, dp, th, s);
2180 else:
2181 setAttribute("circle", "Color", str @#, col);
2182 setAttribute("circle", "Dashed", str @#, dp);
2183 setAttribute("circle", "Thin", str @#, th);
2184 setAttribute("circle", "Shift", str @#, s);
2185 setAttribute("circle", "Diameter", str @#, 2r);
2186 setAttribute("circle", "Center", str @#, o);
2187 setAttribute("circle", "CenterName", str @#, "");
2188 setAttribute("circle", "AName", str @#, "");
2189 setAttribute("circle", "BName", str @#, "");
2190 setAttribute("circle", "CName", str @#, "");
2191 setAttribute("circle", "SpaceRotation", str @#, (0,0,0));
2192 setAttribute("circle", "PlaneVector", str @#, (0,0,1));
2193 setAttribute("circle", "Synonym", str @#, str @#);
2194 fi;
2195 enddef;
2196
2197 vardef byCircleDefineR@#(suffix o)(expr r, col, dp, th, s) =
2198 if str @# = "":
2199 byCircleDefineR.scantokens(str o)(o)(r, col, dp, th, s)
2200 else:
2201 byCircleDefineFree.@#(o, r, col, dp, th, s);
2202 setAttribute("circle", "CenterName", str @#, str o);
2203 fi;
2204 enddef;
2205
2206 vardef byCircleR@#(suffix o)(expr r, col, dp, th, s) =
2207 if str @# = "":
2208 byCircleR.scantokens(str o)(o)(r, col, dp, th, s)
2209 else:
2210 byCircleDefineR.@#(o, r, col, dp, th, s);
2211 byNamedCircle(@#)
2212 fi
2213 enddef;
2214
2215 vardef byCircleDefine@#(suffix o, a)(expr col, dp, th, s) =
2216 if str @# = "":
2217 byCircleDefine.scantokens(str o & str a)(o, a)(col, dp, th, s)
2218 else:
2219 byCircleDefineR.@#(o, abs(a-o), col, dp, th, s);
2220 setAttribute("circle", "AName", str @#, str a);
2221 fi;
2222 enddef;
2223
2224 vardef byCircle@#(suffix o, a)(expr col, dp, th, s) =
2225 if str @# = "":
2226 byCircle.scantokens(str o & str a)(o, a)(col, dp, th, s)
2227 else:
2228 byCircleDefine.@#(o, a, col, dp, th, s);
2229 byNamedCircle(@#)
2230 fi
2231 enddef;
2232
2233 vardef byCircleABCDefine@#(suffix a, b, c)(expr col, dp, th, s) =
2234 if str @# = "":
2235 byCircleABCDefine.scantokens(str a & str b & str c)(a, b, c)(col, dp, th, s)
2236 else:
2237 if mainPictureMode:
2238 appendList(allCirclesList, str @#, 1, true);
2239 fi;
2240 save o, r;
2241 pair o;
2242 numeric r;
2243 o := byFindCircleCenterABC(a, b, c);
2244 if byIsPointInSpace(a, b, c):
2245 r := absXYZ(pointXYZ.a - byFindCircleCenterABCinSpace(pointXYZ.a, pointXYZ.b, pointXYZ.c));
2246 else:
2247 r := abs(o - a);
2248 fi;
2249 byCircleDefineFree.@#(o, r, col, dp, th, s);
2250 setAttribute("circle", "AName", str @#, str a);
2251 setAttribute("circle", "BName", str @#, str b);
2252 setAttribute("circle", "CName", str @#, str c);
2253 if byIsPointInSpace(a, b, c):
2254 save pv;
2255 color pv;
2256 pv := unitvectorXYZ((pointXYZ.a - pointXYZ.b) crossproduct (pointXYZ.c - pointXYZ.b));
2257 setAttribute("circle", "PlaneVector", str @#, pv);
2258 fi;
2259 fi;
2260 enddef;
2261
2262 vardef byCircleABC@#(suffix a, b, c)(expr col, dp, th, s) =
2263 if str @# = "":
2264 byCircleABC.scantokens(str a & str b & str c)(a, b, c)(col, dp, th, s)
2265 else:
2266 byCircleABCDefine@#(a, b, c)(col, dp, th, s);
2267 byNamedCircle(@#)
2268 fi
2269 enddef;
2270
2271 vardef byFindCircleCenterABC (suffix a, b, c) =
2272 save rv;
2273 pair rv;
2274 if byIsPointInSpace(a, b, c):
2275 save rvInSpace;
2276 color rvInSpace;
2277 rvInSpace := byFindCircleCenterABCinSpace(pointXYZ.a, pointXYZ.b, pointXYZ.c) colorrotated projectionAngle;
2278 rv := (redpart(rvInSpace), greenpart(rvInSpace));
2279 else:
2280 rv = whatever[1/2[a, b], 1/2[a, b] + ((a - b) rotated 90)]
2281 = whatever[1/2[b, c], 1/2[b, c] + ((b - c) rotated 90)];
2282 fi;
2283 rv
2284 enddef;
2285
2286 vardef byFindCircleCenterABCinSpace (expr a, b, c) =
2287 save vec;
2288 color vec[];
2289 vec0 := ((a - b) crossproduct (c - b));
2290 vec1 := byRotateAroundAxis (a - b, (0, 0, 0), 90, vec0);
2291 vec2 := byRotateAroundAxis (c - b, (0, 0, 0), 90, vec0);
2292 vec3 = whatever[1/2[a, b], 1/2[a, b] + vec1]
2293 = whatever[1/2[b, c], 1/2[b, c] + vec2]; % Fails from time to time because of some roundoff errors
2294 vec3
2295 enddef;
2296
2297 vardef byReturnCircleCenter (expr circleName) =
2298 if (getAttribute("circle", "CenterName", circleName) <> ""):
2299 scantokens(getAttribute("circle", "CenterName", circleName))
2300 elseif (getAttribute("circle", "BName", circleName) <> ""):
2301 byFindCircleCenterABC(
2302 scantokens(getAttribute("circle", "AName", circleName)),
2303 scantokens(getAttribute("circle", "BName", circleName)),
2304 scantokens(getAttribute("circle", "CName", circleName))
2305 )
2306 else:
2307 projectPointIfInSpace(getAttribute("circle", "Center", circleName))
2308 fi
2309 enddef;
2310
2311 vardef byNamedCircle (text circlesList) =
2312 save circlecenter;
2313 pair circlecenter;
2314 image(
2315 forsuffixes cN=circlesList:
2316 circlecenter := byReturnCircleCenter(str cN);
2317 draw byArcRender (
2318 circlecenter, 0, 8,
2319 getAttribute("circle", "Diameter", str cN),
2320 getAttribute("circle", "SpaceRotation", str cN),
2321 getAttribute("circle", "PlaneVector", str cN),
2322 getAttribute("circle", "Color", str cN),
2323 getAttribute("circle", "Dashed", str cN),
2324 getAttribute("circle", "Thin", str cN),
2325 0,
2326 getAttribute("circle", "Shift", str cN), 0);
2327 if (textLabels and autoLabelingMode):
2328 if (getAttribute("circle", "CenterName", str cN) <> ""):
2329 draw byTextLabel(pointLabel)(
2330 getAttribute("circle", "CenterName", str cN),
2331 scantokens(getAttribute("circle", "CenterName", str cN)),
2332 0, 0);
2333 fi;
2334 if (getAttribute("circle", "AName", str cN) <> ""):
2335 draw byLabelCircle(getAttribute("circle", "AName", str cN), str cN);
2336 fi;
2337 if (getAttribute("circle", "BName", str cN) <> ""):
2338 draw byLabelCircle(getAttribute("circle", "BName", str cN), str cN);
2339 fi;
2340 if (getAttribute("circle", "CName", str cN) <> ""):
2341 draw byLabelCircle(getAttribute("circle", "CName", str cN), str cN);
2342 fi;
2343 fi;
2344 endfor;
2345 )
2346 enddef;
2347
2348 %
2349 % Arbitraty figures
2350 %
2351
2352 vardef byArbitraryFigureDefine@#(expr p, col, dp, th) =
2353 if str @# = "":
2354 byArbitraryFigureDefine.anArbitraryFigure(p, col, dp, th);
2355 else:
2356 setAttribute("arbitraryFigure", "Path", str @#, p);
2357 setAttribute("arbitraryFigure", "Color", str @#, col);
2358 setAttribute("arbitraryFigure", "Dashed", str @#, dp);
2359 setAttribute("arbitraryFigure", "Thin", str @#, th);
2360 setAttribute("arbitraryFigure", "Synonym", str @#, str @#);
2361 fi;
2362 enddef;
2363
2364 vardef byArbitraryFigure@#(expr p, col, dp, th) =
2365 if str @# = "":
2366 byArbitraryFigure.anArbitraryFigure(p, col, dp, th)
2367 else:
2368 byArbitraryFigureDefine@#(p, col, dp, th);
2369 byNamedArbitraryFigure(@#)
2370 fi
2371 enddef;
2372
2373 vardef byNamedArbitraryFigure (text arbitraryFiguresList) =
2374 save p;
2375 path p;
2376 image(
2377 forsuffixes pN=arbitraryFiguresList:
2378 p := getAttribute("arbitraryFigure", "Path", str pN);
2379 draw byArbitraryFigureRender
2380 (p, getAttribute("arbitraryFigure", "Color", str pN),
2381 getAttribute("arbitraryFigure", "Dashed", str pN),
2382 getAttribute("arbitraryFigure", "Thin", str pN));
2383 endfor;
2384 ) rotated globalRotation
2385 enddef;
2386
2387 vardef byArbitraryFigureRender (expr p, col, dp, th) =
2388 image(
2389 draw (p scaled scaleFactor)
2390 withpen pencircle scaled lineThickness(th)
2391 withcolor col
2392 if (dp > 0):
2393 dashed byDashPattern(arclength(p), dp, 0)
2394 fi;
2395 )
2396 enddef;
2397
2398 vardef byArbitraryCurveDefine@#(text pointsList)(expr col, dp, th) =
2399 if str @# = "":
2400 byArbitraryCurveDefine.anArbitraryCurve(pointsList)(col, dp, th);
2401 else:
2402 save pL, p;
2403 string pL;
2404 path p;
2405 pL := "";
2406 p := byListToPath(pointsList);
2407 forsuffixes pN=pointsList:
2408 appendList(pL, str pN, 1, false);
2409 endfor;
2410 setAttribute("arbitraryCurve", "PointsList", str @#, pL);
2411 setAttribute("arbitraryCurve", "Path", str @#, p);
2412 setAttribute("arbitraryCurve", "Color", str @#, col);
2413 setAttribute("arbitraryCurve", "Dashed", str @#, dp);
2414 setAttribute("arbitraryCurve", "Thin", str @#, th);
2415 setAttribute("arbitraryCurve", "Synonym", str @#, str @#);
2416 fi;
2417 enddef;
2418
2419 vardef byNamedArbitraryCurve (text curvesList) =
2420 save p;
2421 path p;
2422 image(
2423 forsuffixes cN=curvesList:
2424 p := byListToPath(scantokens(getAttribute("arbitraryCurve", "PointsList", str cN)));
2425 draw byArbitraryFigureRender
2426 (p, getAttribute("arbitraryCurve", "Color", str cN),
2427 getAttribute("arbitraryCurve", "Dashed", str cN),
2428 getAttribute("arbitraryCurve", "Thin", str cN)) rotated globalRotation;
2429 if (autoLabelingMode):
2430 if (textLabels):
2431 draw byLabelsOnPolygon(scantokens(getAttribute("arbitraryCurve", "PointsList", str cN)), noPoint)(OMIT_LABELS_AT_STRAIGHT_ANGLES, 0);
2432 fi;
2433 fi;
2434 endfor;
2435 )
2436 enddef;
2437
2438 vardef byListToPath(text pointsList) =
2439 save pathString, rp;
2440 string pathString;
2441 path rp;
2442 pathString := "";
2443 forsuffixes pN=pointsList:
2444 pathString := pathString & str pN & "..";
2445 endfor;
2446 rp := scantokens(substring (0, length(pathString) - 2) of pathString);
2447 rp
2448 enddef;
2449
2450 vardef byArbitraryCurve@#(text pointsList)(expr col, dp, th) =
2451 if str @# = "":
2452 byArbitraryCurve.anArbitraryCurve(pointsList)(col, dp, th)
2453 else:
2454 byArbitraryCurveDefine@#(pointsList)(col, dp, th);
2455 byNamedArbitraryCurve(@#)
2456 fi
2457 enddef;
2458
2459 %
2460 % Filled figures
2461 %
2462
2463 vardef byFilledCircleSegment@#(suffix o)(expr r, b, e, col) =
2464 if str @# = "":
2465 byFilledCircleSegment.o(o, r, b, e, col)
2466 else:
2467 setAttribute("filledCircleSegment", "Color", str @#, col);
2468 setAttribute("filledCircleSegment", "Diameter", str @#, 2r);
2469 setAttribute("filledCircleSegment", "Begin", str @#, b);
2470 setAttribute("filledCircleSegment", "End", str @#, e);
2471 setAttribute("filledCircleSegment", "CenterName", str @#, str o);
2472 setAttribute("filledCircleSegment", "Synonym", str @#, str @#);
2473 byNamedFilledCircleSegment(@#)
2474 fi
2475 enddef;
2476
2477 vardef byNamedFilledCircleSegment (text filledCircleSegmentList) =
2478 save p;
2479 path p;
2480 image(
2481 forsuffixes csN=filledCircleSegmentList:
2482 fill (
2483 (subpath
2484 (getAttribute("filledCircleSegment", "Begin", str csN),
2485 getAttribute("filledCircleSegment", "End", str csN))
2486 of fullcircle)
2487 --cycle)
2488 scaled (getAttribute("filledCircleSegment", "Diameter", str csN)*scaleFactor)
2489 shifted (scantokens(getAttribute("filledCircleSegment", "CenterName", str csN))*scaleFactor)
2490 withcolor getAttribute("filledCircleSegment", "Color", str csN);
2491 endfor;
2492 ) rotated globalRotation
2493 enddef;
2494
2495 vardef byFilledCircleSector@#(suffix o)(expr r, b, e, col) =
2496 if str @# = "":
2497 byFilledCircleSector.o(o, r, b, e, col)
2498 else:
2499 setAttribute("filledCircleSector", "Color", str @#, col);
2500 setAttribute("filledCircleSector", "Diameter", str @#, 2r);
2501 setAttribute("filledCircleSector", "Begin", str @#, b);
2502 setAttribute("filledCircleSector", "End", str @#, e);
2503 setAttribute("filledCircleSector", "CenterName", str @#, str o);
2504 byNamedFilledCircleSector(@#)
2505 fi
2506 enddef;
2507
2508 vardef byNamedFilledCircleSector (text filledCircleSectorList) =
2509 image(
2510 forsuffixes csN=filledCircleSectorList:
2511 fill ((subpath
2512 (getAttribute("filledCircleSector", "Begin", str csN),
2513 getAttribute("filledCircleSector", "End", str csN))
2514 of fullcircle) -- (0, 0) -- cycle)
2515 scaled (getAttribute("filledCircleSector", "Diameter", str csN)*scaleFactor)
2516 shifted (scantokens(getAttribute("filledCircleSector", "CenterName", str csN))*scaleFactor)
2517 withcolor getAttribute("filledCircleSector", "Color", str csN);
2518 endfor;
2519 ) rotated globalRotation
2520 enddef;
2521
2522 vardef byPolygonDefine@#(text pointsList)(suffix col) =
2523 if str @# = "":
2524 byPolygonDefine.scantokens(byPolygonName(pointsList))(pointsList)(col);
2525 else:
2526 save pL, pLraw, p, tn;
2527 path p;
2528 string pL, pLraw;
2529 numeric tn;
2530 pL := "";
2531 pLraw := "";
2532 p := forsuffixes pN=pointsList: pN -- endfor cycle;
2533 tn := turningnumber(p);
2534 forsuffixes pN=pointsList:
2535 appendList(pL, str pN, -tn, false);
2536 appendList(pLraw, str pN, 1, false);
2537 endfor;
2538 setAttribute("polygon", "Color", str @#, col);
2539 setAttribute("polygon", "ColorName", str @#, str col);
2540 setAttribute("polygon", "Transparency", str @#, 1);
2541 setAttribute("polygon", "PointsList", str @#, pL);
2542 setAttribute("polygon", "PointsListRaw", str @#, pLraw);
2543 setAttribute("polygon", "Synonym", str @#, str @#);
2544 fi;
2545 enddef;
2546
2547 vardef byPolygonPathFromList (expr pointsList) =
2548 forsuffixes pN=scantokens(pointsList): pN -- endfor cycle
2549 enddef;
2550
2551 vardef byPolygon@#(text pointsList)(suffix col) =
2552 if str @# = "":
2553 byPolygon.scantokens(byPolygonName(pointsList))(pointsList)(col)
2554 else:
2555 byPolygonDefine.@#(pointsList)(col);
2556 byNamedPolygon(@#)
2557 fi
2558 enddef;
2559
2560 vardef byPolygonName(text pointsList) =
2561 save polygonName;
2562 string polygonName;
2563 polygonName := "";
2564 forsuffixes pN=pointsList:
2565 polygonName := polygonName & str pN;
2566 endfor;
2567 polygonName
2568 enddef;
2569
2570 vardef byNamedPolygon (text polygonsList) =
2571 save polyPath, polyCol, polyTransp, polyGhost, shadingFunctionExists;
2572 path polyPath;
2573 numeric polyTransp;
2574 picture polyGhost;
2575 boolean shadingFunctionExists;
2576 image(
2577 draw image(
2578 forsuffixes pN=polygonsList:
2579 polyPath := byPolygonPathFromList(getAttribute("polygon", "PointsList", str pN)) scaled scaleFactor;
2580 defineColor.polyCol(getAttribute("polygon", "Color", str pN));
2581 polyTransp := getAttribute("polygon", "Transparency", str pN);
2582 if known shadingFor.scantokens(getAttribute("polygon", "ColorName", str pN)):
2583 shadingFunctionExists := true;
2584 else:
2585 shadingFunctionExists := false;
2586 fi;
2587 if (not isWhite(polyCol)):
2588 if not shadingFunctionExists:
2589 fill polyPath
2590 withcolor
2591 if polyTransp = 1:
2592 polyCol
2593 else:
2594 transparent(1, polyTransp, polyCol)
2595 fi;
2596 else:
2597 shadingFunction.scantokens(getAttribute("polygon", "ColorName", str pN))
2598 (polyPath, polyCol)(scantokens(getAttribute("polygon", "PointsListRaw", str pN)));
2599 fi;
2600 fi;
2601 polyGhost := image(
2602 if not shadingFunctionExists:
2603 if (not isLight(polyCol)):
2604 draw ghostPicture;
2605 fi;
2606 draw polyPath
2607 withcolor polyCol
2608 withpen pencircle scaled 2lineWidth;
2609 fi;
2610 );
2611 clip polyGhost to polyPath;
2612 draw polyGhost;
2613 if (isLight(polyCol)) and not shadingFunctionExists:
2614 draw (byPolygonPathFromList(getAttribute("polygon", "PointsList", str pN)) scaled scaleFactor)
2615 withcolor selectOutlineColor.scantokens(getAttribute("polygon", "ColorName", str pN))
2616 withpen pencircle scaled lineWidthThin;
2617 fi;
2618 if mainPictureMode and ghostLines:
2619 save gP;
2620 picture gP;
2621 gP := ghostPicture;
2622 ghostPicture := image(
2623 draw gP;
2624 draw polyPath withcolor white withpen pencircle scaled lineWidthHair;
2625 );
2626 fi;
2627 endfor;
2628 ) rotated globalRotation;
2629 if (autoLabelingMode):
2630 draw byLabelPolygon(-1)(polygonsList);
2631 fi;
2632 )
2633 enddef;
2634
2635 vardef byMergePolygons (text polygonsList) =
2636 save pointsList, preList, postList, mergedList, points, postPoints, i, j, k, l, m, n, o, iteration, beforeJoin, polygonChecked, pointChecked;
2637 string pointsList[], preList[], postList[], mergedList, points[], postPoints[];
2638 numeric l, m, n, o, iteration;
2639 boolean beforeJoin, polygonChecked[], pointChecked[];
2640 n := -1;
2641 forsuffixes pN=polygonsList:
2642 n := n + 1;
2643 pointsList[n] := getAttribute("polygon", "PointsList", str pN);
2644 polygonChecked[n] := false;
2645 endfor;
2646 if (n > 0):
2647 iteration := -1;
2648 m := -1;
2649 forever:
2650 preList0 := "";
2651 postList0 := "";
2652 beforeJoin := true;
2653 iteration := iteration + 1;
2654 l := -1;
2655 forsuffixes i = scantokens(pointsList0):
2656 l := l + 1;
2657 if (l > m):
2658 pointChecked[l] := false;
2659 fi;
2660 if (beforeJoin):
2661 preList0 := preList0 & ", " & str i;
2662 if (not pointChecked[l]):
2663 for j := 1 step 1 until n:
2664 if (beforeJoin and not polygonChecked[j]):
2665 preList1 := "";
2666 postList1 := "";
2667 forsuffixes k=scantokens(pointsList[j]):
2668 if (beforeJoin):
2669 preList1 := preList1 & ", " & str k;
2670 if (k = i):
2671 beforeJoin := false;
2672 polygonChecked[j] := true;
2673 pointChecked[l] := true;
2674 o := l;
2675 fi;
2676 else:
2677 postList1 := postList1 & ", " & str k;
2678 fi;
2679 endfor;
2680 fi;
2681 endfor;
2682 fi;
2683 else:
2684 postList0 := postList0 & ", " & str i;
2685 fi;
2686 endfor;
2687 if (not beforeJoin):
2688 mergedList := preList0 & postList1 & preList1 & postList0;
2689 mergedList := substring (2, length(mergedList)) of mergedList;
2690 pointsList0 := polygonCleanup(scantokens(mergedList));
2691 if (length(pointsList0) < length(mergedList)):
2692 pointChecked[o] := false;
2693 mergedList := pointsList0;
2694 fi;
2695 m := l;
2696 fi;
2697 exitif(beforeJoin or (iteration > 100));
2698 endfor;
2699 else:
2700 mergedList := pointsList0;
2701 fi;
2702 mergedList
2703 enddef;
2704
2705 vardef polygonCleanup (text pointsList) =
2706 save cleanedList, points, m, n, iteration;
2707 string cleanedList, points[];
2708 numeric m, n, iteration;
2709 n := -1;
2710 forsuffixes pL = pointsList:
2711 n := n + 1;
2712 points[n] := str pL;
2713 endfor;
2714 iteration := -1;
2715 forever:
2716 iteration := iteration + 1;
2717 m := 0;
2718 for i := 0 step 1 until n:
2719 if (points[cycleval(i-2, n + 1)] = points[i]):
2720 m := m + 2;
2721 else:
2722 points[cycleval(i-m, n + 1)] := points[i];
2723 fi;
2724 endfor;
2725 n := n - m;
2726 exitif((m = 0) or (iteration > 100));
2727 endfor;
2728 for i := 0 step 1 until n:
2729 appendList(cleanedList, points[i], 1, false);
2730 endfor;
2731 cleanedList
2732 enddef;
2733
2734 %
2735 % Angles
2736 %
2737
2738 vardef byAngleDefine@#(suffix a, b, c)(expr col, sty) =
2739 if str @# = "":
2740 byAngleDefine.scantokens(str a & str b & str c)(a, b, c)(col, sty);
2741 else:
2742 if mainPictureMode:
2743 appendList(pointAnglesList.b, str @#, 1, true);
2744 appendList(anglePointsList, str b, 1, true);
2745 fi;
2746 save p, q, d, ad, ac, at;
2747 path p;
2748 picture q;
2749 numeric ad, at;
2750 boolean ac;
2751 if autoRightAngles:
2752 if isRightAngle(a, b, c):
2753 at := 1;
2754 else:
2755 at := 0;
2756 fi;
2757 else:
2758 at := 0;
2759 fi;
2760 p := byConstructAngleArc(a, b, c, at);
2761 ad := angle(point (arctime arclength(p)/2 of p) of p);
2762 if (ypart(unitvector(a-b) rotated -ad)) > 0:
2763 ac := true;
2764 else:
2765 ac := false;
2766 fi;
2767 setAttribute("angle", "Arc", str @#, p);
2768 setAttribute("angle", "ArcClockwise", str @#, ac);
2769 setAttribute("angle", "ArcType", str @#, at);
2770 setAttribute("angle", "Val", str @#, 180*arclength(p scaled 2)/pi);
2771 setAttribute("angle", "AVal", str @#, angle(a-b));
2772 setAttribute("angle", "CVal", str @#, angle(c-b));
2773 setAttribute("angle", "Direction", str @#, ad);
2774 setAttribute("angle", "Color", str @#, col);
2775 setAttribute0("angle", "OptionalColor", str @#, byDefaultAngleOptionalColor);
2776 setAttribute("angle", "Style", str @#, sty);
2777 setAttribute("angle", "AName", str @#, str a);
2778 setAttribute("angle", "BName", str @#, str b);
2779 setAttribute("angle", "CName", str @#, str c);
2780 setAttribute("angle", "ScaleCorrection", str @#, 1);
2781 setAttribute("angle", "Standalone", str @#, 0);
2782 setAttribute("angle", "Synonym", str @#, str @#);
2783 fi;
2784 enddef;
2785
2786 vardef byConsiderAngleRight(suffix angleName) =
2787 setAttribute("angle", "ArcType", str angleName, 1);
2788 enddef;
2789
2790 vardef byConstructAngleArc (suffix a, b, c)(expr s) =
2791 save p, d, cutTime, q;
2792 pair d;
2793 path p[];
2794 color q[];
2795 numeric cutTime[];
2796 d := 1/2[unitvector(a-b),unitvector(c-b)];
2797 if (abs(d) = 0):
2798 d := (unitvector(a-c) rotated 90);
2799 fi;
2800 if (s = 0):
2801 p0 := fullcircle rotated -angle(a-b);
2802 if (attributeExists("point", "XYZ", str a)
2803 and attributeExists("point", "XYZ", str b)
2804 and attributeExists("point", "XYZ", str c)):
2805 p0 := byFullCircleToPlane(
2806 getAttribute("point", "XYZ", str a),
2807 getAttribute("point", "XYZ", str b),
2808 getAttribute("point", "XYZ", str c)
2809 );
2810 fi;
2811 cutTime1 := xpart((p0 scaled angleSize) intersectiontimes ((0, 0)--(unitvector(a - b) scaled 2angleSize)));
2812 cutTime2 := xpart((p0 scaled angleSize) intersectiontimes ((0, 0)--(unitvector(c - b) scaled 2angleSize)));
2813 p1 := subpath (cutTime1, cutTime2) of p0;
2814 p2 := subpath (cutTime1 - length(p0), cutTime2) of p0;
2815 p3 := subpath (cutTime1 + length(p0), cutTime2) of p0;
2816 if (arclength(p2) > arclength(p3)):
2817 p4 := p3;
2818 else:
2819 p4 := p2;
2820 fi;
2821 if (arclength(p1) < arclength(p4)):
2822 p4 := p1;
2823 fi;
2824 elseif (s = 1):
2825 if (attributeExists("point", "XYZ", str a)
2826 and attributeExists("point", "XYZ", str b)
2827 and attributeExists("point", "XYZ", str c)):
2828 q1 := unitvectorXYZ(pointXYZ.a - pointXYZ.b) colorrotated projectionAngle;
2829 q3 := unitvectorXYZ(pointXYZ.c - pointXYZ.b) colorrotated projectionAngle;
2830 q2 := q1 + q3;
2831 p4 := 1/2(redpart(q1), greenpart(q1))
2832 -- 1/2(redpart(q2), greenpart(q2))
2833 -- 1/2(redpart(q3), greenpart(q3));
2834 else:
2835 p4 := 1/2unitvector(a - b)
2836 -- 1/2(unitvector(a - b) + unitvector(c - b))
2837 -- 1/2unitvector(c - b);
2838 fi;
2839 fi;
2840 p4
2841 enddef;
2842
2843 vardef byIsArcClockwise (expr p) =
2844 save rv;
2845 boolean rv;
2846 if ypart(direction (1/2length(p)) of (p rotated -angle(point 1/2length(p) of p))) < 0:
2847 rv := true;
2848 else:
2849 rv := false;
2850 fi;
2851 rv
2852 enddef;
2853
2854 vardef byFullCircleToPlane (expr colPlaneA, colPlaneB, colPlaneC) =
2855 save dvec, fvec, p;
2856 color dvec;
2857 pair fvec;
2858 path p;
2859 dvec := unitvectorXYZ((colPlaneA-colPlaneB) crossproduct (colPlaneC-colPlaneB));
2860 dvec := dvec colorrotated projectionAngle;
2861 fvec := (redpart dvec, greenpart dvec);
2862 if (abs(fvec) > 0):
2863 p := ((fullcircle rotated -angle(fvec)) xscaled sqrt(1-abs(fvec)**2)) rotated angle(fvec);
2864 else:
2865 p := fullcircle;
2866 fi;
2867 p
2868 enddef;
2869
2870 vardef byAngle@#(suffix a, b, c)(expr col, sty) =
2871 if str @# = "":
2872 byAngle.scantokens(str a & str b & str c)(a, b, c, col, sty)
2873 else:
2874 byAngleDefine@#(a, b, c, col, sty);
2875 byNamedAngle(@#)
2876 fi
2877 enddef;
2878
2879 vardef byAngleDefineExtended@#(suffix a, b, c)(expr col, sty)(text optionalColors) =
2880 if str @# = "":
2881 byAngleDefineExtended.scantokens(str a & str b & str c)(a, b, c)(col, sty)(optionalColors);
2882 else:
2883 save n;
2884 numeric n;
2885 n := 0;
2886 byAngleDefine.@#(a, b, c, col, sty);
2887 forsuffixes oC=optionalColors:
2888 setAttribute[n]("angle", "OptionalColor", str @#, oC);
2889 n := n + 1;
2890 endfor;
2891 fi;
2892 enddef;
2893
2894 vardef byAngleExtended@#(suffix a, b, c)(expr col, sty)(text optionalColors) =
2895 if str @# = "":
2896 byAngleExtended.scantokens(str a & str b & str c)(a, b, c)(col, sty)(optionalColors)
2897 else:
2898 byAngleDefineExtended@#(a, b, c)(col, sty)(optionalColors);
2899 byNamedAngle(@#)
2900 fi
2901 enddef;
2902
2903 vardef byNamedAngle (text anglesList) =
2904 save p, q, processedAL;
2905 picture q;
2906 path p;
2907 string processedAL;
2908 image(
2909 processedAL := byProcessAnglesList(anglesList);
2910 forsuffixes aN=scantokens(processedAL):
2911 setAttribute("angle", "ScaleCorrection", str aN, angleScale);
2912 p := byConstructAngleArc(
2913 scantokens(getAttribute("angle", "AName", str aN)),
2914 scantokens(getAttribute("angle", "BName", str aN)),
2915 scantokens(getAttribute("angle", "CName", str aN)),
2916 getAttribute("angle", "ArcType", str aN));
2917 q := image(
2918 if (getAttribute("angle", "Color", str aN) <> white):
2919 if (typeOf(getAttribute("angle", "Style", str aN)) = "numeric"):
2920 draw scantokens(byAngleMacroName[getAttribute("angle", "Style", str aN)]) (p, getAttribute("angle", "Color", str aN), angleOptionalColor.scantokens(angleSynonym.aN));
2921 elseif (typeOf(getAttribute("angle", "Style", str aN)) = "string"):
2922 draw scantokens("byAngleM" & getAttribute("angle", "Style", str aN)) (p, getAttribute("angle", "Color", str aN), angleOptionalColor.scantokens(angleSynonym.aN));
2923 fi;
2924 else:
2925 draw byAngleMWhite(p, 0, 0);
2926 fi;
2927 if solidAngleMode and (textLabels and autoLabelingMode):
2928 draw (p scaled (1/3angleScale*angleSize))--(0,0)--cycle withcolor white;
2929 fill (p scaled (1/3angleScale*angleSize))--(0,0)--cycle withcolor white;
2930 fi;
2931 );
2932 draw (q shifted (scantokens(getAttribute("angle", "BName", str aN)) scaled scaleFactor)) rotated globalRotation;
2933 endfor;
2934 )
2935 enddef;
2936
2937 vardef byNamedSolidAngle (text anglesList) =
2938 save solidAngleMode, processedAL, centerName, rv;
2939 boolean solidAngleMode;
2940 string processedAL, centerName;
2941 processedAL := byProcessAnglesList(anglesList);
2942 solidAngleMode := true;
2943 image(
2944 forsuffixes aN := scantokens(processedAL):
2945 centerName := getAttribute("angle", "BName", str aN);
2946 endfor;
2947 draw byNamedAngle(anglesList);
2948 if (textLabels and autoLabelingMode):
2949 draw byTextLabel(pointLabel)(centerName, scantokens(centerName), 0, 0);
2950 fi;
2951 )
2952 enddef;
2953
2954 vardef byProcessAnglesList@#(text anglesListToProcess)=
2955 save processedAnglesList, compoundAngleString, splitAnglePoints;
2956 string processedAnglesList, compoundAngleString, splitAnglePoints;
2957 processedAnglesList := "";
2958 splitAnglePoints := "";
2959 forsuffixes aN=anglesListToProcess:
2960 if attributeExists("angle", "Synonym", str aN):
2961 appendList(processedAnglesList, str aN, 1, false);
2962 else:
2963 splitAnglePoints := bySplitStringIntoAngles(str aN);
2964 if (known aN) and (length(splitAnglePoints) = 0):
2965 compoundAngleString := pointAnglesList.aN;
2966 else:
2967 compoundAngleString := byConstructCompoundAngle(scantokens(splitAnglePoints));
2968 fi;
2969 if compoundAngleString <> "":
2970 appendList(processedAnglesList, compoundAngleString, 1, false);
2971 if str @# <> "":
2972 appendList(@#, splitAnglePoints, 1, false);
2973 fi;
2974 else:
2975 errmessage("Unknown angle: " & str aN);
2976 fi;
2977 fi;
2978 endfor;
2979 processedAnglesList
2980 enddef;
2981
2982 string byAngleMacroName[];
2983 byAngleMacroName[-2] := "byAngleMWhite";
2984 byAngleMacroName[-1] := "byAngleMThinLine";
2985 byAngleMacroName[0] := "byAngleMSolid";
2986 byAngleMacroName[1] := "byAngleMLine";
2987 byAngleMacroName[2] := "byAngleMDashedLine";
2988
2989 vardef byAngleMWhite (expr angleArc, angleColor)(suffix angleOptionalColors) =
2990 save p;
2991 path p;
2992 p := angleArc scaled ((angleScale*angleSize) - lineWidthThin);
2993 image(
2994 fill ((0, 0)--p--cycle) withcolor white;
2995 draw p withpen pencircle scaled lineWidthThin withcolor black;
2996 )
2997 enddef;
2998
2999 vardef byAngleMSolid (expr angleArc, angleColor)(suffix angleOptionalColors) =
3000 save p;
3001 path p;
3002 p := angleArc scaled (angleScale*angleSize);
3003 image(
3004 fill ((0, 0)--p--cycle) withcolor angleColor;
3005 )
3006 enddef;
3007
3008 vardef byAngleMLine (expr angleArc, angleColor)(suffix angleOptionalColors) =
3009 save p, rv;
3010 path p[];
3011 picture rv;
3012 p0 := angleArc scaled ((angleScale*angleSize) - lineWidth);
3013 p1 := (0, 0)--p0--cycle;
3014 p2 := (0, 0)--(angleArc scaled (angleScale*angleSize))--cycle;
3015 rv := image(
3016 if (angleOptionalColors[0] <> bytransparent):
3017 fill p2 withcolor angleOptionalColors[0];
3018 fi;
3019 draw p0 withpen pencircle scaled lineWidth withcolor angleColor;
3020 );
3021 clip rv to p2;
3022 rv
3023 enddef;
3024
3025 vardef byAngleMThinLine (expr angleArc, angleColor)(suffix angleOptionalColors) =
3026 save p;
3027 path p;
3028 p := angleArc scaled ((angleScale*angleSize) - lineWidthThin);
3029 image(
3030 if (angleOptionalColors[0] <> bytransparent):
3031 fill ((0, 0)--p--cycle) withcolor angleOptionalColors[0];
3032 fi;
3033 draw p withpen pencircle scaled lineWidthThin withcolor angleColor;
3034 )
3035 enddef;
3036
3037 vardef byAngleMDashedLine (expr angleArc, angleColor)(suffix angleOptionalColors) =
3038 save p, q;
3039 path p;
3040 p := angleArc scaled ((angleScale*angleSize) - lineWidth);
3041 image(
3042 if (angleOptionalColors[0] <> bytransparent):
3043 fill (p--(0,0)--cycle) withcolor angleOptionalColors[0];
3044 fi;
3045 draw p withpen pencircle scaled lineWidth withcolor angleColor dashed byDashPattern(arclength(p), 1, 0);
3046 draw (0,0); % draws nothing. for some reason dashing seems to "leak" without this.
3047 )
3048 enddef;
3049
3050 % Following monstrosity draws dummy sides for angles which require them,
3051 % essentially, it takes a list of angles, checks if they all are constructed
3052 % on one point; then it browses through all angles, finds their sides
3053 % and counts them; ones that occur twice it draws as is; ones that occur
3054 % once it shifts inside an angle; and after that it clips everything
3055 % to angle outline.
3056 vardef byNamedAngleSidesFull (text anglesList)(text linesList)=
3057 save centerName, uniqueCenter, angleGenArc, angleDir, angleCount, angleType, angleSide, anglePart, angleNum, angleCheck,
3058 ac, as, ad, p, n, compoundAnglePath, compoundAngleArc, b, e, outputImage, lineCol, lineTh, lineDash, pointName, scaleToUse,
3059 processedAnglesList, processedLinesList,
3060 planePoints, threeDMode, projectedCircle, labelVector,
3061 enteredAnglePointsList, pointNameToUse;
3062 string centerName, pointName[], processedAnglesList, processedLinesList,enteredAnglePointsList, pointNameToUse;
3063 pair ac, ad[], p[], labelVector;
3064 boolean uniqueCenter, angleCheck, doNotLabelCenter, threeDMode;
3065 numeric angleDir[], angleCount[], angleType[], angleSide[], angleNum, anglePart[], as[], n[], b, e, lineTh, lineDash, scaleToUse;
3066 path angleGenArc, compoundAnglePath, compoundAngleArc, projectedCircle;
3067 color planePoints[];
3068 picture outputImage;
3069 centerName := "";
3070 uniqueCenter := true;
3071 doNotLabelCenter := false;
3072 scaleToUse := -1;
3073 enteredAnglePointsList := "";
3074 processedAnglesList := byProcessAnglesList.enteredAnglePointsList(anglesList);
3075 forsuffixes lN=linesList:
3076 appendList(processedLinesList, str lN, 1, false);
3077 endfor;
3078 threeDMode := false;
3079 forsuffixes aN=scantokens(processedAnglesList):
3080 ac := scantokens(getAttribute("angle", "BName", str aN));
3081 if (centerName = ""):
3082 centerName := getAttribute("angle", "BName", str aN);
3083 if (attributeExists("point", "XYZ", getAttribute("angle", "AName", str aN))
3084 and attributeExists("point", "XYZ", getAttribute("angle", "BName", str aN))
3085 and attributeExists("point", "XYZ", getAttribute("angle", "CName", str aN))):
3086 threeDMode := true;
3087 planePoints1 := getAttribute("point", "XYZ", getAttribute("angle", "AName", str aN));
3088 planePoints2 := getAttribute("point", "XYZ", getAttribute("angle", "BName", str aN));
3089 planePoints3 := getAttribute("point", "XYZ", getAttribute("angle", "CName", str aN));
3090 projectedCircle := byFullCircleToPlane(planePoints1, planePoints2, planePoints3);
3091 else:
3092 projectedCircle := fullcircle;
3093 fi;
3094 elseif (centerName <> getAttribute("angle", "BName", str aN)):
3095 uniqueCenter := false;
3096 elseif threeDMode:
3097 if (not isInPlane(getAttribute("point", "XYZ", getAttribute("angle", "AName", str aN)),
3098 planePoints1, planePoints2, planePoints3))
3099 or (not isInPlane(getAttribute("point", "XYZ", getAttribute("angle", "CName", str aN)),
3100 planePoints1, planePoints2, planePoints3)):
3101 errmessage("The angles " & processedAnglesList & " are not coplanar");
3102 fi;
3103 fi;
3104 if (getAttribute("angle", "Standalone", str aN) > 1):
3105 doNotLabelCenter := true;
3106 fi;
3107 if (getAttribute("angle", "ScaleCorrection", str aN) <> angleScale)
3108 and (getAttribute("angle", "ScaleCorrection", str aN) > scaleToUse):
3109 scaleToUse := getAttribute("angle", "ScaleCorrection", str aN);
3110 fi;
3111 endfor;
3112 if (length(processedLinesList) = 0):
3113 if (known pointLinesList.scantokens(centerName)):
3114 processedLinesList := pointLinesList.scantokens(centerName);
3115 fi;
3116 fi;
3117 if (scaleToUse < 0):
3118 scaleToUse := angleScale;
3119 fi;
3120 outputImage := image(
3121 startTempAngleScale(scaleToUse);
3122 if (uniqueCenter):
3123 angleNum := 0;
3124 forsuffixes aN=scantokens(processedAnglesList):
3125 angleGenArc := byConstructAngleArc(
3126 scantokens(getAttribute("angle", "AName", str aN)),
3127 scantokens(getAttribute("angle", "BName", str aN)),
3128 scantokens(getAttribute("angle", "CName", str aN)),
3129 getAttribute("angle", "ArcType", str aN));
3130 anglePart1 := angle(point 0 of angleGenArc);
3131 ad1 := direction 0 of (angleGenArc rotated -anglePart1);
3132 if (ad1 = (0, 0)):
3133 ad1 := direction 1 of (angleGenArc rotated -anglePart1);
3134 fi;
3135 as1 := sign(ypart(ad1));
3136 anglePart2 := angle(point 0 of reverse(angleGenArc));
3137 ad2 := direction 0 of (reverse(angleGenArc) rotated -anglePart2);
3138 if (ad2 = (0, 0)):
3139 ad2 := direction 1 of (reverse(angleGenArc) rotated -anglePart2);
3140 fi;
3141 as2 := sign(ypart(ad2));
3142 for j=1,2:
3143 angleCheck := true;
3144 for i := 1 step 1 until angleNum:
3145 if ((abs(angleDir[i] - anglePart[j]) mod 360) < 1):
3146 angleCount[i] := angleCount[i] + 1;
3147 angleCheck := false;
3148 if ((getAttribute("angle", "Style", str aN) = 0)
3149 or ((getAttribute0("angle", "OptionalColor", str aN) <> white)
3150 and (getAttribute0("angle", "OptionalColor", str aN) <> bytransparent)))
3151 and (getAttribute("angle", "ArcType", str aN) <> 1):
3152 angleType[i] := 0;
3153 fi;
3154 fi;
3155 endfor;
3156 if angleCheck:
3157 angleNum := angleNum + 1;
3158 angleDir[angleNum] := anglePart[j];
3159 angleSide[angleNum] := as[j];
3160 if ((getAttribute("angle", "Style", str aN) <> 0)
3161 and (isWhite(getAttribute0("angle", "OptionalColor", str aN))
3162 or (getAttribute0("angle", "OptionalColor", str aN) = bytransparent)))
3163 or ((getAttribute("angle", "Style", str aN) = 0)
3164 and isWhite(getAttribute("angle", "Color", str aN)))
3165 or (getAttribute("angle", "ArcType", str aN) = 1):
3166 angleType[angleNum] := 1;
3167 else:
3168 angleType[angleNum] := 0;
3169 fi;
3170 if (j = 1):
3171 if (getAttribute("angle", "Standalone", str aN) > 0):
3172 pointName[angleNum] := "";
3173 else:
3174 pointName[angleNum] := getAttribute("angle", "AName", str aN);
3175 fi;
3176 elseif (j = 2):
3177 if (getAttribute("angle", "Standalone", str aN) > 0):
3178 pointName[angleNum] := "";
3179 else:
3180 pointName[angleNum] := getAttribute("angle", "CName", str aN);
3181 fi;
3182 fi;
3183 angleCount[angleNum] := 1;
3184 fi;
3185 endfor;
3186 endfor;
3187 j := 0;
3188 for i := 1 step 1 until angleNum:
3189 if (angleCount[i] = 1):
3190 j := j + 1;
3191 n[j] := i;
3192 fi;
3193 if (angleType[i] <> 0):
3194 p1 := (ac*scaleFactor);
3195 p2 := ((dir(angleDir[i]) scaled (1/2angleSize * angleScale))) shifted p1;
3196 defineColor.lineCol(black);
3197 lineDash := 0;
3198 lineTh := 1;
3199 forsuffixes lN=scantokens(processedLinesList):
3200 if (getAttribute("line", "EndAName", str lN) = centerName):
3201 if (abs(angle(
3202 scantokens(getAttribute("line", "EndBName", str lN))
3203 - scantokens(getAttribute("line", "EndAName", str lN)))
3204 - angleDir[i]) < 1):
3205 defineColor.lineCol(getAttribute("line", "Color", str lN));
3206 lineDash := getAttribute("line", "Dashed", str lN);
3207 lineTh := getAttribute("line", "Thin", str lN);
3208 fi;
3209 elseif (getAttribute("line", "EndBName", str lN) = centerName):
3210 if (abs(angle(
3211 scantokens(getAttribute("line", "EndAName", str lN))
3212 - scantokens(getAttribute("line", "EndBName", str lN)))
3213 - angleDir[i]) < 1):
3214 defineColor.lineCol(getAttribute("line", "Color", str lN));
3215 lineDash := getAttribute("line", "Dashed", str lN);
3216 lineTh := getAttribute("line", "Thin", str lN);
3217 fi;
3218 fi;
3219 endfor;
3220 if (angleCount[i] > 1):
3221 draw byLineRender (p1, p2, lineCol, lineDash, lineTh, p1, p2, t, 0, 0, 0, 1);
3222 else:
3223 draw byLineRender (p1, p2, lineCol, lineDash, lineTh, p1, p2, 0, 0, 0, -angleSide[i], 1);
3224 fi;
3225 fi;
3226 endfor;
3227 if (j = 2):
3228 if not threeDMode:
3229 b := (angleDir[n[1]]/360)*8;
3230 e := (angleDir[n[2]]/360)*8;
3231 else:
3232 b := xpart(projectedCircle intersectiontimes ((0, 0) -- dir(angleDir[n[1]])*10));
3233 e := xpart(projectedCircle intersectiontimes ((0, 0) -- dir(angleDir[n[2]])*10));
3234 fi;
3235 if (b > e): b := b - length(projectedCircle); fi;
3236 if (angleSide[n[1]] > angleSide[n[2]]):
3237 compoundAngleArc := ((subpath (b, e) of projectedCircle) scaled (angleScale*angleSize)) shifted (ac*scaleFactor);
3238 else:
3239 compoundAngleArc := ((subpath (b + 8, e) of projectedCircle) scaled (angleScale*angleSize)) shifted (ac*scaleFactor);
3240 fi;
3241 compoundAnglePath := compoundAngleArc -- (ac*scaleFactor) -- cycle;
3242 elseif (j <> 0):
3243 errmessage("There are gaps in a compound angle");
3244 fi;
3245 else:
3246 errmessage("The angles are not concentric");
3247 fi;
3248 stopTempAngleScale;
3249 );
3250 if (known compoundAnglePath):
3251 clip outputImage to compoundAnglePath;
3252 fi;
3253 image(
3254 startTempAngleScale(scaleToUse);
3255 draw outputImage rotated globalRotation;
3256 if (textLabels and autoLabelingMode) and (j=2) and (known compoundAnglePath):
3257 for i=1,2:
3258 if not threeDMode:
3259 labelVector := dir(angleDir[n[i]]);
3260 else:
3261 labelVector := (projectedCircle intersectionpoint ((0, 0) -- dir(angleDir[n[i]])*10)) scaled 2;
3262 fi;
3263 pointNameToUse := pointName[n[i]];
3264 if length(enteredAnglePointsList) > 0:
3265 show(enteredAnglePointsList);
3266 forsuffixes j = scantokens(enteredAnglePointsList):
3267 if (str j <> centerName):
3268 if ((abs(
3269 angle(scantokens(pointNameToUse) - scantokens(centerName))
3270 - angle(j - scantokens(centerName))
3271 ) mod 360) < 1):
3272 pointNameToUse := str j;
3273 fi;
3274 fi;
3275 endfor;
3276 fi;
3277 draw byLabelAngleSide(scantokens(centerName), scantokens(pointNameToUse));
3278 endfor;
3279 if not doNotLabelCenter:
3280 draw byTextLabel(pointLabel)(centerName, ac, angle((point (arctime (1/2arclength(compoundAngleArc)) of compoundAngleArc) of compoundAngleArc) shifted (-ac*scaleFactor)) + 180, textLabelShift);
3281 fi;
3282 fi;
3283 stopTempAngleScale;
3284 )
3285 enddef;
3286
3287 vardef byLabelAngleSide(suffix centerPoint, targetPoint) =
3288 save labelVector;
3289 pair labelVector;
3290 image(
3291 if (textLabels):
3292 if (attributeExists("point", "XYZ", str centerPoint)
3293 and attributeExists("point", "XYZ", str targetPoint)):
3294 labelVector := projectPointIfInSpace(unitvectorXYZ(pointXYZ.targetPoint - pointXYZ.centerPoint));
3295 else:
3296 labelVector := unitvector(targetPoint-centerPoint);
3297 fi;
3298 draw byTextLabel(pointLabel)(str targetPoint,
3299 centerPoint + (labelVector*1/2angleSize*angleScale)/scaleFactor, angle(targetPoint - centerPoint), textLabelShift);
3300 fi;
3301 )
3302 enddef;
3303
3304 vardef byNamedAngleDummySides(text anglesList)=
3305 byNamedAngleSidesFull(anglesList)(noLine)
3306 enddef;
3307
3308 vardef byNamedAngleSides(text anglesList)(text linesList) =
3309 image(
3310 draw byNamedAngle(anglesList);
3311 draw byNamedAngleSidesFull(anglesList)(linesList);
3312 )
3313 enddef;
3314
3315 vardef byNamedAngleWithDummySides(text anglesList)=
3316 image(
3317 draw byNamedAngle(anglesList);
3318 draw byNamedAngleDummySides(anglesList);
3319 )
3320 enddef;
3321
3322 vardef byNamedAngleResized (text anglesToUse) =
3323 save modAnglesList, modAnglesListNew, anglesToDraw, anglesAdded, angleIsAdded, useThisAngle, sc, v, numOfAngles, i, j, k;
3324 string modAnglesList, modAnglesListNew, anglesToDraw;
3325 numeric anglesAdded, sc, v, numOfAngles;
3326 boolean angleIsAdded, useThisAngle;
3327 numOfAngles := 0;
3328 forsuffixes k=anglesToUse:
3329 if (length(str k) > 0):
3330 if typeOf(getAttribute("angle", "Synonym", str k)) <> "string":
3331 errmessage("Angle " & str k & " is missing");
3332 fi;
3333 numOfAngles := numOfAngles + 1;
3334 fi;
3335 endfor;
3336 image(
3337 forsuffixes i=scantokens(anglePointsList):
3338 modAnglesList := pointAnglesList.i;
3339 modAnglesListNew := "";
3340 forsuffixes j=scantokens(modAnglesList):
3341 if (numOfAngles > 0):
3342 useThisAngle := false;
3343 forsuffixes k=anglesToUse:
3344 if (getAttribute("angle", "Synonym", str j) = getAttribute("angle", "Synonym", str k)):
3345 useThisAngle := true;
3346 fi;
3347 endfor;
3348 else:
3349 useThisAngle := true;
3350 fi;
3351 if (useThisAngle):
3352 if (length(modAnglesListNew) = 0):
3353 modAnglesListNew := str j;
3354 else:
3355 modAnglesListNew := str j & ", " & modAnglesListNew;
3356 fi;
3357 fi;
3358 endfor;
3359 modAnglesList := modAnglesListNew;
3360 if (length(modAnglesList)>0):
3361 forever:
3362 anglesToDraw := "";
3363 forever:
3364 modAnglesListNew := "";
3365 anglesAdded := 0;
3366 forsuffixes j=scantokens(modAnglesList):
3367 if (length(anglesToDraw) = 0):
3368 anglesToDraw := str j;
3369 else:
3370 forsuffixes k=scantokens(anglesToDraw):
3371 if ((abs(getAttribute("angle", "AVal", str k) - getAttribute("angle", "AVal", str j)) mod 360) < 1)
3372 or ((abs(getAttribute("angle", "CVal", str k) - getAttribute("angle", "AVal", str j)) mod 360) < 1)
3373 or ((abs(getAttribute("angle", "AVal", str k) - getAttribute("angle", "CVal", str j)) mod 360) < 1)
3374 or ((abs(getAttribute("angle", "CVal", str k) - getAttribute("angle", "CVal", str j)) mod 360) < 1):
3375 appendList(anglesToDraw, str j, 1, true);
3376 anglesAdded := anglesAdded + 1;
3377 fi;
3378 endfor;
3379 fi;
3380 endfor;
3381 forsuffixes j=scantokens(modAnglesList):
3382 angleIsAdded := false;
3383 forsuffixes k=scantokens(anglesToDraw):
3384 if ((str j) = (str k)):
3385 angleIsAdded := true;
3386 fi;
3387 endfor;
3388 if not angleIsAdded:
3389 appendList(modAnglesListNew, str j, 1, false);
3390 fi;
3391 endfor;
3392 modAnglesList := modAnglesListNew;
3393 exitif (anglesAdded = 0) or (length(modAnglesList)=0);
3394 endfor;
3395 v := 0;
3396 forsuffixes k=scantokens(anglesToDraw):
3397 %v := v + angleVal.k;
3398 if (getAttribute("angle", "Val", str k) > v): v := getAttribute("angle", "Val", str k); fi;
3399 endfor;
3400 sc := angleOpticalScale(v);
3401 startTempAngleScale(angleScale*sc);
3402 draw byNamedAngle(scantokens(anglesToDraw));
3403 stopTempAngleScale;
3404 exitif (length(modAnglesList)=0);
3405 endfor;
3406 fi;
3407 endfor;
3408 )
3409 enddef;
3410
3411 vardef generateAngleSynonyms =
3412 if string anglePointsList:
3413 save n, candidateName, originalName;
3414 numeric n;
3415 string candidateName[], originalName;
3416 forsuffixes i=scantokens(anglePointsList):
3417 n := 0;
3418 pointAnglesList.i := sortPointAnglesList(pointAnglesList.i);
3419 forsuffixes j=scantokens(pointAnglesList.i):
3420 if (length(str j) > 0):
3421 n := n + 1;
3422 originalName := str j;
3423 candidateName0 := getAttribute("angle", "BName", originalName);
3424 candidateName1 := getAttribute("angle", "AName", originalName)
3425 & getAttribute("angle", "BName", originalName)
3426 & getAttribute("angle", "CName", originalName);
3427 candidateName2 := getAttribute("angle", "CName", originalName)
3428 & getAttribute("angle", "BName", originalName)
3429 & getAttribute("angle", "AName", originalName);
3430 if not string getAttribute("angle", "Synonym", candidateName1):
3431 setAttribute("angle", "SynonymPartial", candidateName1, false);
3432 setAttribute("angle", "Synonym", candidateName1, originalName);
3433 fi;
3434 if not string getAttribute("angle", "Synonym", candidateName2):
3435 setAttribute("angle", "SynonymPartial", candidateName2, false);
3436 setAttribute("angle", "Synonym", candidateName2, originalName);
3437 fi;
3438 fi;
3439 endfor;
3440 if (n = 1):
3441 if not string getAttribute("angle", "Synonym", candidateName0):
3442 setAttribute("angle", "SynonymPartial", candidateName0, false);
3443 setAttribute("angle", "Synonym", candidateName0, originalName);
3444 fi;
3445 fi;
3446 endfor;
3447 fi;
3448 enddef;
3449
3450 vardef generateCircleSynonyms =
3451 save pointName, originalName;
3452 string pointName[], originalName;
3453 if string allCirclesList:
3454 forsuffixes i=scantokens(allCirclesList):
3455 originalName := str i;
3456 pointName0 := getAttribute("circle", "AName", originalName);
3457 pointName1 := getAttribute("circle", "BName", originalName);
3458 pointName2 := getAttribute("circle", "CName", originalName);
3459 setAttribute("circle", "Synonym", pointName1 & pointName2 & pointName0, originalName);
3460 setAttribute("circle", "Synonym", pointName2 & pointName0 & pointName1, originalName);
3461 setAttribute("circle", "Synonym", pointName0 & pointName2 & pointName1, originalName);
3462 setAttribute("circle", "Synonym", pointName1 & pointName0 & pointName2, originalName);
3463 setAttribute("circle", "Synonym", pointName2 & pointName1 & pointName0, originalName);
3464 endfor;
3465 fi;
3466 enddef;
3467
3468 vardef sortPointAnglesList (expr anglesListToSort) =
3469 save preList, postList, nPre, nPost, isSorted, value, firstValue, lastValue, rv;
3470 string preList, postList, rv;
3471 boolean isSorted;
3472 numeric value, firstValue, lastValue, nPre, nPost;
3473 lastValue := -360;
3474 isSorted := true;
3475 preList := "";
3476 postList := "";
3477 nPre := 0;
3478 nPost := 0;
3479 forsuffixes i=scantokens(anglesListToSort):
3480 value := getAttribute("angle", "Direction", str i);
3481 if (unknown firstValue):
3482 firstValue := value;
3483 appendList(postList, str i, 0, true);
3484 fi;
3485 if (value < firstValue):
3486 appendList(preList, str i, 0, true);
3487 nPre := nPre + 1;
3488 else:
3489 appendList(postList, str i, 0, true);
3490 nPost := nPost + 1;
3491 fi;
3492 if (lastValue > value):
3493 isSorted := false;
3494 fi;
3495 lastValue := value;
3496 endfor;
3497 if (isSorted):
3498 rv := anglesListToSort;
3499 else:
3500 if (nPre > 1):
3501 preList := sortPointAnglesList(preList);
3502 fi;
3503 if (nPre > 0):
3504 preList := preList & ", ";
3505 fi;
3506 if (nPost > 1):
3507 postList := sortPointAnglesList(postList);
3508 fi;
3509 rv := preList & postList;
3510 fi;
3511 rv
3512 enddef;
3513
3514 vardef bySplitStringIntoAngles (expr angleName) =
3515 save pointA, pointB, pointC, cPointA, cPointB, cPointC, n;
3516 string pointA, pointB, pointC, cPointA, cPointB, cPointC, rv;
3517 numeric n;
3518 n := 0;
3519 for i := 1 step 1 until (length(angleName) - 2):
3520 for j := i + 1 step 1 until (length(angleName) - 1):
3521 cPointA := substring (0, i) of angleName;
3522 cPointB := substring (i, j) of angleName;
3523 cPointC := substring (j, length(angleName)) of angleName;
3524 if known scantokens(cPointA):
3525 if known scantokens(cPointB):
3526 if known scantokens(cPointC):
3527 n := n + 1;
3528 pointA := cPointA;
3529 pointB := cPointB;
3530 pointC := cPointC;
3531 fi;
3532 fi;
3533 fi;
3534 endfor;
3535 endfor;
3536 if (known scantokens(angleName)) and (n = 0):
3537 rv := "";
3538 else:
3539 if n = 0:
3540 errmessage("Can't find points for angle: " & lineName);
3541 fi;
3542 if n > 1:
3543 errmessage("Ambiguous angle name: " & lineName);
3544 fi;
3545 rv := pointA & ", " & pointB & ", " & pointC;
3546 fi;
3547 rv
3548 enddef;
3549
3550 vardef byConstructCompoundAngle@#(suffix pointA, pointB, pointC) =
3551 save angleList, isFirst, isLast, cwA, cwC, cwAName, cwCName, dirA, dirC, rv, threeDMode, notInPlane, clockwiseMode;
3552 string cwAName, cwCName, angleList, pointAnglesListProcessed, rv;
3553 boolean isFirst, isLast, threeDMode, notInPlane, clockwiseMode;
3554 pair cwA, cwC, dirA, dirC;
3555 if str @# = "":
3556 clockwiseMode := angleClockwiseMode;
3557 else:
3558 clockwiseMode := not angleClockwiseMode;
3559 fi;
3560 angleList := "";
3561 dirA := pointA - pointB;
3562 dirC := pointC - pointB;
3563 threeDMode := false;
3564 if byIsPointInSpace(pointA, pointB, pointC):
3565 threeDMode := true;
3566 fi;
3567 isFirst := false;
3568 isLast := false;
3569 pointAnglesListProcessed := pointAnglesList.pointB & ", " & pointAnglesList.pointB;
3570 if clockwiseMode:
3571 pointAnglesListProcessed := reverseList(pointAnglesListProcessed);
3572 fi;
3573 forsuffixes i=scantokens(pointAnglesListProcessed):
3574 if byIsArcClockwise(byConstructAngleArc(
3575 scantokens(getAttribute("angle", "AName", str i)),
3576 scantokens(getAttribute("angle", "BName", str i)),
3577 scantokens(getAttribute("angle", "CName", str i)),
3578 getAttribute("angle", "ArcType", str i))) xor (not clockwiseMode):
3579 cwAName := getAttribute("angle", "CName", str i);
3580 cwCName := getAttribute("angle", "AName", str i);
3581 else:
3582 cwAName := getAttribute("angle", "AName", str i);
3583 cwCName := getAttribute("angle", "CName", str i);
3584 fi;
3585 cwA := scantokens(cwAName) - scantokens(getAttribute("angle", "BName", str i));
3586 cwC := scantokens(cwCName) - scantokens(getAttribute("angle", "BName", str i));
3587 notInPlane := false;
3588 if threeDMode:
3589 if (not isInPlane(pointXYZ.scantokens(cwAName),
3590 pointXYZ.pointA,
3591 pointXYZ.pointB,
3592 pointXYZ.pointC))
3593 or (not isInPlane(pointXYZ.scantokens(cwCName),
3594 pointXYZ.pointA,
3595 pointXYZ.pointB,
3596 pointXYZ.pointC)):
3597 notInPlane := true;
3598 fi;
3599 fi;
3600 if not notInPlane:
3601 if ((abs(angle(dirA) - angle(cwA)) mod 360) < 1) and (not isFirst):
3602 isFirst := true;
3603 fi;
3604 if isFirst and not isLast:
3605 appendList(angleList, str i, 1, true)
3606 fi;
3607 if ((abs(angle(dirC) - angle(cwC)) mod 360) < 1) and isFirst:
3608 isLast := true;
3609 fi;
3610 fi;
3611 endfor;
3612 rv := angleList;
3613 if (not isFirst) or (not isLast):
3614 if (str @# = "") and angleModeFallback:
3615 rv := byConstructCompoundAngle.rev(pointA, pointB, pointC);
3616 else:
3617 errmessage("Can't construct angle: " & str pointA & str pointB & str pointC);
3618 fi;
3619 fi;
3620 rv
3621 enddef;
3622
3623 vardef twoRightAngles =
3624 save textLabels;
3625 boolean textLabels;
3626 textLabels := false;
3627 image(
3628 draw byNamedAngle(rightAngleWN,rightAngleNE);
3629 draw byNamedAngleDummySides(rightAngleWN,rightAngleNE);
3630 )
3631 enddef;
3632
3633 vardef rightAngle =
3634 save textLabels;
3635 boolean textLabels;
3636 textLabels := false;
3637 image(
3638 draw byNamedAngle(rightAngleWN);
3639 draw byNamedAngleDummySides(rightAngleWN);
3640 )
3641 enddef;
3642
3643 % Angles we often need
3644 pair pointN, pointE, pointS, pointW, pointO;
3645 pointN := (0, 2angleSize*angleScale);
3646 pointE := (2angleSize*angleScale, 0);
3647 pointS := (0, -2angleSize*angleScale);
3648 pointW := (-2angleSize*angleScale, 0);
3649 pointO := (0, 0);
3650
3651 vardef byDefineGenericRightAngles(expr angleInsteadOfArc) =
3652 byAngleDefine.rightAngleNE (pointN, pointO, pointE)(black, 1);
3653 byAngleDefine.rightAngleES (pointE, pointO, pointS)(black, 1);
3654 byAngleDefine.rightAngleSW (pointS, pointO, pointW)(black, 1);
3655 byAngleDefine.rightAngleWN (pointW, pointO, pointN)(black, 1);
3656 if (angleInsteadOfArc):
3657 byConsiderAngleRight(rightAngleNE);
3658 byConsiderAngleRight(rightAngleES);
3659 byConsiderAngleRight(rightAngleSW);
3660 byConsiderAngleRight(rightAngleWN);
3661 fi;
3662 enddef;
3663
3664 byDefineGenericRightAngles(false);
3665
3666 %
3667 % Arrows
3668 %
3669
3670 def commonArrowSettings =
3671 save ahlength, ahangle;
3672 ahlength := 7pt;
3673 ahangle := 30;
3674 enddef;
3675
3676 vardef byRotationArrowDefine@#(suffix axisA, axisB, pointA) (expr ang, col, sty) =
3677 if str @# = "":
3678 byRotationArrowDefine.scantokens(str axisA & str axisB & str pointA)(axisA, axisB, pointA) (ang, col, sty);
3679 else:
3680 setAttribute("rotationarrow", "AxisA", str @#, axisA);
3681 setAttribute("rotationarrow", "AxisB", str @#, axisB);
3682 setAttribute("rotationarrow", "Point", str @#, pointA);
3683 setAttribute("rotationarrow", "AxisAName", str @#, str axisA);
3684 setAttribute("rotationarrow", "AxisBName", str @#, str axisB);
3685 setAttribute("rotationarrow", "PointName", str @#, str pointA);
3686 setAttribute("rotationarrow", "Angle", str @#, ang);
3687 setAttribute("rotationarrow", "Color", str @#, col);
3688 setAttribute("rotationarrow", "Style", str @#, sty);
3689 setAttribute("rotationarrow", "Synonym", str @#, str @#);
3690 fi;
3691 enddef;
3692
3693 vardef byGenerateRotationArrowPath (expr axisA, axisB, pointA, ang) =
3694 save arrowPath, spacePoint;
3695 path arrowPath;
3696 color spacePoint;
3697 spacePoint := pointA colorrotated projectionAngle;
3698 arrowPath := (redpart(spacePoint), greenpart(spacePoint));
3699 for i := 0 step (ang/floor(ang/5)) until ang:
3700 spacePoint := byRotateAroundAxis (axisA, axisB, i, pointA) colorrotated projectionAngle;
3701 arrowPath := arrowPath -- (redpart(spacePoint), greenpart(spacePoint));
3702 endfor;
3703 arrowPath
3704 enddef;
3705
3706 vardef byNamedRotationArrow (text arrowsList) =
3707 save arrowPath, arrowPoint, arrowStyle;
3708 path arrowPath;
3709 color arrowPoint;
3710 numeric arrowStyle;
3711 commonArrowSettings;
3712 image(
3713 forsuffixes aN=arrowsList:
3714 arrowPath := byGenerateRotationArrowPath(
3715 pointXYZ.scantokens(getAttribute("rotationarrow", "AxisAName", str aN)),
3716 pointXYZ.scantokens(getAttribute("rotationarrow", "AxisBName", str aN)),
3717 pointXYZ.scantokens(getAttribute("rotationarrow", "PointName", str aN)),
3718 getAttribute("rotationarrow", "Angle", str aN)
3719 ) scaled scaleFactor;
3720 arrowStyle := getAttribute("rotationarrow", "Style", str aN);
3721 if arrowStyle = 0:
3722 arrowPath := subpath (0, (arctime (arclength(arrowPath) - ahlength) of arrowPath)) of arrowPath;
3723 drawarrow arrowPath withpen (pencircle scaled lineWidthThin) withcolor getAttribute("rotationarrow", "Color", str aN);
3724 else:
3725 arrowPath := subpath ((arctime ahlength of arrowPath), (arctime (arclength(arrowPath) - ahlength) of arrowPath)) of arrowPath;
3726 drawdblarrow arrowPath withpen (pencircle scaled lineWidthThin) withcolor getAttribute("rotationarrow", "Color", str aN);
3727 fi;
3728 endfor;
3729 ) rotated globalRotation
3730 enddef;
3731
3732 vardef byFlatArrowDefine@#(suffix pointA, pointB) (expr col, sty, typ) =
3733 if str @# = "":
3734 byFlatArrowDefine.scantokens(str pointA & str pointB)(pointA, pointB) (col, sty, typ);
3735 else:
3736 setAttribute("flatarrow", "PointA", str @#, pointA);
3737 setAttribute("flatarrow", "PointB", str @#, pointB);
3738 setAttribute("flatarrow", "PointAName", str @#, str pointA);
3739 setAttribute("flatarrow", "PointBName", str @#, str pointB);
3740 setAttribute("flatarrow", "Type", str @#, typ);
3741 setAttribute("flatarrow", "Color", str @#, col);
3742 setAttribute("flatarrow", "Style", str @#, sty);
3743 setAttribute("flatarrow", "Synonym", str @#, str @#);
3744 fi;
3745 enddef;
3746
3747 vardef byNamedFlatArrow (text arrowsList) =
3748 save arrowPath, arrowStyle, arrowPoint;
3749 path arrowPath;
3750 numeric arrowStyle;
3751 pair arrowPoint[];
3752 commonArrowSettings;
3753 image(
3754 forsuffixes aN=arrowsList:
3755 arrowType := getAttribute("flatarrow", "Type", str aN);
3756 arrowStyle := getAttribute("flatarrow", "Style", str aN);
3757 arrowPoint1 := scantokens(getAttribute("flatarrow", "PointAName", str aN));
3758 arrowPoint2 := scantokens(getAttribute("flatarrow", "PointBName", str aN));
3759 if arrowType = 0:
3760 arrowPath := arrowPoint1 -- arrowPoint2;
3761 elseif arrowType = 1:
3762 arrowPath := arrowPoint1
3763 .. 1/2[arrowPoint1, arrowPoint2]
3764 shifted (((arrowPoint2 - arrowPoint1) scaled 1/8) rotated 90)
3765 .. arrowPoint2;
3766 fi;
3767 arrowPath := arrowPath scaled scaleFactor;
3768 if arrowStyle = 0:
3769 arrowPath := subpath (
3770 0,
3771 arctime (arclength(arrowPath) - ahlength) of arrowPath
3772 ) of arrowPath;
3773 drawarrow arrowPath withpen (pencircle scaled lineWidthThin) withcolor getAttribute("flatarrow", "Color", str aN);
3774 else:
3775 arrowPath := subpath (
3776 arctime ahlength of arrowPath,
3777 arctime (arclength(arrowPath) - ahlength) of arrowPath
3778 ) of arrowPath;
3779 drawdblarrow arrowPath withpen (pencircle scaled lineWidthThin) withcolor getAttribute("flatarrow", "Color", str aN);
3780 fi;
3781 endfor;
3782 ) rotated globalRotation
3783 enddef;
3784
3785 %
3786 % Spheres
3787 %
3788
3789 vardef bySphereDefine@#(suffix o)(expr r, col, dp, th, s) =
3790 if str @# = "":
3791 errmessage("bySphereDefine needs a name (bySphereDefine.somename...)");
3792 else:
3793 %if mainPictureMode:
3794 % appendList(allSpheresList, str @#, 1, true);
3795 %fi;
3796 setAttribute("sphere", "CenterName", str @#, str o);
3797 setAttribute("sphere", "Radius", str @#, r);
3798 setAttribute("sphere", "Diameter", str @#, 2r);
3799 setAttribute("sphere", "Color", str @#, col);
3800 setAttribute("sphere", "Dashed", str @#, dp);
3801 setAttribute("sphere", "Thin", str @#, th);
3802 setAttribute("sphere", "Shift", str @#, s);
3803 fi;
3804 enddef;
3805
3806 vardef byNamedSphere (text spheresList) =
3807 save omitSpaceRotation;
3808 boolean omitSpaceRotation;
3809 omitSpaceRotation := true;
3810 image(
3811 forsuffixes cN=spheresList:
3812 draw byArcRender (
3813 scantokens(getAttribute("sphere", "CenterName", str cN)), 0, 8,
3814 getAttribute("sphere", "Diameter", str cN),
3815 (0,0,0),
3816 (0,0,1),
3817 getAttribute("sphere", "Color", str cN),
3818 getAttribute("sphere", "Dashed", str cN),
3819 getAttribute("sphere", "Thin", str cN),
3820 0,
3821 getAttribute("sphere", "Shift", str cN), 0);
3822 if (textLabels and autoLabelingMode):
3823 fi;
3824 endfor;
3825 )
3826 enddef;
3827
3828 vardef bySpherePlaneIntersectionDefine@#(suffix sphName, planeA, planeB, planeC)(expr col, dp, th, s) =
3829 if str @# = "":
3830 errmessage("bySpherePlaneIntersectionDefine needs a name (bySpherePlaneIntersectionDefine.somename...)");
3831 else:
3832 save sphCenterPoint, sphRadius, sectCenter, sectRadius, sectPlaneVector, distToIntersection;
3833 color sphCenterPoint, sectionCenter,sectionPlaneVector;
3834 numeric sectionRadius, sphRadius;
3835 sphCenterPoint := pointXYZ.scantokens(getAttribute("sphere", "CenterName", str sphName));
3836 sphRadius := getAttribute("sphere", "Radius", str sphName);
3837 sectionCenter := lineAndPlaneIntersection(sphCenterPoint, sphCenterPoint + normalToPlane(pointXYZ.planeA, pointXYZ.planeB, pointXYZ.planeC), pointXYZ.planeA, pointXYZ.planeB, pointXYZ.planeC);
3838 distToIntersection := absXYZ(sectionCenter-sphCenterPoint);
3839 if (distToIntersection > sphRadius):
3840 errmessage("Sphere " & str sphName & " and plane " & str planeA & str planeB & str planeC & " don't seem to intersect");
3841 fi;
3842 sectionRadius := sqrt(1-(distToIntersection/sphRadius)**2)*sphRadius;
3843 byCircleDefineFree.@#(sectionCenter, sectionRadius, col, dp, th, s);
3844 sectionPlaneVector := unitvectorXYZ((pointXYZ.planeA - pointXYZ.planeB) crossproduct (pointXYZ.planeC - pointXYZ.planeB));
3845 setAttribute("circle", "PlaneVector", str @#, sectionPlaneVector);
3846 fi;
3847 enddef;
3848
3849 %vardef byFindSpheresIntersection (text spheresList) =
3850 %enddef;
3851
3852 %
3853 % Magnitudes
3854 %
3855
3856 % along with 'magnitudesymbol' one symbol long 'magnitude' is also defined at the same time.
3857 vardef byMagnitudeSymbolDefine@#(expr shp, col, sty) =
3858 if str @# = "":
3859 byMagnitudeSymbolDefine.scantokens(shp)(shp, col, sty);
3860 else:
3861 setAttribute("magnitude", "Color", str @#, col);
3862 setAttribute("magnitude", "Shape", str @#, shp);
3863 setAttribute("magnitude", "Style", str @#, sty);
3864 setAttribute1("magnitude", "Symbol", str @#, str @#);
3865 setAttribute1("magnitude", "N", str @#, 1);
3866 setAttribute("magnitude", "Alignment", str @#, 0);
3867 setAttribute("magnitude", "NumberOfRows", str @#, 1);
3868 setAttribute("magnitude", "Horizontal", str @#, false);
3869 fi;
3870 enddef;
3871
3872 vardef byNamedMagnitudeSymbol (expr n, hor)(suffix magnitudeSymbolName) =
3873 save p, q, i, s, magnitudeSizeCor;
3874 path p;
3875 pair s[];
3876 picture q;
3877 numeric magnitudeSizeCor;
3878 p := (0, 0);
3879 magnitudeSizeCor := 1;
3880 if (getAttribute("magnitude", "Shape", str magnitudeSymbolName) = "circle"):
3881 p := fullcircle scaled (magnitudeScale*magnitudeSize);
3882 magnitudeSizeCor := 8/7;
3883 elseif (getAttribute("magnitude", "Shape", str magnitudeSymbolName) = "semicircleUp"):
3884 p := ((subpath (0, 4) of fullcircle) -- (-1/2, -1/2) -- (1/2, -1/2) -- cycle) scaled (magnitudeScale*magnitudeSize);
3885 elseif (getAttribute("magnitude", "Shape", str magnitudeSymbolName) = "semicircleDown"):
3886 p := (((subpath (0, 4) of fullcircle) -- (-1/2, -1/2) -- (1/2, -1/2) -- cycle) yscaled -1) scaled (magnitudeScale*magnitudeSize);
3887 elseif (getAttribute("magnitude", "Shape", str magnitudeSymbolName) = "sectorDown"):
3888 p := ((subpath (0, 4) of fullcircle) -- (0, -1/2) -- cycle) scaled (magnitudeScale*magnitudeSize);
3889 magnitudeSizeCor := 8/7;
3890 elseif (getAttribute("magnitude", "Shape", str magnitudeSymbolName) = "sectorUp"):
3891 p := ((subpath (0, 4) of fullcircle) -- (0, -1/2) -- cycle) scaled (magnitudeScale*magnitudeSize) yscaled -1;
3892 magnitudeSizeCor := 8/7;
3893 elseif (getAttribute("magnitude", "Shape", str magnitudeSymbolName) = "wedgeUp"):
3894 p := (((1/2, 0) -- (1/2, 1/2) -- (-1/2, 1/2) -- (-1/2, 0) -- (0, -1/2) -- cycle) yscaled -1) scaled (magnitudeScale*magnitudeSize);
3895 elseif (getAttribute("magnitude", "Shape", str magnitudeSymbolName) = "wedgeDown"):
3896 p := ((1/2, 0) -- (1/2, 1/2) -- (-1/2, 1/2) -- (-1/2, 0) -- (0, -1/2) -- cycle) scaled (magnitudeScale*magnitudeSize);
3897 elseif (getAttribute("magnitude", "Shape", str magnitudeSymbolName) = "square"):
3898 p := ((1/2, 1/2) -- (-1/2, 1/2) -- (-1/2, -1/2) -- (1/2, -1/2) -- cycle) scaled (magnitudeScale*magnitudeSize);
3899 elseif (getAttribute("magnitude", "Shape", str magnitudeSymbolName) = "halfsquare"):
3900 p := ((1/2, 1/2) -- (-1/2, 1/2) -- (-1/2, 0) -- (1/2, 0) -- cycle) scaled (magnitudeScale*magnitudeSize);
3901 elseif (getAttribute("magnitude", "Shape", str magnitudeSymbolName) = "rhombus"):
3902 p := ((0, 1/2) -- (-1/2, 0) -- (0, -1/2) -- (1/2, 0) -- cycle) scaled (magnitudeScale*magnitudeSize);
3903 magnitudeSizeCor := 8/7;
3904 elseif (getAttribute("magnitude", "Shape", str magnitudeSymbolName) = "halfrhombusUp"):
3905 p := ((0, 1/2) -- (-1/2, 0) -- (1/2, 0) -- cycle) scaled (magnitudeScale*magnitudeSize);
3906 magnitudeSizeCor := 8/7;
3907 elseif (getAttribute("magnitude", "Shape", str magnitudeSymbolName) = "miniTriangleUp"):
3908 p := ((-1/4, -1/4) -- (1/4, -1/4) -- (0, 1/4) -- cycle) scaled (magnitudeScale*magnitudeSize);
3909 magnitudeSizeCor := 8/7;
3910 elseif (getAttribute("magnitude", "Shape", str magnitudeSymbolName) = "miniTriangleDown"):
3911 p := ((-1/4, 1/4) -- (1/4, 1/4) -- (0, -1/4) -- cycle) scaled (magnitudeScale*magnitudeSize);
3912 magnitudeSizeCor := 8/7;
3913 elseif (getAttribute("magnitude", "Shape", str magnitudeSymbolName) = "miniSquare"):
3914 p := ((-1/4, -1/4) -- (1/4, -1/4) -- (1/4, 1/4) -- (-1/4, 1/4) -- cycle) scaled (magnitudeScale*magnitudeSize);
3915 elseif (getAttribute("magnitude", "Shape", str magnitudeSymbolName) = "miniCircle"):
3916 p := fullcircle scaled (1/2magnitudeScale*magnitudeSize);
3917 magnitudeSizeCor := 8/7;
3918 fi;
3919 if (hor):
3920 s1 := (0, ypart(urcorner(p)) - ypart(lrcorner(p)));
3921 s2 := (0, magnitudeGap);
3922 else:
3923 s1 := (xpart(urcorner(p)) - xpart(ulcorner(p)), 0);
3924 s2 := (magnitudeGap, 0);
3925 fi;
3926 p := p scaled magnitudeSizeCor;
3927 q := image(
3928 draw p withpen (pencircle scaled lineWidthThin) withcolor getAttribute("magnitude", "Color", str magnitudeSymbolName);
3929 if (getAttribute("magnitude", "Style", str magnitudeSymbolName) = 0):
3930 fill p withcolor getAttribute("magnitude", "Color", str magnitudeSymbolName);
3931 fi;
3932 );
3933 image(
3934 for i := 1 step 1 until n:
3935 draw q shifted (s1 scaled i) shifted (s2 scaled i);
3936 endfor;
3937 )
3938 enddef;
3939
3940 vardef byMagnitudeDefine@#(expr al, hor)(text rowsList)(text magnitudeSymbolsList) =
3941 if str @# = "":
3942 errmessage("byMagnitudeDefine needs a name (byMagnitudeDefine.somename...)");
3943 else:
3944 setAttribute("magnitude", "Horizontal", str @#, hor);
3945 setAttribute("magnitude", "Alignment", str @#, al);
3946 save i;
3947 numeric i;
3948 i := 0;
3949 forsuffixes rN=rowsList:
3950 i := i + 1;
3951 setAttribute[i]("magnitude", "N", str @#, rN);
3952 endfor;
3953 setAttribute("magnitude", "NumberOfRows", str @#, i);
3954 i := 0;
3955 forever:
3956 forsuffixes sN=magnitudeSymbolsList:
3957 i := i + 1;
3958 if (i <= getAttribute("magnitude", "NumberOfRows", str @#)):
3959 setAttribute[i]("magnitude", "Symbol", str @#, str sN);
3960 fi;
3961 endfor;
3962 exitif (i >= getAttribute("magnitude", "NumberOfRows", str @#));
3963 endfor;
3964 fi;
3965 enddef;
3966
3967 vardef byNamedMagnitude (expr excl) (suffix magnitudeName) =
3968 save p, s, h, magC;
3969 picture p;
3970 pair s[];
3971 numeric h;
3972 h := 0;
3973 image(
3974 for magC := 1 step 1 until getAttribute("magnitude", "NumberOfRows", str magnitudeName):
3975 if (excl = 0) or ((excl < 0) and (magC <> -excl)) or ((excl > 0) and (magC = excl)):
3976 p := byNamedMagnitudeSymbol(getAttribute[magC]("magnitude", "N", str magnitudeName), getAttribute("magnitude", "Horizontal", str magnitudeName))(scantokens(getAttribute[magC]("magnitude", "Symbol", str magnitudeName)));
3977 if (getAttribute("magnitude", "Horizontal", str magnitudeName)):
3978 if (getAttribute("magnitude", "Alignment", str magnitudeName) = 0):
3979 s1 := 1/2[urcorner(p),lrcorner(p)];
3980 elseif (getAttribute("magnitude", "Alignment", str magnitudeName) = 1):
3981 s1 := lrcorner(p);
3982 elseif (getAttribute("magnitude", "Alignment", str magnitudeName) = -1):
3983 s1 := urcorner(p);
3984 fi;
3985 s2 := (h, 0);
3986 else:
3987 if (getAttribute("magnitude", "Alignment", str magnitudeName) = 0):
3988 s1 := 1/2[ulcorner(p),urcorner(p)];
3989 elseif (getAttribute("magnitude", "Alignment", str magnitudeName) = 1):
3990 s1 := urcorner(p);
3991 elseif (getAttribute("magnitude", "Alignment", str magnitudeName) = -1):
3992 s1 := ulcorner(p);
3993 fi;
3994 s2 := (0, -h);
3995 fi;
3996 draw p shifted -s1 shifted s2;
3997 if (getAttribute("magnitude", "Horizontal", str magnitudeName)):
3998 h := h + (abs(ulcorner(p)-urcorner(p))) + magnitudeGap;
3999 else:
4000 h := h + (abs(ulcorner(p)-llcorner(p))) + magnitudeGap;
4001 fi;
4002 fi;
4003 endfor;
4004 )
4005 enddef;
4006
4007 %
4008 % Text labels
4009 %
4010
4011 % If using metafun, use textext
4012
4013 if known metafunversion:
4014 vardef textLabelRender(expr t) =
4015 textext("\pointlabel{" & t & "}") scaled textLabelScaleFactor
4016 enddef;
4017 else:
4018 input TEX;
4019 TEXPRE("%&latex" & char(10) & "\documentclass{article}\usepackage[utf8]{inputenc}\usepackage[russian]{babel}\def\pointlabel#1{\textsf{#1}}\begin{document}");
4020 TEXPOST("\end{document}");
4021 vardef textLabelRender(expr t) =
4022 TEX("\pointlabel{" & t & "}") scaled textLabelScaleFactor
4023 enddef;
4024 fi;
4025
4026 numeric textLabelAvSize;
4027 textLabelAvSize := abs(ulcorner(textLabelRender("A")) - llcorner(textLabelRender("A")));
4028
4029 vardef byTextLabel@#(suffix labelType)(expr t, p, a, d) =
4030 save labelItself, labelString, bb;
4031 path bb;
4032 picture labelItself;
4033 string labelString;
4034 if not string labelType.scantokens(t):
4035 string labelType.scantokens(t);
4036 labelType.scantokens(t) := t;
4037 fi;
4038 labelString := str labelType & "." & t;
4039 if (not isInList(labelString, uniqueTextLabels)) or (not omitDuplicateTextLabels):
4040 appendList(uniqueTextLabels, labelString, 1, false);
4041 if (length(labelType.scantokens(t)) > 0):
4042 labelItself := textLabelRender(labelType.scantokens(t));
4043 if str @# = "r":
4044 labelItself := (labelItself shifted -1/2[ulcorner(labelItself), llcorner(labelItself)]) shifted (-1,0);
4045 elseif str @# = "l":
4046 labelItself := (labelItself shifted -1/2[urcorner(labelItself), lrcorner(labelItself)]) shifted (1,0);
4047 else:
4048 labelItself := labelItself shifted -1/2[ulcorner(labelItself), lrcorner(labelItself)];
4049 fi;
4050 if (d > 0):
4051 bb := ulcorner(labelItself) -- urcorner(labelItself) -- lrcorner(labelItself) -- llcorner(labelItself)--cycle;
4052 labelItself := labelItself shifted -(bb intersectionpoint ((0,0)--(dir(a+180+globalRotation)*1cm)));
4053 fi;
4054 labelItself := image(
4055 draw labelItself
4056 shifted ((p scaled scaleFactor) rotated globalRotation)
4057 shifted (dir(a+globalRotation)*d)
4058 withcolor bylabelcolor;
4059 );
4060 else:
4061 labelItself := image();
4062 fi;
4063 else:
4064 labelItself := image();
4065 fi;
4066 labelItself
4067 enddef;
4068
4069 vardef byLabelPoint(suffix p)(expr a, d) =
4070 image(
4071 if (textLabels):
4072 draw byTextLabel(pointLabel)(str p, p, a, textLabelShift*d);
4073 fi;
4074 )
4075 enddef;
4076
4077 vardef byLabelLine(expr d)(text linesList) =
4078 byLabelLineFull(d, ALL_LABELS)(linesList)
4079 enddef;
4080
4081 vardef byLabelLineFull(expr d, sty)(text linesList) =
4082 save a;
4083 numeric a;
4084 if (d = 0):
4085 a := 90;
4086 else:
4087 a := -90;
4088 fi;
4089 image(
4090 if (textLabels):
4091 forsuffixes lN=linesList:
4092 if (getAttribute("line", "UseLineLabel", str lN)):
4093 draw byTextLabel(pointLabel)(getAttribute("line", "Label", str lN),
4094 1/2[scantokens(getAttribute("line", "EndAName", str lN)),
4095 scantokens(getAttribute("line", "EndBName", str lN))],
4096 getAttribute("line", "Angle", str lN) + a, textLabelShift);
4097 else:
4098 if (sty = ALL_LABELS) or (sty = OMIT_LAST_LABEL):
4099 draw byTextLabel(pointLabel)(
4100 getAttribute("line", "EndAName", str lN),
4101 scantokens(getAttribute("line", "EndAName", str lN)),
4102 getAttribute("line", "Angle", str lN) + a,
4103 textLabelShift);
4104 fi;
4105 if (sty = ALL_LABELS) or (sty = OMIT_FIRST_LABEL):
4106 draw byTextLabel(pointLabel)(
4107 getAttribute("line", "EndBName", str lN),
4108 scantokens(getAttribute("line", "EndBName", str lN)),
4109 getAttribute("line", "Angle", str lN) + a,
4110 textLabelShift);
4111 fi;
4112 fi;
4113 endfor;
4114 fi;
4115 )
4116 enddef;
4117
4118 vardef byLabelPolygon(expr d)(text polygonsList) =
4119 image(
4120 if (textLabels):
4121 draw byLabelsOnPolygon(scantokens byMergePolygons(polygonsList))(OMIT_LABELS_AT_STRAIGHT_ANGLES, d);
4122 fi;
4123 )
4124 enddef;
4125
4126 vardef byLabelCircle(expr l, cn) =
4127 save o, d;
4128 pair o;
4129 numeric d;
4130 o := projectPointIfInSpace(getAttribute("circle", "Center", cn));
4131 d := lineThickness(getAttribute("circle", "Thin", cn))*(1/2-1/2getAttribute("circle", "Shift", cn));
4132 byTextLabel(pointLabel)(l, scantokens(l), angle(o - scantokens(l)), textLabelShift + d)
4133 enddef;
4134
4135 vardef byLabelsOnCircle(text pointsList)(suffix cn) =
4136 save d, t, s, c;
4137 numeric t, s;
4138 pair c;
4139 if string getAttribute("circle", "CenterName", str cn):
4140 t := getAttribute("circle", "Thin", str cn);
4141 s := getAttribute("circle", "Shift", str cn);
4142 c := byReturnCircleCenter(str cn);
4143 elseif string getAttribute("arc", "CenterName", str cn):
4144 t := getAttribute("arc", "Thin", str cn);
4145 s := getAttribute("arc", "Shift", str cn);
4146 c := scantokens(getAttribute("arc", "CenterName", str cn));
4147 else:
4148 errmessage("There is no circle or arc named " & str cn);
4149 fi;
4150 d := lineThickness(t)*(1/2+1/2s);
4151 image(
4152 if (textLabels):
4153 forsuffixes pN=pointsList:
4154 draw byTextLabel(pointLabel)(str pN, pN, angle(pN - c), textLabelShift + d);
4155 endfor;
4156 fi;
4157 )
4158 enddef;
4159
4160 vardef byLabelsOnPolygon(text pointsList)(expr sty, shft)=
4161 image(
4162 if(textLabels):
4163 save pointName, pointLoc, i, j, k, l, p, q, stump, sv;
4164 numeric i, j, k, l, stump, sv;
4165 string pointName[];
4166 pair pointLoc[], p[];
4167 i := -1;
4168 forsuffixes pN=pointsList:
4169 boolean pointDrawn.pN;
4170 pointDrawn.pN := false;
4171 i := i + 1;
4172 pointName[i] := str pN;
4173 if (pointName[i] <> "noPoint"):
4174 pointLoc[i] := pN;
4175 fi;
4176 if (i > 0):
4177 if (pointName[i] = pointName[i-1]):
4178 i := i - 1;
4179 fi;
4180 fi;
4181 endfor;
4182 if (pointName[i] = pointName[0]):
4183 i := i - 1;
4184 fi;
4185 if (i = 1):
4186 i := i + 1;
4187 pointName[i] := "noPoint";
4188 fi;
4189 for j := 0 step 1 until i:
4190 k := cycleval(j - 1, i + 1);
4191 l := cycleval(j + 1, i + 1);
4192 if (pointName[j] <> "noPoint"):
4193 p1 := pointLoc[j];
4194 stump := 0;
4195 if (pointName[k] <> "noPoint") or (pointName[l] <> "noPoint"):
4196 if (pointName[k] = "noPoint"):
4197 p2 := pointLoc[l];
4198 p0 := 2[p2, p1];
4199 stump := -1;
4200 elseif (pointName[l] = "noPoint"):
4201 p0 := pointLoc[k];
4202 p2 := 2[p0, p1];
4203 stump := +1;
4204 else:
4205 p0 := pointLoc[k];
4206 p2 := pointLoc[l];
4207 fi;
4208 fi;
4209 pair q[];
4210 q4 := unitvector(p1-p0) rotated 90;
4211 q5 := unitvector(p2-p1) rotated 90;
4212 if ((abs(angle(q4) - angle(q5)) mod 180) > 1):
4213 q0 := p0 shifted q4;
4214 q1 := p1 shifted q4;
4215 q2 := p1 shifted q5;
4216 q3 := p2 shifted q5;
4217 q6 = whatever[q0, q1] = whatever[q2, q3];
4218 else:
4219 if (sty <> OMIT_LABELS_AT_STRAIGHT_ANGLES) or (stump <> 0):
4220 q6 := p1 shifted q4;
4221 else:
4222 q6 := p1;
4223 fi;
4224 fi;
4225 if ((sty = OMIT_FIRST_LABEL + OMIT_LAST_LABEL) or (sty = OMIT_FIRST_AND_LAST_LABEL)) and ((j = 0) or (j = i)):
4226 pointDrawn.scantokens(pointName[j]) := true;
4227 fi;
4228 if (sty = OMIT_FIRST_LABEL) and (j = 0):
4229 pointDrawn.scantokens(pointName[j]) := true;
4230 fi;
4231 if (sty = OMIT_LAST_LABEL) and (j = i):
4232 pointDrawn.scantokens(pointName[j]) := true;
4233 fi;
4234 if (q6 <> p1) and (not pointDrawn.scantokens(pointName[j])):
4235 sv := byLabelAngleCompensate(p0, p1, p2, shft);
4236 draw byTextLabel(pointLabel)(pointName[j], pointLoc[j], angle(q6-p1), textLabelShift+sv);
4237 pointDrawn.scantokens(pointName[j]):= true;
4238 fi;
4239 fi;
4240 endfor;
4241 fi;
4242 )
4243 enddef;
4244
4245 vardef byLabelLineEnd (suffix a, b)(expr d) =
4246 image(
4247 if (textLabels):
4248 draw byTextLabel(pointLabel)(str a, a, angle(a-b), textLabelShift+(d*lineWidth));
4249 fi;
4250 )
4251 enddef;
4252
4253 vardef byLabelAngleCompensate (expr a, b, c, s) =
4254 save ang, ins;
4255 numeric ang, ins;
4256 ang := angleValue(a, b, c)/2;
4257 if (sind(ang*2)>0):
4258 ins := (cosd(ang)/(2*sind(ang)))*textLabelAvSize;
4259 else:
4260 ins := 0;
4261 fi;
4262 (((abs(sind(ang)) + abs(cosd(ang)/abs(sind(ang))))*(lineWidth*(1/2+1/2s)))/2) + ins
4263 enddef;