Debian Codegraph @

texmf-dist/metapost/fiziko/fiziko.mp

raw
1 % fiziko 0.2.1
2 % MetaPost library for physics textbook illustrations
3 % Copyright 2023 Sergey Slyusarev
4 %
5 % This program is free software: you can redistribute it and/or modify
6 % it under the terms of the GNU General Public License as published by
7 % the Free Software Foundation, either version 3 of the License, or
8 % (at your option) any later version.
9 %
10 % This program is distributed in the hope that it will be useful,
11 % but WITHOUT ANY WARRANTY; without even the implied warranty of
12 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 % GNU General Public License for more details.
14 %
15 % You should have received a copy of the GNU General Public License
16 % along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 % https://github.com/jemmybutton/fiziko
19
20 %
21 % Here we define some things of general interest
22 %
23
24 pi := 3.1415926;
25 radian := 180/pi;
26
27 vardef sin primary x = (sind(x*radian)) enddef;
28
29 vardef cos primary x = (cosd(x*radian)) enddef;
30
31 vardef log (expr n, b) =
32 save rv;
33 numeric rv;
34 if n > 0:
35 rv := (mlog(n)/mlog(b));
36 else:
37 rv := 0;
38 fi;
39 rv
40 enddef;
41
42 vardef arcsind primary x = angle((1+-+x,x)) enddef;
43
44 vardef arccosd primary x = angle((x,1+-+x)) enddef;
45
46 vardef arcsin primary x = ((arcsind(x))/radian) enddef;
47
48 vardef arccos primary x = ((arccosd(x))/radian) enddef;
49
50 vardef angleRad primary x = angle(x)/radian enddef;
51
52 vardef dirRad primary x = dir(x*radian) enddef;
53
54 % used here and there.
55
56 vardef sign (expr x)=
57 if x > 0: 1 fi
58 if x < 0: -1 fi
59 if x = 0: 1 fi
60 enddef;
61
62 % This is inverted `clip`
63
64 primarydef i maskedWith p =
65 begingroup
66 save q, invertedmask, resultimage, breakpoint;
67 pair q[];
68 path invertedmask;
69 picture resultimage;
70 resultimage := i;
71 q1 := ulcorner(i) shifted (-1, 1);
72 q3 := lrcorner(i) shifted (1, -1);
73 q2 := (xpart(q3), ypart(q1));
74 q4 := (xpart(q1), ypart(q3));
75 breakpoint := ypart((ulcorner(p)--llcorner(p)) firstIntersectionTimes p);
76 invertedmask := (subpath (breakpoint, length(p) + breakpoint) of p) -- q1 -- q2 -- q3 -- q4 -- q1 -- cycle;
77 clip resultimage to invertedmask;
78 resultimage
79 endgroup
80 enddef;
81
82 %
83 % Since metapost is somewhat unpredictable in determining where paths intersect, here's macro
84 % that returns first intersection times with first path (ray) priority.
85 % Actually, it is so in most cases, but sometimes second path can take precedence,
86 % so the macro just checks whether reversing 'q' changes something
87 %
88
89 primarydef p firstIntersectionTimes q =
90 begingroup
91 save t;
92 pair t[];
93 t1 := p intersectiontimes q;
94 t2 := p intersectiontimes reverse(q);
95 if xpart(t1) < xpart(t2):
96 t3 := t1;
97 else:
98 t3 := (xpart(t2), length(q) - ypart(t2));
99 fi;
100 if xpart(t1) < 0: t3 := t2; fi;
101 t3
102 endgroup
103 enddef;
104
105 % This checks if point a is inside of closed path p
106
107 primarydef a isInside p =
108 begingroup
109 save ang, v, i, rv, pp;
110 boolean rv;
111 pair pp[];
112 ang := 0;
113 for i := 0 step 1/4 until (length(p)):
114 pp1 := (point i of p) - a;
115 pp2 := (point i + 1/4 of p) - a;
116 if (pp1 <> (0, 0)) and (pp2 <> (0, 0)):
117 v := angle(pp1) - angle(pp2);
118 if v > 180: v := v - 360; fi; if v < -180: v := v + 360; fi;
119 ang := ang + v;
120 fi;
121 endfor;
122 if abs(ang) > 355:
123 rv := true;
124 else:
125 rv := false;
126 fi;
127 rv
128 endgroup
129 enddef;
130
131 % rotation in radians
132
133 primarydef somethingToRotate radRotated radAngle =
134 somethingToRotate rotated ((radAngle/pi)*180)
135 enddef;
136
137 %
138 % some 3D stuff
139 %
140
141 % this one's from byrne.mp
142
143 primarydef colorone dotprodXYZ colortwo =
144 begingroup
145 save xp, yp, zp;
146 numeric xp[], yp[], zp[];
147 xp1 := (redpart colorone);
148 yp1 := (greenpart colorone);
149 zp1 := (bluepart colorone);
150 xp2 := (redpart colortwo);
151 yp2 := (greenpart colortwo);
152 zp2 := (bluepart colortwo);
153 xp1*xp2 + yp1*yp2 + zp1*zp2
154 endgroup
155 enddef;
156
157 %
158 % sometimes it's useful to put some arrows along the path. this macro puts them
159 % in the middles of the segments that have length no less than midArrowLimit;
160 %
161
162 midArrowLimit := 1cm;
163
164 def drawmidarrow (expr p) text t =
165 begingroup
166 save i, j, q;
167 path q;
168 j := 0;
169 for i := 1 upto length(p):
170 if arclength(subpath(i-1, i) of p) >= midArrowLimit:
171 q := subpath(j, i - 1/2) of p;
172 j := i - 1/2;
173 draw q t;
174 filldraw arrowhead q t;
175 fi;
176 endfor;
177 draw subpath(j, length(p)) of p t;
178 endgroup
179 enddef;
180
181 % This macro marks angles, unsurprisingly
182
183 def markAngle (expr a, o, b) (text t) =
184 begingroup
185 save p, an, d;
186 numeric an[], d[];
187 pair p;
188 an1 := angle(a-o);
189 an2 := angle(b-o) - an1;
190 if (an2 < 0): an2 := an2 + 360; fi;
191 an3 := an1 + 1/2an2;
192 p := center(t);
193 d1 := abs(ulcorner(t)-lrcorner(t));
194 if (an2 < 90) and (an2 > 0):
195 d2 := max(1/3cm, (d1/(abs(sind(an2))*1/3cm))*1/3cm);
196 else:
197 d2 := 1/3cm;
198 fi;
199 draw subpath (0, 8an2/360) of fullcircle scaled 2d2 rotated an1 shifted o withpen thinpen;
200 draw (t) shifted -p shifted o shifted (dir(an3)*(d2 + d1));
201 endgroup
202 enddef;
203
204 %
205 % Here we define some auxilary global variables
206 %
207
208 % Offset path algorithm can subdivide original path in order to be more precise
209 offsetPathSteps := 4;
210
211 % The following macro sets all the values related to minimal stroke width at once.
212 % It can be used to easily redefine all of them.
213 def defineMinStrokeWidth (expr msw) =
214 % We don't want to display strokes that are too thin to print. Default value
215 % is subject to change when needed.
216 minStrokeWidth := msw;
217 maxShadingStrokeWidth := 3/2minStrokeWidth;
218
219 % At some point it's useless to display even dashes
220 minDashStrokeWidth := 1/3minStrokeWidth;
221
222 % this value corresponds to particular dashing algorithm and is subject to change whenever this algorithm changes
223 minDashStrokeLength := 6minStrokeWidth;
224
225 dashStrokeWidthStep := (minStrokeWidth - minDashStrokeWidth)/16;
226
227 % all the shading algorithms need to know how close lines should be packed
228 shadingDensity := 3maxShadingStrokeWidth;
229
230 stippleSize := 3/2minStrokeWidth;
231 minStippleStep := 1/2stippleSize;
232 stippleShadingDensity := 3minStippleStep;
233 minStippleStrokeWidth := 1/20stippleSize;
234
235 % here are some pens
236 pen thinpen, thickpen, fatpen, stipplepen;
237
238 thinpen := pencircle scaled minStrokeWidth;
239 thickpen := pencircle scaled 3minStrokeWidth;
240 fatpen := pencircle scaled 6minStrokeWidth;
241 stipplepen := pencircle scaled stippleSize;
242 enddef;
243
244 defineMinStrokeWidth(1/5pt);
245
246 % here we set global light direction
247
248 def defineLightDirection (expr ldx, ldy) =
249 pair lightDirection;
250 color lightDirectionVectorXYZ;
251 lightDirection := (ldx, ldy);
252 lightDirectionVectorXYZ := (0, 0, 1);
253 lightDirectionVectorXYZ := rotateXYZaround.x(lightDirectionVectorXYZ, ldy);
254 lightDirectionVectorXYZ := rotateXYZaround.y(lightDirectionVectorXYZ, ldx);
255 enddef;
256
257 vardef rotateXYZaround@# (expr p, a) =
258 save partProj, rv;
259 pair partProj;
260 color rv;
261 if str @# = "x":
262 partProj := (greenpart(p), bluepart(p)) radRotated -a;
263 rv := (redpart(p), xpart(partProj), ypart(partProj));
264 elseif str @# = "y":
265 partProj := (redpart(p), bluepart(p)) radRotated -a;
266 rv := (xpart(partProj), greenpart(p), ypart(partProj));
267 elseif str @# = "z":
268 partProj := (redpart(p), greenpart(p)) radRotated -a;
269 rv := (xpart(partProj), ypart(partProj), bluepart(p));
270 else:
271 errmessage("What axis is " & str @# & "?");
272 fi;
273 rv
274 enddef;
275
276 defineLightDirection(-1/8pi, 1/8pi);
277
278 boolean shadowsEnabled;
279 shadowsEnabled := false;
280
281 %
282 % To simplify further calculations we need subdivided original path
283 %
284
285 vardef pathSubdivideBase (expr p, subdivideStep, i) =
286 save returnPath, sp;
287 path returnPath, sp;
288 returnPath := point i of p;
289 if i<length(p):
290 sp := subpath(i, i + subdivideStep) of p;
291 returnPath := returnPath .. controls (postcontrol 0 of sp) and (precontrol 1 of sp) .. pathSubdivideBase (p, subdivideStep, i + subdivideStep);
292 fi;
293 if (i = 0) and (cycle p):
294 (subpath(0, length(returnPath)-1) of returnPath) .. controls (postcontrol length(returnPath)-1 of returnPath) and (precontrol length(returnPath) of returnPath) .. cycle
295 else:
296 returnPath
297 fi
298 enddef;
299
300 vardef offsetPathSubdivide (expr p) =
301 pathSubdivideBase(p, 1/offsetPathSteps, 0)
302 enddef;
303
304 vardef pathSubdivide (expr p, n) =
305 pathSubdivideBase(p, 1/n, 0)
306 enddef;
307
308 %
309 % This macro creates a template offset path to a straight line, so we can correct angles
310 % It might appear as if we need to calculate derivative of the function somehow, instead of mocking it
311 % but this function might be anything, function of coordinates of distance to some point etc.,
312 % so consider this a lazy way to do the right thing.
313 %
314 % either offsetPathTime or offsetPathLength are intended to be used as arguments. offsetPathTime is for time and offsetPathLength is for distance
315 %
316
317 vardef offsetPathTemplate (expr p, i) (text offsetFunction) =
318 save returnPath, offsetPathTime, offsetPathLength, instantDirection, nextDirection;
319 numeric offsetPathTime, offsetPathLength, currentAngle;
320 pair instantDirection, nextDirection;
321 path returnPath;
322 if (i <= length(p)):
323 offsetPathTime := i;
324 else:
325 offsetPathTime := length(p);
326 fi;
327 if (arclength(p) > 0):
328 offsetPathLength := arclength(subpath (0, i) of p)/arclength(p);
329 else:
330 offsetPathLength := 0;
331 fi;
332 returnPath := (arclength(subpath (0, i) of p), offsetFunction);
333 if (i < length(p)):
334 % this thing is glitchy, but should be more accurate
335 %if (arclength(subpath (0, i) of p) < arclength(subpath (0, i + 1/4) of p)):
336 % offsetPathTime := i + 1/4;
337 % offsetPathLength := arclength(subpath (0, i + 1/4) of p)/arclength(p);
338 % instantDirection := unitvector((arclength(subpath (0, i + 1/4) of p), offsetFunction) - point 0 of returnPath);
339 % offsetPathTime := i + 1;
340 % offsetPathLength := arclength(subpath (0, i + 1) of p)/arclength(p);
341 % nextDirection := (arclength(subpath (0, i + 1) of p), offsetFunction);
342 % offsetPathTime := i + 3/4;
343 % offsetPathLength := arclength(subpath (0, i + 3/4) of p)/arclength(p);
344 % nextDirection := unitvector(nextDirection - (arclength(subpath (0, i + 3/4) of p), offsetFunction));
345 % returnPath := returnPath{instantDirection} .. {nextDirection}offsetPathTemplate(p, i + 1)(offsetFunction);
346 % returnPath := returnPath -- offsetPathTemplate(p, i + 1)(offsetFunction);
347 %else:
348 returnPath := returnPath -- offsetPathTemplate(p, i + 1)(offsetFunction);
349 %fi;
350 fi;
351 returnPath
352 enddef;
353
354 %
355 % This macro creates offset path p based on previously built template q, instead of function itself
356 % It is loosely based on something called Tiller-Hanson heuristic as described here:
357 % http://math.stackexchange.com/questions/465782/control-points-of-offset-bezier-curve
358 %
359
360 vardef offsetPathGenerate (expr p, q, i) =
361 save returnPath, c, d, a, pl, ps;
362 path returnPath, pl[];
363 pair c[], d[];
364 numeric a[];
365 c1 := precontrol i of p;
366 c2 := point i of p;
367 c3 := postcontrol i of p;
368 if abs(c1-c2) = 0:
369 c1 := c2 shifted (c2-c3);
370 fi;
371 if abs(c3-c2) = 0:
372 c3 := c2 shifted (c2-c1);
373 fi;
374 if (abs(c1-c2) > 0) and (abs(c2-c3) > 0):
375 d1 := unitvector(c1-c2) rotated -90;
376 d2 := unitvector(c2-c3) rotated -90;
377 pl1 := (unitvector(c2-c1)--unitvector(c1-c2))
378 scaled arclength(subpath (i - 1/2, i + 1/2) of p)
379 shifted (point i of p shifted (d1 scaled ypart(point i of q)));
380 pl2 := (unitvector(c2-c3)--unitvector(c3-c2))
381 scaled arclength(subpath (i - 1/2, i + 1/2) of p)
382 shifted (point i of p shifted (d2 scaled ypart(point i of q)));
383 if (abs(angle(d1) - angle(d2)) > 2) and (xpart(pl1 intersectiontimes pl2) > 0):
384 c4 := pl1 intersectionpoint pl2;
385 else:
386 c4 := c2 shifted (d1 scaled ypart(point i of q));
387 fi;
388 returnPath := c4;
389 else:
390 returnPath := c2 shifted (unitvector( (point i-1 of p) - (point i+1 of p) rotated -90) scaled ypart (point i of q));
391 fi;
392 if i < length(p):
393 path ps;
394 ps := subpath (i, i + 1) of p;
395 c1 := point 0 of ps;
396 c2 := postcontrol 0 of ps;
397 c3 := precontrol 1 of ps;
398 c4 := point 1 of ps;
399 c5 := point 0 of returnPath;
400 if (abs(c3-c4)>0)
401 and (abs(c1-c2)>0)
402 and (abs(c1-c4)>0)
403 and (abs(direction i of q) > 0):
404 c6 := c4 shifted (unitvector(c4 - c3) rotated 90 scaled ypart(point i + 1 of q));
405 %if abs(direction i of q) > 0:
406 a1 := angle(direction i of q);
407 %else:
408 % a1 := angle((point (i + 1/10) of q) - (point (i - 1/10) of q));
409 %fi;
410 %if abs(direction i + 1 of q) > 0:
411 a2 := angle(direction i + 1 of q);
412 %else:
413 % a2 := angle((point (i + 1 + 1/10) of q) - (point (i + 1 - 1/10) of q));
414 %fi;
415 c7 := (c2 - c1) scaled (abs(c5-c6)/abs(c1-c4)) rotated a1 shifted c5;
416 c8 := (c3 - c4) scaled (abs(c5-c6)/abs(c1-c4)) rotated a2 shifted c6;
417 returnPath := returnPath .. controls c7 and c8 .. offsetPathGenerate (p, q, i + 1);
418 else:
419 returnPath := returnPath -- offsetPathGenerate (p, q, i + 1);
420 fi;
421 fi;
422 returnPath
423 enddef;
424
425 %
426 % Frontend for offsetPathGenerate and offsetPathTemplate
427 %
428
429 vardef offsetPath (expr p)(text offsetFunction) =
430 offsetPathGenerate (p, offsetPathTemplate(p, 0)(offsetFunction), 0)
431 enddef;
432
433 %
434 % Brush macro. It draws line with brush of variable width.
435 % For parts thicker than minStrokeWidth it uses offsetPath functions'
436 % results, for thiner parts it draws dashed lines of fixed width
437 %
438
439 def brushGenerate (expr p, q, i) =
440 begingroup
441 save w, brushPath, bt, t;
442 numeric w[], t[];
443 path brushPath[], bt;
444 bt := q;
445 w0 := (ypart(urcorner(bt)));
446 w1 := (ypart(lrcorner(bt)));
447 t := cutPathTime(bt, minStrokeWidth);
448 if ((w0 > minStrokeWidth)
449 and (w1 < minStrokeWidth)
450 and (t > 0)
451 and (t < length(p))
452 and (arclength(p) > minDashStrokeLength)
453 and (i < 10)):
454 brushGenerate (subpath (0, t) of p, subpath (0, t) of q, i + 1);
455 brushGenerate (subpath (t, length(p)) of p, subpath (t, length(q)) of q, i + 1);
456 elseif (arclength(p) > 0):
457 if (w0 > 99/100minStrokeWidth)
458 and (w1 > 99/100minStrokeWidth):
459 brushPath1 := offsetPathGenerate (p, q yscaled 1/2, 0);
460 brushPath2 := offsetPathGenerate (p, q yscaled -1/2, 0);
461 fill brushPath1 -- reverse(brushPath2) -- cycle;
462 elseif (w0 < 101/100minStrokeWidth) and (w1 < 101/100minStrokeWidth):
463 thinBrushGenerate (p, q, 0)
464 fi;
465 fi;
466 endgroup
467 enddef;
468
469 %
470 % macro for thin lines which are actually dashed
471 %
472
473 vardef thinBrushGenerate@#(expr p, q, i) =
474 begingroup
475 save w, brushPath, bt, t, h, minLength, minWidth, dashPatternImage;
476 numeric w[], t[];
477 path brushPath[], bt;
478 picture dashPatternImage;
479 if (str @# = "") or (str @# = "hatches"):
480 minLength := minDashStrokeLength;
481 minWidth := minDashStrokeWidth;
482 elseif str @# = "stipples":
483 minLength := minStippleStep;
484 minWidth := minStippleStrokeWidth;
485 fi;
486 bt := q;
487 w0 := ypart(urcorner(bt));
488 w1 := ypart(lrcorner(bt));
489 if (w0 > minWidth + 1/100):
490 if (w1 > minWidth - 1/100):
491 w2 := floor((1/2(w0 + w1) - minWidth)/dashStrokeWidthStep)*dashStrokeWidthStep + minWidth;
492 else:
493 w2 := minWidth;
494 fi;
495 t := cutPathTime(bt, w2);
496 brushPath1 := subpath (0, t) of p;
497 brushPath2 := subpath (t, length(p)) of p;
498 if (((w0 - w1) >= dashStrokeWidthStep) and (i < 15))
499 and ((arclength(brushPath1) > minLength)
500 or (arclength(brushPath2) > minLength))
501 and (t > 1/100) and (t < length(p) - 1/100):
502 thinBrushGenerate@#(brushPath1, subpath (0, t) of q, i + 1);
503 thinBrushGenerate@#(brushPath2, subpath (t, length(q)) of q, i + 1);
504 else:
505 if (str @# = "") or (str @# = "hatches"):
506 if (w2 > minStrokeWidth):
507 w2 := minStrokeWidth;
508 fi;
509 if (w2 >= minWidth) and (arclength(p) > 0):
510 if (w2 < minStrokeWidth) and (arclength(p) > minLength):
511 draw p withpen thinpen dashed thinBrushPattern(w2, arclength(p));
512 else:
513 draw p withpen thinpen;
514 fi;
515 fi;
516 elseif str @# = "stipples":
517 begingroup
518 interim linecap := rounded;
519 save stippleSizeVar;
520 stippleSizeVar := stippleSize;
521 save stippleSize;
522 w2 := 1/3w2;
523 if (w2 >= minWidth) and (arclength(p) > 0):
524 stippleSize := stippleSizeVar * (0.9 + uniformdeviate(0.3));
525 dashPatternImage := stipplesBrushPattern(w2, arclength(p));
526 if urcorner(dashPatternImage) <> (0,0):
527 brushPath1 := offsetPathGenerate (p, (q yscaled 0) shifted (0, 1/3stippleShadingDensity), 0);
528 draw brushPath1 withpen (pencircle scaled stippleSize) dashed dashPatternImage;
529 fi;
530 stippleSize := stippleSizeVar * (0.9 + uniformdeviate(0.3));
531 dashPatternImage := stipplesBrushPattern(w2, arclength(p));
532 if urcorner(dashPatternImage) <> (0,0):
533 brushPath2 := offsetPathGenerate (p, (q yscaled 0) shifted (0, -1/3stippleShadingDensity), 0);
534 draw brushPath2 withpen (pencircle scaled stippleSize) dashed dashPatternImage;
535 fi;
536 stippleSize := stippleSizeVar * (0.9 + uniformdeviate(0.3));
537 dashPatternImage := stipplesBrushPattern(w2, arclength(p));
538 if urcorner(dashPatternImage) <> (0,0):
539 draw p withpen (pencircle scaled stippleSize) dashed dashPatternImage;
540 fi;
541 fi;
542 endgroup
543 fi;
544 fi;
545 fi;
546 endgroup
547 enddef;
548
549 %
550 % this macro returns path as a shaded edge
551 %
552
553 vardef shadedEdge (expr p) =
554 image(
555 brushGenerate (p,
556 offsetPathTemplate (p, 0) (
557 1/2minStrokeWidth + 2*minStrokeWidth
558 * normalVectorToLightness(
559 sphereAnglesToNormalVector(
560 (angleRad(point offsetPathTime of p), arcsin(1/2))
561 ), 0, point offsetPathTime of p
562 )
563 ), 0);
564 )
565 enddef;
566
567 %
568 % Whenever we have brush thinner than minStrokeWidth we call this dash pattern macro
569 %
570
571 vardef thinBrushPattern (expr w, l) =
572 save d;
573 numeric d[];
574 d0 := w;
575 if d0 > minStrokeWidth: d0 := minStrokeWidth; fi;
576 % d1 is a result of some arbitrary function of line width
577 % we do not use simple linear function because minimal dash length
578 % also shouldn't be less than minStrokeWidth.
579 % After we get d1 other measurements are calculated,
580 % so filled area per unit length remains adequate and dashes are aligned
581 % with segments
582 % d1*mSW = (d1*w + d2*w) -> d2=d1(mSW-w)/w
583 d1 := (1/2minDashStrokeLength) + (((d0/minStrokeWidth)**(5/2))*1/2minDashStrokeLength);
584 d1 := d1 + 1/2uniformdeviate(d1);
585 d2 := (minStrokeWidth - d0)*(d1/d0);
586 d3 := round(l/(d2 + d1));
587 if (d3 < 1): d3 := 1; fi;
588 d4 := (l/d3)/(d2 + d1);
589 d1 := d1*d4;
590 d2 := d2*d4;
591 if (uniformdeviate(2) > 1):
592 dashpattern (on d1/2 off d2 on d1/2)
593 else:
594 dashpattern (off d2/2 on d1 off d2/2)
595 fi
596 enddef;
597
598
599 %
600 % Stipples are also dashes
601 %
602
603 vardef stipplesBrushPattern (expr w, l) =
604 save d, n, rn, rv, ss;
605 ss := 1/1000;
606 numeric d[];
607 picture rv;
608 %if w > stippleSize:
609 % d0 := minStippleStep;
610 %else:
611 n := (w*l)/(stippleSize**2);
612 rn := floor(n);
613 if rn > 0:
614 d0 := l/rn;
615 fi;
616 %fi;
617 if rn > 0:
618 d1 := uniformdeviate(d0);
619 d2 := d0-d1;
620 if rn >=3:
621 d3 := uniformdeviate(d0);
622 d4 := d0-d3;
623 %if uniformdeviate(2) > 1:
624 % d5 := uniformdeviate(d0);
625 % d6 := d0-d5;
626 % rv := dashpattern (off d1 on ss off (d2+d5)-ss on ss off (d4+d6)-ss on ss off d3-ss);
627 %else:
628 rv := dashpattern (off d1 on ss off (d2+d3)-ss on ss off d4-ss);
629 %fi;
630 else:
631 rv := dashpattern (off d1 on ss off d2-ss);
632 fi;
633 else:
634 if uniformdeviate(1) < n:
635 rv := dashpattern (off uniformdeviate(l-ss) on ss off l);
636 else:
637 rv := image();
638 fi;
639 fi;
640 rv
641 enddef;
642
643 %
644 % macro that actually draws line of variable width
645 %
646
647 vardef brush (expr p) (text offsetFunction) =
648 image(
649 brushGenerate (p, offsetPathTemplate(p, 0)(offsetFunction), 0);
650 )
651 enddef;
652
653 %
654 % same, but only for thin brushes
655 %
656
657 vardef thinBrush@#(expr p) (text offsetFunction) =
658 image(
659 thinBrushGenerate@#(p, offsetPathTemplate(p, 0)(offsetFunction), 0);
660 )
661 enddef;
662
663 %
664 % This macro generates tube between paths p and q, of variable width d
665 % Tube is subdivided into segments in such a way that within every segment
666 % we need 2**n lines to generate even fill
667 %
668
669 vardef tubeGenerate@#(expr p, q, d, i) =
670 save w, bw, k, t, tubeWidth, sp, currentPath, currentTubePath, currentDepth, lineDensity;
671 numeric w[], bw[], t, currentDepth;
672 path tubeWidth, sp, currentPath, currentTubePath;
673 tubeWidth := d yscaled 2;
674 if (str @# = "") or (str @# = "hatches"):
675 lineDensity := shadingDensity;
676 elseif (str @# = "stipples"):
677 lineDensity := stippleShadingDensity;
678 fi;
679 w0 := (ypart(urcorner(tubeWidth))) - 1/1000;
680 w1 := (ypart(lrcorner(tubeWidth))) + 1/1000;
681 w2 := ceiling(log(w0/lineDensity, 2));
682 w3 := ceiling(log(w1/lineDensity, 2));
683 if ((w2 > w3) and (i<20)):
684 t := cutPathTime(tubeWidth, lineDensity*(2**(w2-1)));
685 tubeGenerate@#(subpath (0, t) of p, subpath (0, t) of q, subpath (0, t) of d, i + 1);
686 tubeGenerate@#(subpath (t, length(p)) of p, subpath (t, length(q)) of q, subpath (t, length(d)) of d, i + 1);
687 else:
688 if (arclength(p) > 0) and (arclength(q) > 0):
689 bw1 := 2**w2;
690 currentTubePath := interpath (1/2, q, p);
691 for k := 0 upto bw1:
692 currentPath := interpath (k/bw1, q, p);
693 angleOnTube := arcsin(((k/bw1)*2) - 1);
694 currentDepth := -abs((1-sin(angleOnTube + 1/2pi))*w0);
695 if shadowsEnabled:
696 currentPath := shadowCut(currentPath, currentDepth);
697 fi;
698 if (str @# = "") or (str @# = "hatches"):
699 brushGenerate (currentPath,
700 offsetPathTemplate(currentPath, 0)(
701 maxShadingStrokeWidth
702 if odd (k): * (abs(ypart(point offsetPathTime of tubeWidth)/bw1) - 1/2lineDensity) fi
703 * normalVectorToLightness(
704 tubeAnglesToNormalVector((
705 angleOnTube,
706 angleRad(direction offsetPathTime of currentTubePath),
707 angleRad(direction offsetPathTime of (tubeWidth yscaled 1/2))
708 )), currentDepth, point offsetPathTime of currentPath)
709 ), 0);
710 elseif (str @# = "stipples"):
711 begingroup
712 save stippleShadingDensity;
713 if w2 > 0:
714 stippleShadingDensity := 2w0/(2**w2); % When the distance between the lines changes, wtipples should spread further apart
715 else:
716 stippleShadingDensity := w0;
717 fi;
718 thinBrushGenerate.@#(currentPath,
719 offsetPathTemplate(currentPath, 0)(
720 stippleSize
721 if odd (k): * (abs(ypart(point offsetPathTime of tubeWidth)/bw1) - 1/2lineDensity) fi
722 * normalVectorToLightness(
723 tubeAnglesToNormalVector((
724 angleOnTube,
725 angleRad(direction offsetPathTime of currentTubePath),
726 angleRad(direction offsetPathTime of (tubeWidth yscaled 1/2))
727 )), currentDepth, point offsetPathTime of currentPath)
728 ), 0);
729 endgroup
730 fi;
731 endfor;
732 fi;
733 fi;
734 enddef;
735
736 %
737 % This macro is analogous to tubeGenerate, but draws transverse strokes
738 % result is somewhat suboptimal for now, but in simple cases it works ok
739 %
740
741 def tubeGenerateAlt (expr p, q, d) =
742 begingroup
743 save spth, lpth, currentPath, pos, t, pthdir, corr, o, l, i, j, k, tubeAngle, pathAngle, scorr, dt;
744 numeric l[];
745 path spth, lpth, currentPath;
746 pos := 0;
747 j := 0;
748 forever:
749 dt := (xpart(point pos of d) + 1/2shadingDensity);
750 scorr := cosd(angle(direction xpart(d intersectiontimes ((dt, ypart(lrcorner(d))) -- (dt, ypart(urcorner(d))))) of d));
751 t1 := arctime ((arclength(subpath(0, pos) of p)) + shadingDensity/scorr) of p;
752 t2 := arctime ((arclength(subpath(0, pos) of q)) + shadingDensity/scorr) of q;
753 if (arclength(subpath(pos, t1) of p) < arclength(subpath(pos, t1) of q)):
754 pthdir := -1;
755 t3 := t1;
756 else:
757 pthdir := 1;
758 t3 := t2;
759 fi;
760 corr := round(arclength(subpath(pos, t3) of if pthdir = 1: p else: q fi)/(shadingDensity/scorr));
761 if (corr < 1): corr := 1; fi;
762 corr := (arclength(subpath(pos, t3) of if pthdir = 1: p else: q fi) - (corr*(shadingDensity/scorr)))/corr;
763 t3 := arctime (arclength(subpath(0, t3) of if pthdir = 1: q else: p fi) - 1/3corr) of if pthdir = 1: q else: p fi;
764 spth := subpath(pos, t3) of if pthdir = 1: q else: p fi;
765 lpth := subpath(pos, t3) of if pthdir = 1: p else: q fi;
766 tubeAngle := angleRad(direction 1/2[pos, t3] of d);
767 pathAngle := angleRad(direction 1/2 of interpath (1/2, spth, lpth));
768 pos := t3;
769 l1 := round(arclength(lpth)/(shadingDensity/scorr));
770 if (l1 < 1): l1 := 1; fi;
771 l2 := arclength(lpth)/(l1*(shadingDensity/scorr));
772 for i := 0 upto l1 - 1:
773 j := j + 1;
774 k := i*(arclength(lpth)/l1);
775 currentPath := point (arctime k of lpth) of spth -- point (arctime k of lpth) of lpth;
776 currentPath := offsetPathSubdivide(currentPath);
777 brushGenerate (
778 currentPath,
779 offsetPathTemplate(currentPath, 0)(
780 maxShadingStrokeWidth
781 * orderFade(offsetPathLength[1/l1, l2], j)
782 * normalVectorToLightness(
783 tubeAnglesToNormalVector((
784 arcsin(pthdir*((offsetPathLength*2)-1)),
785 pathAngle,
786 tubeAngle)
787 ), -2(1/2arclength(currentPath))+sqrt(1 - (2offsetPathLength - 1)**2)*(1/2arclength(currentPath)), point offsetPathTime of currentPath)
788 )
789 , 0);
790 endfor;
791 exitif pos >= length(p);
792 endfor;
793 endgroup
794 enddef;
795
796 %
797 % This macro converts some measurements of point on tube to absolute angle.
798 % Since there are three such measurements, macro gets them as as a single
799 % argument of "color" type, in case it would eventually appear as a result
800 % of some other macro.
801 %
802 % redpart is the angle on the tube's circumference
803 % greenpart is the angle of the tube path
804 % bluepart is the angle of the tube's outline
805 %
806
807 vardef tubeAnglesToNormalVector (expr p) =
808 save normalVector;
809 color normalVector;
810 normalVector := (0, 0, 1);
811 normalVector := rotateXYZaround.y(normalVector, -bluepart(p));
812 normalVector := rotateXYZaround.x(normalVector, redpart(p));
813 normalVector := rotateXYZaround.z(normalVector, -greenpart(p));
814 normalVector
815 enddef;
816
817 %
818 % frontend to simplify tube drawing. tubeOutline variable changes on every call
819 % of the function and can be used afterwards.
820 %
821
822 path tubeOutline;
823 boolean drawTubeEnds;
824 drawTubeEnds := true;
825
826 vardef tube@#(expr p)(text offsetFunction)=
827 save q, respic;
828 path q[];
829 picture respic;
830 q0 := offsetPathSubdivide(p);
831 q1 := offsetPathTemplate(q0, 0)(offsetFunction);
832 q2 := offsetPathGenerate (q0, q1, 0);
833 q3 := offsetPathGenerate (q0, q1 yscaled -1, 0);
834 tubeOutline := q3--reverse(q2)--cycle;
835 if str @# = "e":
836 if not drawTubeEnds:
837 image(
838 draw q2 withpen thinpen;
839 draw q3 withpen thinpen;
840 )
841 else:
842 tubeOutline
843 fi
844 else:
845 image(
846 if str @# = "l":
847 respic := image(tubeGenerate (q2, q3, q1, 0););
848 elseif str @# = "s":
849 respic := image(tubeGenerate.stipples(q2, q3, q1, 0);)
850 elseif str @# = "t":
851 respic := image(tubeGenerateAlt (q2, q3, q1););
852 fi;
853 if (cycle p) or (not drawTubeEnds):
854 draw q2 withpen thinpen;
855 draw q3 withpen thinpen;
856 else:
857 draw q2--reverse(q3)--cycle withpen thinpen;
858 fi;
859 clip respic to (q2--reverse(q3)--cycle);
860 draw respic;
861 )
862 fi
863 enddef;
864
865 %
866 % Sphere can be used as a cap for a tube, so it has same 2**n lines.
867 %
868
869 vardef sphere@#(expr d) =
870 save currentCircle, origCircle, currentRadius, currentDepth, order, circleThickness, lineDensity, shadingPicture;
871 path currentCircle, origCircle;
872 numeric currentRadius, currentDepth, order;
873 picture shadingPicture;
874 if (str @# = "") or (str @# = "c"):
875 lineDensity := shadingDensity;
876 elseif (str @# = "s"):
877 lineDensity := stippleShadingDensity;
878 fi;
879 origCircle := fullcircle;
880 order := 2**ceiling(log((1/2d)/lineDensity, 2));
881 image(
882 draw fullcircle scaled d withpen thinpen;
883 shadingPicture := image(
884 for i := 1 upto order:
885 currentRadius := i/order;
886 currentCircle := origCircle scaled (currentRadius*d) rotated uniformdeviate (1/4pi);
887 if odd(i):
888 circleThickness := maxShadingStrokeWidth * ((abs(d - (lineDensity*order)))/order);
889 else:
890 circleThickness := maxShadingStrokeWidth;
891 fi;
892 currentDepth:= -(1-sqrt(1-currentRadius**2))*(1/2d);
893 if shadowsEnabled:
894 currentCircle := shadowCut(currentCircle, currentDepth);
895 fi;
896 if (str @# = "") or (str @# = "c"):
897 brushGenerate (currentCircle,
898 offsetPathTemplate (currentCircle, 0) (
899 circleThickness
900 * normalVectorToLightness(
901 sphereAnglesToNormalVector(
902 (angleRad(point offsetPathTime of currentCircle), arcsin(currentRadius))
903 ), currentDepth, point offsetPathTime of currentCircle
904 )
905 ), 0);
906 elseif (str @# = "s"):
907 begingroup
908 save stippleShadingDensity;
909 if order > 0:
910 stippleShadingDensity := d/order; % When the distance between the lines changes, wtipples should spread further ap
911 else:
912 stippleShadingDensity := 1/2d;
913 fi;
914 thinBrushGenerate.stipples(currentCircle,
915 offsetPathTemplate (currentCircle, 0) (
916 circleThickness
917 * normalVectorToLightness(
918 sphereAnglesToNormalVector(
919 (angleRad(point offsetPathTime of currentCircle), arcsin(currentRadius))
920 ), currentDepth, point offsetPathTime of currentCircle
921 )
922 ), 0);
923 endgroup
924 fi;
925 endfor;
926 );
927 clip shadingPicture to (fullcircle scaled d);
928 draw shadingPicture;
929 )
930 enddef;
931
932 %
933 % Alternative sphere macro. It's all about latitudinal strokes.
934 % The idea is: when we have a sphere with evenly distributed parallel strokes
935 % we know how their density rises towards edge in a projection,
936 % so all we need to do is to fade lines correspondingly
937 %
938
939 vardef sphereLat (expr d, lat) =
940 save p, a, x, y, sphlat, latrad, n, c, currentPath, nline, tlat;
941 path p[], currentPath, currentArc;
942 sphlat := 0;
943 nline := 0;
944 latrad := (2pi*lat/360);
945 n := ceiling((pi*1/2d)/shadingDensity);
946 if (cosd(lat) <> 0): tlat := (sind(lat)/cosd(lat)); fi;
947 image(
948 draw fullcircle scaled d withpen thinpen;
949 p0 := fullcircle rotated 90;
950 for nline := 1 upto n-1:
951 sphlat := nline*(pi/n);
952 if (sphlat + latrad < pi) and (sphlat + latrad > 0):
953 if (cosd(lat) <> 0):
954 if (sin(sphlat) <> 0):
955 x := tlat*(cos(sphlat)/sin(sphlat));
956 else:
957 x := 0;
958 fi;
959 else:
960 if ((sphlat > 1/2pi) and (lat > 0)) or ((sphlat < 1/2pi) and (lat < 0)):
961 x := -2;
962 else:
963 x := 2;
964 fi;
965 fi;
966 if (abs(x) <= 1):
967 y := arcsin(x);
968 p1 := subpath(6 + 8y/2pi, 2 - 8y/2pi) of p0;
969 else:
970 p1 := p0;
971 fi;
972 if (x > -1) and (arclength(p1) > 0):
973 currentPath := (p1 scaled (d*sin(sphlat)) yscaled sind(lat)) shifted (0, 1/2d*cos(sphlat)*cosd(lat));
974 currentPath := offsetPathSubdivide(currentPath);
975 brushGenerate(currentPath,
976 offsetPathTemplate(currentPath, 0)(
977 maxShadingStrokeWidth * orderFade(
978 sqrt(1 -
979 abs(
980 ypart(point offsetPathTime of currentPath)/(1/2d),
981 1 - abs(
982 1 - abs(
983 xpart(point offsetPathTime of currentPath)
984 /(1/2d)
985 )
986 )**abs(sind(lat))
987 )**2)
988 , nline)
989 * normalVectorToLightness(
990 sphereAnglesToNormalVector((
991 (
992 if (abs(point offsetPathTime of currentPath) > 0):
993 angleRad(point offsetPathTime of currentPath)
994 else:
995 0
996 fi
997 ), arcsin(2abs(point offsetPathTime of currentPath)/(d+1)))
998 ), 0, point offsetPathTime of currentPath)
999 ), 0);
1000 fi;
1001 fi;
1002 endfor;
1003 )
1004 enddef;
1005
1006 vardef orderFade (expr v, n)=
1007 save o;
1008 if (v > 1/256):
1009 o := 2**ceiling(log(1/v, 2));
1010 if ((n mod 1/2o) = 0):
1011 if ((n mod o) = 0):
1012 1
1013 else:
1014 (v*o) - 1
1015 fi
1016 else:
1017 0
1018 fi
1019 else:
1020 0
1021 fi
1022 enddef;
1023
1024 %
1025 % This one converts point location on sphere to normal vector
1026 %
1027
1028 vardef sphereAnglesToNormalVector (expr p) =
1029 save normalVector;
1030 color normalVector;
1031 normalVector := (0, 0, 1);
1032 normalVector := rotateXYZaround.y(normalVector, ypart(p));
1033 normalVector := rotateXYZaround.z(normalVector, -xpart(p));
1034 normalVector
1035 enddef;
1036
1037 %
1038 % Once we get two angles at some point of some surface, we can compute light intensity there.
1039 %
1040
1041 % In case you need to have your shaded objects white-on-black,
1042 % you can just switch invertedLight invertedLight to 'true'
1043
1044 boolean invertedLight;
1045 invertedLight := false;
1046
1047 vardef normalVectorToLightness (expr normalVector, d, q) =
1048 save returnValue, shiftedShadowPath;
1049 path shiftedShadowPath;
1050 if shadowsEnabled:
1051 for i := 0 step 1 until numberOfShadows:
1052 shiftedShadowPath := shadowPath[i] shifted
1053 ((redpart(lightDirectionVectorXYZ), greenpart(lightDirectionVectorXYZ))
1054 scaled ((d-shadowDepth[i])*bluepart(lightDirectionVectorXYZ)));
1055 if q isInside shiftedShadowPath:
1056 returnValue := 1;
1057 fi;
1058 endfor;
1059 fi;
1060 if not known returnValue:
1061 returnValue := 1 - (normalVector dotprodXYZ lightDirectionVectorXYZ);
1062 returnValue := lightnessPP(returnValue);
1063 fi;
1064 if returnValue > 1:
1065 returnValue := 1;
1066 fi;
1067 if returnValue < 0:
1068 returnValue := 0;
1069 fi;
1070 if invertedLight:
1071 returnValue := 1 - returnValue;
1072 fi;
1073 returnValue
1074 enddef;
1075
1076 vardef lightnessPP (expr v) =
1077 v
1078 enddef;
1079
1080 % Shadows are global
1081
1082 path shadowPath[];
1083 numeric shadowDepth[];
1084
1085 % Shadows either require high path resolution, or some points
1086 % on a path in just the right place for shadows.
1087 % This macro adds such points.
1088
1089 vardef shadowCut (expr pathToCut, currentDepth)=
1090 save shiftedShadowPath, pathShadowIntersection, pathShadowCut, currentPath;
1091 path shiftedShadowPath, currentPath;
1092 pair pathShadowIntersection;
1093 numeric pathShadowCut;
1094 currentPath := pathToCut;
1095 for j := 0 step 1 until numberOfShadows:
1096 shiftedShadowPath := shadowPath[j] shifted
1097 ((redpart(lightDirectionVectorXYZ), greenpart(lightDirectionVectorXYZ))
1098 scaled ((currentDepth - shadowDepth[j])*bluepart(lightDirectionVectorXYZ)));
1099 forever:
1100 pathShadowIntersection := shiftedShadowPath firstIntersectionTimes currentPath;
1101 pathShadowCut := ypart(pathShadowIntersection);
1102 if (pathShadowCut > 1/10) and (pathShadowCut < length(currentPath) - 1/10):
1103 currentPath := (subpath (0, pathShadowCut - 1/20) of currentPath) .. (subpath (pathShadowCut + 1/20, length(currentPath)) of currentPath);
1104 fi;
1105 shiftedShadowPath := subpath (xpart(pathShadowIntersection) + 1/5, length(shiftedShadowPath)) of shiftedShadowPath;
1106 exitif (pathShadowCut = -1);
1107 endfor;
1108 endfor;
1109 currentPath
1110 enddef;
1111
1112 %
1113 % Several macros rely on cutting offset path template at given height.
1114 % Taking cutting points closer to the middle gives better results, and that's
1115 % just what this macro tries to do.
1116 %
1117
1118 vardef cutPathTime (expr p, h) =
1119 save cutTime, d;
1120 numeric cutTime[], d[];
1121 d1 := xpart(urcorner(p));
1122 d2 := xpart(ulcorner(p));
1123 if (d2 < d1):
1124 d3 := 1/2(d1 + d2);
1125 cutTime1 := ypart(((d3, h) -- (d1, h)) firstIntersectionTimes p);
1126 cutTime2 := ypart(((d3, h) -- (d2, h)) firstIntersectionTimes p);
1127 d4 := xpart (point cutTime1 of p);
1128 d5 := xpart (point cutTime2 of p);
1129 if abs(d4-d3) < abs(d5-d3):
1130 cutTime3 := cutTime1
1131 else:
1132 cutTime3 := cutTime2
1133 fi;
1134 else:
1135 cutTime3 := -1;
1136 fi;
1137 cutTime3
1138 enddef;
1139
1140 %
1141 % This macro calculates ray angle after refraction. It takes raw angles (one of ray — p and one of surface — q)
1142 % and refraction indices ratio. Whether ray comes from opticaly denser material is determined by direction of q
1143 % relative to that of p
1144 %
1145
1146 vardef refractionAngle (expr p, q, n) =
1147 save a;
1148 numeric a[];
1149 a0 := p - q;
1150 if (sin(a0) < 0):
1151 a1 := cos(a0 + pi) * n;
1152 a2 := pi;
1153 else:
1154 a1 := cos(a0) / n;
1155 a2 := 0;
1156 fi;
1157 if abs(a1) <= 1:
1158 a3 := arccos(a1) + q + a2;
1159 else:
1160 a3 := -1000;
1161 fi;
1162 a3
1163 enddef;
1164
1165 %
1166 % Same thing for reflection angle, just in case
1167 %
1168
1169 vardef reflectionAngle (expr p, q) =
1170 (2pi - p + 2q)
1171 enddef;
1172
1173 %
1174 % This macro returns path of ray 'sa' (which can actually be any path, but only ray from next to last to last point
1175 % will count) refracted with coef. n through some shape p; if ray can't be refracted and, therefore, totally reflected,
1176 % it will contunue as reflected from that point. i is total number of refractions to compute;
1177 %
1178
1179 vardef refractionPathR (expr sa, p, n, i, mn) =
1180 save ray, resultRay, d, s, a, iT;
1181 path ray, resultRay;
1182 pair s, iT;
1183 numeric d[], a;
1184 s := point (length(sa) - 1) of sa;
1185 a := angleRad((point (length(sa)) of sa)-(point (length(sa) - 1) of sa));
1186 ray := (s shifted (-dirRad(a) scaled 2)) -- s -- (s shifted (dirRad(a) scaled (abs(llcorner(p)-(urcorner(p))) + abs(s-(center(p))))));
1187 if (i > 0): ray := subpath (1 + 1/1000, 2) of ray; fi;
1188 iT := ray firstIntersectionTimes p;
1189 d1 := xpart(iT);
1190 d2 := ypart(iT);
1191 d3 := a;
1192 d4 := angleRad(direction d2 of p);
1193 if (n > 0):
1194 d5 := refractionAngle(d3, d4, n);
1195 if (d5 < -100) and (d2 >= 0):
1196 d5 := reflectionAngle(d3, d4);
1197 fi;
1198 else:
1199 d5 := reflectionAngle(d3, d4);
1200 fi;
1201 if (d1 >= 0) and (i < mn) and (d5 > -100):
1202 resultRay := (subpath (0, length(sa) - 1) of sa) -- refractionPathR(point d2 of p -- (point d2 of p shifted dirRad(d5)), p, n, i + 1, mn);
1203 else:
1204 if (d5 > -100) or (d1 < 0):
1205 resultRay := subpath (0, 1/2) of ray;
1206 else:
1207 resultRay := subpath (0, d1) of ray;
1208 fi;
1209 fi;
1210 resultRay
1211 enddef;
1212
1213 vardef refractionPath (expr sa, p, n) =
1214 refractionPathR(sa, p, n, 0, 10)
1215 enddef;
1216
1217 %
1218 % These macros are for isolines. cLine draws continuous line and is called by isoLines.
1219 % For now they are only used to draw wood texture, but can be used elsewhere
1220 %
1221
1222 %
1223 % isoLines goes through i by j matrix of nodes (xy), looking for a square, that has some of its
1224 % angles below zero and some - above, when found, it calls cLine, that tries to build segment of
1225 % isoline, that happen to go through abovementioned square. Thickness of line is
1226 % controlled by values in v array.
1227 % All squares with lines already drawn through are ignored.
1228 %
1229
1230 vardef isoLines (suffix xy)(expr cs, l, s) =
1231 save xxyy, i, j, c, v, sqB, iL, lvl, isNotMasked;
1232 numeric c[], v[], sqB, sqbM;
1233 boolean isNotMasked, xxyy[][];
1234 isNotMasked := true;
1235 lvl := l;
1236 path iL;
1237 image(
1238 for i := 0 step 1 until xpart(cs) - 1:
1239 for j := 0 step 1 until ypart(cs) - 1:
1240 if (boolean isoLinesMask[i][j]):
1241 isNotMasked := isoLinesMask[i][j];
1242 fi;
1243 if isNotMasked:
1244 if (unknown xxyy[i][j]):
1245 c1 := xy[i][j]+lvl;
1246 c2 := xy[i][j+1]+lvl;
1247 c3 := xy[i+1][j]+lvl;
1248 c4 := xy[i+1][j+1]+lvl;
1249 sqB := 0;
1250 sqBm := 0;
1251 if (abs(sign(c1)+sign(c2)+sign(c3)+sign(c4)) < 4):
1252 iL := cLine (xy)((i, j), (0, 0), 0, cs) scaled s;
1253 brushGenerate (reverse(iL),
1254 offsetPathTemplate(iL, 0)(
1255 1/16minStrokeWidth
1256 /(1/64 + 2(
1257 if (offsetPathTime < length(iL) - 1):
1258 (offsetPathTime - floor(offsetPathTime))
1259 [v[floor(sqB + offsetPathTime)],
1260 v[ceiling(sqB + offsetPathTime)]]
1261 else:
1262 1
1263 fi
1264 ))
1265 )
1266 , 0);
1267 fi;
1268 fi;
1269 fi;
1270 endfor;
1271 endfor;
1272 draw (0,0);
1273 )
1274 enddef;
1275
1276 % cLine tries to generate continouos segment of an isoline
1277
1278 vardef cLine (suffix xy)(expr ij, dr, st, cs) =
1279 save p, d, dd, n, i, j, k, outputPath, sqS, cp, dp, nd, isNotMasked;
1280 pair p[], d[], dd[], cp[], dp[];
1281 path outputPath;
1282 boolean isNotMasked;
1283 isNotMasked := true;
1284 i := xpart(ij);
1285 j := ypart(ij);
1286 sqS := 0;
1287 if (boolean isoLinesMask[i][j]):
1288 isNotMasked := isoLinesMask[i][j];
1289 fi;
1290 if (i >= 0) and (i <= xpart(cs)-1) and (j >= 0) and (j <= ypart(cs)-1):
1291 n := 0;
1292 c1 := xy[i][j] + lvl; d1:= (i, j);
1293 c2 := xy[i][j+1] + lvl; d2:= (i, j+1);
1294 c3 := xy[i+1][j] + lvl; d3:= (i+1, j);
1295 c4 := xy[i+1][j+1] + lvl; d4:= (i+1, j+1);
1296 cp1 := (1, 2); dp1 := (-1, 0);
1297 cp2 := (1, 3); dp2 := (0, -1);
1298 cp3 := (2, 4); dp3 := (0, 1);
1299 cp4 := (3, 4); dp4 := (1, 0);
1300 for k := 1 upto 4:
1301 c5 := c[xpart(cp[k])]; d5 := d[xpart(cp[k])];
1302 c6 := c[ypart(cp[k])]; d6 := d[ypart(cp[k])];
1303 if (sign(c5)) <> (sign(c6)):
1304 n := n + 1;
1305 p[n] := (abs(-c5/(c6-c5)))[d5, d6];
1306 dd[n] := dp[k];
1307 fi;
1308 endfor;
1309 sqS := max(c1, c2, c3, c4) - min(c1, c2, c3, c4);
1310 if (unknown xxyy[i][j]) and isNotMasked:
1311 xxyy[i][j] := true;
1312 if (boolean isoLinesMask[i][j]):
1313 isoLinesMask[i][j] := false;
1314 fi;
1315 if (dr = (0, 0)):
1316 outputPath := cLine (xy)(ij shifted dd2, dd2, st + 1,cs) -- p1 -- p2 -- cLine (xy)(ij shifted dd1, dd1, st - 1,cs);
1317 else:
1318 nd := 0;
1319 if (unknown(xxyy[i + xpart(dd1)][j + ypart(dd1)])):
1320 nd := 1;
1321 elseif (unknown(xxyy[i + xpart(dd2)][j + ypart(dd2)])):
1322 nd := 2;
1323 fi;
1324 if nd > 0:
1325 outputPath := cLine (xy)(ij shifted dd[nd], dd[nd], st + sign(st),cs);
1326 p3 := p[nd];
1327 if (st > 0):
1328 outputPath := outputPath -- p3;
1329 else:
1330 outputPath := p3 -- outputPath;
1331 fi;
1332 else:
1333 outputPath := 1/2[p1, p2];
1334 fi;
1335 fi;
1336 else:
1337 outputPath := 1/2[p1, p2];
1338 fi;
1339 else:
1340 outputPath := (i, j);
1341 xxyy[i][j] := true;
1342 if (boolean isoLinesMask[i][j]):
1343 isoLinesMask[i][j] := false;
1344 fi;
1345 fi;
1346 if (st < sqB): sqB := st; fi;
1347 if (st > sqBm): sqBm := st; fi;
1348 v[st] := sqS;
1349 outputPath
1350 enddef;
1351
1352 %
1353 %
1354 %
1355 % In following section are gathered all the things for some commonly used
1356 % real life objects
1357 %
1358 %
1359 %
1360
1361 %
1362 % Though there's a decent library to deal with geography for mp already
1363 % (http://melusine.eu.org/lab/bmp/a_mp-geo), here's some basic globe-drawing
1364 % routine for simple cases, note that latitude starts from the pole,
1365 % not from the equator (for convenience sake)
1366 % Below some landmasses are defined
1367 %
1368
1369 path landmass[];
1370 landmass1 := (206, 122.33)--(211.07, 116)--(213.3, 109.94)--(218.57, 106.03)--(218.38, 97.36)--(220.28, 91.28)--(229.75, 78.07)--(221.41, 78.29)--(220.78, 76.52)--(218.07, 74.48)--(213.8, 66.08)--(213.38, 62.04)--(222.31, 77.1)--(233.88, 72.27)--(237.79, 68.59)--(234.88, 64.69)--(229.83, 65.57)--(228.98, 64.73)--(227.37, 59.82)--(250.57, 68.12)--(254.63, 80.83)--(257.07, 80.93)--(257.38, 80.52)--(258.64, 75.5)--(266.4, 68.48)--(269.56, 67.49)--(271.88, 70.43)--(272.67, 74.49)--(275.36, 72.94)--(276.87, 78.6)--(276.68, 79.04)--(276.11, 79.28)--(276.3, 80.22)--(276.75, 79.96)--(276.56, 82.38)--(277.05, 82.04)--(280.5, 86.44)--(277.25, 85.56)--(276.55, 88.03)--(279.47, 92.77)--(283.29, 92.25)--(282.68, 90.91)--(283.74, 90.4)--(282.53, 89.58)--(283.03, 88.6)--(278.44, 80.08)--(279.15, 76.64)--(281.08, 78.25)--(282.29, 80.21)--(285.35, 79.72)--(288, 77.83)--(284.21, 71.22)--(287.94, 68.57)--(288, 68.6)--(288.74, 69.82)--(300.09, 61.89)--(300.86, 59.94)--(299.36, 59.63)--(297.64, 55.13)--(301.24, 52.55)--(296.1, 51.5)--(300.45, 49.51)--(299.83, 50.75)--(299.84, 50.82)--(299.44, 51.42)--(303.59, 50.57)--(302.72, 51.9)--(302.96, 52.12)--(304.97, 52.87)--(304.12, 55.13)--(307.89, 53.38)--(306.37, 50.11)--(308.65, 47.92)--(315.01, 45.12)--(319.69, 40.31)--(320.43, 44.25)--(321.66, 44.31)--(323.19, 41.66)--(320.37, 35.59)--(318.47, 37.21)--(315.99, 36.32)--(313.68, 35.16)--(320.43, 31.11)--(332.73, 30.38)--(338.5, 28.24)--(340.91, 28.61)--(334.92, 32.27)--(335, 39.2)--(340.58, 35.32)--(341.69, 32.15)--(340.43, 31.93)--(344.49, 29.68)--(352.49, 28.33)--(355.9, 25.35)--(358.67, 24.01)--(366.1, 25.61)--(368.78, 23.99)--(319.11, 17.34)--(309.82, 19)--(308.23, 18.4)--(307.69, 16.74)--(297.49, 16.63)--(290.61, 13.26)--(285.38, 13.37)--(284.06, 12.79)--(258.59, 16.61)--(260.79, 18.13)--(254.13, 18.01)--(253.53, 17.04)--(252.25, 17.02)--(252.44, 18.56)--(253.69, 19.64)--(251.71, 20.89)--(249.66, 16.97)--(245.54, 19.39)--(236.64, 19.73)--(239.08, 21)--(237.57, 21.46)--(232.4, 21.62)--(232.29, 21.34)--(225.16, 22.27)--(221.46, 21.23)--(218.52, 25.09)--(216.81, 24.69)--(214.76, 24.93)--(214.95, 25.52)--(213.66, 25.25)--(211.67, 23.33)--(215.44, 23.49)--(217.75, 21.65)--(200.52, 19.57)--(194.37, 21.1)--(186.19, 26.3)--(183.33, 30.4)--(187.61, 31.42)--(191.44, 33.88)--(194.61, 33.54)--(197.17, 30.74)--(196.08, 28.46)--(196.04, 27.66)--(203.54, 24.58)--(203.45, 24.88)--(200.38, 27.18)--(200.91, 27.54)--(200.05, 29.69)--(199.62, 29.91)--(201.03, 30.25)--(207.36, 29.93)--(205.2, 31.05)--(199.88, 30.97)--(199.94, 31.44)--(200.26, 32.2)--(202.19, 31.76)--(202.85, 32.2)--(199.62, 32.83)--(199.15, 34.61)--(189.46, 35.87)--(189.93, 35.46)--(191.12, 35.08)--(190.83, 34.56)--(188.1, 33.45)--(186.87, 34.75)--(187.11, 36.02)--(176.39, 40.19)--(176.65, 41.24)--(173.41, 42.02)--(176.82, 43.77)--(169.68, 46.56)--(169.15, 53.05)--(171.1, 53.62)--(173.12, 53.39)--(178.7, 51.26)--(183.17, 46.73)--(186.38, 46.75)--(192.72, 49.52)--(191.46, 52.64)--(193.74, 52.83)--(196.74, 50.32)--(190.71, 44.65)--(191.74, 44.4)--(198.11, 50.06)--(198.89, 52.03)--(200.95, 53.75)--(202.49, 51.99)--(201.15, 49.3)--(204.15, 49.28)--(206.54, 53.44)--(214.39, 53.25)--(211.18, 58.75)--(198.36, 57.7)--(197.88, 59.47)--(188.5, 55.87)--(189.63, 53.18)--(189.49, 52.79)--(173.31, 54.75)--(168.56, 58.21)--(161.34, 69.75)--(160.58, 75.18)--(161.25, 77.58)--(162.08, 79.09)--(163.71, 80.23)--(165.04, 82.46)--(168.88, 84.86)--(182.72, 83.77)--(184.88, 85.79)--(187.22, 85.99)--(186.79, 90.34)--(190.56, 95.97)--(190.23, 105.47)--(193.05, 115.74)--(196.18, 121.46)--(196.92, 124.65)--(206, 122.33)--(206, 122.33);
1371 landmass2 := (111.44, 45.06)--(113.41, 44.75)--(111.77, 46)--(111.77, 46.07)--(118.69, 43.98)--(118.13, 42.88)--(116.49, 43.6)--(114.48, 42.7)--(114.1, 43.65)--(114.04, 41.9)--(113.28, 42.04)--(108.57, 42.18)--(114.57, 39.81)--(120.91, 38.84)--(119.04, 41.38)--(119.53, 41.59)--(122.9, 42.64)--(121.94, 42.77)--(121.82, 43.31)--(124.3, 42.48)--(125.29, 42.37)--(125.59, 41.84)--(125.41, 41.3)--(124.75, 41.38)--(122.58, 40.38)--(122.97, 39.95)--(121.71, 40.06)--(123.02, 38.38)--(121.65, 38.53)--(120.78, 35.69)--(116.8, 33.48)--(114.09, 29.59)--(110.59, 31.42)--(108.79, 28.81)--(104.18, 27.54)--(99.54, 29.42)--(100.85, 30.15)--(100.61, 31.83)--(100.51, 34.6)--(98.7, 35.56)--(99.11, 36.37)--(99.33, 38.41)--(96.3, 36.78)--(85.98, 32.83)--(83.79, 29.73)--(86.02, 28)--(87.63, 26.53)--(92.02, 23.6)--(94.53, 23.9)--(94.57, 23.59)--(95.6, 23.75)--(96.08, 21.63)--(96.05, 20.47)--(99.61, 19.73)--(103.5, 21.33)--(105.9, 22.76)--(103.75, 23.87)--(104.54, 24.37)--(101.78, 25.83)--(112.52, 27.74)--(113.4, 27.33)--(113.39, 27.23)--(110.6, 24.25)--(110.62, 24.18)--(111.27, 23.6)--(116.34, 23.81)--(117.17, 23.3)--(110.5, 21.34)--(111.78, 20.87)--(110.82, 20.4)--(111.3, 20.21)--(109.74, 19.84)--(110.11, 19.43)--(102.09, 17.29)--(97.38, 16.64)--(92.88, 17.67)--(92.21, 18.01)--(91.83, 17.28)--(89.16, 18.82)--(92.76, 20.05)--(93.22, 20.96)--(92.06, 21.98)--(91.69, 22.39)--(88.11, 21.59)--(87.79, 20.91)--(86.11, 20.22)--(86.94, 19.77)--(84.28, 18.1)--(84.81, 17.32)--(85.92, 16.37)--(82.62, 16.82)--(82.26, 18.46)--(82.22, 20.63)--(80.29, 21.42)--(73.81, 21.72)--(73.19, 21.56)--(73.02, 21.15)--(75.97, 21.21)--(75.92, 20.34)--(77.6, 20.64)--(73.05, 17.2)--(70.79, 18.11)--(67.81, 17.27)--(64.31, 17.23)--(54.27, 15.93)--(52.31, 18.03)--(60.06, 17.29)--(60.43, 18.73)--(59.79, 19.06)--(65.44, 20.05)--(71.86, 20.7)--(72.16, 20.95)--(69.43, 21.73)--(70.4, 21.96)--(70.06, 22.49)--(69.48, 22.4)--(63.3, 21.97)--(48.4, 19.77)--(41.64, 20.99)--(22.72, 18.59)--(11.06, 21.67)--(16.58, 23.75)--(14.57, 23.75)--(10.29, 24.54)--(17.36, 25.3)--(17.42, 26.18)--(12.61, 29.54)--(15.96, 29.89)--(15.52, 31.43)--(20.94, 31.31)--(13.31, 35.43)--(16.05, 35.66)--(19.1, 35.11)--(18.12, 34.75)--(27.83, 28.87)--(28.89, 30.18)--(33.87, 29.92)--(33.36, 30.35)--(38.31, 30.44)--(42.27, 33.16)--(42.87, 32.94)--(47.84, 35.37)--(50.85, 39.13)--(53.79, 42.3)--(54.39, 50.26)--(64.16, 61.59)--(63.13, 61.47)--(62.78, 61.95)--(64.93, 63.33)--(66.24, 65.62)--(67.78, 65.49)--(65.67, 61.75)--(65.09, 58.99)--(66.42, 61.44)--(68.81, 63.42)--(72.94, 69.36)--(81.5, 74.4)--(83.61, 73.92)--(85.99, 75.53)--(90.7, 76.75)--(93.55, 80.26)--(95.61, 82.43)--(96.7, 82.3)--(98.47, 82.54)--(101.05, 86.23)--(98.22, 93.14)--(100.55, 101.04)--(102.97, 105.27)--(107.45, 108.16)--(104.04, 132.27)--(104.08, 133.7)--(103.49, 134.62)--(102.5, 136.82)--(104.04, 136.98)--(103.44, 141.15)--(104.17, 143.63)--(108.08, 144.9)--(110.27, 145.35)--(111.35, 145.04)--(113.34, 144.63)--(109.31, 140.79)--(112.69, 137.21)--(111.47, 135.36)--(113.17, 133.67)--(113.34, 130.92)--(116.56, 129.1)--(119.97, 124.37)--(128.41, 119.87)--(133.19, 114.17)--(136.37, 113.08)--(138.56, 109.76)--(141.63, 100.78)--(139.94, 93.61)--(133.56, 91.17)--(129.89, 90.92)--(128.12, 89.29)--(123.75, 83.93)--(119.97, 83.01)--(117.24, 81.38)--(117.85, 80.79)--(116.9, 80.06)--(117.65, 79.02)--(114.24, 79.23)--(110.12, 78.93)--(108.35, 77.69)--(102.27, 80.15)--(102.6, 80.45)--(101.42, 81.66)--(96.3, 80.94)--(95.6, 75.16)--(91.88, 73.8)--(89.69, 73.89)--(91.19, 69.61)--(91.08, 68.3)--(87.73, 69.96)--(81.32, 69.32)--(81.63, 61.96)--(88.41, 60.66)--(88.78, 61.23)--(92.41, 60.31)--(95.93, 65.23)--(96.7, 65.47)--(97.12, 65.14)--(97.12, 59.81)--(100.34, 56.62)--(103.22, 54.85)--(104.21, 50.99)--(106.28, 48.84)--(108.51, 48.83)--(108.23, 48.05)--(111.68, 45.41);
1372 landmass3 := (309.58, 101.89)--(307.85, 104.82)--(304.23, 104.36)--(301.98, 106.89)--(301.81, 107.2)--(301.39, 106.32)--(297.9, 109.91)--(292.61, 112.27)--(292.42, 116.24)--(291.76, 116.34)--(293.22, 123.3)--(295.54, 125.57)--(300.79, 123.84)--(313.31, 124.74)--(314.35, 125.13)--(314.72, 125.92)--(316.53, 125.74)--(318.45, 127.71)--(324.06, 128.78)--(326.88, 127.94)--(328.82, 125.64)--(331.64, 120.19)--(331.26, 115.24)--(328.34, 111.83)--(327.46, 109.78)--(327.32, 109.75)--(323.91, 106.24)--(320.57, 100.41)--(318.09, 106.52)--(315.29, 105.19)--(314.65, 104.27)--(314.4, 103.64)--(314.38, 101.88)--(314.02, 100.8)--(310.93, 100.72)--(310, 101.21)--(309.58, 101.89);
1373 landmass4 := (360, 173.94)--(347.31, 173.02)--(337.83, 170.31)--(340.03, 168.66)--(345.63, 168.61)--(346.01, 167.92)--(341.09, 166.89)--(341.61, 165.5)--(343.88, 164.64)--(343.24, 163.51)--(349.37, 161.91)--(322.58, 157.73)--(323.11, 157.14)--(263.4, 156.39)--(263.26, 156.59)--(263.82, 157.03)--(245.18, 163.09)--(245.85, 157.68)--(234.93, 156.8)--(226.52, 156.65)--(194.69, 160.02)--(194.23, 160.12)--(182.27, 160.22)--(175.88, 161.21)--(175.09, 160.92)--(174.87, 161.24)--(171.99, 160.65)--(165.8, 161.33)--(163.91, 162.6)--(161.68, 163.73)--(141.54, 169.39)--(118.58, 172.13)--(116.78, 171.69)--(102.46, 169.67)--(94.78, 168.49)--(97.74, 167.88)--(101.28, 166.75)--(117.86, 164.33)--(118.52, 163.02)--(117.24, 161.46)--(117.41, 160.81)--(114.93, 158.64)--(113.38, 158.53)--(113.67, 158.17)--(112.94, 157.45)--(115.93, 156.83)--(115.81, 156.34)--(116.18, 155.88)--(116.84, 155.67)--(119.7, 154.63)--(119.77, 154.11)--(121.16, 153.99)--(121.76, 153.53)--(116.74, 154)--(113.84, 154.79)--(110.65, 157.28)--(111.18, 157.79)--(111.19, 159.12)--(104.73, 163.13)--(104.3, 163)--(90.27, 162.69)--(86.46, 162.59)--(74.77, 162.74)--(78.14, 164.63)--(64.48, 164.84)--(65.31, 164.46)--(50.04, 164.22)--(50.29, 164.61)--(32.44, 165.76)--(29.52, 166.6)--(27.21, 166.74)--(27.17, 166.97)--(22.41, 166.97)--(23.06, 168.33)--(21.43, 168.63)--(29.78, 169.97)--(27.58, 170.36)--(29.1, 170.81)--(23.15, 171.94)--(24.93, 172.46)--(17.69, 173.06)--(13.37, 172.69)--(3.71, 172.63)--(13.68, 173.98)--(0, 174.21);
1374 landmass5 := (124.62, 19.08)--(123.98, 19.56)--(126.36, 20.09)--(127.24, 20.59)--(124.98, 23.19)--(130.77, 29.28)--(135.8, 28.99)--(137.84, 25.04)--(141.86, 24.34)--(146.13, 22.18)--(157, 19.51)--(155.91, 17.98)--(155.55, 16.8)--(159.25, 15.48)--(158.44, 14.93)--(159.9, 14.05)--(157.97, 12.46)--(159.52, 10.77)--(159.19, 10.3)--(167.06, 8.42)--(159.95, 8.2)--(156.76, 8.73)--(153.02, 7.87)--(131.44, 7.07)--(130.82, 7.37)--(127.12, 7.44)--(116.94, 8.32)--(111.03, 9.65)--(105.32, 11.75)--(107.03, 12.86)--(108.98, 13.43)--(112.23, 14.12)--(119.83, 14.46)--(121.54, 15.67)--(120.54, 15.91)--(122.91, 16.69)--(121.82, 17.23)--(124.62, 19.08);
1375 landmass6 := (307.49, 56.47)--(307.06, 57.11)--(308.22, 57.5)--(310.39, 56.79)--(310.75, 57.43)--(312.59, 56.96)--(313.38, 56.17)--(316.84, 55.24)--(317.14, 55.51)--(319.46, 54.34)--(320.21, 51.88)--(320.32, 48.77)--(319.72, 48.29)--(319.35, 47.51)--(322.01, 46.93)--(323.56, 45.58)--(319.79, 44.82)--(319.05, 44.6)--(318.2, 46.06)--(317.67, 48.01)--(318.7, 48.63)--(315.73, 52.34)--(311.83, 54.71)--(307.49, 56.47);
1376 landmass7 := (172.44, 33.8)--(171.76, 34.44)--(174.73, 35.22)--(173.52, 37.33)--(172.83, 38.17)--(172.56, 39.98)--(179.32, 38.52)--(178.63, 37.06)--(175.74, 33.85)--(176.19, 30.59)--(172.04, 32.2)--(171.52, 33.36)--(172.44, 33.8);
1377 landmass8 := (222.04, 111.1)--(224.53, 115.35)--(228.6, 106.45)--(226.81, 103.35)--(222.04, 111.1);
1378
1379 %
1380 % This macro draws contures for a landmass on globe centered on lon,lat
1381 %
1382
1383 vardef drawLandMass (expr p, lon, lat) =
1384 save i, j, k, l, horizon, currentPoint, horizonTimes, outHorizon, inHorizon, visibleContours, pathNumber, horizonArc, arcTimes, resultPath;
1385 path resultPath, visibleContours[], horizon, horizonArc;
1386 pair currentPoint, horizonTimes[];
1387 numeric pathNumber, outHorizon, arcTimes[];
1388 horizon := fullcircle scaled 2;
1389 pathNumber := 0;
1390 outHorizon := -1;
1391 inHorizon := -1;
1392
1393 % In the following loop visible segments of landmass and points of
1394 % horizon-crossing are calculated
1395 % visibleContours are just what they are called and horizonTimes are
1396 % times on horizon circle where visible segment should cross it
1397 for i := 0 upto length(p):
1398 currentPoint := pointOnGlobe (point i of p, lon, lat);
1399 if (horizonOnGlobe (point i of p, lon, lat) < 0):
1400 if (unknown visibleContours[pathNumber]):
1401 visibleContours[pathNumber] := currentPoint;
1402 if (i > 0):
1403 outHorizon := xpart(horizon intersectiontimes ((0,0) -- (findHorizonPoint (subpath(i-1, i) of p, lon, lat, 0) scaled 5)));
1404 if (outHorizon < inHorizon): outHorizon := outHorizon + 8; fi;
1405 horizonTimes[pathNumber - 1] := (inHorizon, outHorizon);
1406 fi;
1407 else:
1408 visibleContours[pathNumber] := visibleContours[pathNumber] -- currentPoint;
1409 fi;
1410 else:
1411 if (known visibleContours[pathNumber]):
1412 pathNumber := pathNumber + 1;
1413 inHorizon := xpart(horizon intersectiontimes ((0,0) -- (findHorizonPoint (subpath(i, i-1) of p, lon, lat, 0) scaled 5)));
1414 fi;
1415 fi;
1416 endfor;
1417 if (unknown visibleContours0):
1418 resultPath := (1,0);
1419 else:
1420 if (unknown visibleContours[pathNumber]): pathNumber := pathNumber - 1; fi;
1421 if (unknown horizonTimes[-1]):
1422 if (pathNumber > 0):
1423 visibleContours0 := visibleContours[pathNumber] -- visibleContours0;
1424 fi;
1425 pathNumber := pathNumber - 1;
1426 else:
1427 if (ypart(horizonTimes[-1]) < inHorizon):
1428 horizonTimes[pathNumber] := (inHorizon, ypart(horizonTimes[-1]) + 8);
1429 else:
1430 horizonTimes[pathNumber] := (inHorizon, ypart(horizonTimes[-1]));
1431 fi;
1432 fi;
1433 % In these loops horizon arcs directions should be handled
1434 % The idea is that when we have path with no self-intersections arcs should
1435 % not cross one another, these conflicts are resolved here
1436 % It's important to note, that horizon-time detecting algorithm is
1437 % not absolutely precise for now, so at times in will work incorrect.
1438 for i := 0 upto pathNumber - 1:
1439 for j := i + 1 upto pathNumber:
1440 l := 0;
1441 for k := -8, 0, 8:
1442 if (xpart(horizonTimes[j]) > xpart(horizonTimes[i]) + k) and (xpart(horizonTimes[j]) < ypart(horizonTimes[i]) + k): l := l + 1; fi;
1443 if (xpart(horizonTimes[i]) > xpart(horizonTimes[j]) + k) and (xpart(horizonTimes[i]) < ypart(horizonTimes[j]) + k): l := l + 1; fi;
1444 endfor;
1445 if (l > 1): horizonTimes[j] := (xpart(horizonTimes[j]) + 8, ypart(horizonTimes[j])); fi;
1446 endfor;
1447 endfor;
1448
1449 % In the following loop previously calculated segments of a landmass and
1450 % arcs of the horizon are sewed together in order
1451 resultPath := visibleContours0
1452 for i := 1 upto pathNumber + 1:
1453 if (known horizonTimes[i-1]): -- (subpath horizonTimes[i-1] of horizon) fi
1454 if (i <= pathNumber): -- visibleContours[i] fi
1455 endfor
1456 fi;
1457 resultPath -- cycle
1458 enddef;
1459
1460 % This thing just converts coordinates on globe rotated by lon, lat to screen coordinates
1461
1462 vardef pointOnGlobe (expr p, lon, lat) =
1463 (cosd(lon + xpart(p)) * sind(ypart(p)),
1464 cosd(ypart(p)) * cosd(lat)
1465 + sind(lon + xpart(p)) * sind(ypart(p)) * sind(lat))
1466 enddef;
1467
1468 % This one is needed to check if point on globe is in view
1469
1470 vardef horizonOnGlobe (expr p, lon, lat) =
1471 (sind(lon + xpart(p)) * cosd(lat) * sind(ypart(p))) - (cosd(ypart(p)) * sind(lat))
1472 enddef;
1473
1474 % This macro calculates horizon crossing point with given precision (recursion depth).
1475 % Likely, this could be done analytically, though.
1476
1477 vardef findHorizonPoint (expr p, lon, lat, i) =
1478 save selecthalf, returnpoint;
1479 pair selecthalf, returnpoint;
1480 if (horizonOnGlobe (point 1/2 of p, lon, lat) < 0):
1481 selecthalf := (0, 1/2);
1482 else:
1483 selecthalf := (1/2, 1);
1484 fi;
1485 if (i < 5):
1486 returnpoint := findHorizonPoint (subpath selecthalf of p, lon, lat, i + 1)
1487 else:
1488 returnpoint := pointOnGlobe (point 1/2 of p, lon, lat);
1489 fi;
1490 returnpoint
1491 enddef;
1492
1493 vardef globe (expr s, lon, lat) =
1494 save i, p, lm;
1495 picture p[];
1496 path lm;
1497 begingroup
1498 save lightnessPP;
1499 vardef lightnessPP (expr v) =
1500 1/2v
1501 enddef;
1502 p1 := image(draw sphereLat(2s, lat));
1503 vardef lightnessPP (expr v) =
1504 if (abs(cos(sphlat)) > 7/8 + uniformdeviate (1/20)):
1505 1/4v
1506 else:
1507 1/3v + 2/3
1508 fi
1509 enddef;
1510 p2 := image(draw sphereLat(2s, lat));
1511 endgroup;
1512 image(
1513 draw fullcircle scaled 2s withpen thinpen;
1514 for i := 1 upto 8:
1515 lm := drawLandMass(landmass[i], lon + 90, lat) scaled s;
1516 p3 := p2;
1517 clip p3 to lm;
1518 draw p3;
1519 thinBrushGenerate (lm,
1520 offsetPathTemplate (lm, 0) (
1521 2/3minStrokeWidth + 1/3minStrokeWidth
1522 * normalVectorToLightness(
1523 sphereAnglesToNormalVector(
1524 (angleRad(point offsetPathTime of lm), arcsin(abs(point offsetPathTime of lm)/2s))
1525 ), 0, point offsetPathTime of lm
1526 )
1527 ), 0);
1528 p1 := p1 maskedWith lm;
1529 endfor;
1530 draw p1;
1531 )
1532 enddef;
1533
1534 %
1535 % This macro draws an eye pointed in the direction a (in degrees)
1536 % Eye is opened at random angle and pupil is scaled randomly by design
1537 % Scaling below some level, dependent on minStrokeWidth, simplifies image
1538 %
1539
1540 eyescale := 1/2cm;
1541
1542 vardef eye (expr a) =
1543 save s, eyelids, pupil, eyeball, eyelash, loopstep, p, o;
1544 path eyelids[], pupil[], eyelash;
1545 pair p[];
1546 picture eyeball;
1547 numeric s, loopstep;
1548 o := 10 + (15/(1 + 2**(3/2normaldeviate)));
1549 s := eyescale;
1550 p1 := (-3/4s, 0);
1551 pupil1 := ((subpath (-2, 2) of fullcircle xscaled 3/5) .. (subpath (3, 5) of fullcircle xscaled 2/5) .. cycle) scaled 3/5s;
1552 pupil2 := fullcircle scaled (1/3s + uniformdeviate(1/5s)) xscaled 1/3;
1553 p2 := ((p1 -- ((1/2s, 0) rotatedabout (p1, o - 5))) intersectionpoint (subpath (0, 4) of pupil1));
1554 eyelids1 := (p1 shifted (0, -1/16s)){dir(1/3o)} .. {dir(0)}p2;
1555 eyelids2 := p1 {dir(-1/3o)} .. {dir(0)}((1/6s, 0) rotatedabout (p1, -2/3o - 5));
1556 eyelids2 := subpath(xpart(eyelids2 intersectiontimes eyelids1), length(eyelids2)) of eyelids2;
1557 eyelids3 := (p1){dir(2/3o)} .. tension 3/2 .. {dir(o-1/3o)}p2 rotatedabout (p1, 1/3o + 7);
1558 eyelids3 := subpath (1/8, length(eyelids3)) of eyelids3;
1559 eyelids4 := (p1 shifted (0, -1/16s)) .. {dir(1/4o - 2/3o)}((1/6s, -1/6s) rotatedabout (p1, -2/3o - 5));
1560 eyelids4 := subpath (1/2, length(eyelids4)) of eyelids4;
1561 loopstep := length(eyelids1)/20;
1562 if (arclength(subpath(0, loopstep) of eyelids1) < 5minStrokeWidth):
1563 loopstep := arctime (5minStrokeWidth) of eyelids1;
1564 fi;
1565 eyeball := image(
1566 if (5loopstep <= 1):
1567 draw pupil1 withpen thinpen;
1568 fill pupil2;
1569 for i := 0 step 5loopstep until length pupil1:
1570 draw brush ((point i of pupil1) -- (point i + 6 of pupil2 scaled 5/6 yscaled 1/2))(cos(offsetPathLength*1/2pi)*2minStrokeWidth);
1571 endfor;
1572 else:
1573 fill pupil1;
1574 fi;
1575 );
1576 clip eyeball to (eyelids1{(1, 0)} .. (s, 0) .. {-1, 0}reverse(eyelids2) -- cycle);
1577 eyeball := eyeball maskedWith ((fullcircle scaled (1/4s) xscaled 1/2 rotated 2 shifted (1/12s, 0) rotatedabout (p1, 1/3o + 2)));
1578 image(
1579 draw brush (eyelids1 .. tension 2.5 .. reverse (eyelids3))((1-offsetPathLength)*2minStrokeWidth);
1580 draw brush (eyelids2)((offsetPathLength)*2minStrokeWidth);
1581 draw brush (eyelids4)(sin(offsetPathLength*pi)*minStrokeWidth);
1582 draw eyeball;
1583 for i := length(eyelids1) step -loopstep until 0:
1584 eyelash := (point i of eyelids1) {dir(angle(direction i of eyelids1) - 60 + 50*(i/length(eyelids1)))}
1585 .. (point i of eyelids1) shifted (1/16s + (i/length(eyelids1))*1/4s, (i/length(eyelids1))*1/5s);
1586 if (arclength(eyelash) > 2/3minDashStrokeLength):
1587 draw brush (eyelash shifted ((-1/32s, uniformdeviate(1/12s)) scaled ((length(eyelids1)-i)/length(eyelids1))) )(minStrokeWidth + (1-offsetPathLength)*minStrokeWidth);
1588 fi;
1589 endfor;
1590 for i := length(eyelids2) step -3/2loopstep until 0:
1591 eyelash := (point i of eyelids2) {dir(angle(direction i of eyelids2) + 20 - 40*(i/length(eyelids2)))}
1592 .. (point i of eyelids2) shifted (1/16s + (i/length(eyelids2))**2*1/7s, 1/16s - (i/length(eyelids2))*1/5s);
1593 if (arclength(eyelash) > minDashStrokeLength):
1594 draw brush (eyelash shifted (-1/32s, -uniformdeviate(1/24s)))(minStrokeWidth + (1-offsetPathLength)*minStrokeWidth);
1595 fi;
1596 endfor;
1597 ) if cosd(a) < 0: yscaled -1 rotated (a) else: rotated a fi
1598 enddef;
1599
1600 %
1601 % This macro draws solid surface
1602 %
1603
1604 numeric solidSurfaceThickness;
1605 solidSurfaceThickness := 1/4cm;
1606
1607 vardef solidSurface (expr p) =
1608 save q, s, d, stripes;
1609 path q, s;
1610 picture stripes;
1611 q := offsetPath(p) (-solidSurfaceThickness);
1612 s := p -- reverse(q) -- cycle;
1613 image(
1614 draw solid (s, 45, 0);
1615 draw p withpen thinpen;
1616 )
1617 enddef;
1618
1619 vardef solid (expr p, a, t) =
1620 save stripes, stripeskind, d, i, j, c, strokeVariation;
1621 picture stripes, stripeskind;
1622 pair c;
1623 stripes := image(
1624 d1 := abs(ulcorner(p rotated (90 - a)) - urcorner(p rotated (90 - a)));
1625 d2 := abs(ulcorner(p rotated (90 - a)) - llcorner(p rotated (90 - a)));
1626 stripeskind := dashpattern (on 1mm);
1627 c := 1/2[ulcorner(p rotated (90 - a)), lrcorner(p rotated (90 - a))] rotated (a - 90);
1628 for i:= 0 step (3/2shadingDensity)/d1 until 1:
1629 if (t = 1):
1630 strokeVariation := uniformdeviate(1)-1/2;
1631 j := round(i*d1/(3/2shadingDensity));
1632 if (j mod 4) = 0:
1633 stripeskind := dashpattern (on (8-strokeVariation)*shadingDensity off (4+strokeVariation)*shadingDensity);
1634 fi;
1635 if ((j mod 4) = 1) or ((j mod 4) = 3):
1636 stripeskind := dashpattern (off 1shadingDensity on (6-strokeVariation)*shadingDensity off (5+strokeVariation)*shadingDensity);
1637 fi;
1638 if (j mod 4) = 2:
1639 stripeskind := dashpattern (on 0 off 12shadingDensity);
1640 fi;
1641 fi;
1642 draw ((dir(a) scaled 1/2(d2-uniformdeviate(3shadingDensity))) -- (dir(a + 180) scaled 1/2d2)) shifted c shifted i[dir(a + 90) scaled 1/2d1, dir(a -90) scaled 1/2d1] withpen thinpen
1643 dashed stripeskind;
1644 endfor;
1645 );
1646 clip stripes to p;
1647 stripes
1648 enddef;
1649
1650 %
1651 % Returns a picture of shaded gradient inside shape p at an angle a
1652 %
1653
1654 vardef gradientShade (expr p, a) =
1655 save stripes, stripeshd, d, i, j, s;
1656 picture stripes;
1657 path s;
1658 stripes := image(
1659 d1 := abs(ulcorner(p rotated (90 - a)) - urcorner(p rotated (90 - a)));
1660 d2 := abs(ulcorner(p rotated (90 - a)) - llcorner(p rotated (90 - a)));
1661 for i:= 0 step (shadingDensity)/d1 until 1:
1662 s := ((dir(a) scaled 1/2d2) -- (dir(a + 180) scaled 1/2d2)) shifted 1/2[ulcorner(p), lrcorner(p)] shifted i[dir(a + 90) scaled 1/2d1, dir(a -90) scaled 1/2d1];
1663 stripeshd := 1/4 + 3/4i;
1664 draw brush(s)(minStrokeWidth*stripeshd);
1665 endfor;
1666 );
1667 clip stripes to p;
1668 stripes
1669 enddef;
1670
1671 %
1672 % This one draws a spring between points p and q with n steps
1673 % if stretched more than possible, displayed as a straight line
1674 % if compressed too much, displayed as having less steps
1675 %
1676
1677 springwidth := 1/8cm;
1678
1679 vardef spring (expr p, q, n) =
1680 save sp, ss, t, x, springstep, springcoef, springsegment;
1681 transform t;
1682 path ss[];
1683 pair sp;
1684 picture springsegment;
1685 springstep := (arclength(p--q) - 2springwidth)/(n+1);
1686 if (springstep < 6minStrokeWidth): springstep := arclength(p--q)/round(arclength(p--q)/6minStrokeWidth); fi;
1687 if (springstep < (springwidth*pi)):
1688 springcoef := (1-(springstep/(springwidth*pi)));
1689 else:
1690 springcoef := 0;
1691 fi;
1692 image(
1693 for i := 0 step 30 until 360:
1694 sp := ((cosd(i - 90), sind(i - 90)) scaled springwidth xscaled 1/4 yscaled springcoef) shifted (springstep*(i/360) - 1/2springstep, 0);
1695 if (i = 0):
1696 ss1 := sp;
1697 else:
1698 ss1 := ss1 -- sp;
1699 fi;
1700 endfor;
1701 ss2 := subpath (0, 6) of ss1;
1702 ss3 := subpath (6, 12) of ss1;
1703 ss4 := subpath (0, ypart(ss2 shifted (-3minStrokeWidth, 0) intersectiontimes ss3)) of ss3;
1704 x := ypart(ss2 shifted (3minStrokeWidth, 0) intersectiontimes ss3);
1705 if (x > 0):
1706 ss5 := subpath (x, length(ss3)) of ss3;
1707 else:
1708 ss5 := point length(ss3) of ss3;
1709 fi;
1710 if (xpart(llcorner(ss4)) - 3minStrokeWidth < xpart(urcorner(ss2 shifted (-springstep, 0)))):
1711 x := ypart((subpath (3, 6) of ss2 shifted (-springstep + 3minStrokeWidth, 0)) intersectiontimes ss4);
1712 if (x > 0):
1713 ss6 := subpath (0, x) of ss4;
1714 else:
1715 ss6 := point 0 of ss4;
1716 fi;
1717 x := ypart((subpath (0, 3) of ss2 shifted (-springstep + 3minStrokeWidth, 0)) intersectiontimes ss4);
1718 if (x > 0):
1719 ss7 := subpath (x, length(ss4)) of ss4;
1720 else:
1721 ss7 := point length(ss4) of ss4;
1722 fi;
1723 fi;
1724 springsegment := image(
1725 draw brush (ss2)(minStrokeWidth + sin(offsetPathLength*pi)*minStrokeWidth);
1726 if (unknown(ss6)):
1727 if (arclength(ss4) > minStrokeWidth): draw ss4 withpen thinpen; fi;
1728 else:
1729 if (arclength(ss6) > minStrokeWidth): draw ss6 withpen thinpen; fi
1730 if (arclength(ss7) > minStrokeWidth): draw ss7 withpen thinpen; fi
1731 fi;
1732 if (arclength(ss5) > minStrokeWidth): draw ss5 withpen thinpen; fi;
1733 );
1734 ss8 := (ss2 shifted (-2minStrokeWidth, 0)) rotated angle(q-p) shifted ((springwidth + 1/2springstep)/arclength(p--q))[p, q];
1735 for i := springwidth + 1/2springstep step springstep until arclength(p--q) - springwidth - 1/2springstep + 1:
1736 t := identity rotated angle(q-p) shifted (i/arclength(p--q))[p, q];
1737 draw springsegment transformed t;
1738 if (i <= springwidth + 1/2springstep + 2/3springwidth):
1739 ss9 := ss3 transformed t;
1740 ss10 := subpath (
1741 xpart(ss9 intersectiontimes (subpath (0, 3) of ss8)),
1742 xpart(ss9 intersectiontimes (subpath (3, 6) of ss8))
1743 ) of ss9;
1744 if (arclength(ss10) > minStrokeWidth): draw ss10 withpen thinpen; fi;
1745 fi;
1746 endfor;
1747 draw brush (((springwidth)/arclength(p--q))[p, q] shifted (dir(angle(p-q) + 90)*springwidth * springcoef){(p-q)} .. {(p-q)}p)(minStrokeWidth);
1748 draw brush (((springwidth)/arclength(p--q))[q, p] shifted (dir(angle(p-q) + 90)*springwidth * springcoef){(q-p)} .. {(q-p)}q)(minStrokeWidth);
1749 )
1750 enddef;
1751
1752 %
1753 % This macro draws some kind of weight. Not very nice one at the moment
1754 %
1755
1756 vardef weight.h (expr h) =
1757 save auricle, q, r;
1758 path q[];
1759 auricle.d := 2mm;
1760 auricle.t := 2shadingDensity;
1761 r := 2/5(h-auricle.d);
1762 image(
1763 q0 := offsetPathSubdivide((0, -h) -- (0, -auricle.d - 1/6h));
1764 q1 := offsetPathTemplate(q0, 0)(r-(offsetPathLength*(1/8r)));
1765 q2 := offsetPathGenerate (q0, q1, 0);
1766 q3 := offsetPathGenerate (q0, q1 yscaled -1, 0);
1767 tubeGenerate (q2, q3, q1, 0);
1768 draw reverse(q2) -- q3 withpen thinpen;
1769 q0 := offsetPathSubdivide((0, -auricle.d - 1/6h) -- (0, -auricle.d));
1770 q1 := offsetPathTemplate(q0, 0)(2/8r + 5/8r*(sqrt(1-offsetPathLength**2)));
1771 q2 := offsetPathGenerate (q0, q1, 0);
1772 q3 := offsetPathGenerate (q0, q1 yscaled -1, 0);
1773 tubeGenerateAlt (q2, q3, q1);
1774 draw q3 -- reverse(q2) withpen thinpen;
1775 q5 := offsetPathSubdivide(point 0 of q3 -- point 0 of q2);
1776 q6 := tube.e((0,0) -- (0, -3/2auricle.t))(1/2auricle.t);
1777 draw image(
1778 q4 := (((0, -1/2) {dir(90)} .. (1/2, 1/2) .. (0, 1) .. {dir(-90)}(-1/2, 1/4)) shifted (0, -1)) scaled 2/3auricle.d;
1779 draw shadedEdge(tube.e(q4) (1/4auricle.t)) shifted (0, -1/2auricle.t);
1780 ) maskedWith (q3 -- reverse(q2) -- (q2 yscaled 0 shifted (0, -h)) -- (reverse(q3) yscaled 0 shifted (0, -h)) -- cycle)
1781 maskedWith q6;
1782 draw shadedEdge(q6);
1783 )
1784 enddef;
1785
1786 vardef weight.s (expr h) =
1787 save q,r;
1788 path q[];
1789 r := 2/5h;
1790 image(
1791 q0 := offsetPathSubdivide((0, 0) -- (0, h-2/3r));
1792 q1 := offsetPathTemplate(q0, 0)(r);
1793 q2 := offsetPathGenerate (q0, q1, 0);
1794 q3 := offsetPathGenerate (q0, q1 yscaled -1, 0);
1795 tubeGenerate (q2, q3, q1, 0);
1796 draw reverse(q2)--q3 withpen thinpen;
1797 q0 := offsetPathSubdivide((0, h-2/3r) -- (0, h - 1/8r));
1798 q1 := offsetPathTemplate(q0, 0)(r-sqrt(1- (1- offsetPathLength*2)**2)*1/3r - 1/6r*offsetPathLength);
1799 q2 := offsetPathGenerate (q0, q1, 0);
1800 q3 := offsetPathGenerate (q0, q1 yscaled -1, 0);
1801 tubeGenerateAlt (q2, q3, q1);
1802 draw q2 withpen thinpen;
1803 draw q3 withpen thinpen;
1804 q0 := offsetPathSubdivide((0, h-1/8r) -- (0, h));
1805 q1 := offsetPathTemplate(q0, 0)((r-1/6r)+sqrt(1- (1- offsetPathLength*2)**2)*1/16r);
1806 q2 := offsetPathGenerate (q0, q1, 0);
1807 q3 := offsetPathGenerate (q0, q1 yscaled -1, 0);
1808 tubeGenerateAlt (q2, q3, q1);
1809 draw q2 --reverse(q3) withpen thinpen;
1810 )
1811 enddef;
1812
1813 %
1814 % This macro makes a lens-shaped clockwise path with radii
1815 % r = (left radius, right radius), thickness t and diameter d
1816 %
1817
1818 vardef lens (expr r, t, d, s) =
1819 save p, q, m, c;
1820 pair c[];
1821 path p[], q[];
1822 if (xpart(r) = infinity):
1823 p1 := (0, d) -- (0, -d);
1824 else:
1825 p1 := subpath (2, 6) of fullcircle scaled 2xpart(r);
1826 fi;
1827 if (ypart(r) = infinity):
1828 p2 := (0, d) -- (0, -d);
1829 else:
1830 p2 := subpath (-2, 2) of fullcircle scaled 2ypart(r);
1831 fi;
1832 q1 := (min(xpart(ulcorner(p1)), xpart(ulcorner(p2))) - 1, -1/2d)--(max(xpart(urcorner(p1)), xpart(urcorner(p2))) + 1,-1/2d);
1833 q2 := q1 shifted (0, d);
1834 q3 := q1 shifted (0, 1/2d);
1835 c1 := p1 intersectiontimes q1;
1836 c2 := p1 intersectiontimes q2;
1837 c3 := p2 intersectiontimes q2;
1838 c4 := p2 intersectiontimes q1;
1839 if (xpart(c1) > 0):
1840 p1 := subpath (xpart(c1), xpart(c2)) of p1;
1841 fi;
1842 if (xpart(c3) > 0):
1843 p2 := subpath (xpart(c3), xpart(c4)) of p2;
1844 fi;
1845 p1 := p1 shifted (-xpart(point xpart(p1 intersectiontimes q3) of p1), 0);
1846 p2 := p2 shifted (-xpart(point xpart(p2 intersectiontimes q3) of p2) + t, 0);
1847 reverse(p1--p2--cycle) scaled s
1848 enddef;
1849
1850 %
1851 % This one returns a picture of a pulley with diameter d and it's support rotated
1852 % at angle a. pulleyOutline path changes every time
1853 %
1854
1855 path pulleyOutline;
1856 numeric pulleySupportSize;
1857 pulleySupportSize := 2/3;
1858
1859 vardef pulley (expr d, a) =
1860 save pw, p, r;
1861 picture pw;
1862 path p[];
1863 r1 := 3/5d;
1864 r2 := 1/6d;
1865 image(
1866 p0 := fullcircle scaled d;
1867 p1 := (subpath (3, 9) of fullcircle) scaled r1;
1868 p0 := subpath (
1869 xpart(p0 intersectiontimes ((point 0 of p1) -- (point 0 of p1 shifted (0, d)))),
1870 8 + xpart(p0 intersectiontimes ((point 0 of reverse(p1)) -- (point 0 of reverse(p1) shifted (0, d)))))
1871 of p0;
1872 p0 := ((xpart(point 0 of reverse(p0)), pulleySupportSize*d) -- (xpart(point 0 of p0), pulleySupportSize*d) -- p0 -- cycle) rotated a;
1873 pulleyOutline := p0;
1874 p1 := (p1 -- (xpart(point 0 of reverse(p1)), pulleySupportSize*d) -- (xpart(point 0 of p1), pulleySupportSize*d) -- cycle) rotated a;
1875 pw := pulleyWheel(d) maskedWith p1;
1876 draw pw;
1877 draw p1 withpen thinpen;
1878 draw shadedEdge(reverse(fullcircle) scaled r2);
1879 )
1880 enddef;
1881
1882 vardef pulleyWheel (expr d) =
1883 save pw, r, i;
1884 picture pw;
1885 r1 := 7/9d;
1886 r2 := 8/9d;
1887 r3 := 3/5d;
1888 r4 := 1/8d;
1889 if (r2-r1) > shadingDensity:
1890 pw := image(
1891 for i := r3 step 2shadingDensity until r2:
1892 if (i <= r1) or (i >= r2):
1893 thinBrushGenerate (fullcircle scaled i,
1894 offsetPathTemplate (fullcircle scaled i, 0) (
1895 2/3minStrokeWidth + minStrokeWidth
1896 * normalVectorToLightness(
1897 sphereAnglesToNormalVector(
1898 (angleRad(point offsetPathTime of fullcircle), arcsin(i/4d))
1899 ), 0, point offsetPathTime of fullcircle scaled i
1900 )
1901 ), 0);
1902 else:
1903 thinBrushGenerate (fullcircle scaled i,
1904 offsetPathTemplate (fullcircle scaled i, 0) (
1905 1/4minStrokeWidth + minStrokeWidth
1906 * normalVectorToLightness(
1907 sphereAnglesToNormalVector(
1908 (angleRad(point offsetPathTime of fullcircle) + pi, arcsin(1/2))
1909 ), 0, point offsetPathTime of fullcircle scaled i
1910 )
1911 ), 0);
1912 fi;
1913 endfor;
1914 draw brush (fullcircle scaled r1)(minStrokeWidth);
1915 draw brush (fullcircle scaled r2)(minStrokeWidth);
1916 draw fullcircle scaled d withpen thinpen;
1917 draw fullcircle scaled r3 withpen thinpen;
1918 draw shadedEdge(reverse(fullcircle) scaled r4);
1919 );
1920 else:
1921 pw := image(
1922 draw shadedEdge (reverse(fullcircle) scaled r1);
1923 draw fullcircle scaled d withpen thinpen;
1924 draw fullcircle scaled r4 withpen thickpen;
1925 )
1926 fi;
1927 pw
1928 enddef;
1929
1930 %
1931 % This macro draws a wheel
1932 %
1933
1934 vardef wheel (expr d, a) =
1935 save pc, p, r, i;
1936 picture pc[];
1937 path p[];
1938 r1 := 2/8d;
1939 r2 := d-6minStrokeWidth;
1940 r3 := 7/8d-2shadingDensity;
1941 if r1 > 3shadingDensity:
1942 pc1 := image(
1943 for i := r1 step 2shadingDensity until r3:
1944 thinBrushGenerate (fullcircle scaled i,
1945 offsetPathTemplate (fullcircle scaled i, 0) (
1946 2/3minStrokeWidth + minStrokeWidth
1947 * normalVectorToLightness(
1948 sphereAnglesToNormalVector(
1949 (angleRad(point offsetPathTime of fullcircle) + pi, arcsin(i/4d))
1950 ), 0, point offsetPathTime of fullcircle scaled i
1951 )
1952 ), 0);
1953 endfor;
1954 draw sphere.c(r1);
1955 draw shadedEdge (reverse(fullcircle) scaled r3);
1956 );
1957 else:
1958 pc1 := image(
1959 draw shadedEdge (fullcircle scaled d);
1960 draw shadedEdge (fullcircle scaled r1);
1961 draw shadedEdge (reverse(fullcircle) scaled r2);
1962 );
1963 fi;
1964 pc2 := image(fill fullcircle scaled d;) maskedWith (fullcircle scaled r2);
1965 pc3 := image(
1966 p1 := reverse((1/3d, 1/4d) -- (-1/3d, 1/4d) -- (-1/2d, 1/2d) -- (1/2d, 1/2d) -- cycle) rotated a;
1967 draw p1 withpen thinpen;
1968 draw gradientShade(p1, 180);
1969 );
1970 pc3 := pc3 maskedWith (fullcircle scaled d);
1971 image(
1972 draw pc1;
1973 draw pc2;
1974 draw pc3;
1975 )
1976 enddef;
1977
1978
1979 %
1980 % This one roughly finds the center of mass for a closed shape
1981 %
1982
1983 vardef shapeCenterOfMass (expr p) =
1984 save i, a, aTotal, q;
1985 numeric i, a, aTotal;
1986 pair rv, q[];
1987 q0 := point 0 of p;
1988 aTotal := 0;
1989 rv := (0, 0);
1990 for i := 1 step 1 until (length(p)-2):
1991 q1 := point i of p;
1992 q2 := point (i+1) of p;
1993 if (xpart(q1-q0) = 0):
1994 a := (abs(ypart(q1-q0)/cm)*abs(xpart(q2-q0)/cm))/2;
1995 elseif (ypart(q1-q0) = 0):
1996 a := (abs(xpart(q1-q0)/cm)*abs(ypart(q2-q0)/cm))/2;
1997 elseif (abs(xpart(q1-q0)) > abs(ypart(q1-q0))):
1998 begingroup
1999 save qA;
2000 pair qA;
2001 qA = whatever[q0, q0 + (0,1)];
2002 qA = whatever[q2, q2 + (q1-q0)];
2003 a := (abs(ypart(qA-q0)/cm)*abs(xpart(q1-q0)/cm))/2;
2004 endgroup
2005 else:
2006 begingroup
2007 save qA;
2008 pair qA;
2009 qA = whatever[q0, q0 + (1,0)];
2010 qA = whatever[q2, q2 + (q1-q0)];
2011 a := (abs(xpart(qA-q0)/cm)*abs(ypart(q1-q0)/cm))/2;
2012 endgroup
2013 fi;
2014 %a := 2;
2015 aTotal := aTotal + a;
2016 rv := rv + ((q0 + q1 + q2) scaled (a/3));
2017 endfor;
2018 if (aTotal > 0):
2019 rv := rv scaled (1/aTotal);
2020 else:
2021 rv := (0, 0);
2022 fi;
2023 rv
2024 enddef;
2025
2026 %
2027 % This macro is for drawing shaded flat surfaces
2028 %
2029
2030 vardef flatSurface@#(expr surfPath, normalVector, hatchAngle) =
2031 save p, aHatch, hatchImage, surfLight, totalHeight, totalWidth, lineDensity, distFromEdge, hatchLength;
2032 path p, aHatch;
2033 picture hatchImage;
2034 numeric distFromEdge, hatchLength;
2035 surfLight := normalVectorToLightness(normalVector, 0, (0, 0));
2036 p := surfPath rotated -hatchAngle;
2037 if (str @# = "") or (str @# = "hatches"):
2038 lineDensity := shadingDensity;
2039 elseif (str @# = "stipples"):
2040 lineDensity := stippleShadingDensity;
2041 fi;
2042 hatchImage := image(
2043 totalHeight := abs(ypart(llcorner(p)) - ypart(urcorner(p)));
2044 totalWidth := abs(xpart(llcorner(p)) - xpart(urcorner(p)));
2045 %fill surfPath withcolor (surfLight, surfLight, surfLight);
2046 for i := ypart(llcorner(p)) step (totalHeight/round(totalHeight/lineDensity)) until ypart(urcorner(p)):
2047 distFromEdge := abs((i - ypart(llcorner(p)))/(ypart(urcorner(p))-ypart(llcorner(p))));
2048 aHatch := (xpart(llcorner(p)),i) -- (xpart(lrcorner(p)), i);
2049 aHatch := (point xpart(aHatch firstIntersectionTimes p) of aHatch) --
2050 (point xpart(reverse(aHatch) firstIntersectionTimes p) of reverse(aHatch));
2051 hatchLength := arclength(aHatch);
2052 if arclength(aHatch) > 0:
2053 aHatch := aHatch rotated hatchAngle;
2054 aHatch := pathSubdivide(aHatch,3+round(uniformdeviate(2)));
2055 if (str @# = "") or (str @# = "hatches"):
2056 draw brush(aHatch)(
2057 (2minStrokeWidth*(1/15+surfLight))*
2058 (1-6/7sqrt(
2059 sin(offsetPathLength*pi)*(hatchLength/totalWidth)*
2060 sin(distFromEdge*pi)
2061 ))
2062 );
2063 elseif (str @# = "stipples"):
2064 draw thinBrush.stipples(aHatch)(
2065 (stippleSize*(1/15+surfLight))*
2066 (1-5/6sqrt(
2067 sin(offsetPathLength*pi)*(hatchLength/totalWidth)*
2068 sin(distFromEdge*pi)
2069 ))
2070 );
2071 fi;
2072 fi;
2073 endfor;
2074 );
2075 clip hatchImage to surfPath;
2076 image(
2077 draw hatchImage;
2078 )
2079 enddef;
2080
2081 %
2082 % These macros are for drawing wood texture. A bunch of wood-related global
2083 % variables are also here.
2084 %
2085
2086 woodBlockRes := 1/3mm;
2087 woodBlockDetail := 1/5;
2088 woodBlockYRdensity := 1/24;
2089 woodKnotAngle := 1/3pi;
2090 woodKnotRadius := 1/8cm;
2091 woodKnotDensity := 2cm;
2092
2093 % wField returns a value on a scalar field, that is surface of year ring
2094
2095 vardef wField (suffix wk)(expr i, j, cs, nwk)=
2096 save x, y, csx, csy, ba, a, r, outputValue, k, bd;
2097 csx := xpart(cs);
2098 csy := ypart(cs);
2099 x := i*woodBlockDetail;
2100 y := j*woodBlockDetail;
2101 bd := 1/2(woodKnotRadius/woodBlockRes)*woodBlockDetail;
2102 outputValue := 0;
2103 for k := 0 upto nwk:
2104 r := (abs(((x, y) shifted -wk[k]) yscaled (sin(woodKnotAngle))));
2105 a := angleRad((x, y) shifted -wk[k]);
2106 if (r >= 2bd) and (1/2r-bd < 8):
2107 outputValue := outputValue + ((sqrt(1/2r-bd)/(3**(1/2r-bd)))*(sin(woodKnotAngle)*(1/2sin(-a)) + 1)*(1/3 + 2/3abs(cos(a))))**2;
2108 elseif (r < 2bd):
2109 outputValue := outputValue - 10sqrt(1-(r/2bd)**4);
2110 fi;
2111 outputValue := outputValue + 1/64cos(2pi*1/30r);
2112 endfor;
2113 outputValue := outputValue - (i*woodBlockYRdensity)/5 + 1/32sin(pi*i/xpart(cs) + 4pi*j/ypart(cs));
2114 outputValue
2115 enddef;
2116
2117 % woodBlock generates coordinates of knots, calls wField to
2118 % generate matrix of heights (one for all years, that's simplification, of course)
2119 % and then calls isoLines for each year ring (shifting values in matrix by woodBlockYRdensity)
2120
2121 vardef woodBlock (expr w, l) =
2122 save wood, isoLinesMask, maskCoordinates, minWithinMask, maxWithinMask, isNotMasked, wKnot, nwKnot, p, q, i, j, k, cl, tr, wS, lS;
2123 numeric wood[][];
2124 boolean isNotMasked, isoLinesMask[][];
2125 isNotMasked := true;
2126 pair wKnot[], maskCoordinates;
2127 path q;
2128 picture p;
2129 if (l > w):
2130 wS := round(w/woodBlockRes);
2131 lS := round(l/woodBlockRes);
2132 else:
2133 wS := round(l/woodBlockRes);
2134 lS := round(w/woodBlockRes);
2135 fi;
2136 for i := -1 step 1 until wS + 1:
2137 for j := -1 step 1 until lS + 1:
2138 isoLinesMask[i][j] := true;
2139 if (path woodBlockMaskPath):
2140 if (l > w):
2141 maskCoordinates := (i, j);
2142 else:
2143 maskCoordinates := (j, i);
2144 fi;
2145 if not (maskCoordinates*woodBlockRes isInside woodBlockMaskPath):
2146 isoLinesMask[i][j] := false;
2147 fi;
2148 fi;
2149 endfor;
2150 endfor;
2151 image(
2152 p := image(
2153 i := -1;
2154 j := 0;
2155 forever:
2156 wKnot[-1] := (uniformdeviate(wS*woodBlockDetail + woodKnotRadius*woodBlockDetail/woodBlockRes), uniformdeviate(lS*woodBlockDetail + woodKnotRadius*woodBlockDetail/woodBlockRes)) shifted (-1/2woodKnotRadius*woodBlockDetail/woodBlockRes, -1/2woodKnotRadius*woodBlockDetail/woodBlockRes);
2157 cl := 0;
2158 if (i > -1):
2159 for k := 0 step 1 until i:
2160 if (woodBlockRes*abs(wKnot[k]-wKnot[-1])/woodBlockDetail) < woodKnotDensity:
2161 cl := 1;
2162 fi;
2163 endfor;
2164 fi;
2165 if cl > 0:
2166 j := j + 1;
2167 else:
2168 i := i + 1;
2169 wKnot[i] := wKnot[-1];
2170 for tr := 2woodKnotRadius step -8minStrokeWidth until 2minStrokeWidth:
2171 draw brush(
2172 ((fullcircle scaled tr yscaled (1/sin(woodKnotAngle))) shifted (woodBlockRes*wKnot[i]/woodBlockDetail))
2173 )(
2174 minStrokeWidth*(1/2+abs(sin(woodKnotAngle))*abs(sin(angleRad(point offsetPathTime of fullcircle))))
2175 );
2176 endfor;
2177 fi;
2178 exitif (j >= 10) or (i >= 1/2((w/cm)*(l*cm))/(woodKnotDensity/cm));
2179 endfor;
2180 nwKnot := i;
2181 minWithinMask := infinity;
2182 maxWithinMask := -infinity;
2183 for i := 0 step 1 until wS:
2184 k := uniformdeviate(1/8woodBlockYRdensity);
2185 for j := 0 step 1 until lS:
2186 if (boolean isoLinesMask[i][j]):
2187 isNotMasked := isoLinesMask[i][j];
2188 fi;
2189 wood[i][j] := wField (wKnot)(i, j, (wS, lS), nwKnot) + k;
2190 if isNotMasked:
2191 if wood[i][j] > maxWithinMask:
2192 maxWithinMask := wood[i][j];
2193 fi;
2194 if wood[i][j] < minWithinMask:
2195 minWithinMask := wood[i][j];
2196 fi;
2197 fi;
2198 endfor;
2199 endfor;
2200 for i := -maxWithinMask/woodBlockYRdensity - 1 upto -minWithinMask/woodBlockYRdensity + 1:
2201 if (((minWithinMask + woodBlockYRdensity*i) < 0) and ((maxWithinMask + woodBlockYRdensity*i) > 0)):
2202 draw isoLines(wood)((wS, lS), woodBlockYRdensity*i + uniformdeviate(1/8woodBlockYRdensity), woodBlockRes);
2203 fi;
2204 endfor;
2205 );
2206 q := (0, 0) -- (w, 0) -- (w, l) -- (0, l) -- cycle;
2207 if (w > l): q := q xscaled -1 rotated -90; fi;
2208 clip p to q;
2209 draw p;
2210 draw q withpen thinpen;
2211 ) if (l < w): xscaled -1 rotated -90 fi
2212 enddef;
2213
2214 % woodenThing fits a woodBlock into thingOutline at a given woodAngle
2215
2216 vardef woodenThing (expr thingOutline, woodAngle) =
2217 save shiftedThing, woodBlockMaskPath, thingOrigin, thingWoodBlock;
2218 path shiftedThing, woodBlockMaskPath;
2219 pair thingOrigin;
2220 picture thingWoodBlock;
2221 shiftedThing := thingOutline rotated -woodAngle;
2222 thingOrigin := llcorner(shiftedThing);
2223 shiftedThing := shiftedThing shifted -thingOrigin;
2224 if turningnumber(shiftedThing) > 0:
2225 woodBlockMaskPath := offsetPath(shiftedThing, -woodBlockRes);
2226 else:
2227 woodBlockMaskPath := offsetPath(shiftedThing, woodBlockRes);
2228 fi;
2229 thingWoodBlock := woodBlock(xpart(urcorner(shiftedThing)), ypart(urcorner(shiftedThing)));
2230 clip thingWoodBlock to shiftedThing;
2231 thingWoodBlock := (thingWoodBlock shifted thingOrigin) rotated woodAngle;
2232 image(
2233 draw thingOutline withpen thinpen;
2234 draw thingWoodBlock;
2235 )
2236 enddef;
2237
2238 % similar to solidSurfece, but with wood texture
2239
2240 vardef woodenSurface (expr p) =
2241 save surfaceOutline, surfaceAngle;
2242 numeric surfaceAngle;
2243 path surfaceOutline;
2244 surfaceOutline := p -- reverse(offsetPath(p) (-solidSurfaceThickness)) -- cycle;
2245 surfaceAngle := findNarrowest(surfaceOutline);
2246 woodenThing (surfaceOutline,-surfaceAngle)
2247 enddef;
2248
2249 % finds the orientation for path p in which it is the narrowest
2250
2251 vardef findNarrowest (expr p) =
2252 save minWidth, currentWidth, minWidthAngle, rotatedPath, i;
2253 numeric minWidth, currentWidth, minWidthAngle, i;
2254 path rotatedPath;
2255 minWidth := infinity;
2256 for i := 0 step 10 until 170:
2257 rotatedPath := p rotated i;
2258 currentWidth := ypart(urcorner(rotatedPath)-llcorner(rotatedPath));
2259 if currentWidth < minWidth:
2260 minWidth := currentWidth;
2261 minWidthAngle := i;
2262 fi;
2263 endfor;
2264 for i := minWidthAngle -9 step 1 until minWidthAngle + 9:
2265 rotatedPath := p rotated i;
2266 currentWidth := ypart(urcorner(rotatedPath)-llcorner(rotatedPath));
2267 if currentWidth < minWidth:
2268 minWidth := currentWidth;
2269 minWidthAngle := i;
2270 fi;
2271 endfor;
2272 minWidthAngle
2273 enddef;
2274
2275 %
2276 % This part is related to knots
2277 %
2278
2279 % lists are only used in knots so far.
2280 % The following two macros are taken from byrne.mp
2281
2282 vardef appendList@#(suffix listName)(expr valueToAdd, whereToAdd, omitDuplicates) =
2283 save v, valueExists;
2284 string v;
2285 boolean valueExists;
2286 if str @# = "":
2287 if not string listName:
2288 string listName;
2289 fi;
2290 else:
2291 if not string listName0:
2292 string listName[];
2293 fi;
2294 fi;
2295 if unknown listName@#:
2296 listName@# := "";
2297 fi;
2298 valueExists := false;
2299 if omitDuplicates:
2300 valueExists := isInList@#(valueToAdd, listName)
2301 fi;
2302 if not valueExists:
2303 if string valueToAdd:
2304 v := valueToAdd;
2305 else:
2306 v := decimal(valueToAdd);
2307 fi;
2308 if length(listName@#) = 0:
2309 listName@# := v;
2310 else:
2311 if (whereToAdd = 1):
2312 listName@# := listName@# & ", " & v;
2313 else:
2314 listName@# := v & ", " & listName@#;
2315 fi;
2316 fi;
2317 fi;
2318 enddef;
2319
2320 vardef isInList@#(expr valueToLookFor)(suffix listName) =
2321 save rv, i;
2322 boolean rv;
2323 rv := false;
2324 if str @# = "":
2325 if not string listName:
2326 string listName;
2327 fi;
2328 else:
2329 if not string listName0:
2330 string listName[];
2331 fi;
2332 fi;
2333 if unknown listName@#:
2334 listName@# := "";
2335 fi;
2336 if string valueToLookFor:
2337 forsuffixes i=scantokens(listName@#):
2338 if (str i = valueToLookFor):
2339 rv := true;
2340 fi;
2341 endfor;
2342 else:
2343 for i=scantokens(listName@#):
2344 if (i = valueToLookFor):
2345 rv := true;
2346 fi;
2347 endfor;
2348 fi;
2349 rv
2350 enddef;
2351
2352 vardef sortList (expr listToSort, ascending) =
2353 save nPre, nPost, pivot, isSorted, lastValue, preList, postList, rv;
2354 numeric nPre, nPost, pivot;
2355 boolean isSorted;
2356 string preList, postList, rv;
2357 nPre := 0;
2358 nPost := 0;
2359 isSorted := true;
2360 if ascending:
2361 lastValue := -infinity;
2362 else:
2363 lastValue := infinity;
2364 fi;
2365 for i=scantokens(listToSort):
2366 if (unknown pivot):
2367 pivot := i;
2368 fi;
2369 if ((i < pivot) and ascending) or ((i > pivot) and not ascending):
2370 appendList (preList, i, -1, false);
2371 nPre := nPre + 1;
2372 else:
2373 appendList (postList, i, -1, false);
2374 nPost := nPost + 1;
2375 fi;
2376 if ((lastValue > i) and ascending) or ((lastValue < i) and not ascending):
2377 isSorted := false;
2378 fi;
2379 lastValue := i;
2380 endfor;
2381 if isSorted:
2382 rv := listToSort;
2383 else:
2384 if nPre > 1:
2385 preList := sortList(preList, ascending);
2386 fi;
2387 if nPre > 0:
2388 preList := preList & ", ";
2389 else:
2390 preList := "";
2391 fi;
2392 if nPost > 1:
2393 postList := sortList(postList, ascending);
2394 fi;
2395 rv := preList & postList;
2396 fi;
2397 rv
2398 enddef;
2399
2400
2401 % When looking for intersections, knot is browsed with knotStepSize step.
2402 % Affects nothing interesting.
2403 numeric knotStepSize;
2404 knotStepSize := 1/8;
2405
2406 vardef knotFromStrands (suffix knotName) =
2407 save slidingSegment, timeToAdd, pathSegments, intTimes, tSegWidth, tSegStyle, numberOfSegments, segmentWidth, totalNumberOfSegments, intersections, intersectionsList, layerContents, layersList, b, e, n, layerContents, segmentPicture;
2408 save shadowsEnabled, allShadowPaths, totalNumberOfShadows, numberOfShadows, shadowPath, tmpShadows;
2409 boolean shadowsEnabled;
2410 path shadowPath[], allShadowPaths[];
2411 numeric timeToAdd, numberOfShadows, totalNumberOfShadows;
2412 totalNumberOfShadows := -1;
2413 tmpShadows := -1;
2414 path inspectedPath, slidingSegment, pathSegments[];
2415 pair intTimes[];
2416 numeric intersections[], numberOfSegments[], segmentWidth[], totalNumberOfSegments, tSegWidth, b, e, n;
2417 picture layerPicture[];
2418 string layerContents[], segmentStyle[], tSegStyle;
2419 totalNumberOfSegments := 0;
2420 for sNa := 1 step 1 until knotName.nStrands:
2421 inspectedPath := knotName.strandPath[sNa];
2422 tSegWidth := knotName.strandWidth[sNa];
2423 tSegStyle := knotName.strandStyle[sNa];
2424 for sNb := 1 step 1 until knotName.nStrands:
2425 for i := -knotStepSize step knotStepSize until length(knotName.strandPath[sNb]):
2426 slidingSegment := subpath (i, i + 2knotStepSize) of knotName.strandPath[sNb];
2427 intTimes0 := (inspectedPath firstIntersectionTimes slidingSegment);
2428 intTimes1 := (reverse(inspectedPath) firstIntersectionTimes slidingSegment);
2429 if (sNb = sNa):
2430 if (xpart(intTimes0) >= i)
2431 and (xpart(intTimes0) <= i + 2knotStepSize):
2432 intTimes0 := (-1, -1);
2433 fi;
2434 if ((length(inspectedPath) - xpart(intTimes1)) >= i)
2435 and ((length(inspectedPath) - xpart(intTimes1)) <= i + 2knotStepSize):
2436 intTimes1 := (-1, -1);
2437 fi;
2438 if (i+knotStepSize >= length(inspectedPath)):
2439 if (xpart(intTimes0) <= knotStepSize)
2440 or (xpart(intTimes0) = length(inspectedPath)):
2441 intTimes0 := (-1, -1);
2442 fi;
2443 if ((length(inspectedPath) - xpart(intTimes1)) <= knotStepSize)
2444 or (xpart(intTimes1) = 0):
2445 intTimes1 := (-1, -1);
2446 fi;
2447 fi;
2448 if (i-knotStepSize <= length(inspectedPath)):
2449 if (xpart(intTimes0) >= (length(inspectedPath) - knotStepSize))
2450 or (xpart(intTimes0) = 0):
2451 intTimes0 := (-1, -1);
2452 fi;
2453 if ((length(inspectedPath) - xpart(intTimes1)) >= (length(inspectedPath) - knotStepSize))
2454 or (xpart(intTimes1) = length(inspectedPath)):
2455 intTimes1 := (-1, -1);
2456 fi;
2457 fi;
2458 fi;
2459 timeToAdd := -1;
2460 if ((ypart(intTimes0) > 0) and (ypart(intTimes0) < length(slidingSegment)))
2461 and ((xpart(intTimes0) >= 0) and (xpart(intTimes0) <= length(inspectedPath)))
2462 and ((sNb <> sNa) or ((xpart(intTimes0) < i) or (xpart(intTimes0) > i + 1))):
2463 timeToAdd := xpart(intTimes0);
2464 elseif (sNb = sNa):
2465 if ((ypart(intTimes1) > 0) and (ypart(intTimes1) < length(slidingSegment)))
2466 and ((xpart(intTimes1) >= 0) and (xpart(intTimes1) <= length(inspectedPath)))
2467 and ((length(inspectedPath) - xpart(intTimes1) < i) or (length(inspectedPath) - xpart(intTimes1) > i + 1)):
2468 timeToAdd := length(inspectedPath) - xpart(intTimes1);
2469 fi;
2470 fi;
2471 if (timeToAdd >= 0):
2472 if (timeToAdd = length(inspectedPath)):
2473 timeToAdd := 0;
2474 fi;
2475 appendList[sNa](intersectionsList, round(timeToAdd*10)/10, 1, true);
2476 fi;
2477 endfor;
2478 endfor;
2479 if known intersectionsList[sNa]:
2480 numberOfSegments[sNa] := 0;
2481 for i := scantokens(sortList(intersectionsList[sNa], true)):
2482 numberOfSegments[sNa] := numberOfSegments[sNa] + 1;
2483 intersections[numberOfSegments[sNa]] := i;
2484 endfor;
2485 intersections[0] := 0;
2486 if (not cycle knotName.strandPath[sNa]):
2487 intersections[numberOfSegments[sNa] + 1] := length(knotName.strandPath[sNa]);
2488 fi;
2489 for i := 1 step 1 until numberOfSegments[sNa]:
2490 totalNumberOfSegments := totalNumberOfSegments + 1;
2491 if (i > 1):
2492 b := 1/2[intersections[i-1], intersections[i]];
2493 else:
2494 if (not cycle knotName.strandPath[sNa]):
2495 b := 0;
2496 else:
2497 b := 1/2[intersections[numberOfSegments[sNa]] - length(knotName.strandPath[sNa]), intersections[i]];
2498 fi;
2499 fi;
2500 if (i < numberOfSegments[sNa]):
2501 e := 1/2[intersections[i], intersections[i+1]];
2502 else:
2503 if (not cycle knotName.strandPath[sNa]):
2504 e := length(inspectedPath);
2505 else:
2506 e := 1/2[intersections[i], intersections[1] + length(knotName.strandPath[sNa])];
2507 fi;
2508 fi;
2509 pathSegments[totalNumberOfSegments] := subpath (b, e) of inspectedPath;
2510 if (length(pathSegments[totalNumberOfSegments])<=2):
2511 pathSegments[totalNumberOfSegments] := pathSubdivide(pathSegments[totalNumberOfSegments], 2);
2512 fi;
2513 segmentWidth[totalNumberOfSegments] := tSegWidth;
2514 segmentStyle[totalNumberOfSegments] := tSegStyle;
2515 endfor;
2516 else:
2517 totalNumberOfSegments := totalNumberOfSegments + 1;
2518 numberOfSegments[sNa] := 1;
2519 pathSegments[totalNumberOfSegments] := inspectedPath;
2520 segmentWidth[totalNumberOfSegments] := tSegWidth;
2521 segmentStyle[totalNumberOfSegments] := tSegStyle;
2522 fi;
2523 n := 0;
2524 for i := scantokens(knotName.intLayers[sNa]):
2525 n := n + 1;
2526 if (n <= numberOfSegments[sNa]):
2527 appendList[i](layerContents, totalNumberOfSegments - numberOfSegments[sNa] + n, 1, true);
2528 appendList(layersList, i, 1, true);
2529 fi;
2530 endfor;
2531 if n > 0:
2532 if n < numberOfSegments[sNa]:
2533 for i := n + 1 step 1 until numberOfSegments[sNa]:
2534 appendList0(layerContents, totalNumberOfSegments - numberOfSegments[sNa] + i, 1, true);
2535 endfor;
2536 appendList(layersList, 0, 1, true);
2537 fi;
2538 else:
2539 for i := 1 step 1 until numberOfSegments[sNa]:
2540 appendList0(layerContents, totalNumberOfSegments - numberOfSegments[sNa] + i, 1, true);
2541 endfor;
2542 appendList(layersList, 0, 1, true);
2543 fi;
2544 endfor;
2545 image(
2546 for i := scantokens(sortList(layersList, false)):
2547 layerPicture[i] := image(
2548 for j := scantokens(layerContents[i]):
2549 numberOfShadows := -1;
2550 shadowsEnabled := false;
2551 for k := 0 step 1 until totalNumberOfShadows:
2552 if xpart((subpath (1/10, length(pathSegments[j]) - 1/10) of pathSegments[j])
2553 intersectiontimes allShadowPaths[k]) > 0:
2554 shadowsEnabled := true;
2555 numberOfShadows := numberOfShadows + 1;
2556 shadowPath[numberOfShadows] := allShadowPaths[k];
2557 shadowDepth[numberOfShadows] := 3/2segmentWidth[j];
2558 fi;
2559 endfor;
2560 save drawTubeEnds;
2561 boolean drawTubeEnds;
2562 drawTubeEnds := false;
2563 draw tube.scantokens(segmentStyle[j])(pathSegments[j])(segmentWidth[j]) if segmentStyle[j] = "e": withpen thickpen fi;
2564 tmpShadows := tmpShadows + 1;
2565 allShadowPaths[tmpShadows] := tubeOutline;
2566 endfor;
2567 );
2568 for j := 0 step 1 until totalNumberOfShadows:
2569 layerPicture[i] := layerPicture[i] maskedWith allShadowPaths[j];
2570 endfor;
2571 totalNumberOfShadows := tmpShadows;
2572 endfor;
2573 for i := scantokens(sortList(layersList, true)):
2574 draw layerPicture[i];
2575 endfor;
2576 )
2577 enddef;
2578
2579 vardef initKnot (suffix knotName) =
2580 numeric knotName.nStrands;
2581 knotName.nStrands := 0;
2582 path knotName.strandPath[];
2583 numeric knotName.strandWidth[];
2584 string knotName.strandStyle[];
2585 string knotName.intLayers[];
2586 enddef;
2587
2588 vardef addStrandToKnot (suffix knotName) (expr p, w, s, intersectionLayers) =
2589 save n;
2590 if not known knotName.nStrands:
2591 numeric knotName.nStrands;
2592 knotName.nStrands := 0;
2593 fi;
2594 if not path knotName.strandPath0:
2595 path knotName.strandPath[];
2596 fi;
2597 if not numeric knotName.strandWidth0:
2598 numeric knotName.strandWidth[];
2599 fi;
2600 if not string knotName.strandStyle0:
2601 string knotName.strandStyle[];
2602 fi;
2603 if not string knotName.intLayers0:
2604 string knotName.intLayers[];
2605 fi;
2606 knotName.nStrands := knotName.nStrands + 1;
2607 n := knotName.nStrands;
2608 knotName.strandPath[n] := p;
2609 knotName.strandWidth[n] := w;
2610 knotName.strandStyle[n] := s;
2611 knotName.intLayers[n] := intersectionLayers;
2612 enddef;