| 1 |
%% |
| 2 |
%% This is file `grafbase.mf', |
| 3 |
%% generated with the docstrip utility. |
| 4 |
%% |
| 5 |
%% The original source files were: |
| 6 |
%% |
| 7 |
%% grafbase.dtx (with options: `MF') |
| 8 |
%% |
| 9 |
%% ------------------------------------------------------------------- |
| 10 |
%% |
| 11 |
%% Copyright 2002--2012, Daniel H. Luecking |
| 12 |
%% |
| 13 |
%% Mfpic may be distributed and/or modified under the conditions of the |
| 14 |
%% LaTeX Project Public License, either version 1.3c of this license or (at |
| 15 |
%% your option) any later version. The latest version of this license is in |
| 16 |
%% <http://www.latex-project.org/lppl.txt> |
| 17 |
%% and version 1.3c or later is part of all distributions of LaTeX version |
| 18 |
%% 2008/12/01 or later. |
| 19 |
%% |
| 20 |
%% Mfpic has maintenance status "author-maintained". The Current Maintainer |
| 21 |
%% is Daniel H. Luecking. There are several Base Interpreters associated |
| 22 |
%% with mfpic: plain TeX, LaTeX, plain Metafont and plain MetaPost. |
| 23 |
%% |
| 24 |
if (known grafbaseversion) or (known grafbase): |
| 25 |
message "Grafbase (" & jobname & "): You have loaded grafbase more " |
| 26 |
& "than once! Please make sure that it is loaded only once."; |
| 27 |
endinput; |
| 28 |
fi |
| 29 |
boolean grafbase; grafbase := true; |
| 30 |
|
| 31 |
string fileversion, filedate; |
| 32 |
fileversion := "1.10"; filedate := "2012/12/03"; |
| 33 |
|
| 34 |
message " Loading grafbase macros, version " & fileversion & ", " & |
| 35 |
filedate & "."; |
| 36 |
message " "; |
| 37 |
|
| 38 |
def GBmsg expr s = message "Grafbase (" & jobname & "): " & s; |
| 39 |
enddef; |
| 40 |
def GBwarn expr s = GBmsg "Warning, " & s; enddef; |
| 41 |
def GBerrmsg (expr s) expr t = errhelp t; |
| 42 |
errmessage "Grafbase (" & jobname & "): " & s; errhelp ""; |
| 43 |
enddef; |
| 44 |
|
| 45 |
boolean MFPIC; MFPIC := false; |
| 46 |
|
| 47 |
def checkversions (expr g)= |
| 48 |
numeric grafbaseversion; grafbaseversion := g; |
| 49 |
if unknown mfpicversion: % no mfpic, or < 0.63 |
| 50 |
GBmsg "Recent mfpic not detected."; |
| 51 |
elseif g = mfpicversion: |
| 52 |
MFPIC := true; |
| 53 |
else: |
| 54 |
message ""; |
| 55 |
GBwarn "Version mismatch: " |
| 56 |
& "mfpic and grafbase versions do not match."; |
| 57 |
message ""; |
| 58 |
fi |
| 59 |
enddef; |
| 60 |
|
| 61 |
checkversions (110); |
| 62 |
|
| 63 |
if unknown base_name : input plain; |
| 64 |
elseif not string base_name: input plain; |
| 65 |
elseif base_name <> "plain": input plain; |
| 66 |
fi |
| 67 |
|
| 68 |
if not boolean debug: boolean debug; debug := false; fi |
| 69 |
|
| 70 |
def GBdebug = |
| 71 |
begingroup |
| 72 |
save >>; def >> = message " " & enddef; |
| 73 |
message "Grafbase DEBUG"; |
| 74 |
enddef; |
| 75 |
def GBenddebug = |
| 76 |
message "End DEBUG"; |
| 77 |
>> ""; |
| 78 |
endgroup |
| 79 |
enddef; |
| 80 |
|
| 81 |
vardef mftitle expr t = |
| 82 |
if string t: t; message t; fi |
| 83 |
enddef; |
| 84 |
|
| 85 |
boolean METAFONT, METAPOST; |
| 86 |
METAPOST := known color Carl Philipp Emanuel Bach; |
| 87 |
if METAPOST: METAFONT := false; else: METAFONT := true; fi |
| 88 |
|
| 89 |
if METAPOST: |
| 90 |
GBerrmsg ("wrong compiler.") |
| 91 |
"This file is for Metafont. For Metapost, use grafbase.mp."; |
| 92 |
fi |
| 93 |
|
| 94 |
if unknown mode: |
| 95 |
GBerrmsg ("Metafont mode is unknown.") |
| 96 |
"Set mode to a known mode, perhaps ljfour. " |
| 97 |
& "If you proceed, localfont will be tried. " |
| 98 |
& "If that is unknown, a generic mode will be tried."; |
| 99 |
if known localfont: mode := localfont; |
| 100 |
else: |
| 101 |
if unknown GBresolution: GBresolution := 600 fi; |
| 102 |
mode_def GBgeneric = |
| 103 |
mode_param (pixels_per_inch, GBresolution); |
| 104 |
mode_param (blacker, 0); |
| 105 |
mode_param (fillin, 0); |
| 106 |
mode_param (o_correction, 1); |
| 107 |
mode_common_setup_; |
| 108 |
enddef; |
| 109 |
mode := GBgeneric; |
| 110 |
fi |
| 111 |
fi |
| 112 |
mode_setup; |
| 113 |
if debug: |
| 114 |
GBdebug; |
| 115 |
>> "pixels_per_inch = " & decimal pixels_per_inch; |
| 116 |
GBenddebug; |
| 117 |
fi |
| 118 |
|
| 119 |
font_identifier := "MFpic graphics"; |
| 120 |
font_coding_scheme := "Arbitrary"; |
| 121 |
interim designsize := 128pt#; |
| 122 |
|
| 123 |
if unknown aspect_ratio: aspect_ratio := 1; fi |
| 124 |
if unknown hppp: hppp := 1 fi; |
| 125 |
if unknown currenttransform: |
| 126 |
transform currenttransform; |
| 127 |
currenttransform := identity yscaled aspect_ratio; |
| 128 |
fi |
| 129 |
|
| 130 |
interim warningcheck := 0; |
| 131 |
interim turningcheck := 0; |
| 132 |
|
| 133 |
numeric unitlen, xscale, yscale, xneg, xpos, yneg, ypos; |
| 134 |
|
| 135 |
unitlen := 1 bp#; |
| 136 |
xscale := 7.2; |
| 137 |
yscale := 7.2; |
| 138 |
xneg := 0; xpos := 10; |
| 139 |
yneg := 0; ypos := 10; |
| 140 |
|
| 141 |
newinternal deg, pi, radian; |
| 142 |
deg := 1; pi := 3.14159; |
| 143 |
radian := 57.29578; |
| 144 |
numeric degree; degree := deg; |
| 145 |
|
| 146 |
newinternal penwd; penwd := 0.5pt; |
| 147 |
pen drawpen; |
| 148 |
|
| 149 |
def resizedrawpen (expr s) = |
| 150 |
interim penwd := s; |
| 151 |
setvariable (pen) (drawpen) pencircle scaled penwd; |
| 152 |
save currentpen; pen currentpen; pickup drawpen; |
| 153 |
enddef; |
| 154 |
|
| 155 |
numeric hatchwd; hatchwd := 0.5bp; |
| 156 |
pen hatchpen; hatchpen := pencircle scaled hatchwd; |
| 157 |
|
| 158 |
boolean clipall; clipall := false; |
| 159 |
boolean ClipOn; ClipOn := false; |
| 160 |
path ClipPath[]; numeric ClipPath; ClipPath = 0; |
| 161 |
boolean truebbox; truebbox := false; |
| 162 |
|
| 163 |
def DoClip (suffix v) = |
| 164 |
if ClipOn and (ClipPath > 0): clipsto (v, ClipPath); fi |
| 165 |
enddef; |
| 166 |
|
| 167 |
def noclip (text t) = |
| 168 |
hide ( setboolean (ClipOn) false; t) |
| 169 |
enddef; |
| 170 |
|
| 171 |
boolean showbbox; showbbox := false; |
| 172 |
|
| 173 |
let color = numeric; color black, white; |
| 174 |
let rgbcolor = numeric; |
| 175 |
let cmykcolor = numeric; |
| 176 |
black := 0; white := 1; |
| 177 |
def withcolor text t = enddef; |
| 178 |
def _wc_ = withcolor enddef; |
| 179 |
|
| 180 |
color currentcolor, drawcolor, fillcolor, hatchcolor, |
| 181 |
headcolor, pointcolor, tlabelcolor, background; |
| 182 |
currentcolor := fillcolor := drawcolor := hatchcolor := |
| 183 |
headcolor := pointcolor := tlabelcolor := black; |
| 184 |
background := white; |
| 185 |
|
| 186 |
vardef snapto expr t = |
| 187 |
if numeric t: |
| 188 |
if unknown t: 0 |
| 189 |
elseif t < 0: 0 |
| 190 |
elseif t > 1: 1 |
| 191 |
else: t |
| 192 |
fi |
| 193 |
else: |
| 194 |
GBerrmsg ("Improper expression type.") |
| 195 |
"The argument to `snapto' must be a numeric."; |
| 196 |
fi |
| 197 |
|
| 198 |
enddef; |
| 199 |
|
| 200 |
vardef rgbgray (expr g) = (snapto g) * white enddef; |
| 201 |
vardef cmykgray (expr g) = cmyk(0,0,0,1 - snapto g) enddef; |
| 202 |
|
| 203 |
vardef knowncolor expr clr = (known clr) and (iscolor clr) enddef; |
| 204 |
|
| 205 |
vardef grayscalegray (expr g) = snapto g enddef; |
| 206 |
vardef gray (expr g) = grayscalegray (g) enddef; |
| 207 |
vardef cmyk (expr c, m, y, k) = rgb (1-c-k, 1-m-k, 1-y-k) enddef; |
| 208 |
vardef makegray primary clr = |
| 209 |
if knowncolor clr: clr else: black fi |
| 210 |
enddef; |
| 211 |
def makergb = makegray enddef; |
| 212 |
def makecmyk = makegray enddef; |
| 213 |
vardef iscolor expr clr = (color clr) enddef; |
| 214 |
|
| 215 |
vardef forceclr (expr c) = |
| 216 |
if unknown c : |
| 217 |
if numeric c: grayscaleblack |
| 218 |
elseif rgbcolor c: rgbblack |
| 219 |
elseif cmykcolor c: cmykblack |
| 220 |
else: black |
| 221 |
fi |
| 222 |
elseif numeric c: gray (c) |
| 223 |
elseif iscolor c: c |
| 224 |
else: black |
| 225 |
fi |
| 226 |
enddef; |
| 227 |
vardef named (suffix c) = forceclr (c) enddef; |
| 228 |
|
| 229 |
vardef togray (expr r, g, b) = |
| 230 |
gray (sqrt((2r*r + 4g*g + b*b)/7)) |
| 231 |
enddef; |
| 232 |
|
| 233 |
vardef rgbtogray (expr r, g, b) = |
| 234 |
togray(snapto r, snapto g, snapto b) |
| 235 |
enddef; |
| 236 |
vardef cmyktogray (expr c, m, y, k) = |
| 237 |
rgbtogray (1-c-k,1-m-k,1-y-k) |
| 238 |
enddef; |
| 239 |
|
| 240 |
vardef cmyktorgb (expr c,m,y,k) = |
| 241 |
rgb(1-c-k,1-m-k,1-y-k) |
| 242 |
enddef; |
| 243 |
vardef rgbtocmyk (expr r,g,b) = |
| 244 |
cmyk(1-r,1-g,1-b,0) |
| 245 |
enddef; |
| 246 |
|
| 247 |
vardef rgb (expr r, g, b) = |
| 248 |
togray (snapto r, snapto g, snapto b) |
| 249 |
enddef; |
| 250 |
vardef RGB (expr R, G, B) = |
| 251 |
rgb (R/255, G/255, B/255) |
| 252 |
enddef; |
| 253 |
|
| 254 |
def list (suffix v) (text lst) = |
| 255 |
v := 0; for _itm = lst: v[incr v] := _itm; endfor |
| 256 |
if v = 0: |
| 257 |
GBerrmsg ("No list to process!") |
| 258 |
"An attempt was made to produce an array from a " |
| 259 |
& "list of expressions having no valid entries."; |
| 260 |
fi |
| 261 |
enddef; |
| 262 |
|
| 263 |
def map (text proc) (text lst) = |
| 264 |
hide (_map := 0;) |
| 265 |
for _a = lst: |
| 266 |
if _map = 0: hide (_map := 1;) else: , fi |
| 267 |
proc (_a) |
| 268 |
endfor |
| 269 |
enddef; |
| 270 |
|
| 271 |
vardef knownnumericarray suffix arr = |
| 272 |
setboolean (_kna) (known arr) and (numeric arr); |
| 273 |
if _kna : |
| 274 |
_kna := (arr = floor arr) and (arr >= 1); |
| 275 |
for _idx = 1 upto arr : |
| 276 |
exitif not _kna; |
| 277 |
_kna := (known arr[_idx]) and (numeric arr[_idx]); |
| 278 |
endfor |
| 279 |
fi |
| 280 |
_kna |
| 281 |
enddef; |
| 282 |
|
| 283 |
def copyarray (suffix src, dest) = |
| 284 |
for _idx = 1 upto src: dest[_idx] := src[_idx]; endfor |
| 285 |
dest := src; |
| 286 |
enddef; |
| 287 |
|
| 288 |
def maparr (text proc) (suffix p) = |
| 289 |
for _idx = 1 upto p: proc (p[_idx]); endfor |
| 290 |
enddef; |
| 291 |
|
| 292 |
def textpairs = gsetarray (pair) enddef; |
| 293 |
|
| 294 |
def setuniquepairs (suffix p) (text t) = |
| 295 |
save p; pair p[]; |
| 296 |
setpairs (_up) (t); |
| 297 |
if _up > 0: |
| 298 |
p := 1; p1 := _up1; |
| 299 |
for _i = 2 upto _up: |
| 300 |
if _up[_i] <> p[p]: p[incr p] := _up[_i]; fi |
| 301 |
endfor |
| 302 |
else: |
| 303 |
p := 0; |
| 304 |
fi |
| 305 |
enddef; |
| 306 |
|
| 307 |
vardef chpair (text proc) (expr p) = |
| 308 |
(proc (xpart p), proc (ypart p)) |
| 309 |
enddef; |
| 310 |
|
| 311 |
vardef floorpair (expr p) = (floor (xpart p), floor (ypart p)) |
| 312 |
enddef; |
| 313 |
vardef ceilingpair (expr p) = (ceiling (xpart p), ceiling (ypart p)) |
| 314 |
enddef; |
| 315 |
|
| 316 |
def hroundpair (expr p) = (hround (xpart p), hround (ypart p)) |
| 317 |
enddef; |
| 318 |
vardef goodpair (expr p) = hroundpair(p.t_) enddef; |
| 319 |
|
| 320 |
vardef emin (expr a, b) = if a < b: a else: b fi enddef; |
| 321 |
vardef emax (expr a, b) = if a > b: a else: b fi enddef; |
| 322 |
|
| 323 |
vardef pairmin (expr z, w) = |
| 324 |
( emin (xpart z, xpart w), emin (ypart z, ypart w ) ) |
| 325 |
enddef; |
| 326 |
vardef pairmax (expr z, w) = |
| 327 |
( emax (xpart z, xpart w), emax (ypart z, ypart w ) ) |
| 328 |
enddef; |
| 329 |
|
| 330 |
vardef minpair (suffix p) = setpair (_mp) p1; |
| 331 |
for _idx = 2 upto p - 1: _mp := pairmin (_mp, p[_idx]); endfor |
| 332 |
pairmin (_mp, p[p]) |
| 333 |
enddef; |
| 334 |
vardef maxpair (suffix p) = setpair (_mp) p1; |
| 335 |
for _idx = 2 upto p - 1: _mp := pairmax (_mp, p[_idx]); endfor |
| 336 |
pairmax (_mp, p[p]) |
| 337 |
enddef; |
| 338 |
|
| 339 |
primarydef Z xprod W = (xpart Z * ypart W - xpart W * ypart Z) |
| 340 |
enddef; |
| 341 |
|
| 342 |
def force_initial (expr p) (suffix f) = |
| 343 |
hide( setnumeric (_n) length f; |
| 344 |
f := p |
| 345 |
if _n = 0: |
| 346 |
{0,0} |
| 347 |
else: |
| 348 |
..controls post0 (f) and pre 1 (f).. subpath (1,_n) of f |
| 349 |
fi;) |
| 350 |
enddef; |
| 351 |
|
| 352 |
def force_terminal (expr p) (suffix f) = |
| 353 |
hide(setpath (_f) reverse f; |
| 354 |
force_initial (p) (_f); |
| 355 |
f := reverse _f;) |
| 356 |
enddef; |
| 357 |
|
| 358 |
def force_equal_ends (suffix f, g) = |
| 359 |
hide(save _p; pair _p; |
| 360 |
_p := .5[pnt[length f] (f), pnt0(g)]; |
| 361 |
force_terminal (_p) (f); force_initial (_p) (g);) |
| 362 |
enddef; |
| 363 |
|
| 364 |
def replace_ends_of_cycle (expr p) (suffix f) = |
| 365 |
hide( |
| 366 |
if cycle f: |
| 367 |
save _n; _n := length f; |
| 368 |
f := p |
| 369 |
if _n = 0: &cycle |
| 370 |
else: .. controls post0 (f) and pre 1 (f) .. |
| 371 |
if _n = 1: cycle |
| 372 |
else: subpath (1, _n - 1) of f .. |
| 373 |
controls post[_n - 1](f) and pre[_n](f) .. cycle |
| 374 |
fi |
| 375 |
fi; |
| 376 |
fi) |
| 377 |
enddef; |
| 378 |
|
| 379 |
pair thetimes; |
| 380 |
numeric _Xtime, _Ytime; |
| 381 |
tertiarydef a intersects b = |
| 382 |
begingroup |
| 383 |
thetimes := a intersectiontimes b; |
| 384 |
_Xtime := xpart thetimes; |
| 385 |
_Ytime := ypart thetimes; |
| 386 |
(_Xtime > -1) |
| 387 |
endgroup |
| 388 |
enddef; |
| 389 |
|
| 390 |
tertiarydef a misses b = ((a intersectiontimes b) < origin) enddef; |
| 391 |
|
| 392 |
vardef makepicture (expr s) = |
| 393 |
if picture s: s |
| 394 |
elseif path s: picpath (s) |
| 395 |
else: nullpicture |
| 396 |
fi |
| 397 |
enddef; |
| 398 |
|
| 399 |
vardef onepointpath (expr cyclic, q) = |
| 400 |
q if cyclic: &cycle else: {0,0} fi |
| 401 |
enddef; |
| 402 |
|
| 403 |
vardef fallbackpath (expr cyclic, p) (text t) = |
| 404 |
onepointpath (cyclic, p) |
| 405 |
enddef; |
| 406 |
|
| 407 |
def even = not odd enddef; |
| 408 |
|
| 409 |
primarydef a divides b = |
| 410 |
((b mod a) = 0) |
| 411 |
enddef; |
| 412 |
|
| 413 |
vardef image (text t) = |
| 414 |
newpicture (currentpicture); |
| 415 |
t; |
| 416 |
currentpicture |
| 417 |
enddef; |
| 418 |
|
| 419 |
def beginimage = |
| 420 |
begingroup |
| 421 |
newpicture (currentpicture); |
| 422 |
enddef; |
| 423 |
def endimage = |
| 424 |
; currentpicture |
| 425 |
endgroup |
| 426 |
enddef; |
| 427 |
|
| 428 |
def makeimage (suffix name) (expr refpt) = |
| 429 |
setpair (_image_reference_point) zconv (refpt); |
| 430 |
setpicture (name) beginimage |
| 431 |
enddef; |
| 432 |
def concludeimage = |
| 433 |
endimage shifted |
| 434 |
-goodpair (_image_reference_point) |
| 435 |
enddef; |
| 436 |
|
| 437 |
def setvariable (text kind) (suffix name) = |
| 438 |
save name; kind name; name := |
| 439 |
enddef; |
| 440 |
def gsetvariable (text kind) (suffix name) = kind name; name := |
| 441 |
enddef; |
| 442 |
|
| 443 |
def setnumeric (suffix name) = save name; name := enddef; |
| 444 |
def setboolean = setvariable (boolean) enddef; |
| 445 |
def setpair = setvariable (pair) enddef; |
| 446 |
def setpath = setvariable (path) enddef; |
| 447 |
def setpicture = setvariable (picture) enddef; |
| 448 |
def setstring = setvariable (string) enddef; |
| 449 |
def settransform = setvariable (transform) enddef; |
| 450 |
def setpen = setvariable (pen) enddef; |
| 451 |
def settension (suffix tn) expr tens = |
| 452 |
setnumeric (tn) if tens > 0: tens else: default_tension fi; |
| 453 |
enddef; |
| 454 |
def fixtension (suffix tn) = if tn < .75: tn := .75; fi enddef; |
| 455 |
|
| 456 |
def newpicture (suffix pic) = setpicture (pic) nullpicture; enddef; |
| 457 |
def convertpath (suffix g) expr f = setpath (g) zconv (f); enddef; |
| 458 |
|
| 459 |
def setarray (text kind) (suffix name) = |
| 460 |
save name; kind name[]; list (name) |
| 461 |
enddef; |
| 462 |
def setpairs = setarray (pair) enddef; |
| 463 |
def gsetarray (text kind) (suffix name) = |
| 464 |
numeric name; kind name[]; list (name) |
| 465 |
enddef; |
| 466 |
|
| 467 |
|
| 468 |
def setbbox (suffix ll, ur) = |
| 469 |
save ll, ur; pair ll, ur; getbbox (ll, ur) |
| 470 |
enddef; |
| 471 |
|
| 472 |
def setsplit (suffix s) expr ss = |
| 473 |
setnumeric (s) emax (1, ceiling ss); |
| 474 |
enddef; |
| 475 |
|
| 476 |
def setrgbcolor = setcolor enddef; |
| 477 |
def setcmykcolor = setcolor enddef; |
| 478 |
def setcolor = setvariable (color) enddef; |
| 479 |
def gsetcolor = gsetvariable (color) enddef; |
| 480 |
|
| 481 |
setcolor(rgbblack) rgb(0,0,0); |
| 482 |
setcolor(red) rgb(1,0,0); |
| 483 |
setcolor(green) rgb(0,1,0); |
| 484 |
setcolor(blue) rgb(0,0,1); |
| 485 |
setcolor(rgbwhite) rgb(1,1,1); |
| 486 |
setcolor(cmykwhite) cmyk(0,0,0,0); |
| 487 |
setcolor(cyan) cmyk(1,0,0,0); % Maybe these should |
| 488 |
setcolor(magenta) cmyk(0,1,0,0); % be rbg for backward |
| 489 |
setcolor(yellow) cmyk(0,0,1,0); % compatibility? |
| 490 |
setcolor(cmykblack) cmyk(0,0,0,1); |
| 491 |
setcolor(grayscaleblack) gray(0); |
| 492 |
setcolor(grayscalewhite) gray(1); |
| 493 |
|
| 494 |
def setoutputtemplate text garbage = enddef; |
| 495 |
|
| 496 |
vardef GBromannumeral (expr X) = |
| 497 |
save Y, _tmp, U; string U; |
| 498 |
|
| 499 |
Y.m := X div 1000; % thousands digit |
| 500 |
_tmp := X - 1000Y.m; % hundreds digits and lower |
| 501 |
Y.c := _tmp div 100; % hundreds |
| 502 |
_tmp := _tmp - 100Y.c; % tens and units |
| 503 |
Y.x := _tmp div 10; % tens |
| 504 |
Y.i := _tmp - 10Y.x; % units |
| 505 |
|
| 506 |
strrepeat("m", Y.m) & |
| 507 |
GBromandigit("c", "d", "m", Y.c) & |
| 508 |
GBromandigit("x", "l", "c", Y.x) & |
| 509 |
GBromandigit("i", "v", "x", Y.i) |
| 510 |
enddef; |
| 511 |
|
| 512 |
vardef GBromandigit (expr bot, mid, top, n) = |
| 513 |
if n > 9 : top & strrepeat(bot, n-10) % shouldn't happen |
| 514 |
elseif n > 8 : |
| 515 |
bot & top % "ix" |
| 516 |
elseif n > 4 : mid & strrepeat (bot, n-5) % "v"--"viii" |
| 517 |
elseif n > 3 : |
| 518 |
bot & mid % "iv" |
| 519 |
else: strrepeat (bot, n) % ""--"iii" for 0--3 |
| 520 |
fi |
| 521 |
enddef; |
| 522 |
|
| 523 |
vardef strrepeat (expr st, rep) = |
| 524 |
"" for i = 1 upto rep: & st endfor |
| 525 |
enddef; |
| 526 |
|
| 527 |
transform ztr, vtr; |
| 528 |
def setztr = |
| 529 |
if debug: |
| 530 |
GBdebug; |
| 531 |
>> "charwd = " & decimal charwd & "pt#"; |
| 532 |
>> "charht = " & decimal charht & "pt#"; |
| 533 |
>> "w_ = " & decimal w_ & " pixels"; |
| 534 |
>> "h_ = " & decimal h_ & " pixels"; |
| 535 |
>> "unitlen = " & decimal unitlen & "pt#"; |
| 536 |
>> "hppp = " & decimal hppp; |
| 537 |
>> "xneg = " & decimal xneg; |
| 538 |
>> "xpos = " & decimal xpos; |
| 539 |
>> "yneg = " & decimal yneg; |
| 540 |
>> "ypos = " & decimal ypos; |
| 541 |
>> "xscale = " & decimal xscale; |
| 542 |
>> "yscale = " & decimal yscale; |
| 543 |
GBenddebug; |
| 544 |
fi |
| 545 |
save ztr, vtr; |
| 546 |
transform ztr, vtr; |
| 547 |
vtr := identity xscaled xscale yscaled yscale scaled (unitlen*hppp); |
| 548 |
ztr := identity shifted (-xneg, -yneg) transformed vtr; |
| 549 |
if debug: |
| 550 |
GBdebug; |
| 551 |
>> "ztr is"; |
| 552 |
show ztr; |
| 553 |
>> "vtr is"; |
| 554 |
show vtr; |
| 555 |
GBenddebug; |
| 556 |
fi |
| 557 |
enddef; |
| 558 |
|
| 559 |
vardef zconv (expr a) = a transformed ztr enddef; |
| 560 |
vardef invzconv (expr a) = a transformed (inverse ztr) enddef; |
| 561 |
vardef vconv (expr v) = v transformed vtr enddef; |
| 562 |
vardef invvconv (expr v) = v transformed (inverse vtr) enddef; |
| 563 |
|
| 564 |
def active_plane = currentpicture enddef; |
| 565 |
|
| 566 |
def initpic = |
| 567 |
setztr; |
| 568 |
resizedrawpen (penwd); |
| 569 |
if ClipOn: ClipPath := 1; |
| 570 |
ClipPath1 := rect (origin, (w_, h_)); |
| 571 |
fi |
| 572 |
if debug: |
| 573 |
GBdebug; |
| 574 |
>> "Drawing nominal bounding box around picture"; |
| 575 |
GBenddebug; |
| 576 |
noclip ( safedraw rect (origin, (w_, h_)) ); |
| 577 |
fi |
| 578 |
enddef; |
| 579 |
|
| 580 |
def mfpicenv = enddef; |
| 581 |
def endmfpicenv = enddef; |
| 582 |
def bounds (expr a, b, c, d) = |
| 583 |
xneg := a; xpos := b; |
| 584 |
yneg := c; ypos := d; |
| 585 |
enddef; |
| 586 |
|
| 587 |
string extra_beginmfpic; extra_beginmfpic := ""; |
| 588 |
string extra_endmfpic; extra_endmfpic := ""; |
| 589 |
|
| 590 |
def beginmfpic (expr ch) = |
| 591 |
begingroup |
| 592 |
gcode := ch; |
| 593 |
save w_, h_, d_; |
| 594 |
charwd := (xpos-xneg)*xscale*unitlen; |
| 595 |
charht := (ypos-yneg)*yscale*unitlen; |
| 596 |
chardp := 0; |
| 597 |
charcode := if known ch: byte ch else: 0 fi; |
| 598 |
w_ := hround (charwd*hppp); |
| 599 |
h_ := vround (charht*hppp); |
| 600 |
d_ := vround (chardp*hppp); |
| 601 |
charic := 0; clearxy; clearit; clearpen; |
| 602 |
scantokens extra_beginchar; |
| 603 |
initpic; |
| 604 |
scantokens extra_beginmfpic; |
| 605 |
enddef; |
| 606 |
|
| 607 |
def endmfpic = |
| 608 |
scantokens extra_endmfpic; |
| 609 |
if debug: |
| 610 |
GBdebug; |
| 611 |
>> "TFM charwd = " & decimal charwd & "pt#"; |
| 612 |
>> "TFM charht = " & decimal charht & "pt#"; |
| 613 |
GBenddebug; |
| 614 |
fi |
| 615 |
DoClip (active_plane); |
| 616 |
if clipall: clipto (active_plane) rect (origin, (w_, h_)); fi |
| 617 |
if showbbox: noclip ( safedraw rect (origin, (w_, h_)) ); fi |
| 618 |
scantokens extra_endchar; |
| 619 |
if proofing > 0: makebox (proofrule); fi |
| 620 |
chardx := w_; % desired width of character in pixels |
| 621 |
shipit; |
| 622 |
if displaying > 0: makebox (screenrule); showit; fi |
| 623 |
endgroup |
| 624 |
enddef; |
| 625 |
|
| 626 |
pair label_adjust; |
| 627 |
label_adjust := origin; |
| 628 |
numeric label_sep, labelpath_sep ; |
| 629 |
label_sep := 0; labelpath_sep := 0; |
| 630 |
def verbatimtex text t = enddef; |
| 631 |
|
| 632 |
vardef newgblabel (expr hf, vf, BL, r) (text s) (text pts) = |
| 633 |
enddef; |
| 634 |
|
| 635 |
vardef gblabel (expr a, b, c, d, r) (text s) (text t) = |
| 636 |
newgblabel (b, d, (c = 0) and (d = 0), r) (s) (t); |
| 637 |
enddef; |
| 638 |
|
| 639 |
vardef ref_shift (expr hf, vf, BL, ll, ur) = |
| 640 |
- ( (hf)[xpart ll, xpart ur], |
| 641 |
(vf)[if BL: 0 else: (ypart ll) fi, ypart ur] ) |
| 642 |
enddef; |
| 643 |
|
| 644 |
vardef thegblabel (expr z, r, p) = |
| 645 |
((p shifted z) rotated r) shifted label_adjust |
| 646 |
enddef; |
| 647 |
|
| 648 |
vardef textrect (expr lbl, rad, loc) = |
| 649 |
textrectx (.5, .5, false, 0) (origin, lbl, rad, loc) |
| 650 |
enddef; |
| 651 |
vardef textoval (expr lbl, mult, loc) = |
| 652 |
xellipse (true, .5, .5, false, 0) (origin, lbl, mult, loc) |
| 653 |
enddef; |
| 654 |
vardef textellipse (expr lbl, rat, loc) = |
| 655 |
xellipse (false, .5, .5, false, 0) (origin, lbl, rat, loc) |
| 656 |
enddef; |
| 657 |
|
| 658 |
boolean roundends; roundends := true; |
| 659 |
vardef textrectx (expr a, b, c, rot, xy, lbl, rad, loc) = |
| 660 |
save ll, ur, _r, f, zz; |
| 661 |
pair ll, ur, zz; path f; |
| 662 |
pathdims (xy, lbl) (ll, ur); |
| 663 |
readjustdims (ll, ur) (labelpath_sep) |
| 664 |
_r := if numeric rad: rad |
| 665 |
elseif not boolean rad: 0 |
| 666 |
elseif rad: emin (xpart(ur-ll), ypart (ur-ll))/sqrt(2) |
| 667 |
else: 0 |
| 668 |
fi; |
| 669 |
if _r = 0: |
| 670 |
f := rect (ll, ur); |
| 671 |
else: |
| 672 |
save p, q; |
| 673 |
pair p[]; path q; |
| 674 |
p1 := ur - _r*dir(45); |
| 675 |
p3 := ll + _r*dir(45); |
| 676 |
p2 := (xpart p3, ypart p1); |
| 677 |
p4 := (xpart p1, ypart p3); |
| 678 |
q := if _r < 0: reverse fi quartercircle scaled 2_r; |
| 679 |
f := |
| 680 |
(q shifted p1)--(q rotated 90 shifted p2) |
| 681 |
--(q rotated 180 shifted p3) |
| 682 |
--(q rotated -90 shifted p4)--cycle; |
| 683 |
|
| 684 |
fi |
| 685 |
readjustdims (ll, ur) (label_sep - labelpath_sep); |
| 686 |
invvconv (thegblabel (ref_shift(a, b, c, ll, ur), rot, f)) |
| 687 |
shifted loc |
| 688 |
enddef; |
| 689 |
|
| 690 |
def textovalx = xellipse (true) enddef; |
| 691 |
def textellipsex = xellipse (false) enddef; |
| 692 |
|
| 693 |
vardef xellipse (expr aspect, a, b, c, r, xy, lbl, mult, loc) = |
| 694 |
if mult = 0: |
| 695 |
textrectx (a, b, c, r) (xy, lbl, 0, loc) |
| 696 |
else: |
| 697 |
save ll, ur, cc, ww, hh, f; |
| 698 |
pair ll, ur, cc; path f; |
| 699 |
pathdims (xy, lbl) (ll, ur); |
| 700 |
readjustdims (ll, ur) (labelpath_sep) |
| 701 |
cc := .5[ll, ur]; |
| 702 |
(ww, hh) = ur - cc; |
| 703 |
if (ww = 0) or (hh = 0): |
| 704 |
f = (ll--ur); |
| 705 |
else: |
| 706 |
save aa, bb; |
| 707 |
aa := ww ++ if aspect: ww else: hh fi *mult; |
| 708 |
bb := hh ++ if aspect: hh else: ww fi /mult; |
| 709 |
f := ellipse (cc, aa, bb, 0); |
| 710 |
fi |
| 711 |
readjustdims (ll, ur) (label_sep - labelpath_sep); |
| 712 |
invvconv (thegblabel (ref_shift(a, b, c, ll, ur), r, f)) |
| 713 |
shifted loc |
| 714 |
fi |
| 715 |
enddef; |
| 716 |
|
| 717 |
def pathdims (expr xy, lbl) (suffix ll, ur) = |
| 718 |
if pair lbl: |
| 719 |
ll := xy; ur := lbl; |
| 720 |
else: |
| 721 |
ll := ur := origin; |
| 722 |
fi |
| 723 |
enddef; |
| 724 |
|
| 725 |
def readjustdims (suffix ll, ur) (expr s) = |
| 726 |
ll := ll - s*(1,1); |
| 727 |
ur := ur + s*(1,1); |
| 728 |
enddef; |
| 729 |
|
| 730 |
newinternal reallysmall; reallysmall := 3epsilon; |
| 731 |
newinternal nottoosmall; nottoosmall := eps/2 + 2epsilon; |
| 732 |
def signof (expr X) = if X < 0: - fi enddef; |
| 733 |
def TruncateWarn expr s = |
| 734 |
GBwarn s & " is too large or undefined, so it will be truncated."; |
| 735 |
enddef; |
| 736 |
|
| 737 |
vardef secd primary X = |
| 738 |
setnumeric (temp) cosd(X); |
| 739 |
if abs(temp) < reallysmall: |
| 740 |
TruncateWarn "Secant or Tangent"; |
| 741 |
temp := signof (temp) reallysmall; |
| 742 |
fi |
| 743 |
1/temp |
| 744 |
enddef; |
| 745 |
vardef tand primary X = sind(X)*secd(X) enddef; |
| 746 |
|
| 747 |
vardef cscd primary X = |
| 748 |
setnumeric (temp) sind(X); |
| 749 |
if abs(temp) < reallysmall: |
| 750 |
TruncateWarn "Cosecant or Cotangent"; |
| 751 |
temp := signof(temp) reallysmall; |
| 752 |
fi |
| 753 |
1/temp |
| 754 |
enddef; |
| 755 |
vardef cotd primary X = cosd(X)*cscd(X) enddef; |
| 756 |
|
| 757 |
vardef acos primary X = |
| 758 |
if abs X > 1: |
| 759 |
TruncateWarn "Argument of arccosine"; |
| 760 |
angle (signof(X) 1, 0) |
| 761 |
else: |
| 762 |
angle (X, 1 +-+ X) |
| 763 |
fi |
| 764 |
enddef; |
| 765 |
vardef asin primary X = |
| 766 |
if abs X > 1: |
| 767 |
TruncateWarn "Argument of arcsine"; |
| 768 |
angle (0, signof(X) 1) |
| 769 |
else: |
| 770 |
angle (1 +-+ X, X) |
| 771 |
fi |
| 772 |
enddef; |
| 773 |
vardef atan primary X = angle (1, X) enddef; |
| 774 |
|
| 775 |
vardef sin primary X = sind (X*radian) enddef; |
| 776 |
vardef cos primary X = cosd (X*radian) enddef; |
| 777 |
vardef tan primary X = tand (X*radian) enddef; |
| 778 |
vardef cot primary X = cotd (X*radian) enddef; |
| 779 |
vardef sec primary X = secd (X*radian) enddef; |
| 780 |
vardef csc primary X = cscd (X*radian) enddef; |
| 781 |
|
| 782 |
vardef degrees (expr t) = t*radian enddef; |
| 783 |
vardef radians (expr t) = t/radian enddef; |
| 784 |
|
| 785 |
vardef invcos primary X = radians (acos X) enddef; |
| 786 |
vardef invsin primary X = radians (asin X) enddef; |
| 787 |
vardef invtan primary X = radians (atan X) enddef; |
| 788 |
|
| 789 |
vardef exp primary X = mexp (256 * X) enddef; |
| 790 |
vardef ln primary X = (mlog X) / 256 enddef; |
| 791 |
vardef log primary X = ln (X) enddef; |
| 792 |
vardef logbase (expr B) primary X = (mlog X)/(mlog B) enddef; |
| 793 |
vardef logtwo primary X = logbase( 2) (X) enddef; |
| 794 |
vardef logten primary X = logbase(10) (X) enddef; |
| 795 |
|
| 796 |
vardef cosh primary X = |
| 797 |
setnumeric (temp) 2 exp (-abs(X)); |
| 798 |
if temp < reallysmall: |
| 799 |
TruncateWarn "Cosh"; |
| 800 |
temp := reallysmall; |
| 801 |
fi |
| 802 |
1/temp + temp/4 |
| 803 |
enddef; |
| 804 |
|
| 805 |
vardef sinh primary X = |
| 806 |
setnumeric (temp) 2 exp (-abs(X)); |
| 807 |
if temp < reallysmall: |
| 808 |
TruncateWarn "Sinh"; |
| 809 |
temp := reallysmall; |
| 810 |
fi |
| 811 |
signof (X) (1/temp - temp/4) |
| 812 |
enddef; |
| 813 |
|
| 814 |
vardef sech primary X = |
| 815 |
setnumeric (temp) exp(-(abs (X))); |
| 816 |
2temp/(1 + temp*temp) |
| 817 |
enddef; |
| 818 |
|
| 819 |
vardef tanh primary X = |
| 820 |
setnumeric (temp) exp(-2(abs (X))); |
| 821 |
signof (X) (1 - temp)/(1 + temp) |
| 822 |
enddef; |
| 823 |
|
| 824 |
vardef csch primary X = |
| 825 |
save temp, tempa; temp := exp(-(abs (X))); |
| 826 |
tempa := (1 - temp*temp)/2; |
| 827 |
if tempa < reallysmall: |
| 828 |
TruncateWarn "Csch"; |
| 829 |
tempa := reallysmall; |
| 830 |
fi |
| 831 |
signof (X) temp / tempa |
| 832 |
enddef; |
| 833 |
|
| 834 |
vardef coth primary X = |
| 835 |
setnumeric (temp) tanh(X); |
| 836 |
if abs(temp) < reallysmall: |
| 837 |
TruncateWarn "Coth"; |
| 838 |
temp := signof (X) reallysmall; |
| 839 |
fi |
| 840 |
1/temp |
| 841 |
enddef; |
| 842 |
|
| 843 |
vardef acosh primary y = |
| 844 |
if y < 1: |
| 845 |
TruncateWarn "acosh"; |
| 846 |
0 |
| 847 |
else: |
| 848 |
ln (y + (y +-+ 1)) |
| 849 |
fi |
| 850 |
enddef; |
| 851 |
|
| 852 |
vardef asinh primary y = ln (y + (y ++ 1)) enddef; |
| 853 |
|
| 854 |
vardef atanh primary y = |
| 855 |
if abs (y) < 1: |
| 856 |
(ln (1 + y) - ln (1 - y))/2 |
| 857 |
else: |
| 858 |
TruncateWarn "atanh"; |
| 859 |
signof (y) infinity |
| 860 |
fi |
| 861 |
enddef; |
| 862 |
|
| 863 |
vardef Arg primary Z = (angle Z)/radian enddef; |
| 864 |
vardef Log primary Z = (ln (abs Z), Arg Z) enddef; |
| 865 |
vardef cis primary T = dir (T*radian) enddef; |
| 866 |
vardef zexp primary Z = (exp (xpart Z)) * cis (ypart Z) enddef; |
| 867 |
vardef sgn primary Z = if not (Z = origin): unitvector fi Z |
| 868 |
enddef; |
| 869 |
vardef zsqrt primary Z = |
| 870 |
if Z = origin: origin else: sqrt(abs(Z)) * dir ((angle Z)/2) fi |
| 871 |
enddef; |
| 872 |
vardef conj primary Z = (xpart Z, -ypart Z) enddef; |
| 873 |
|
| 874 |
primarydef Z zmul W = Z zscaled W enddef; |
| 875 |
primarydef Z zdiv W = |
| 876 |
Z zmul ( unitvector (conj W) / (abs W) ) |
| 877 |
enddef; |
| 878 |
|
| 879 |
vardef Moebius (expr A) primary Z = |
| 880 |
save _D; pair _D; |
| 881 |
_D := (1, 0) + (Z zscaled (conj A)); |
| 882 |
(Z + A)/(abs _D) rotated (- angle _D) |
| 883 |
enddef; |
| 884 |
vardef pshdist (expr Z,W) = abs(Moebius(-W)(Z)) enddef; |
| 885 |
vardef pshdist_hp (expr Z,W) = abs(Z-W)/abs(Z-conj(W)) enddef; |
| 886 |
vardef kelvin (expr Z) = |
| 887 |
save tmp_; tmp_ = abs(Z); |
| 888 |
if tmp_ = 0: |
| 889 |
(infinity, infinity) |
| 890 |
elseif tmp_ < reallysmall: |
| 891 |
infinity*unitvector Z |
| 892 |
else: |
| 893 |
(1/tmp_)*unitvector Z |
| 894 |
fi |
| 895 |
enddef; |
| 896 |
|
| 897 |
vardef polar primary p = (xpart p) * dir (ypart p) enddef; |
| 898 |
def id (expr x) = x enddef; |
| 899 |
|
| 900 |
primarydef x**y = |
| 901 |
if y=2: x*x |
| 902 |
elseif (x = floor x) and (abs y = floor y): |
| 903 |
1 for n=1 upto y: *x endfor |
| 904 |
else: takepower y of x |
| 905 |
fi |
| 906 |
enddef; |
| 907 |
let ^ = **; |
| 908 |
|
| 909 |
transform T_stack[]; |
| 910 |
numeric T_stack; T_stack := 0; |
| 911 |
|
| 912 |
def T_push (expr T) = T_stack[incr T_stack] := T; enddef; |
| 913 |
def T_pop (suffix $) = |
| 914 |
if T_stack > 0: |
| 915 |
$ := T_stack[T_stack]; |
| 916 |
T_stack := T_stack - 1; |
| 917 |
fi |
| 918 |
enddef; |
| 919 |
|
| 920 |
def bcoords = hide ( T_push (ztr) ) enddef; |
| 921 |
def ecoords = hide ( T_pop (ztr); vtr := vectorpart ztr ) enddef; |
| 922 |
|
| 923 |
vardef vectorpart primary T = T shifted -(origin transformed T) |
| 924 |
enddef; |
| 925 |
|
| 926 |
def apply_t (text Transformer) = |
| 927 |
ztr := identity Transformer transformed ztr; |
| 928 |
vtr := vectorpart ztr; |
| 929 |
enddef; |
| 930 |
|
| 931 |
def xslant = slanted enddef; % (x+sy, y). |
| 932 |
def yslant primary s = % (x, y+sx). |
| 933 |
transformed |
| 934 |
begingroup |
| 935 |
save T; transform T; |
| 936 |
origin transformed T = origin; |
| 937 |
(1, 0) transformed T = (1, s); |
| 938 |
(0, 1) transformed T = (0, 1); |
| 939 |
T |
| 940 |
endgroup |
| 941 |
enddef; |
| 942 |
|
| 943 |
def zslant primary p = % (xu+yv, xv+yu), where p = (u, v). |
| 944 |
transformed |
| 945 |
begingroup |
| 946 |
save T; transform T; |
| 947 |
xpart T = ypart T = 0; |
| 948 |
xxpart T = yypart T = xpart p; |
| 949 |
xypart T = yxpart T = ypart p; |
| 950 |
T |
| 951 |
endgroup |
| 952 |
enddef; |
| 953 |
|
| 954 |
def xyswap = zslant (0, 1) enddef; |
| 955 |
def boost primary X = zslant (cosh X, sinh X) enddef; |
| 956 |
|
| 957 |
vardef transformedpath (text Transformer) expr f = f Transformer |
| 958 |
enddef; |
| 959 |
|
| 960 |
def rotatedpath (expr p, th) = |
| 961 |
transformedpath ( |
| 962 |
transformed vtr |
| 963 |
rotatedaround (p transformed vtr, th) |
| 964 |
transformed (inverse vtr) |
| 965 |
) |
| 966 |
enddef; |
| 967 |
def reflectedpath (expr p, q) = |
| 968 |
transformedpath ( |
| 969 |
transformed vtr |
| 970 |
reflectedabout (p transformed vtr, q transformed vtr) |
| 971 |
transformed (inverse vtr) |
| 972 |
) |
| 973 |
enddef; |
| 974 |
|
| 975 |
def scaledpath (expr p, s) = |
| 976 |
transformedpath (shifted -p scaled s shifted p) |
| 977 |
enddef; |
| 978 |
def xscaledpath (expr a, s) = |
| 979 |
transformedpath (shifted (-a, 0) xscaled s shifted (a, 0)) |
| 980 |
enddef; |
| 981 |
def yscaledpath (expr b, s) = |
| 982 |
transformedpath (shifted (0, -b) yscaled s shifted (0, b)) |
| 983 |
enddef; |
| 984 |
|
| 985 |
def slantedpath = xslantedpath enddef; |
| 986 |
def xslantedpath (expr b, s) = |
| 987 |
transformedpath (shifted (0, -b) slanted s shifted (0, b)) |
| 988 |
enddef; |
| 989 |
def yslantedpath (expr a, s) = |
| 990 |
transformedpath (shifted (-a, 0) yslant s shifted (0, a)) |
| 991 |
enddef; |
| 992 |
|
| 993 |
def shiftedpath (expr v) = transformedpath (shifted v) enddef; |
| 994 |
|
| 995 |
def xyswappedpath = transformedpath (xyswap) enddef; |
| 996 |
|
| 997 |
vardef partialpath (expr a, b) expr f = |
| 998 |
save flag, flo, fhi, lo, hi, n; |
| 999 |
boolean flag; flag = true; |
| 1000 |
convertpath (g) f; |
| 1001 |
n := length f; |
| 1002 |
|
| 1003 |
flo := snapto emin(a,b); |
| 1004 |
if flo = 0: |
| 1005 |
lo := 0; |
| 1006 |
elseif flo < 1: |
| 1007 |
setuplengtharray (cum, tot, idx) g; |
| 1008 |
flag := false; |
| 1009 |
lo := gettime (cum, idx) (flo*tot); |
| 1010 |
else: |
| 1011 |
lo := n; |
| 1012 |
fi |
| 1013 |
|
| 1014 |
fhi := snapto emax (a,b); |
| 1015 |
if flo = fhi: |
| 1016 |
hi := lo; |
| 1017 |
elseif fhi < 1: |
| 1018 |
if flag: setuplengtharray (cum, tot, idx) g; fi |
| 1019 |
hi := gettime (cum, idx) (fhi*tot); |
| 1020 |
else: |
| 1021 |
hi := n; |
| 1022 |
fi |
| 1023 |
|
| 1024 |
if a > b: reverse fi subpath (lo, hi) of f |
| 1025 |
enddef; |
| 1026 |
|
| 1027 |
vardef gsubpath (expr a, b) expr f = subpath (a, b) of f enddef; |
| 1028 |
|
| 1029 |
def setuplengtharray (suffix cum, tot, idx) = |
| 1030 |
save cum, tot, idx; idx := 0; tot := makelengtharray (cum) |
| 1031 |
enddef; |
| 1032 |
|
| 1033 |
vardef pathtime@# (suffix p) = |
| 1034 |
if @# <= 0: 0 |
| 1035 |
elseif @# >= 1: length p |
| 1036 |
else: |
| 1037 |
setuplengtharray (cum, tot, idx) p; |
| 1038 |
gettime (cum, idx) (@#*tot) |
| 1039 |
fi |
| 1040 |
enddef; |
| 1041 |
|
| 1042 |
vardef pathpoint (expr frac) (suffix p) = |
| 1043 |
convertpath (_pp) p; pnt[pathtime[frac] (_pp)] (p) |
| 1044 |
enddef; |
| 1045 |
|
| 1046 |
def mono (suffix u) = cull u keeping (1, infinity); enddef; |
| 1047 |
|
| 1048 |
def andto (suffix u) (expr v) = |
| 1049 |
mono (u); addto u also v; cull u keeping (2, 2); |
| 1050 |
enddef; |
| 1051 |
primarydef u picand v = |
| 1052 |
begingroup setpicture (t) u; andto (t, v); t endgroup |
| 1053 |
enddef; |
| 1054 |
|
| 1055 |
def orto (suffix u) (expr v) = |
| 1056 |
mono (u); addto u also v; cull u keeping (1, infinity); |
| 1057 |
enddef; |
| 1058 |
primarydef u picor v = |
| 1059 |
begingroup setpicture (t) u; orto (t, v); t endgroup |
| 1060 |
enddef; |
| 1061 |
|
| 1062 |
def xorto (suffix u) (expr v) = |
| 1063 |
mono (u); addto u also v; cull u keeping (1, 1); |
| 1064 |
enddef; |
| 1065 |
primarydef u picxor v = |
| 1066 |
begingroup setpicture (t) u; xorto (t, v); t endgroup |
| 1067 |
enddef; |
| 1068 |
|
| 1069 |
def subto (suffix u) (expr v) = |
| 1070 |
mono (u); addto u also -v; cull u keeping (1, infinity); |
| 1071 |
enddef; |
| 1072 |
primarydef u picsub v = |
| 1073 |
begingroup setpicture (t) u; mono (t); subto (t, v); t endgroup |
| 1074 |
enddef; |
| 1075 |
|
| 1076 |
def coloraddto (expr clr) (suffix u) (expr v) = |
| 1077 |
if clr < white: |
| 1078 |
orto (u, v); |
| 1079 |
else: |
| 1080 |
subto (u, v); |
| 1081 |
fi; |
| 1082 |
enddef; |
| 1083 |
|
| 1084 |
def coloraddon (expr clr) (suffix v) = |
| 1085 |
if clr < white: |
| 1086 |
_orto (active_plane, v); |
| 1087 |
else: |
| 1088 |
_subto (active_plane, v); |
| 1089 |
fi; |
| 1090 |
enddef; |
| 1091 |
|
| 1092 |
def _orto (suffix u, v) = |
| 1093 |
mono (u); mono (v); |
| 1094 |
addto u also v; |
| 1095 |
cull u keeping (1, 2); |
| 1096 |
enddef; |
| 1097 |
|
| 1098 |
def _subto (suffix u, v) = |
| 1099 |
mono (u); mono (v); addto u also -v; cull u keeping (1, 1); |
| 1100 |
enddef; |
| 1101 |
|
| 1102 |
vardef interior expr c = |
| 1103 |
newpicture (v); |
| 1104 |
addto v contour (c.t_); |
| 1105 |
cull v dropping (0,0); |
| 1106 |
v |
| 1107 |
enddef; |
| 1108 |
|
| 1109 |
vardef interiors suffix cc = |
| 1110 |
newpicture (_ints); |
| 1111 |
for _idx = 1 upto cc: |
| 1112 |
addto _ints also interior cc[_idx]); |
| 1113 |
endfor |
| 1114 |
mono (_ints); |
| 1115 |
_ints |
| 1116 |
enddef; |
| 1117 |
|
| 1118 |
def clipto (suffix vt) expr c = |
| 1119 |
if path c: |
| 1120 |
andto (vt, interior c); |
| 1121 |
fi |
| 1122 |
enddef; |
| 1123 |
def clipsto (suffix vt, cc) = |
| 1124 |
andto (vt, interiors cc); |
| 1125 |
enddef; |
| 1126 |
|
| 1127 |
vardef Clipped (suffix vt) expr c = |
| 1128 |
setpicture (_Cl) vt; clipto (_Cl) c; _Cl |
| 1129 |
enddef; |
| 1130 |
def clip = Clipped enddef; |
| 1131 |
|
| 1132 |
vardef picneg (suffix vt) expr c = |
| 1133 |
setpicture (_pn) interior c; |
| 1134 |
_subto (_pn, vt); |
| 1135 |
_pn |
| 1136 |
enddef; |
| 1137 |
|
| 1138 |
def shpath (suffix v) (expr q, f) = |
| 1139 |
addto v doublepath (f.t_) withpen (q.t_); |
| 1140 |
enddef; |
| 1141 |
|
| 1142 |
numeric minpenwd; |
| 1143 |
minpenwd := 1; % 1 pixel |
| 1144 |
vardef picpath expr d = |
| 1145 |
newpicture (v); |
| 1146 |
if penwd >= minpenwd: |
| 1147 |
shpath (v, drawpen) (d); |
| 1148 |
mono (v); |
| 1149 |
fi |
| 1150 |
v |
| 1151 |
enddef; |
| 1152 |
|
| 1153 |
def picdot (suffix v) (expr w, p) = |
| 1154 |
addto v also |
| 1155 |
(w shifted goodpair (p)); |
| 1156 |
enddef; |
| 1157 |
|
| 1158 |
vardef setdot (expr apath, sc) = |
| 1159 |
if cycle apath: interior |
| 1160 |
else: picpath |
| 1161 |
fi |
| 1162 |
(apath scaled emax (ceiling (sc), minpenwd)) |
| 1163 |
enddef; |
| 1164 |
|
| 1165 |
numeric shadepicsize; shadepicsize := 0.8bp; |
| 1166 |
vardef shadepic (suffix dims) (expr grparam) = |
| 1167 |
pair dims; |
| 1168 |
setnumeric (_frac) 2*emin (grparam, 1 - grparam); |
| 1169 |
save _hp, _vp, _dotwd, _dotht; |
| 1170 |
if aspect_ratio < 1: |
| 1171 |
_vp := emax (2, hround (shadepicsize.o_)); |
| 1172 |
_hp := hround (_vp._o_); |
| 1173 |
_dotwd := hround (_hp*sqrt _frac); |
| 1174 |
_dotht := if _dotwd = 0: 0 |
| 1175 |
else: hround (_hp*_vp*_frac/_dotwd) |
| 1176 |
fi; |
| 1177 |
else: |
| 1178 |
_hp := emax (2, hround (shadepicsize)); |
| 1179 |
_vp := hround (_hp.o_); |
| 1180 |
_dotht := hround (_vp*sqrt _frac); |
| 1181 |
_dotwd := if _dotht = 0: 0 |
| 1182 |
else: hround (_hp*_vp*_frac/_dotht) |
| 1183 |
fi; |
| 1184 |
fi |
| 1185 |
dims := ( _hp, _vp._o_ ); |
| 1186 |
newpicture (_shp); |
| 1187 |
addto _shp contour rect (origin, (_dotwd, _dotht)); |
| 1188 |
picdot (_shp, _shp, dims); |
| 1189 |
dims := 2dims; mono (_shp); |
| 1190 |
if grparam >= .5: _shp |
| 1191 |
else: (interior (rect (origin, dims))) picsub _shp |
| 1192 |
fi |
| 1193 |
enddef; |
| 1194 |
|
| 1195 |
vardef shaded (expr clr) expr c = |
| 1196 |
if cycle c: |
| 1197 |
if (clr <= black) or (clr >= white): |
| 1198 |
interior c |
| 1199 |
else: |
| 1200 |
save shdims, shpic; |
| 1201 |
picture shpic; pair shdims; |
| 1202 |
shpic := shadepic (shdims) (clr); |
| 1203 |
setbbox (ll, ur) c; |
| 1204 |
newpicture (vsh); |
| 1205 |
fillwith (vsh) (shpic, shdims, ll, ur); |
| 1206 |
clipto (vsh) c; vsh |
| 1207 |
fi |
| 1208 |
else: picpath c % should we? or just make it null? |
| 1209 |
fi |
| 1210 |
enddef; |
| 1211 |
|
| 1212 |
vardef fillwith (suffix v) (expr pic, dims, ll, ur) = |
| 1213 |
newpicture (b); |
| 1214 |
save fwdims, _ll, _ur; pair fwdims, _ll, _ur; |
| 1215 |
fwdims := goodpair (dims); |
| 1216 |
_ll := floorpair (ll.t_); |
| 1217 |
_ur := ur.t_; |
| 1218 |
for s = xpart _ll step xpart fwdims until xpart _ur: |
| 1219 |
addto b also pic shifted (s, 0); |
| 1220 |
endfor |
| 1221 |
for s = ypart _ll step ypart fwdims until ypart _ur: |
| 1222 |
addto v also b shifted (0, s); |
| 1223 |
endfor |
| 1224 |
mono (v); |
| 1225 |
enddef; |
| 1226 |
|
| 1227 |
def thatchf (suffix v) (expr CT, sp, a, b) = |
| 1228 |
begingroup |
| 1229 |
setnumeric (_sp) signof (ypart b - ypart a) abs(sp); |
| 1230 |
for _y = _sp*( ceiling ((ypart a)/_sp) ) step _sp until ypart b: |
| 1231 |
shpath (v, hatchpen) |
| 1232 |
( ( (xpart a, _y)--(xpart b, _y) ) transformed CT ); |
| 1233 |
endfor |
| 1234 |
mono (v); |
| 1235 |
endgroup |
| 1236 |
enddef; |
| 1237 |
|
| 1238 |
def axialgradientf (suffix clr, v) (expr theta, sp, a, b) = |
| 1239 |
begingroup |
| 1240 |
save _hh, _sp, _nn, _y; |
| 1241 |
_hh := ypart b - ypart a; |
| 1242 |
_sp := signof (_hh) abs(sp); |
| 1243 |
_nn := emax (1, round (_hh/_sp)); |
| 1244 |
_sp := _hh/_nn + signof (_hh) epsilon; |
| 1245 |
_nn := _nn-1; |
| 1246 |
setpath (_p) rect ((xpart a, 0),(xpart b, _sp)); |
| 1247 |
_y := ypart a; |
| 1248 |
for _i = 0 upto _nn: |
| 1249 |
if (clr(_i/_nn)) < white : |
| 1250 |
addto v also shaded (clr(_i/_nn)) ( _p shifted (0,_y)) |
| 1251 |
rotated theta; |
| 1252 |
fi |
| 1253 |
_y := _y + _sp; |
| 1254 |
endfor |
| 1255 |
mono (v); |
| 1256 |
endgroup |
| 1257 |
enddef; |
| 1258 |
|
| 1259 |
def areagradientf (suffix clr, v) (expr sp, tp, a, b) = |
| 1260 |
begingroup |
| 1261 |
save _ww, _hh, _sp, _tp, _nn, _mm, _x, _y; |
| 1262 |
_ww := xpart b - xpart a; |
| 1263 |
_hh := ypart b - ypart a; |
| 1264 |
_sp := signof (_ww) abs(sp); |
| 1265 |
_tp := signof (_hh) abs(tp); |
| 1266 |
_nn := emax (1, round (_ww/_sp)); |
| 1267 |
_mm := emax (1, round (_hh/_tp)); |
| 1268 |
_sp := _ww/_nn + signof (_ww) epsilon; |
| 1269 |
_tp := _hh/_mm + signof (_hh) epsilon; |
| 1270 |
_mm := _mm-1; _nn := _nn-1; |
| 1271 |
setpath (_p) rect (origin,(_sp,_tp)); |
| 1272 |
_x := xpart a; y_a := ypart a; |
| 1273 |
for _i = 0 upto _nn: |
| 1274 |
_y := y_a; |
| 1275 |
for _j = 0 upto _mm: |
| 1276 |
if (clr(_i/_nn,_j/_mm)) < white: |
| 1277 |
addto v also shaded (clr(_i/_nn,_j/_mm)) (_p shifted (_x,_y)); |
| 1278 |
fi |
| 1279 |
_y := _y + _tp; |
| 1280 |
endfor |
| 1281 |
_x := _x + _sp; |
| 1282 |
endfor |
| 1283 |
mono (v); |
| 1284 |
endgroup |
| 1285 |
enddef; |
| 1286 |
|
| 1287 |
path unitcircle; |
| 1288 |
unitcircle := fullcircle scaled 2; |
| 1289 |
def radialgradientf (suffix clr, v) (expr sp, ctr, rad) = |
| 1290 |
begingroup |
| 1291 |
save _sp, _r, _nn; |
| 1292 |
_nn := emax (1, round (rad/sp)); |
| 1293 |
_sp := rad/_nn + epsilon; |
| 1294 |
_nn := _nn - 1; |
| 1295 |
_r := _sp; |
| 1296 |
% fill the small center circle first |
| 1297 |
if (clr(0)) < white : |
| 1298 |
addto v also shaded (clr(0)) (unitcircle scaled _r shifted ctr); |
| 1299 |
fi |
| 1300 |
for _i = 1 upto _nn: |
| 1301 |
if (clr(_i/_nn)) < white : |
| 1302 |
addto v also shaded (clr(_i/_nn)) |
| 1303 |
(unitcircle scaled (_r + _sp) -- reverse unitcircle scaled _r |
| 1304 |
--cycle) shifted ctr; |
| 1305 |
fi |
| 1306 |
_r := _r + _sp; |
| 1307 |
endfor |
| 1308 |
mono (v); |
| 1309 |
endgroup |
| 1310 |
enddef; |
| 1311 |
|
| 1312 |
def tile (suffix atile) (expr unit, width, height, clipit) = |
| 1313 |
picture atile.pic; atile.pic := nullpicture; |
| 1314 |
pair atile.dims; |
| 1315 |
atile.dims := round ((width, height)*unit); |
| 1316 |
begingroup |
| 1317 |
save active_plane; |
| 1318 |
def active_plane = atile.pic enddef; |
| 1319 |
save ztr, vtr; transform ztr, vtr; |
| 1320 |
ztr := identity scaled unit; vtr := ztr; |
| 1321 |
save xneg, xpos, yneg, ypos; |
| 1322 |
xneg := 0; xpos := width; yneg := 0; ypos := height; |
| 1323 |
save ClipOn; boolean ClipOn; |
| 1324 |
if clipit: |
| 1325 |
ClipOn := true; |
| 1326 |
setarray (path) (ClipPath) (rect(origin, atile.dims)); |
| 1327 |
else: |
| 1328 |
ClipOn := false; |
| 1329 |
fi |
| 1330 |
enddef; |
| 1331 |
def endtile = |
| 1332 |
DoClip (active_plane); |
| 1333 |
endgroup |
| 1334 |
enddef; |
| 1335 |
|
| 1336 |
vardef is_tile (suffix atile) = |
| 1337 |
(known atile.pic ) and (picture atile.pic) and |
| 1338 |
(known atile.dims) and (pair atile.dims ) |
| 1339 |
enddef; |
| 1340 |
|
| 1341 |
vardef pnt@# (expr p) = point @# of p enddef; |
| 1342 |
vardef pre@# (expr p) = precontrol @# of p enddef; |
| 1343 |
vardef post@# (expr p) = postcontrol @# of p enddef; |
| 1344 |
|
| 1345 |
numeric bbox_split; bbox_split := 4; |
| 1346 |
def getbbox (suffix ll, ur) expr g = |
| 1347 |
setsplit (_s) bbox_split; |
| 1348 |
ur := ll := pnt 0 (g); |
| 1349 |
for _j = 1 upto length g: |
| 1350 |
ll := pairmin (ll, pnt[_j] (g)); ur := pairmax (ur, pnt[_j] (g)); |
| 1351 |
endfor |
| 1352 |
for _j = 1 upto _s*(length g): |
| 1353 |
ctrlsbbox (ll, ur) subpath ((_j-1)/_s, _j/_s) of g; |
| 1354 |
endfor |
| 1355 |
if showbbox: noclip ( safedraw rect (ll, ur) ); fi |
| 1356 |
enddef; |
| 1357 |
|
| 1358 |
def ctrlsbbox (suffix ll, ur) expr p = |
| 1359 |
ll := pairmin ( pairmin (ll, post0 (p)), pre 1 (p) ); |
| 1360 |
ur := pairmax ( pairmax (ur, post0 (p)), pre 1 (p) ); |
| 1361 |
enddef; |
| 1362 |
|
| 1363 |
def getradius (suffix rad) expr g = |
| 1364 |
setsplit (_s) bbox_split; |
| 1365 |
rad := abs (pnt0 (g)); |
| 1366 |
for _j = 1 upto length g: |
| 1367 |
rad := emax(rad, abs(pnt[_j] (g))); |
| 1368 |
endfor |
| 1369 |
for _j = 1 upto _s*(length g): |
| 1370 |
ctrlsradius (rad) subpath ((_j-1)/_s, _j/_s) of g; |
| 1371 |
endfor |
| 1372 |
enddef; |
| 1373 |
|
| 1374 |
def ctrlsradius (suffix rad) expr p = |
| 1375 |
rad := emax( emax (rad, abs(post0 (p))), abs(pre1 (p) )) |
| 1376 |
enddef; |
| 1377 |
|
| 1378 |
def safedraw = colorsafedraw (drawcolor) enddef; |
| 1379 |
def colorsafedraw (expr clr) expr d = |
| 1380 |
begingroup |
| 1381 |
setpicture (v) picpath d; |
| 1382 |
DoClip (v); coloraddon (clr, v); |
| 1383 |
endgroup |
| 1384 |
enddef; |
| 1385 |
|
| 1386 |
def NoCycle (expr s) expr p = |
| 1387 |
GBwarn s & " cannot be applied to an open path." |
| 1388 |
& " The path will be drawn instead."; |
| 1389 |
safedraw p; |
| 1390 |
enddef; |
| 1391 |
|
| 1392 |
vardef isgray (expr X) = (X > black) and (X < white) enddef; |
| 1393 |
|
| 1394 |
def safefill = colorsafefill (fillcolor) enddef; |
| 1395 |
vardef colorsafefill (expr clr) expr c = |
| 1396 |
if cycle c: |
| 1397 |
setpicture (v) interior c; |
| 1398 |
DoClip (v); |
| 1399 |
if isgray (clr): |
| 1400 |
_subto (active_plane) (v); |
| 1401 |
v := nullpicture; |
| 1402 |
v := shaded (clr) c; |
| 1403 |
fi |
| 1404 |
coloraddon (clr, v); |
| 1405 |
else: NoCycle("fill") c; |
| 1406 |
fi |
| 1407 |
enddef; |
| 1408 |
def safeunfill expr c = |
| 1409 |
if cycle c: noclip (colorsafefill (background) c); |
| 1410 |
else: NoCycle("unfill") c; |
| 1411 |
fi |
| 1412 |
enddef; |
| 1413 |
|
| 1414 |
def safeclip expr c = |
| 1415 |
if cycle c: clipto (active_plane) c; |
| 1416 |
else: NoCycle("clip") c; |
| 1417 |
fi |
| 1418 |
enddef; |
| 1419 |
|
| 1420 |
def store (suffix fs) expr f = |
| 1421 |
hide ( |
| 1422 |
if (not path f) and (not pair f): |
| 1423 |
GBerrmsg ("Improper expression type.") |
| 1424 |
"The second argument to `store' must be a path or pair."; |
| 1425 |
fi |
| 1426 |
if not path fs: path fs; fi |
| 1427 |
fs := f |
| 1428 |
) |
| 1429 |
enddef; |
| 1430 |
vardef stored (suffix fs) expr f = store (fs) f; f enddef; |
| 1431 |
|
| 1432 |
def drawn = colordrawn (drawcolor) enddef; |
| 1433 |
vardef colordrawn (expr clr) expr f = |
| 1434 |
colorsafedraw (clr) (zconv (f)); f |
| 1435 |
enddef; |
| 1436 |
|
| 1437 |
def zigzag = colorzigzag (drawcolor) enddef; |
| 1438 |
def colorzigzag (expr clr) = colorwiggle (false, clr, 0) enddef; |
| 1439 |
def sinewave = colorsinewave (drawcolor) enddef; |
| 1440 |
def colorsinewave = colorwiggle (true) enddef; |
| 1441 |
|
| 1442 |
vardef colorwiggle (expr smth, clr, tens, blen, elen, len, wid) expr f |
| 1443 |
= |
| 1444 |
convertpath (g) f; |
| 1445 |
setuplengtharray (cumlen, totlen, ct) g; |
| 1446 |
save B; |
| 1447 |
if cycle f: |
| 1448 |
B := 0; |
| 1449 |
else: |
| 1450 |
B := abs(blen)/_rescale_factor; |
| 1451 |
totlen := totlen - B - abs(elen)/_rescale_factor; |
| 1452 |
fi |
| 1453 |
setnumeric (n) 2*round (totlen/len*_rescale_factor); |
| 1454 |
if n < 2: |
| 1455 |
colorsafedraw (clr) g; |
| 1456 |
else: |
| 1457 |
save T, U, X, Y, Z, p; |
| 1458 |
pair U, X, Y, Z; path p; |
| 1459 |
T := if cycle f: 0 else: gettime (cumlen, ct) (B) fi; |
| 1460 |
Z := pnt[T] (g); |
| 1461 |
p :=if not cycle f: (subpath (0,T) of g) |
| 1462 |
if smth: {curl 0} ..tension tens.. else: -- fi |
| 1463 |
fi |
| 1464 |
for i = 1 upto n: |
| 1465 |
hide( |
| 1466 |
T := gettime (cumlen, ct) (B+(i/n)*totlen); |
| 1467 |
X := Z; Z := pnt[T] (g); |
| 1468 |
Y := .5[X,Z]; U := sgn (Z-X); |
| 1469 |
) |
| 1470 |
(Y + (U zscaled (0, if even i: - fi wid))) |
| 1471 |
if smth: {U}..tension tens.. else: -- fi |
| 1472 |
endfor |
| 1473 |
if cycle f: cycle |
| 1474 |
else: if smth: {curl 0} fi (subpath (T, length g) of g) |
| 1475 |
fi; |
| 1476 |
newpicture (v); |
| 1477 |
if smth: |
| 1478 |
save n, k; |
| 1479 |
n := length p; k = n div 50; |
| 1480 |
for i = 0 step 50 until 50*(k-1): |
| 1481 |
shpath (v, drawpen) (subpath (i,i+50) of p); |
| 1482 |
endfor |
| 1483 |
shpath (v, drawpen) (subpath (50k,n) of p); |
| 1484 |
else: shpath (v, drawpen) (p); |
| 1485 |
fi |
| 1486 |
DoClip(v); coloraddon (clr, v); |
| 1487 |
fi |
| 1488 |
f |
| 1489 |
enddef; |
| 1490 |
|
| 1491 |
def corkscrew = colorcorkscrew (drawcolor) enddef; |
| 1492 |
vardef colorcorkscrew (expr clr, tens, blen, elen, len, wid) expr f = |
| 1493 |
convertpath (g) f; |
| 1494 |
setuplengtharray (cumlen, totlen, ct) g; |
| 1495 |
save B; |
| 1496 |
if cycle f: |
| 1497 |
B := 0; |
| 1498 |
else: |
| 1499 |
B := abs(blen)/_rescale_factor; |
| 1500 |
totlen := totlen - B - abs(elen)/_rescale_factor; |
| 1501 |
fi |
| 1502 |
setnumeric (n) round (totlen/len*_rescale_factor); |
| 1503 |
if n < 2: |
| 1504 |
colorsafedraw (clr) g; |
| 1505 |
else: |
| 1506 |
save T, U, X, Y, Z, p; |
| 1507 |
pair U, X, Y, Z; path p; |
| 1508 |
T := if cycle f: 0 else: gettime (cumlen, ct) (B) fi; |
| 1509 |
Z := pnt[T] (g); |
| 1510 |
p :=if (not cycle f) and (B > 0): (subpath (0,T) of g)-- fi |
| 1511 |
for i = 1 upto n: |
| 1512 |
hide( |
| 1513 |
T := gettime (cumlen, ct) (B+(i/n)*totlen); |
| 1514 |
X := Z; Z := pnt[T] (g); |
| 1515 |
Y := .5[X,Z]; U := sgn (Z-X); |
| 1516 |
) |
| 1517 |
(X + (U zscaled (0,-wid))){ U}..tension tens.. |
| 1518 |
(Y + (U zscaled (0, wid))){-U}..tension tens.. |
| 1519 |
endfor |
| 1520 |
if cycle f: cycle |
| 1521 |
else: |
| 1522 |
{U}(Z + (U zscaled (0,-wid))) |
| 1523 |
if elen <> 0: --(subpath(T, length g) of g) fi |
| 1524 |
fi; |
| 1525 |
newpicture (v); |
| 1526 |
save n, k; |
| 1527 |
n := length p; k = n div 50; |
| 1528 |
for i = 0 step 50 until 50*(k-1): |
| 1529 |
shpath (v, drawpen) (subpath (i,i+50) of p); |
| 1530 |
endfor |
| 1531 |
shpath (v, drawpen) (subpath (50k,n) of p); |
| 1532 |
DoClip(v); coloraddon (clr, v); |
| 1533 |
fi |
| 1534 |
f |
| 1535 |
enddef; |
| 1536 |
|
| 1537 |
def filled = colorfilled (fillcolor) enddef; |
| 1538 |
vardef colorfilled (expr clr) expr c = |
| 1539 |
colorsafefill (clr) zconv (c); c |
| 1540 |
enddef; |
| 1541 |
vardef unfilled expr c = safeunfill zconv (c); c enddef; |
| 1542 |
vardef Clip expr c = safeclip zconv (c); c enddef; |
| 1543 |
|
| 1544 |
numeric shadewd; shadewd := 0.5bp; |
| 1545 |
path shadedotpath; |
| 1546 |
shadedotpath := fullcircle; |
| 1547 |
|
| 1548 |
vardef shade (expr sp) expr f = |
| 1549 |
convertpath (g) f; |
| 1550 |
setnumeric (gr) 1 - (.88*abs(shadewd)/sp)**2; |
| 1551 |
if not cycle g: NoCycle("shade") g; |
| 1552 |
elseif gr <= 0: safefill g; |
| 1553 |
else: |
| 1554 |
setbbox (ll, ur) g; |
| 1555 |
ll := floorpair (ll); |
| 1556 |
% setpair (dv) ceiling (sp/(sqrt 2))*(1,1); |
| 1557 |
% test hex spacing: |
| 1558 |
setpair (dv) ( ceiling(.5sp), ceiling(.5sp*sqrt 3) ); |
| 1559 |
setpicture (sh) setdot (shadedotpath, abs(shadewd)); |
| 1560 |
newpicture (v); |
| 1561 |
fillwith (v) (sh, 2dv, ll, ur); |
| 1562 |
newpicture (w); |
| 1563 |
addto w also v shifted goodpair (dv); |
| 1564 |
DoClip (v); DoClip (w); |
| 1565 |
clipto (v) (g); clipto (w) (g); |
| 1566 |
_orto (active_plane, v); |
| 1567 |
v := nullpicture; |
| 1568 |
_orto (active_plane, w); |
| 1569 |
fi |
| 1570 |
f |
| 1571 |
enddef; |
| 1572 |
|
| 1573 |
polkadotwd := 5bp; |
| 1574 |
mindotspace := 1bp; |
| 1575 |
path polkadotpath; polkadotpath := fullcircle; |
| 1576 |
|
| 1577 |
vardef polkadot (expr sp) expr f = |
| 1578 |
convertpath (g) f; |
| 1579 |
if not cycle g: NoCycle("polkadot") g; |
| 1580 |
elseif sp <= emax (2*polkadotwd/3, mindotspace): |
| 1581 |
safefill g; |
| 1582 |
else: |
| 1583 |
setbbox (ll, ur) g; |
| 1584 |
save dx, dy, dshift; pair dshift; |
| 1585 |
dx := sp/2; dy := dx*sqrt 3; |
| 1586 |
dshift := (xpart(ur - ll) mod dx, ypart (ur - ll) mod dy)/2; |
| 1587 |
save p, dims; pair p, dims; |
| 1588 |
p := ll + dshift; |
| 1589 |
dims := 2(dx, dy); |
| 1590 |
setpicture (thepolkadot) setdot (polkadotpath, polkadotwd); |
| 1591 |
newpicture (v); |
| 1592 |
fillwith (v) (thepolkadot, dims, p, ur); |
| 1593 |
fillwith (v) (thepolkadot, dims, p + (dx, dy), ur); |
| 1594 |
DoClip (v); clipto (v) g; |
| 1595 |
if isgray (fillcolor): |
| 1596 |
_subto (active_plane) (v); |
| 1597 |
v := nullpicture; |
| 1598 |
thepolkadot := |
| 1599 |
shaded (fillcolor) polkadotpath scaled ceiling (polkadotwd); |
| 1600 |
fillwith (v) (thepolkadot, dims, p, ur); |
| 1601 |
fillwith (v) (thepolkadot, dims, p + (dx, dy), ur); |
| 1602 |
DoClip (v); clipto (v) g; |
| 1603 |
fi |
| 1604 |
coloraddon (fillcolor, v); |
| 1605 |
fi |
| 1606 |
f |
| 1607 |
enddef; |
| 1608 |
|
| 1609 |
def thatch = colorthatch (hatchcolor) enddef; |
| 1610 |
vardef colorthatch (expr clr) (expr sp, theta) expr f = |
| 1611 |
convertpath (g) f; |
| 1612 |
if not cycle g: NoCycle("hatch") g; |
| 1613 |
elseif sp <= abs(hatchwd): colorsafefill (clr) g; |
| 1614 |
else: |
| 1615 |
newpicture (v); |
| 1616 |
setbbox (ll, ur) g rotated -theta; |
| 1617 |
thatchf (v, identity rotated theta, sp, ll, ur); |
| 1618 |
DoClip (v); clipto (v) (g); |
| 1619 |
coloraddon (clr, v); |
| 1620 |
fi |
| 1621 |
f |
| 1622 |
enddef; |
| 1623 |
|
| 1624 |
def hhatch (expr sp) = thatch (sp, 0) enddef; |
| 1625 |
def vhatch (expr sp) = thatch (sp, 90) enddef; |
| 1626 |
def lhatch (expr sp) = thatch (sp, -45) enddef; |
| 1627 |
def rhatch (expr sp) = thatch (sp, 45) enddef; |
| 1628 |
|
| 1629 |
def xhatch = colorxhatch (hatchcolor) enddef; |
| 1630 |
def colorxhatch (expr clr, sp) = |
| 1631 |
colorthatch (clr) (sp, 45) colorthatch (clr) (sp, -45) |
| 1632 |
enddef; |
| 1633 |
|
| 1634 |
vardef axialgradient (suffix clr) (expr sp, theta) expr f = |
| 1635 |
convertpath (g) f; |
| 1636 |
if not cycle g: NoCycle("axialgradient") g; |
| 1637 |
else: |
| 1638 |
newpicture (_grd); |
| 1639 |
setbbox (ll, ur) g rotated -theta; |
| 1640 |
axialgradientf (clr, _grd) (theta, sp, ll, ur); |
| 1641 |
DoClip (_grd); clipto (_grd) (g); |
| 1642 |
safeunfill g; |
| 1643 |
_orto (active_plane, _grd); |
| 1644 |
fi |
| 1645 |
f |
| 1646 |
enddef; |
| 1647 |
|
| 1648 |
vardef areagradient (suffix clr) (expr sp, tp) expr f = |
| 1649 |
convertpath (g) f; |
| 1650 |
if not cycle g: NoCycle("areagradient") g; |
| 1651 |
else: |
| 1652 |
newpicture (_agr); |
| 1653 |
setbbox (ll, ur) g; |
| 1654 |
areagradientf (clr, _agr) (sp, tp, ll, ur); |
| 1655 |
DoClip (_agr); clipto (_agr) (g); |
| 1656 |
safeunfill g; |
| 1657 |
_orto (active_plane, _agr); |
| 1658 |
fi |
| 1659 |
f |
| 1660 |
enddef; |
| 1661 |
|
| 1662 |
vardef radialgradient (suffix clr) (expr sp, ctr) expr f = |
| 1663 |
convertpath (g) f; |
| 1664 |
if not cycle g: NoCycle("radialgradient") g; |
| 1665 |
else: |
| 1666 |
setpair (_ctr) zconv (ctr); |
| 1667 |
newpicture (_agr); |
| 1668 |
save _rad; |
| 1669 |
getradius (_rad) g shifted - _ctr; |
| 1670 |
radialgradientf (clr, _agr) (sp, _ctr, _rad); |
| 1671 |
DoClip (_agr); clipto (_agr) (g); |
| 1672 |
safeunfill g; |
| 1673 |
_orto (active_plane, _agr); |
| 1674 |
fi |
| 1675 |
f |
| 1676 |
enddef; |
| 1677 |
|
| 1678 |
vardef NoTile (suffix atile) expr g = |
| 1679 |
GBwarn str atile & " is not a valid tile for tess()." |
| 1680 |
& " The path will be drawn instead."; |
| 1681 |
safedraw g; |
| 1682 |
enddef; |
| 1683 |
vardef tess (suffix atile) expr c = |
| 1684 |
convertpath (_g) c; |
| 1685 |
if not cycle _g: NoCycle("tess") _g; |
| 1686 |
elseif not is_tile (atile): NoTile (atile) _g; |
| 1687 |
else: |
| 1688 |
setbbox (_ll, _ur) _g; |
| 1689 |
newpicture (_ts); |
| 1690 |
fillwith (_ts) (atile.pic, atile.dims, _ll, _ur); |
| 1691 |
DoClip (_ts); clipto (_ts) _g; |
| 1692 |
_orto (active_plane, _ts); |
| 1693 |
fi |
| 1694 |
c |
| 1695 |
enddef; |
| 1696 |
|
| 1697 |
if unknown segment_split: segment_split := 8; fi |
| 1698 |
if unknown dashsize: dashsize := 3bp; fi |
| 1699 |
if unknown dashgap: dashgap := dashsize + 2penwd; fi |
| 1700 |
if unknown dash_finish: dash_finish := .5; fi |
| 1701 |
if unknown dash_start: dash_start := .5; fi |
| 1702 |
if unknown _rescale_factor: _rescale_factor := 0.1in; fi |
| 1703 |
|
| 1704 |
numeric last_dot_size; last_dot_size := 0; |
| 1705 |
vardef gendashed (suffix pat) expr f = |
| 1706 |
convertpath (_g) f; |
| 1707 |
save _dpat; |
| 1708 |
if not mkdasharrays (pat) (_dpat): |
| 1709 |
GBwarn "Dash pattern " & str pat |
| 1710 |
& " undefined. Path will be drawn instead."; |
| 1711 |
safedraw _g; |
| 1712 |
elseif _dpat.rep < 2: |
| 1713 |
safedraw _g; |
| 1714 |
else: |
| 1715 |
save _dl; |
| 1716 |
forsuffixes _s = start, rep, finish: |
| 1717 |
_dl._s := 0; |
| 1718 |
for i = 1 upto _dpat._s: |
| 1719 |
_dpat._s[i] := _dpat._s[i]/_rescale_factor; |
| 1720 |
_dl._s := _dl._s + _dpat._s[i]; |
| 1721 |
endfor |
| 1722 |
endfor |
| 1723 |
|
| 1724 |
if _dl.rep = 0: |
| 1725 |
GBwarn "Dash pattern " & str pat & " has length 0. " |
| 1726 |
& "Path will be drawn instead."; |
| 1727 |
safedraw _g; |
| 1728 |
else: |
| 1729 |
setuplengtharray (_cumlen, _totlen, _ct) _g; |
| 1730 |
save _n, _sf, _no_dots; |
| 1731 |
boolean _no_dots; _no_dots := true; |
| 1732 |
_sf := scale_adjust (_n, _dl) (_totlen); |
| 1733 |
if _n < 0: safedraw _g; |
| 1734 |
else: |
| 1735 |
forsuffixes _s = start, rep, finish: |
| 1736 |
for _i = 1 upto _dpat._s: |
| 1737 |
if (_dpat._s[_i] = 0) and _no_dots: _no_dots := false; |
| 1738 |
else: _dpat._s[_i] := _dpat._s[_i]*_sf; |
| 1739 |
fi |
| 1740 |
endfor |
| 1741 |
_dl._s := _dl._s*_sf; |
| 1742 |
endfor |
| 1743 |
if _no_dots: |
| 1744 |
else: |
| 1745 |
if unknown plot_pic: |
| 1746 |
save plot_pic; path plot_pic; |
| 1747 |
plot_pic := dotpath; |
| 1748 |
fi; |
| 1749 |
last_dot_size := |
| 1750 |
if known plot_pic.size: plot_pic.size else: penwd fi; |
| 1751 |
setpicture (dashingdot) makesymbol (plot_pic, last_dot_size); |
| 1752 |
fi |
| 1753 |
save _t, _d, _v; |
| 1754 |
picture _v; _v := nullpicture; |
| 1755 |
_d0 := 0; _t0 := 0; |
| 1756 |
dashit (_dpat.start) (_v); |
| 1757 |
if _n > 0: |
| 1758 |
save _m; _m := ceiling sqrt(_n); |
| 1759 |
for _j = 0 step _m until _n - 1: |
| 1760 |
for _i = 0 upto _m - 1: |
| 1761 |
exitif (_i + _j) > _n - 1; |
| 1762 |
_d0 := _dl.start + (_j + _i)*_dl.rep; |
| 1763 |
_t0 := gettime (_cumlen, _ct) (_d0); |
| 1764 |
dashit (_dpat.rep) (_v); |
| 1765 |
endfor |
| 1766 |
DoClip (_v); |
| 1767 |
coloraddon (drawcolor, _v); |
| 1768 |
_v := nullpicture; |
| 1769 |
endfor |
| 1770 |
fi |
| 1771 |
|
| 1772 |
_d0 := _totlen - _dl.finish; |
| 1773 |
_t0 := gettime (_cumlen, _ct) (_d0); |
| 1774 |
dashit (_dpat.finish) (_v); |
| 1775 |
DoClip (_v); |
| 1776 |
coloraddon (drawcolor, _v); |
| 1777 |
fi |
| 1778 |
fi |
| 1779 |
fi |
| 1780 |
f |
| 1781 |
enddef; |
| 1782 |
|
| 1783 |
vardef makelengtharray (suffix clen) suffix p = |
| 1784 |
setsplit (_s) segment_split; |
| 1785 |
numeric clen[]; |
| 1786 |
clen := _s * length p; clen0 := 0; |
| 1787 |
for _i = 1 upto clen: |
| 1788 |
clen[_i] := clen[_i-1] + abs (pnt[_i/_s] (p) - pnt[(_i-1)/_s] (p)) |
| 1789 |
/ _rescale_factor; |
| 1790 |
endfor |
| 1791 |
clen[clen] |
| 1792 |
enddef; |
| 1793 |
|
| 1794 |
vardef scale_adjust (suffix n, pl) (expr lngth) = |
| 1795 |
n := (lngth - pl.start - pl.finish)/pl.rep; |
| 1796 |
n := if n < 0: -1 else: round(n) fi; |
| 1797 |
lngth/(pl.start + emax (n, 0)*pl.rep + pl.finish) |
| 1798 |
enddef; |
| 1799 |
|
| 1800 |
vardef gettime (suffix arr, ct) (expr lngth) = |
| 1801 |
setnumeric (_gtl) emax (arr[ct], emin (arr[arr], lngth)); |
| 1802 |
setsplit (_s) segment_split; |
| 1803 |
forever: exitif inrange (arr[ct], arr[ct+1]) (_gtl); |
| 1804 |
next ct; |
| 1805 |
endfor |
| 1806 |
if arr[ct] = arr[ct+1]: ct |
| 1807 |
else: ( ct + (_gtl - arr[ct]) / (arr[ct+1] - arr[ct]) ) |
| 1808 |
fi /_s |
| 1809 |
enddef; |
| 1810 |
|
| 1811 |
def next suffix X = X := X + 1; enddef; |
| 1812 |
|
| 1813 |
def dashit (suffix pos) (suffix pic) = |
| 1814 |
for _k = 1 upto pos: |
| 1815 |
if odd _k: |
| 1816 |
if pos[_k] = 0: |
| 1817 |
_d1 := _d0; _t1 := _t0; |
| 1818 |
picdot (pic, dashingdot, pnt [_t0] (_g)); |
| 1819 |
else: |
| 1820 |
_d1 := _d0 + pos[_k]; |
| 1821 |
_t1 := gettime (_cumlen, _ct) (_d1); |
| 1822 |
shpath (pic, drawpen) (subpath (_t0, _t1) of _g); |
| 1823 |
fi |
| 1824 |
else: |
| 1825 |
_d0 := _d1 + pos[_k]; |
| 1826 |
_t0 := gettime (_cumlen, _ct) (_d0); |
| 1827 |
fi |
| 1828 |
endfor |
| 1829 |
enddef; |
| 1830 |
|
| 1831 |
def dashpat (suffix pat) (text t) = |
| 1832 |
list (pat) (t); |
| 1833 |
if (pat = 0) or (odd (pat) and (pat > 1)): |
| 1834 |
pat[incr pat] := 0; |
| 1835 |
fi |
| 1836 |
enddef; |
| 1837 |
|
| 1838 |
vardef mkdasharrays (suffix src, dest) = |
| 1839 |
save _bad; boolean _bad; _bad := false; |
| 1840 |
forsuffixes _s = start, rep, finish: |
| 1841 |
numeric dest._s, dest._s[]; |
| 1842 |
boolean _bad._s; |
| 1843 |
if knownnumericarray src._s: |
| 1844 |
copyarray (src._s) (dest._s); |
| 1845 |
_bad._s := false; |
| 1846 |
else: |
| 1847 |
_bad := _bad._s := true; |
| 1848 |
fi |
| 1849 |
endfor |
| 1850 |
% _bad = one of the three arrays not copied. |
| 1851 |
if _bad: |
| 1852 |
if knownnumericarray src: _bad := false; |
| 1853 |
if _bad.rep: % make dest.rep = src |
| 1854 |
copyarray (src) (dest.rep); |
| 1855 |
fi |
| 1856 |
if _bad.start: % shrink first dash to get dest.start |
| 1857 |
copyarray (src) (dest.start); |
| 1858 |
dest.start1 := dash_start*src1; |
| 1859 |
fi |
| 1860 |
if _bad.finish: % use partial first dash for dest.finish |
| 1861 |
dest.finish := 1; |
| 1862 |
dest.finish1 := dash_finish*src1; |
| 1863 |
fi |
| 1864 |
fi |
| 1865 |
fi |
| 1866 |
not _bad |
| 1867 |
enddef; |
| 1868 |
|
| 1869 |
vardef Dashed (expr dlen, dgap) expr f = |
| 1870 |
save dashes; dashpat (dashes) (dlen, dgap); |
| 1871 |
gendashed (dashes) f |
| 1872 |
enddef; |
| 1873 |
|
| 1874 |
def DASHED = Dashed enddef; |
| 1875 |
def dashed = Dashed enddef; |
| 1876 |
|
| 1877 |
vardef doplot (expr spath, sc, dgap) expr f = |
| 1878 |
save dots; dashpat (dots) (0, dgap); |
| 1879 |
setpicture (plot_pic) makesymbol (spath, sc); |
| 1880 |
plot_pic.size := sc; |
| 1881 |
gendashed (dots) f |
| 1882 |
enddef; |
| 1883 |
|
| 1884 |
path dotpath; dotpath := fullcircle; |
| 1885 |
def dotted = doplot (dotpath) enddef; |
| 1886 |
|
| 1887 |
vardef plotnodes (expr symbol, size) expr f = |
| 1888 |
if size > 0: |
| 1889 |
save pln; pair pln[]; |
| 1890 |
pln := 0; |
| 1891 |
for _a = 0 upto (length f) if cycle f: - 1 fi: |
| 1892 |
pln[incr pln] := pnt[_a] (f); |
| 1893 |
endfor |
| 1894 |
dosymbols (drawcolor, symbol, size) (pln); |
| 1895 |
fi |
| 1896 |
f |
| 1897 |
enddef; |
| 1898 |
|
| 1899 |
def showcontrols = colorshowcontrols (pointcolor) enddef; |
| 1900 |
vardef colorshowcontrols (expr clr, syma, symb, size) expr f = |
| 1901 |
save shpre, shpost; |
| 1902 |
pair shpre[], shpost[]; |
| 1903 |
shpre := 0; shpost := 0; |
| 1904 |
for a = 0 upto (length f) if cycle f: - 1 fi: |
| 1905 |
shpre [incr shpre] := pre [a] (f); |
| 1906 |
shpost[incr shpost] := post[a] (f); |
| 1907 |
colorsafedraw (clr) |
| 1908 |
(zconv (shpre[shpre]--pnt[a](f)--shpost[shpost])); |
| 1909 |
endfor |
| 1910 |
if size > 0: |
| 1911 |
if not numeric syma: dosymbols (clr, syma, size) (shpre) ; fi |
| 1912 |
if not numeric symb: dosymbols (clr, symb, size) (shpost); fi |
| 1913 |
fi |
| 1914 |
f |
| 1915 |
enddef; |
| 1916 |
|
| 1917 |
def doubledraw = colordoubledraw (drawcolor) enddef; |
| 1918 |
vardef colordoubledraw (expr clr, sep) expr f = |
| 1919 |
convertpath (g) f; |
| 1920 |
colorsafedraw (clr) (parapath ( sep/2) g); |
| 1921 |
colorsafedraw (clr) (parapath (-sep/2) g); |
| 1922 |
f |
| 1923 |
enddef; |
| 1924 |
|
| 1925 |
vardef makesymbol (expr spath, sc) = |
| 1926 |
if picture spath : |
| 1927 |
setpicture (v) spath; mono (v); v |
| 1928 |
elseif path spath: setdot (spath, sc) |
| 1929 |
else: |
| 1930 |
GBwarn "Undefined symbol for plotting, " |
| 1931 |
& "dotpath will be used instead."; |
| 1932 |
setdot (dotpath, sc) |
| 1933 |
fi |
| 1934 |
enddef; |
| 1935 |
|
| 1936 |
vardef bpoint (expr ptwd, b) = |
| 1937 |
fullcircle scaled ptwd shifted b |
| 1938 |
enddef; |
| 1939 |
|
| 1940 |
def pointd (expr ptwd, filled) (text t) = |
| 1941 |
if filled: |
| 1942 |
plotsymbol (SolidCircle, ptwd) (t); |
| 1943 |
else: |
| 1944 |
begingroup; |
| 1945 |
setboolean (clearsymbols) true; |
| 1946 |
plotsymbol (Circle, ptwd) (t); |
| 1947 |
endgroup |
| 1948 |
fi |
| 1949 |
enddef; |
| 1950 |
|
| 1951 |
boolean clearsymbols; clearsymbols := false; |
| 1952 |
vardef clearable (expr pth) = |
| 1953 |
if path pth: |
| 1954 |
( pnt0 (pth) = pnt[length pth] (pth) ) and (not cycle pth) |
| 1955 |
and (length pth > 0) |
| 1956 |
else: false |
| 1957 |
fi |
| 1958 |
enddef; |
| 1959 |
|
| 1960 |
def clearopenpath expr f = |
| 1961 |
if clearable (f): safeunfill f & cycle; fi |
| 1962 |
enddef; |
| 1963 |
|
| 1964 |
def plotsymbol = colorplotsymbol (pointcolor) enddef; |
| 1965 |
def colorplotsymbol (expr clr, spath, sc) (text t) = |
| 1966 |
if sc > 0: |
| 1967 |
begingroup |
| 1968 |
setpairs (_cpls) (t); |
| 1969 |
if _cpls > 0: dosymbols (clr, spath, sc) (_cpls); fi |
| 1970 |
endgroup |
| 1971 |
fi |
| 1972 |
enddef; |
| 1973 |
|
| 1974 |
def dosymbols (expr clr, spath, sc) (suffix arr) = |
| 1975 |
if clearsymbols and clearable (spath): |
| 1976 |
addsymbols (background, makesymbol (spath&cycle, sc)) (arr); |
| 1977 |
fi |
| 1978 |
addsymbols (clr, makesymbol (spath, sc)) (arr); |
| 1979 |
enddef; |
| 1980 |
|
| 1981 |
def addsymbols (expr clr, symb) (suffix arr) = |
| 1982 |
newpicture (_pls); |
| 1983 |
for _idx = 1 upto arr: |
| 1984 |
picdot (_pls, symb, zconv (arr[_idx])); |
| 1985 |
endfor |
| 1986 |
DoClip (_pls); coloraddon (clr, _pls); |
| 1987 |
enddef; |
| 1988 |
|
| 1989 |
def putimage (suffix pic) (text t) = |
| 1990 |
newpicture (_pti); |
| 1991 |
for _itm = t: |
| 1992 |
addto _pti also |
| 1993 |
(pic shifted goodpair (zconv (_itm))); |
| 1994 |
DoClip (_pti); addto active_plane also _pti; |
| 1995 |
_pti := nullpicture; |
| 1996 |
endfor |
| 1997 |
mono active_plane |
| 1998 |
enddef; |
| 1999 |
|
| 2000 |
def arrowdraw (expr hlen) (expr f) = |
| 2001 |
store (curpath) headpath (hlen, 0, 0) drawn f; |
| 2002 |
enddef; |
| 2003 |
|
| 2004 |
def xaxis (expr hlen) = arrowdraw (hlen) ((xneg, 0)--(xpos, 0)); |
| 2005 |
enddef; |
| 2006 |
def yaxis (expr hlen) = arrowdraw (hlen) ((0, yneg)--(0, ypos)); |
| 2007 |
enddef; |
| 2008 |
def axes (expr hlen) = xaxis (hlen); yaxis (hlen); enddef; |
| 2009 |
|
| 2010 |
laxis := baxis := raxis := taxis := 0; |
| 2011 |
|
| 2012 |
vardef xlow = xneg + laxis enddef; |
| 2013 |
vardef xhigh = xpos - raxis enddef; |
| 2014 |
vardef ylow = yneg + baxis enddef; |
| 2015 |
vardef yhigh = ypos - taxis enddef; |
| 2016 |
|
| 2017 |
vardef axisline.x = (xlow, 0)--(xhigh, 0) enddef; |
| 2018 |
vardef axisline.y = (0, ylow)--(0, yhigh) enddef; |
| 2019 |
vardef axisline.l = axisline.y shifted (xlow, 0) enddef; |
| 2020 |
vardef axisline.b = axisline.x shifted (0, ylow) enddef; |
| 2021 |
vardef axisline.r = axisline.y shifted (xhigh, 0) enddef; |
| 2022 |
vardef axisline.t = axisline.x shifted (0, yhigh) enddef; |
| 2023 |
|
| 2024 |
vardef axis@# (expr len) = headpath (len, 0, 0) axisline@# enddef; |
| 2025 |
|
| 2026 |
vardef borderrect = |
| 2027 |
rect((xlow,ylow),(xhigh,yhigh)) |
| 2028 |
enddef; |
| 2029 |
|
| 2030 |
vardef between (expr A, B, X) = (A < X) and (X < B) enddef; |
| 2031 |
vardef inrange (expr A, B, X) = (A <= X) and (X <= B) enddef; |
| 2032 |
|
| 2033 |
vardef inbounds (expr Z) = |
| 2034 |
inrange (xlow, xhigh) (xpart Z) and inrange (ylow, yhigh) (ypart Z) |
| 2035 |
enddef; |
| 2036 |
|
| 2037 |
tertiarydef X isbetween P = between (xpart P, ypart P, X) enddef; |
| 2038 |
tertiarydef X isinrange P = inrange (xpart P, ypart P, X) enddef; |
| 2039 |
tertiarydef P contains X = between (xpart P, ypart P, X) enddef; |
| 2040 |
|
| 2041 |
numeric inside, outside, centered, onleft, onright, ontop, onbottom; |
| 2042 |
inside := -2; |
| 2043 |
outside := -1; |
| 2044 |
onright := 1; |
| 2045 |
onleft := 2; |
| 2046 |
centered := .5[onright, onleft]; |
| 2047 |
onbottom := onright; |
| 2048 |
ontop := onleft; |
| 2049 |
ltick := rtick := ttick := btick := inside; |
| 2050 |
xtick := ytick := centered; |
| 2051 |
|
| 2052 |
vardef axismarks (expr inang, tp, loc, pdir) (expr len) (text t) = |
| 2053 |
save _tp, _U, _P, _tic, _ticang; |
| 2054 |
pair _U, _P; path _tic; |
| 2055 |
_ticang := if tp < 0: inang else: 90 fi; |
| 2056 |
_tp := abs(tp) - 1; |
| 2057 |
_U := unitvector (vconv (pdir)) rotated _ticang; |
| 2058 |
_tic := (-_U--(0,0)) shifted (_tp*_U) scaled len; |
| 2059 |
for _a = t: |
| 2060 |
safedraw (_tic shifted zconv (loc + _a*pdir)); |
| 2061 |
endfor |
| 2062 |
enddef; |
| 2063 |
|
| 2064 |
def xmarks = axismarks ( 90, xtick, origin, right) enddef; |
| 2065 |
def ymarks = axismarks (-90, ytick, origin, up) enddef; |
| 2066 |
def lmarks = axismarks (-90, ltick, (xlow, 0), up) enddef; |
| 2067 |
def bmarks = axismarks ( 90, btick, (0, ylow), right) enddef; |
| 2068 |
def rmarks = axismarks ( 90, rtick, (xhigh, 0), up) enddef; |
| 2069 |
def tmarks = axismarks (-90, ttick, (0, yhigh), right) enddef; |
| 2070 |
|
| 2071 |
path griddotpath; griddotpath := fullcircle; |
| 2072 |
def grid = vargrid (0.5bp) enddef; |
| 2073 |
vardef vargrid (expr dsize, xsp, ysp) = |
| 2074 |
save gdot, gridpic; picture gdot, gridpic; |
| 2075 |
gdot := setdot (griddotpath, dsize); |
| 2076 |
gridpic := nullpicture; |
| 2077 |
for n = ceiling ((xlow)/xsp) upto floor ((xhigh)/xsp): |
| 2078 |
for m = ceiling ((ylow)/ysp) upto floor ((yhigh)/ysp): |
| 2079 |
picdot (gridpic, gdot, zconv ((n*xsp, m*ysp))); |
| 2080 |
endfor |
| 2081 |
endfor |
| 2082 |
coloraddon (pointcolor, gridpic); |
| 2083 |
enddef; |
| 2084 |
def vgrid = vargrid enddef; |
| 2085 |
|
| 2086 |
def hgridlines (expr ysp) = |
| 2087 |
for n = ceiling ((ylow)/ysp) upto floor ((yhigh)/ysp): |
| 2088 |
safedraw zconv ((xlow, n*ysp)--(xhigh, n*ysp)); |
| 2089 |
endfor |
| 2090 |
enddef; |
| 2091 |
def vgridlines (expr xsp) = |
| 2092 |
for n = ceiling ((xlow)/xsp) upto floor ((xhigh)/xsp): |
| 2093 |
safedraw zconv ((n*xsp, ylow)--(n*xsp, yhigh)); |
| 2094 |
endfor |
| 2095 |
enddef; |
| 2096 |
def gridlines (expr xsp, ysp) = |
| 2097 |
vgridlines (xsp); hgridlines (ysp); |
| 2098 |
enddef; |
| 2099 |
|
| 2100 |
def vectorfield (expr len, xsp, ysp) (text fcn) (text cond) = |
| 2101 |
save _vf, _is_OK; |
| 2102 |
vardef _vf (expr x,y) = ((0,0)--(fcn)) shifted (x,y) enddef; |
| 2103 |
vardef _is_OK (expr x,y) = cond enddef; |
| 2104 |
mkvectorfield (len, xsp, ysp) (_vf, _is_OK); |
| 2105 |
enddef; |
| 2106 |
|
| 2107 |
vardef mkvectorfield (expr len, xsp, ysp) (suffix vf, isOK) = |
| 2108 |
for n = ceiling ((xlow)/xsp) upto floor ((xhigh)/xsp): |
| 2109 |
for m = ceiling ((ylow)/ysp) upto floor ((yhigh)/ysp): |
| 2110 |
if isOK (n*xsp,m*ysp): arrowdraw (len) (vf(n*xsp,m*ysp)); fi |
| 2111 |
endfor |
| 2112 |
endfor |
| 2113 |
enddef; |
| 2114 |
|
| 2115 |
def plrvectorfield (expr len, rsp, tsp) (text fcn) (text cond) = |
| 2116 |
save _vf, _is_OK, _A, _B, _C, _D; |
| 2117 |
_A := xlow; _B := xhigh; |
| 2118 |
_C := ylow; _D := yhigh; |
| 2119 |
vardef _vf (expr r,t) = ((0,0)--(fcn)) shifted (r*dir t) enddef; |
| 2120 |
vardef _is_OK (expr r,t) = |
| 2121 |
save _X, _Y; _X := r*cosd t; _Y := r*sind t; |
| 2122 |
(cond) and between (_A, _B) (_X) and between (_C, _D) (_Y) |
| 2123 |
enddef; |
| 2124 |
mkplrvectorfield (len, rsp, tsp) (_vf, _is_OK); |
| 2125 |
enddef; |
| 2126 |
|
| 2127 |
vardef mkplrvectorfield (expr len, rsp, tsp) (suffix vf, isOK) = |
| 2128 |
save rmin, rmax, tmin, tmax; |
| 2129 |
getpolarbounds; |
| 2130 |
if rmin = 0: |
| 2131 |
if isOK (0,tmin): arrowdraw (len) (vf (0,tmin)); fi |
| 2132 |
rmin := rsp; |
| 2133 |
fi |
| 2134 |
for n = ceiling (rmin/rsp) upto floor (rmax/rsp): |
| 2135 |
for m = ceiling (tmin/tsp) upto floor (tmax/tsp): |
| 2136 |
if isOK (n*rsp,m*tsp): arrowdraw (len) (vf (n*rsp,m*tsp)); fi |
| 2137 |
endfor |
| 2138 |
endfor |
| 2139 |
enddef; |
| 2140 |
|
| 2141 |
def patcharcs (suffix X) (expr rstart, rstop, rstep, tstart, tstop) = |
| 2142 |
for rad = (if rstart = 0: rstep else: rstart fi) |
| 2143 |
step rstep until rstop: |
| 2144 |
orto (X, picpath zconv (arcplr (origin, tstart, tstop, rad)) ); |
| 2145 |
endfor |
| 2146 |
enddef; |
| 2147 |
def patchrays (suffix X) (expr tstart, tstop, tstep, rstart, rstop) = |
| 2148 |
for _ang = tstart step tstep until tstop: |
| 2149 |
orto (X) (picpath zconv ((rstart*dir _ang)--(rstop*dir _ang))); |
| 2150 |
endfor |
| 2151 |
enddef; |
| 2152 |
|
| 2153 |
def plrpatch (expr rstart, rstop, rstep, tstart, tstop, tstep) = |
| 2154 |
begingroup |
| 2155 |
newpicture (v); |
| 2156 |
patcharcs (v) (rstart, rstop, rstep, tstart, tstop); |
| 2157 |
coloraddon (drawcolor, v); |
| 2158 |
v := nullpicture; |
| 2159 |
patchrays (v) (tstart, tstop, tstep, rstart, rstop); |
| 2160 |
coloraddon (drawcolor, v); |
| 2161 |
endgroup |
| 2162 |
enddef; |
| 2163 |
|
| 2164 |
def gridarcs (expr rstep) = |
| 2165 |
beginpolargrid; |
| 2166 |
if rmin = 0: |
| 2167 |
picdot (gridpic, setdot (griddotpath, penwd), zconv (origin)); |
| 2168 |
fi |
| 2169 |
rmin := rstep * floor (rmin/rstep + 1); |
| 2170 |
rmax := rstep * ceiling (rmax/rstep - 1); |
| 2171 |
patcharcs (gridpic) (rmin, rmax, rstep, tmin, tmax); |
| 2172 |
endpolargrid (drawcolor, .5penwd); |
| 2173 |
enddef; |
| 2174 |
|
| 2175 |
def gridrays (expr tstep) = |
| 2176 |
beginpolargrid; |
| 2177 |
tmin := tstep * ceiling (tmin/tstep); |
| 2178 |
tmax := tstep * floor (tmax/tstep); |
| 2179 |
patchrays (gridpic) (tmin, tmax, tstep, rmin, rmax); |
| 2180 |
endpolargrid (drawcolor, .5penwd); |
| 2181 |
enddef; |
| 2182 |
|
| 2183 |
def polargrid (expr rstep, tstep) = |
| 2184 |
gridarcs (rstep); gridrays (tstep); |
| 2185 |
enddef; |
| 2186 |
|
| 2187 |
def polargridpoints (expr dsize, rstep, tstep) = |
| 2188 |
beginpolargrid; |
| 2189 |
setpicture (gdot) setdot (griddotpath, dsize); |
| 2190 |
if rmin = 0: |
| 2191 |
picdot (gridpic, gdot, zconv (origin)); |
| 2192 |
rmin := rstep; |
| 2193 |
fi |
| 2194 |
for n = ceiling (rmin/rstep) upto floor (rmax/rstep): |
| 2195 |
for m = ceiling (tmin/tstep) upto floor (tmax/tstep): |
| 2196 |
picdot ( gridpic, gdot, zconv ( polar ((n*rstep, m*tstep)) ) ); |
| 2197 |
endfor |
| 2198 |
endfor |
| 2199 |
endpolargrid (pointcolor, .5dsize); |
| 2200 |
enddef; |
| 2201 |
|
| 2202 |
def beginpolargrid = |
| 2203 |
begingroup; |
| 2204 |
save rmax, rmin, tmax, tmin; |
| 2205 |
getpolarbounds; |
| 2206 |
newpicture (gridpic); |
| 2207 |
enddef; |
| 2208 |
|
| 2209 |
def getpolarbounds = |
| 2210 |
save p, r, t; |
| 2211 |
pair p[]; |
| 2212 |
p0 := (xneg, yneg); p1 := (xneg, ypos); |
| 2213 |
p2 := (xpos, ypos); p3 := (xpos, yneg); |
| 2214 |
r0 := abs(p0); rmax := r0; |
| 2215 |
for j = 1 upto 3: |
| 2216 |
r[j] := abs(p[j]); |
| 2217 |
if rmax < r[j]: rmax := r[j]; fi |
| 2218 |
endfor |
| 2219 |
rmin := 0; |
| 2220 |
if between (xneg, xpos) (0) and between (yneg, ypos) (0): |
| 2221 |
tmin := 0; tmax := 360; |
| 2222 |
elseif (p0 = origin): tmin := 0; tmax := 90; |
| 2223 |
elseif (p1 = origin): tmin := -90; tmax := 0; |
| 2224 |
elseif (p2 = origin): tmin := -180; tmax := -90; |
| 2225 |
elseif (p3 = origin): tmin := 90; tmax := 180; |
| 2226 |
else: |
| 2227 |
tmax := tmin := t0 := angle p0; |
| 2228 |
for j = 1 upto 3: |
| 2229 |
t := t0 + anglefromto (p0, p[j]); |
| 2230 |
if tmax < t: tmax := t; fi |
| 2231 |
if tmin > t: tmin := t; fi |
| 2232 |
endfor |
| 2233 |
if between (xneg, xpos) (0): |
| 2234 |
rmin := emin (abs(yneg), abs(ypos)); |
| 2235 |
elseif between (yneg, ypos) (0): |
| 2236 |
rmin := emin (abs(xneg), abs(xpos)); |
| 2237 |
else: |
| 2238 |
rmin := min (r0, r1, r2, r3); |
| 2239 |
fi |
| 2240 |
fi |
| 2241 |
enddef; |
| 2242 |
|
| 2243 |
|
| 2244 |
def endpolargrid (expr clr, size)= |
| 2245 |
clipto (gridpic) rect ( zconv ((xneg, yneg)) - size*(1,1), |
| 2246 |
zconv ((xpos, ypos)) + size*(1,1) ); |
| 2247 |
coloraddon (clr, gridpic); |
| 2248 |
endgroup |
| 2249 |
enddef; |
| 2250 |
|
| 2251 |
vardef polarpatch (expr rstart, rstop, rstep, tstart, tstop, tstep) = |
| 2252 |
plrpatch (rstart, rstop, rstep, tstart, tstop, tstep); |
| 2253 |
safedraw zconv ( arcplr (origin, tstart, tstop, rstop) ); |
| 2254 |
safedraw zconv ( ((rstart, 0)--(rstop, 0)) rotated tstop ); |
| 2255 |
enddef; |
| 2256 |
|
| 2257 |
vardef rect (expr ll, ur) = |
| 2258 |
ll--(xpart ur, ypart ll)--ur--(xpart ll, ypart ur)--cycle |
| 2259 |
enddef; |
| 2260 |
vardef triangle (expr A, B, C) = A--B--C--cycle enddef; |
| 2261 |
|
| 2262 |
vardef regularpolygon (expr n) (suffix Bob) (text eqns) = |
| 2263 |
pair Bob[]; Bob := emax (round (abs (n)), 2); |
| 2264 |
eqns; |
| 2265 |
for _uncle = 1 upto Bob - 1: |
| 2266 |
(Bob1 - Bob0) rotated (360/Bob*_uncle) = Bob[_uncle+1] - Bob0; |
| 2267 |
endfor |
| 2268 |
mkpoly (true) (Bob) |
| 2269 |
enddef; |
| 2270 |
|
| 2271 |
vardef altitudept expr n of t = |
| 2272 |
save A, B, C, zz; pair A, B, C, zz; |
| 2273 |
B := pnt[n + 1] (t); |
| 2274 |
C := pnt[n + 2] (t); |
| 2275 |
zz = whatever[B,C]; |
| 2276 |
zz = pnt[n](t) + whatever*((C-B) rotated 90); |
| 2277 |
zz |
| 2278 |
enddef; |
| 2279 |
|
| 2280 |
vardef altitude expr n of t = |
| 2281 |
(pnt[n](t))--(altitudept n of t) |
| 2282 |
enddef; |
| 2283 |
|
| 2284 |
vardef medianpt expr n of t = |
| 2285 |
0.5[pnt[n + 1] (t), pnt[n + 2] (t)] |
| 2286 |
enddef; |
| 2287 |
|
| 2288 |
vardef median expr n of t = |
| 2289 |
(pnt[n](t))--(medianpt n of t) |
| 2290 |
enddef; |
| 2291 |
|
| 2292 |
vardef anglebisectorpt expr n of t = |
| 2293 |
save A, B, C; pair A, B, C; |
| 2294 |
A := pnt[n ] (t); |
| 2295 |
B := pnt[n + 1] (t); |
| 2296 |
C := pnt[n + 2] (t); |
| 2297 |
save zz; pair zz; |
| 2298 |
zz = whatever[B,C]; |
| 2299 |
zz = A + whatever*((B-A) rotated (.5*cornerangle (A,B,C))); |
| 2300 |
zz |
| 2301 |
enddef; |
| 2302 |
|
| 2303 |
vardef anglebisector expr n of t = |
| 2304 |
(pnt[n](t))--(anglebisectorpt n of t) |
| 2305 |
enddef; |
| 2306 |
|
| 2307 |
vardef anglefromto (expr u, v) = |
| 2308 |
if (u = origin) or (v = origin): 0 |
| 2309 |
else: angle (v rotated (-angle u)) |
| 2310 |
fi |
| 2311 |
enddef; |
| 2312 |
|
| 2313 |
vardef cornerangle (expr A, B, C) = |
| 2314 |
if (A = B) or (A = C) : |
| 2315 |
if (B = C) : 60 |
| 2316 |
else: 90 |
| 2317 |
fi |
| 2318 |
else: anglefromto (B - A, C - A) |
| 2319 |
fi |
| 2320 |
enddef; |
| 2321 |
|
| 2322 |
vardef mkpath (expr smooth, tens, cyclic) (suffix pts) = |
| 2323 |
if smooth: mksmooth (tens) |
| 2324 |
else: mkpoly |
| 2325 |
fi (cyclic, pts) |
| 2326 |
enddef; |
| 2327 |
|
| 2328 |
vardef mkpoly (expr cyclic) (suffix pts) = |
| 2329 |
for _i = 1 upto pts-1: pts[_i]-- endfor |
| 2330 |
pts[pts] if cyclic: -- cycle else: {0,0} fi |
| 2331 |
enddef; |
| 2332 |
|
| 2333 |
vardef polyline (expr cyclic) (text t) = |
| 2334 |
setpairs (_pl) (t); |
| 2335 |
if _pl=0: NoPoints ("polyline", _pl); fi |
| 2336 |
mkpoly (cyclic, _pl) |
| 2337 |
enddef; |
| 2338 |
|
| 2339 |
def NoPoints (expr s) (suffix pts) = |
| 2340 |
GBwarn s & " attempted with empty list."; pts[incr pts] := origin; |
| 2341 |
enddef; |
| 2342 |
|
| 2343 |
vardef turtle (text t) = |
| 2344 |
setnumeric (_tu) 0; |
| 2345 |
setpair (_tmp) origin; |
| 2346 |
pair _tu[]; |
| 2347 |
for _a = t: |
| 2348 |
_tmp := _tmp + _a; |
| 2349 |
_tu[incr _tu] := _tmp; |
| 2350 |
endfor |
| 2351 |
if _tu = 0: NoPoints("turtle", _tu); fi |
| 2352 |
mkpoly (false, _tu) |
| 2353 |
enddef; |
| 2354 |
|
| 2355 |
vardef brownianpath (expr start, num, sc) = |
| 2356 |
setnumeric (_brp) 1; |
| 2357 |
setpair (_tmp) start; |
| 2358 |
pair _brp[]; _brp1 := _tmp; |
| 2359 |
for _idx := 1 upto num: |
| 2360 |
_tmp := _tmp + sc/(sqrt 2)*(normaldeviate,normaldeviate); |
| 2361 |
_brp[incr _brp] := _tmp; |
| 2362 |
endfor |
| 2363 |
mkpoly (false, _brp) |
| 2364 |
enddef; |
| 2365 |
vardef randomwalk (expr start, num, dst) = |
| 2366 |
setnumeric (_rdw) 1; |
| 2367 |
setpair (_tmp) start; |
| 2368 |
pair _rdw[]; _rdw1 := _tmp; |
| 2369 |
for _idx := 1 upto num: |
| 2370 |
_tmp := _tmp + dst*dir(uniformdeviate(360)); |
| 2371 |
_rdw[incr _rdw] := _tmp; |
| 2372 |
endfor |
| 2373 |
mkpoly (false, _rdw) |
| 2374 |
enddef; |
| 2375 |
vardef browniangraph (expr num, scst) = |
| 2376 |
setnumeric (_brg) 1; |
| 2377 |
pair _tmp, _brg[]; _tmp := _brg1 := (0,0); |
| 2378 |
for _idx := 1 upto num: |
| 2379 |
_tmp := _tmp + scst*(1,normaldeviate); |
| 2380 |
_brg[incr _brg] := _tmp; |
| 2381 |
endfor |
| 2382 |
mkpoly (false, _brg) |
| 2383 |
enddef; |
| 2384 |
|
| 2385 |
vardef mksmooth (expr tens, cyclic) (suffix pts) = |
| 2386 |
if pts = 1: onepointpath (cyclic, pts1) |
| 2387 |
else: |
| 2388 |
settension (_tn) tens; fixtension (_tn); |
| 2389 |
pts1 if cyclic: {pts[2]-pts[pts]} fi |
| 2390 |
for _i = 2 upto pts-1: |
| 2391 |
..tension _tn..pts[_i]{pts[_i+1]-pts[_i-1]} |
| 2392 |
endfor |
| 2393 |
..tension _tn..pts[pts] |
| 2394 |
if cyclic: {pts[1]-pts[pts-1]}..tension _tn..cycle fi |
| 2395 |
fi |
| 2396 |
enddef; |
| 2397 |
|
| 2398 |
vardef mktenser (expr tens, cyclic) (suffix pts) = |
| 2399 |
if pts = 1: onepointpath (cyclic, pts1) |
| 2400 |
else: |
| 2401 |
settension (_tn) tens; fixtension (_tn); |
| 2402 |
pts1 if cyclic: {pts[2]-pts[pts]} fi |
| 2403 |
for _i = 2 upto pts-1: |
| 2404 |
..tension atleast _tn..pts[_i]{pts[_i+1]-pts[_i-1]} |
| 2405 |
endfor |
| 2406 |
..tension atleast _tn..pts[pts] |
| 2407 |
if cyclic: {pts[1]-pts[pts-1]}..tension atleast _tn..cycle fi |
| 2408 |
fi |
| 2409 |
enddef; |
| 2410 |
|
| 2411 |
vardef mkconvex (expr tens, cyclic) (suffix pts) = |
| 2412 |
save _B, _d, _tmp; pair _d[]; |
| 2413 |
settension (_tn) tens; fixtension (_tn); |
| 2414 |
if pts < 4: mktenser (_tn, cyclic) (pts) |
| 2415 |
else: |
| 2416 |
for _j = 2 upto pts - 1: |
| 2417 |
_B[_j] := sqrt(abs((pts[_j]-pts[_j-1])xprod(pts[_j+1]-pts[_j]))); |
| 2418 |
endfor |
| 2419 |
if cyclic: |
| 2420 |
_B1 := sqrt(abs((pts1 - pts[pts])xprod(pts2 - pts1))); |
| 2421 |
_B[pts] := sqrt(abs((pts[pts]-pts[pts-1])xprod(pts1 - pts[pts]))); |
| 2422 |
else: |
| 2423 |
_B1 := _B2; |
| 2424 |
_B[pts] := _B[pts-1]; |
| 2425 |
fi |
| 2426 |
for _j = 2 upto pts - 1: |
| 2427 |
_tmp := _B[_j-1] + _B[_j+1]; |
| 2428 |
_d[_j] := |
| 2429 |
if _tmp = 0: origin % signal to use curl1 |
| 2430 |
else: |
| 2431 |
( _B[_j+1]*(pts[_j] - pts[_j-1]) + |
| 2432 |
_B[_j-1]*(pts[_j+1] - pts[_j]) )/_tmp |
| 2433 |
fi; |
| 2434 |
endfor |
| 2435 |
if cyclic: |
| 2436 |
_tmp := _B[pts] + _B2; |
| 2437 |
_d1 := |
| 2438 |
if _tmp = 0: origin |
| 2439 |
else: |
| 2440 |
(_B2*(pts1 - pts[pts]) + _B[pts]*(pts2 - pts1))/_tmp |
| 2441 |
fi; |
| 2442 |
_tmp := _B[pts-1] + _B1; |
| 2443 |
_d[pts] := |
| 2444 |
if _tmp = 0: origin |
| 2445 |
else: |
| 2446 |
( _B1*(pts[pts] - pts[pts-1]) + |
| 2447 |
_B[pts-1]*(pts1 - pts[pts]) )/_tmp |
| 2448 |
fi; |
| 2449 |
else: |
| 2450 |
_d1 := origin; _d[pts] := origin; |
| 2451 |
fi |
| 2452 |
pts1 |
| 2453 |
for _j = 1 upto pts-1: |
| 2454 |
{if _d[_j] = origin: curl1 else: _d[_j] fi} |
| 2455 |
..tension atleast _tn..pts[_j+1] |
| 2456 |
endfor |
| 2457 |
{if _d[pts] = origin: curl1 else: _d[pts] fi} |
| 2458 |
if cyclic: ..tension atleast _tn..cycle fi |
| 2459 |
fi |
| 2460 |
enddef; |
| 2461 |
|
| 2462 |
numeric default_tension; default_tension := 1; |
| 2463 |
def curve = tcurve (default_tension) enddef; |
| 2464 |
vardef tcurve (expr tens, cyclic) (text t) = |
| 2465 |
setpairs (_tc) (t); |
| 2466 |
if _tc=0: NoPoints("curve", _tc); fi |
| 2467 |
mksmooth (tens, cyclic, _tc) |
| 2468 |
enddef; |
| 2469 |
|
| 2470 |
def ccurve = tccurve (default_tension) enddef; |
| 2471 |
vardef tccurve (expr tens, cyclic) (text t) = |
| 2472 |
setuniquepairs (_tcc) (t); |
| 2473 |
if _tcc=0: NoPoints("ccurve", _tcc); fi |
| 2474 |
mkconvex (tens, cyclic, _tcc) |
| 2475 |
enddef; |
| 2476 |
|
| 2477 |
vardef mkbezier (expr tens, cyclic) (suffix pts) = |
| 2478 |
settension (_tn) tens; fixtension (_tn); |
| 2479 |
pts1 |
| 2480 |
for _i = 2 upto pts: ..tension _tn..pts[_i] endfor |
| 2481 |
if cyclic: ..tension _tn..cycle else: {0,0} fi |
| 2482 |
enddef; |
| 2483 |
|
| 2484 |
def bezier = tbezier (default_tension) enddef; |
| 2485 |
vardef tbezier (expr tens, cyclic) (text t) = |
| 2486 |
setpairs (_tbs) (t); |
| 2487 |
if _tbs=0: NoPoints ("bezier", _tbs); fi |
| 2488 |
mkbezier (tens, cyclic) (_tbs) |
| 2489 |
enddef; |
| 2490 |
|
| 2491 |
vardef mkqbezier (expr cyclic) (suffix pts) = |
| 2492 |
pts1 |
| 2493 |
if pts=1: {0,0} |
| 2494 |
else: |
| 2495 |
for _i = 2 step 2 until pts - 1: |
| 2496 |
..controls 1/3[pts[_i], pts[_i-1]] |
| 2497 |
and 1/3[pts[_i], pts[_i+1]].. pts[_i+1] |
| 2498 |
endfor |
| 2499 |
if cyclic: |
| 2500 |
..controls 1/3[ pts[pts], pts[pts - 1] ] |
| 2501 |
and 1/3[ pts[pts], pts1 ]..cycle |
| 2502 |
fi |
| 2503 |
fi |
| 2504 |
enddef; |
| 2505 |
|
| 2506 |
vardef qbezier (expr cyclic) (text t) = |
| 2507 |
setpairs (_qbz) (t); |
| 2508 |
if _qbz=0: NoPoints ("qbezier", _qbz); |
| 2509 |
else: |
| 2510 |
if (cyclic and odd _qbz) or (not cyclic and even _qbz): |
| 2511 |
_qbz[incr _qbz] := _qbz[_qbz-1]; |
| 2512 |
fi |
| 2513 |
mkqbezier (cyclic) (_qbz) |
| 2514 |
fi |
| 2515 |
enddef; |
| 2516 |
|
| 2517 |
vardef mkcbezier (expr cyclic) (suffix pts) = |
| 2518 |
pts1 |
| 2519 |
if pts=1: {0,0} |
| 2520 |
else: |
| 2521 |
for _i = 1 step 3 until pts - 3: |
| 2522 |
..controls pts[_i+1] and pts[_i+2] .. pts[_i+3] |
| 2523 |
endfor |
| 2524 |
if cyclic: |
| 2525 |
..controls pts[pts - 1] and pts[pts]..cycle |
| 2526 |
fi |
| 2527 |
fi |
| 2528 |
enddef; |
| 2529 |
|
| 2530 |
vardef cbezier (expr cyclic) (text t) = |
| 2531 |
setpairs (_cbz) (t); |
| 2532 |
if _cbz=0: NoPoints ("qbezier", _cbz); |
| 2533 |
else: |
| 2534 |
% Need 0 mod 3 for cyclic, otherwise 1 mod 3 |
| 2535 |
setnumeric (_mdt) _cbz mod 3; |
| 2536 |
if cyclic: |
| 2537 |
if _mdt <> 0: _cbz[incr _cbz] := _cbz[_cbz-1]; fi |
| 2538 |
if _mdt = 1 : _cbz[incr _cbz] := _cbz1; fi |
| 2539 |
else: % need 1 more, duplicate next to last |
| 2540 |
if _mdt = 0: |
| 2541 |
_cbz := _cbz + 1; |
| 2542 |
_cbz[_cbz] := _cbz[_cbz-1]; |
| 2543 |
_cbz[_cbz-1] := _cbz[_cbz-2]; |
| 2544 |
fi |
| 2545 |
if _mdt = 2: % need 2 more, duplicate last 2. |
| 2546 |
_cbz := _cbz + 2; % add 2 slots |
| 2547 |
_cbz[_cbz] := _cbz[_cbz-2]; % fill them |
| 2548 |
_cbz[_cbz-1] := _cbz[_cbz-2]; % with last node |
| 2549 |
_cbz[_cbz-2] := _cbz[_cbz-3]; % orig last slot = orig previous. |
| 2550 |
fi |
| 2551 |
fi |
| 2552 |
mkcbezier (cyclic) (_cbz) |
| 2553 |
fi |
| 2554 |
enddef; |
| 2555 |
|
| 2556 |
vardef fcncontrol (expr ftens, X, Y, Z) = |
| 2557 |
Y if (xpart(Z-Y) <> 0) and (xpart(Y-X) <> 0): |
| 2558 |
+ xpart(Z-Y)/3/xpart(Z-X)*(Z - X)/ftens fi |
| 2559 |
enddef; |
| 2560 |
|
| 2561 |
vardef mkfcnpath (expr ftens) (suffix q) = |
| 2562 |
settension (_tn) ftens; |
| 2563 |
if _tn <= 0: _tn := 1; fi |
| 2564 |
for _i = 1 upto q - 1: |
| 2565 |
q[_i]..controls fcncontrol (_tn) (q[_i-1], q[_i], q[_i+1]) |
| 2566 |
and fcncontrol (_tn) (q[_i+2], q[_i+1], q[_i]).. |
| 2567 |
endfor |
| 2568 |
q[q]{0,0} |
| 2569 |
enddef; |
| 2570 |
|
| 2571 |
def fcncurve = functioncurve (default_tension) enddef; |
| 2572 |
def tfcncurve = functioncurve enddef; |
| 2573 |
vardef functioncurve (expr ftens) (text t) = |
| 2574 |
settension (_ftens) ftens; if _ftens < 1/3: _ftens := 1/3; fi |
| 2575 |
setuniquepairs (_fc) (t); |
| 2576 |
if _fc=0: NoPoints ("functioncurve", _fc); fi |
| 2577 |
if _fc > 1: _fc0 := _fc1; _fc[_fc+1] := _fc[_fc]; fi |
| 2578 |
mkfcnpath (_ftens) (_fc) |
| 2579 |
enddef; |
| 2580 |
|
| 2581 |
def openqbs = qspline (false) enddef; |
| 2582 |
def closedqbs = qspline (true) enddef; |
| 2583 |
|
| 2584 |
vardef mkqbs (suffix b) = |
| 2585 |
0.5[ b1, b2] |
| 2586 |
if b<3: {0,0} |
| 2587 |
else: |
| 2588 |
for _i = 2 upto b-1: |
| 2589 |
..controls 1/6[ b[_i], b[_i-1] ] and 1/6[ b[_i], b[_i+1] ].. |
| 2590 |
0.5[ b[_i], b[_i+1] ] |
| 2591 |
endfor |
| 2592 |
fi |
| 2593 |
enddef; |
| 2594 |
|
| 2595 |
vardef qspline (expr cyclic) (text t) = |
| 2596 |
setpairs (_qs) (t); |
| 2597 |
if _qs=0: NoPoints ("qspline", _qs); fi |
| 2598 |
if _qs=1: _qs[incr _qs] := _qs1; fi |
| 2599 |
if cyclic: |
| 2600 |
_qs[incr _qs] := _qs1; _qs[incr _qs] := _qs2; |
| 2601 |
fi |
| 2602 |
mkqbs (_qs) if cyclic: & cycle fi |
| 2603 |
enddef; |
| 2604 |
|
| 2605 |
vardef mkcbs (suffix b) = |
| 2606 |
(b[1]+4b[2]+b[3])/6 |
| 2607 |
if b < 4: {0,0} |
| 2608 |
else: |
| 2609 |
for _i = 3 upto b-1: |
| 2610 |
..controls 1/3[ b[_i-1], b[_i] ] and 1/3[ b[_i], b[_i-1] ] |
| 2611 |
.. (b[_i-1] + 4b[_i] + b[_i+1])/6 |
| 2612 |
endfor |
| 2613 |
fi |
| 2614 |
enddef; |
| 2615 |
|
| 2616 |
def mkopencbs = mkcbs enddef; |
| 2617 |
vardef mkclosedcbs (suffix b) = |
| 2618 |
mkcbs (b) & opencbs (b[b-2],b[b-1],b[b], b1, b2, b3) & cycle |
| 2619 |
enddef; |
| 2620 |
|
| 2621 |
def opencbs = cspline (false) enddef; |
| 2622 |
def closedcbs = cspline (true) enddef; |
| 2623 |
|
| 2624 |
vardef cspline (expr cyclic) (text t) = |
| 2625 |
setpairs (_cs) (t); |
| 2626 |
if _cs=0: NoPoints ("cspline", _cs); fi |
| 2627 |
for _idx = _cs upto 2: _cs[incr _cs] := _cs[_idx]; endfor |
| 2628 |
if cyclic: |
| 2629 |
for _idx = 1 upto 3: _cs[incr _cs] := _cs[_idx]; endfor |
| 2630 |
fi |
| 2631 |
mkcbs (_cs) if cyclic: & cycle fi |
| 2632 |
enddef; |
| 2633 |
|
| 2634 |
def init_spline_eqns (suffix pts) = |
| 2635 |
save _spl_pre, _spl_post; |
| 2636 |
pair _spl_pre[], _spl_post[]; |
| 2637 |
for j= 2 upto pts - 1: |
| 2638 |
_spl_post[j] + _spl_pre[j] = 2pts[j]; |
| 2639 |
_spl_pre[j+1]+2_spl_pre[j] = 2_spl_post[j]+_spl_post[j-1]; |
| 2640 |
endfor |
| 2641 |
enddef; |
| 2642 |
|
| 2643 |
def closed_spline_eqns (suffix pts) = |
| 2644 |
_spl_post1 + _spl_pre1 = 2pts1; |
| 2645 |
_spl_post[pts] + _spl_pre[pts] = 2pts[pts]; |
| 2646 |
_spl_pre2 + 2_spl_pre1 = 2_spl_post1 + _spl_post[pts]; |
| 2647 |
_spl_pre1+2_spl_pre[pts] = 2_spl_post[pts]+_spl_post[pts-1]; |
| 2648 |
enddef; |
| 2649 |
|
| 2650 |
def relaxed_spline_eqns (suffix pts) = |
| 2651 |
_spl_pre2 + pts1 = 2_spl_post1; |
| 2652 |
pts[pts] + _spl_post[pts-1] = 2_spl_pre[pts]; |
| 2653 |
enddef; |
| 2654 |
|
| 2655 |
vardef mksplinepath (expr closed) (suffix pts) = |
| 2656 |
pts1..controls _spl_post1 and |
| 2657 |
for j = 2 upto pts if not closed: -1 fi: |
| 2658 |
_spl_pre[j]..pts[j]..controls _spl_post[j] and |
| 2659 |
endfor |
| 2660 |
if closed: _spl_pre1..cycle else: _spl_pre[pts]..pts[pts] fi |
| 2661 |
enddef; |
| 2662 |
|
| 2663 |
def mkspline (expr closed) (suffix pts) = |
| 2664 |
init_spline_eqns (pts); |
| 2665 |
if closed: closed_spline_eqns (pts); |
| 2666 |
else: relaxed_spline_eqns (pts); |
| 2667 |
fi |
| 2668 |
mksplinepath (closed) (pts) |
| 2669 |
enddef; |
| 2670 |
|
| 2671 |
vardef dospline (expr closed) (text the_list) = |
| 2672 |
setpairs (_sp) (the_list); |
| 2673 |
if _sp=0: NoPoints ("dospline", _sp); fi |
| 2674 |
if _sp=1: _sp[incr _sp] := _sp1; fi |
| 2675 |
mkspline (closed) (_sp) |
| 2676 |
enddef; |
| 2677 |
|
| 2678 |
def init_fcnspl_eqns (suffix pts) = |
| 2679 |
save _dx, _sl; numeric _dx[], _sl[]; |
| 2680 |
_dx1 := xpart (pts2 - pts1); |
| 2681 |
for j = 2 upto pts - 1: |
| 2682 |
_dx[j] := xpart (pts[j+1] - pts[j]); |
| 2683 |
_sl[j + 1]*_dx[j] + _sl[j-1]*_dx[j-1] + 2_sl[j]*(_dx[j] + _dx[j-1]) |
| 2684 |
= 3*ypart(pts[j+1] - pts[j-1]); |
| 2685 |
endfor |
| 2686 |
enddef; |
| 2687 |
|
| 2688 |
def periodic_fcnspl_eqns (suffix pts) = |
| 2689 |
_sl1 = _sl[pts]; |
| 2690 |
_sl2*_dx1 + 2_sl1*_dx1 + 2_sl[pts]*_dx[pts-1] + _sl[pts-1]*_dx[pts-1] |
| 2691 |
= 3 * ypart (pts[2] - pts[pts-1]); |
| 2692 |
enddef; |
| 2693 |
|
| 2694 |
def relaxed_fcnspl_eqns (suffix pts) = |
| 2695 |
_sl2*_dx1 + 2_sl1*_dx1 = 3 * ypart(pts2 - pts1); |
| 2696 |
_sl[pts-1]*_dx[pts-1] + 2_sl[pts]*_dx[pts-1] |
| 2697 |
= 3 * ypart(pts[pts] - pts[pts-1]); |
| 2698 |
enddef; |
| 2699 |
|
| 2700 |
vardef mkfcnsplpath (suffix pts) = |
| 2701 |
pts1..controls (pts1 + (1, _sl1)/3*_dx1) and |
| 2702 |
for j = 2 upto pts - 1: |
| 2703 |
(pts[j] - (1, _sl[j])/3*_dx[j-1]) ..pts[j].. |
| 2704 |
controls (pts[j] + (1,_sl[j])/3*_dx[j]) and |
| 2705 |
endfor |
| 2706 |
(pts[pts] - (1,_sl[pts])*_dx[pts-1]/3)..pts[pts] |
| 2707 |
enddef; |
| 2708 |
|
| 2709 |
vardef mkfcnspline (expr periodic) (suffix pts) = |
| 2710 |
init_fcnspl_eqns (pts); |
| 2711 |
if periodic: periodic_fcnspl_eqns (pts); |
| 2712 |
else: relaxed_fcnspl_eqns (pts); |
| 2713 |
fi |
| 2714 |
mkfcnsplpath (pts) |
| 2715 |
enddef; |
| 2716 |
|
| 2717 |
vardef fcnspline (expr periodic) (text the_list) = |
| 2718 |
setpairs (_fs) (the_list); |
| 2719 |
if _fs<2: |
| 2720 |
if _fs=0: NoPoints ("fcnspline", _fs); fi |
| 2721 |
onepointpath (false, _fs1) |
| 2722 |
else: |
| 2723 |
mkfcnspline (periodic) (_fs) |
| 2724 |
fi |
| 2725 |
enddef; |
| 2726 |
|
| 2727 |
vardef mkarc (expr center, begpt, endpt, sweep) = |
| 2728 |
if (sweep = 0): begpt--endpt |
| 2729 |
else: |
| 2730 |
setnumeric (n) ceiling (abs(sweep)/45); |
| 2731 |
setpair (d) (begpt - center) rotated (signof (sweep) 90); |
| 2732 |
begpt{d} |
| 2733 |
for j = 1 upto n-1: |
| 2734 |
..(begpt rotatedabout (center, j/n*sweep)){d rotated (j/n*sweep)} |
| 2735 |
endfor ..endpt{d rotated sweep} |
| 2736 |
fi |
| 2737 |
enddef; |
| 2738 |
|
| 2739 |
vardef arc (expr center, begpt, sweep) = |
| 2740 |
if (center = begpt) or (sweep = 0): begpt--begpt |
| 2741 |
else: |
| 2742 |
mkarc (center, begpt, begpt rotatedabout (center, sweep), sweep) |
| 2743 |
fi |
| 2744 |
enddef; |
| 2745 |
def arccps = arc enddef; |
| 2746 |
|
| 2747 |
vardef arcpps (expr begpt, endpt, sweep) = |
| 2748 |
if (begpt = endpt) or (sweep = 0): begpt--endpt |
| 2749 |
else: |
| 2750 |
setpair (cd) unitvector (endpt-begpt); |
| 2751 |
if abs(sweep) <= 45: |
| 2752 |
begpt{cd rotated (-sweep/2)}..endpt{cd rotated (sweep/2)} |
| 2753 |
elseif abs(sweep) <= 90: |
| 2754 |
save m; pair m; |
| 2755 |
m = begpt + whatever*( cd rotated (-sweep/4)); |
| 2756 |
m = 0.5[begpt, endpt] + whatever*(cd rotated 90); |
| 2757 |
begpt{cd rotated (-sweep/2)}..m{cd}..endpt{cd rotated (sweep/2)} |
| 2758 |
else: |
| 2759 |
setnumeric (ang) 90 - ((sweep/2) mod 180); |
| 2760 |
if abs(ang) = 90: |
| 2761 |
GBwarn "undefined arc. A line segment will be used instead."; |
| 2762 |
begpt--endpt |
| 2763 |
else: |
| 2764 |
save c; pair c; |
| 2765 |
c = begpt + whatever*(cd rotated ang); |
| 2766 |
c = if abs(ang) < 30: |
| 2767 |
(0.5)[begpt, endpt] + whatever*(cd rotated 90) |
| 2768 |
else: |
| 2769 |
endpt + whatever*(-cd rotated -ang) |
| 2770 |
fi; |
| 2771 |
mkarc (c, begpt, endpt, sweep) |
| 2772 |
fi |
| 2773 |
fi |
| 2774 |
fi |
| 2775 |
enddef; |
| 2776 |
|
| 2777 |
vardef arcpp (expr small, begpt, endpt, rad) = |
| 2778 |
save full, diam, chord, ang; |
| 2779 |
full := signof (rad) 360; |
| 2780 |
diam := 2rad; |
| 2781 |
chord := abs(endpt-begpt); |
| 2782 |
if chord < abs(diam): |
| 2783 |
ang := if not small: full - fi 2*asin (chord/diam); |
| 2784 |
else: ang := signof (rad) 180; |
| 2785 |
fi |
| 2786 |
arcpps (begpt, endpt, ang) |
| 2787 |
enddef; |
| 2788 |
def arcppr (expr begpt, endpt, rad, small) = |
| 2789 |
arcpp (small, begpt, endpt, rad) |
| 2790 |
enddef; |
| 2791 |
|
| 2792 |
vardef arcplr (expr center, frtheta, totheta, rad) = |
| 2793 |
if rad = 0: center--center |
| 2794 |
else: |
| 2795 |
mkarc (center, center + rad*dir frtheta, |
| 2796 |
center + rad*dir totheta, totheta - frtheta) |
| 2797 |
fi |
| 2798 |
enddef; |
| 2799 |
|
| 2800 |
vardef arcalt (expr center, radius, frtheta, totheta) = |
| 2801 |
arcplr (center, frtheta, totheta, radius) |
| 2802 |
enddef; |
| 2803 |
|
| 2804 |
vardef arcppp (expr first, second, third) = |
| 2805 |
arcpps (first, second, 2*cornerangle (third, first, second)) & |
| 2806 |
arcpps (second, third, 2*cornerangle (first, second, third)) |
| 2807 |
enddef; |
| 2808 |
|
| 2809 |
vardef ellipse (expr center, radx, rady, angle) = |
| 2810 |
fullcircle xscaled (2*radx) yscaled (2*rady) rotated angle |
| 2811 |
shifted center |
| 2812 |
enddef; |
| 2813 |
|
| 2814 |
vardef circle (expr center, rad) = |
| 2815 |
fullcircle scaled (2*rad) shifted center |
| 2816 |
enddef; |
| 2817 |
|
| 2818 |
vardef circlecp (expr center, point) = |
| 2819 |
mkarc (center, point, point, 360) & cycle |
| 2820 |
enddef; |
| 2821 |
|
| 2822 |
vardef circleppp (expr one, two, three) = |
| 2823 |
arcpps (one, two, 2*cornerangle (three, one, two)) |
| 2824 |
& arcpps (two, three, 2*cornerangle (one, two, three)) |
| 2825 |
& arcpps (three, one, 2*cornerangle (two, three, one)) |
| 2826 |
& cycle |
| 2827 |
enddef; |
| 2828 |
|
| 2829 |
vardef circlepps (expr one, two, sweep) = |
| 2830 |
save ang, full; |
| 2831 |
full := signof (sweep) 360; |
| 2832 |
ang := sweep mod full; |
| 2833 |
arcpps (one, two, ang) & arcpps (two, one, full - ang) & cycle |
| 2834 |
enddef; |
| 2835 |
|
| 2836 |
vardef circlepp (expr small, one, two, rad) = |
| 2837 |
arcpp (small, one, two, rad) & arcpp (not small, two, one, rad) |
| 2838 |
& cycle |
| 2839 |
enddef; |
| 2840 |
|
| 2841 |
def circleppr (expr one, two, rad, small) = |
| 2842 |
circleppr (one, two, rad, small) |
| 2843 |
enddef; |
| 2844 |
|
| 2845 |
vardef quarterellipse(expr A,B,C) = |
| 2846 |
save T_; |
| 2847 |
transform T_; |
| 2848 |
(1,0) transformed T_ = A; |
| 2849 |
(1,1) transformed T_ = B; |
| 2850 |
(0,1) transformed T_ = C; |
| 2851 |
quartercircle scaled 2 transformed T_ |
| 2852 |
enddef; |
| 2853 |
|
| 2854 |
vardef halfellipse (expr A,B,C) = |
| 2855 |
save P_; pair P_; |
| 2856 |
P_ = (C - A)/2; |
| 2857 |
quarterellipse (A, B - P_, B) & quarterellipse (B, B + P_, C) |
| 2858 |
enddef; |
| 2859 |
|
| 2860 |
vardef fullellipse (expr C, A, B) = |
| 2861 |
save P_; pair P_; |
| 2862 |
P_ := 2[A,C]; |
| 2863 |
halfellipse (A,B,P_) & halfellipse (P_,2[B,C],A) & cycle |
| 2864 |
enddef; |
| 2865 |
|
| 2866 |
vardef pathcenter expr p = |
| 2867 |
save a, cntr, n; pair cntr, a[]; |
| 2868 |
n := length p; |
| 2869 |
a1 = pnt 0 (p); |
| 2870 |
a3 = pnt [n/2] (p); |
| 2871 |
if cycle p: |
| 2872 |
a2 = pnt [ n/4] (p); |
| 2873 |
a4 = pnt [3n/4] (p); |
| 2874 |
else: |
| 2875 |
a2 := a3; |
| 2876 |
a4 := pnt[n] (p); |
| 2877 |
fi |
| 2878 |
cntr = .5[a1, a3] + whatever*((a3 - a1) rotated 90); |
| 2879 |
cntr = .5[a2, a4] + whatever*((a4 - a2) rotated 90); |
| 2880 |
cntr |
| 2881 |
enddef; |
| 2882 |
|
| 2883 |
vardef circumcircle expr t = |
| 2884 |
circleppp (pnt0 (t), pnt1 (t), pnt2 (t)) |
| 2885 |
enddef; |
| 2886 |
|
| 2887 |
vardef incircle expr t = |
| 2888 |
save A, B, C; pair A, B, C; |
| 2889 |
A := pnt0 (t); |
| 2890 |
B := pnt1 (t); |
| 2891 |
C := pnt2 (t); |
| 2892 |
save a, b, c, D, E, F; |
| 2893 |
D := abs (B-A) = a + b; |
| 2894 |
E := abs (C-B) = b + c; |
| 2895 |
F := abs (A-C) = a + c; |
| 2896 |
circleppp ((a/D)[A,B], (b/E)[B,C], (c/F)[C,A]) |
| 2897 |
enddef; |
| 2898 |
|
| 2899 |
vardef excircle expr n of t = |
| 2900 |
save A, B, C; pair A, B, C; |
| 2901 |
A := pnt[n] (t); |
| 2902 |
B := pnt[n + 1] (t); |
| 2903 |
C := pnt[n + 2] (t); |
| 2904 |
save a, b, c, D, E, F; |
| 2905 |
D := abs (B-A) = a - b; |
| 2906 |
E := abs (C-B) = b + c; |
| 2907 |
F := abs (C-A) = a - c; |
| 2908 |
circleppp ((a/D)[A,B], (b/E)[B,C], (c/F)[A,C]) |
| 2909 |
enddef; |
| 2910 |
|
| 2911 |
vardef ninepointcircle expr t = |
| 2912 |
circleppp (medianpt 0 of t, medianpt 1 of t, medianpt 2 of t) |
| 2913 |
enddef; |
| 2914 |
|
| 2915 |
vardef pshcircle (expr disk, ctr, rad) = |
| 2916 |
if disk: |
| 2917 |
if rad >= 1 : |
| 2918 |
if rad > 1: |
| 2919 |
GBerrmsg ("Impossible radius of pseudohyperbolic circle.") |
| 2920 |
"The radius of a pseudohyperbolic circle can be at most 1."; |
| 2921 |
fi |
| 2922 |
circle ((0,0),1) |
| 2923 |
elseif abs(ctr) >= 1 : |
| 2924 |
if abs(ctr) > 1: |
| 2925 |
GBerrmsg ("Impossible center of pseudohyperbolic circle.") |
| 2926 |
"The center of this pseudohyperbolic circle must be in " |
| 2927 |
& "the unit disk."; |
| 2928 |
fi |
| 2929 |
onepointpath (true,ctr) |
| 2930 |
else: |
| 2931 |
save _r, _dnm; |
| 2932 |
_r := abs(ctr); |
| 2933 |
_dnm := 1 - _r*_r*rad*rad; |
| 2934 |
circle ((1 - rad*rad)/_dnm*ctr, rad*(1 - _r*_r)/_dnm) |
| 2935 |
fi |
| 2936 |
else: |
| 2937 |
if rad >= 1 : |
| 2938 |
GBerrmsg ("Impossible radius of pseudohyperbolic circle.") |
| 2939 |
"The radius of a pseudohyperbolic circle must be less than 1."; |
| 2940 |
onepointpath (true,ctr) |
| 2941 |
elseif ypart ctr <= 0: |
| 2942 |
if ypart ctr < 0: |
| 2943 |
GBerrmsg ("Impossible center of pseudohyperbolic circle.") |
| 2944 |
"The center of this pseudohyperbolic circle must be in " |
| 2945 |
& "the upper half-plane."; |
| 2946 |
fi |
| 2947 |
onepointpath (true,ctr) |
| 2948 |
else: |
| 2949 |
save _y, _dnm; |
| 2950 |
_y := ypart ctr; |
| 2951 |
_dnm := 1 - rad*rad; |
| 2952 |
circle ((xpart ctr, (1 + rad*rad)/_dnm * _y), 2rad/_dnm*_y) |
| 2953 |
fi |
| 2954 |
fi |
| 2955 |
enddef; |
| 2956 |
|
| 2957 |
vardef UHPgeodesic (expr A, B) = |
| 2958 |
if xpart A = xpart B: |
| 2959 |
A--B |
| 2960 |
else: |
| 2961 |
save ang_, C_; pair C_; |
| 2962 |
if abs(ypart A) < abs(ypart B): |
| 2963 |
C_ := conj B; |
| 2964 |
else: |
| 2965 |
C_ := conj A; |
| 2966 |
fi |
| 2967 |
if ypart C_ = 0: % both on x-axis |
| 2968 |
ang_ := anglefromto(up, B - A); |
| 2969 |
else: |
| 2970 |
ang_ := anglefromto(A - C_, B - C_); |
| 2971 |
fi |
| 2972 |
arcpps(A, B, 2ang_) |
| 2973 |
fi |
| 2974 |
enddef; |
| 2975 |
|
| 2976 |
vardef UDgeodesic (expr A, B) = |
| 2977 |
save a_, b_; |
| 2978 |
a_ := abs(A); b_ = abs(B); |
| 2979 |
if (a_ = 0) or (b_ = 0): |
| 2980 |
A--B |
| 2981 |
elseif angle A = angle B: |
| 2982 |
A--B |
| 2983 |
else: % note: A, B and B-A are all nonzero from this point |
| 2984 |
save ang_; |
| 2985 |
if a_ = 1: |
| 2986 |
ang_ := anglefromto (if b_>1: A else: -A fi, B-A) |
| 2987 |
elseif b_ = 1: |
| 2988 |
ang_ := anglefromto (A-B, if a_>1: B else: -B fi) |
| 2989 |
else: |
| 2990 |
save C_; pair C_; |
| 2991 |
% reflecting A |
| 2992 |
if a_ < eps: |
| 2993 |
C_ := unitvector A; |
| 2994 |
ang_1 := anglefromto(a_*A - C_, a_*B - C_); |
| 2995 |
else: |
| 2996 |
C_ := (1/a_)*unitvector A; |
| 2997 |
ang_1 := anglefromto(A - C_, B - C_); |
| 2998 |
fi |
| 2999 |
% reflecting B |
| 3000 |
if b_ < eps: |
| 3001 |
C_ := unitvector B; |
| 3002 |
ang_2 := anglefromto(b_*A - C_, b_*B - C_); |
| 3003 |
else: |
| 3004 |
C_ := (1/b_)*unitvector B; |
| 3005 |
ang_2 := anglefromto(A - C_, B - C_); |
| 3006 |
fi |
| 3007 |
ang_ := if abs(ang_1) < abs(ang_2): ang_1 else: ang_2 fi; |
| 3008 |
fi |
| 3009 |
arcpps(A, B, 2ang_) |
| 3010 |
fi |
| 3011 |
enddef; |
| 3012 |
|
| 3013 |
vardef barycenter expr t = |
| 3014 |
save m; m := length t if not cycle t: + 1 fi; |
| 3015 |
pnt0(t)/m for k = 1 upto m - 1: + pnt[k](t)/m endfor |
| 3016 |
enddef; |
| 3017 |
|
| 3018 |
vardef sector (expr center, rad, frtheta, totheta) = |
| 3019 |
center -- arcalt (center, rad, frtheta, totheta) -- cycle |
| 3020 |
enddef; |
| 3021 |
|
| 3022 |
vardef mkbrace (expr S, C, E) = |
| 3023 |
save R_, U_, V_, Z_; |
| 3024 |
pair U_, V_, Z_[]; |
| 3025 |
U_ := unitvector (E-S); |
| 3026 |
V_ := U_ rotated 90; |
| 3027 |
|
| 3028 |
R_ := 0.5*(C-S) dotprod V_; |
| 3029 |
if R_ = 0: |
| 3030 |
S--C |
| 3031 |
else: |
| 3032 |
if R_ < 0 : V_ := -V_; R_ := -R_; fi |
| 3033 |
V_ := R_*V_; U_ := R_*U_; |
| 3034 |
Z_1 := S + V_ + U_; |
| 3035 |
Z_2 := C - V_ - U_; |
| 3036 |
Z_3 := C - V_ + U_; |
| 3037 |
Z_4 := E + V_ - U_; |
| 3038 |
S{V_}..{U_}Z_1--Z_2{U_}..{V_}C{-V_}..{U_}Z_3--Z_4{U_}..{-V_}E |
| 3039 |
fi |
| 3040 |
enddef; |
| 3041 |
|
| 3042 |
vardef mkfcn (expr sm, tens) (expr bmin, bmax, bst) (text pf) = |
| 3043 |
save _p; pair _p[]; _p := 0; |
| 3044 |
save _dx, _n, _r; numeric _dx, _n, _r; |
| 3045 |
if bmax = bmin: _n := 1; |
| 3046 |
else: |
| 3047 |
_r := bmax - bmin; |
| 3048 |
_dx := max (abs(bst), nottoosmall*abs(_r), epsilon); |
| 3049 |
_n := emax (round(abs(_r)/_dx), 1); |
| 3050 |
fi |
| 3051 |
for _i = 0 upto _n: _p[incr _p] := pf(bmin + _i/_n*_r); endfor |
| 3052 |
mkpath (sm, tens, false, _p) |
| 3053 |
enddef; |
| 3054 |
|
| 3055 |
def tfcn (expr sm) = mkfcn (sm, default_tension) enddef; |
| 3056 |
|
| 3057 |
def parafcn (expr sm) = tparafcn (sm, default_tension) enddef; |
| 3058 |
vardef tparafcn (expr sm, tn) (expr bmin, bmax, bst) (text pf) = |
| 3059 |
save _fp; vardef _fp (expr t) = pf enddef; |
| 3060 |
mkfcn (sm, tn) (bmin, bmax, bst) (_fp) |
| 3061 |
enddef; |
| 3062 |
|
| 3063 |
vardef xfcn (expr sm) (expr xmin, xmax, st) (text _fx) = |
| 3064 |
save _fp; vardef _fp (expr _x) = (_x, _fx(_x)) enddef; |
| 3065 |
mkfcn (sm, default_tension) (xmin, xmax, st) (_fp) |
| 3066 |
enddef; |
| 3067 |
|
| 3068 |
def function (expr sm) = tfunction (sm, default_tension) enddef; |
| 3069 |
vardef tfunction (expr sm, tens, xmin, xmax, st) (text _fx) = |
| 3070 |
save _fp; vardef _fp (expr x) = (x, _fx) enddef; |
| 3071 |
mkfcn (sm, tens) (xmin, xmax, st) (_fp) |
| 3072 |
enddef; |
| 3073 |
|
| 3074 |
def btwnfcn (expr sm) = tbtwnfcn (sm, default_tension) enddef; |
| 3075 |
vardef tbtwnfcn (expr sm, tn, xlo, xhi, st)(text _fx)(text _gx) = |
| 3076 |
tfunction (sm, tn) (xlo, xhi, st) (_fx) -- |
| 3077 |
( reverse tfunction (sm, tn) (xlo, xhi, st) (_gx) ) -- cycle |
| 3078 |
enddef; |
| 3079 |
|
| 3080 |
def belowfcn (expr sm) = tbelowfcn (sm, default_tension) enddef; |
| 3081 |
vardef tbelowfcn (expr sm, tn, xlo, xhi, st)(text _fx) = |
| 3082 |
(xlo,0)--(xhi,0)-- |
| 3083 |
(reverse tfunction (sm, tn, xlo, xhi, st)(_fx))--cycle |
| 3084 |
enddef; |
| 3085 |
|
| 3086 |
vardef rfcn (expr sm, tmin, tmax, st) (text ft) = |
| 3087 |
save _fq; vardef _fq (expr t) = (ft(t)) * (dir t) enddef; |
| 3088 |
mkfcn (sm, default_tension) (tmin, tmax, st) (_fq) |
| 3089 |
enddef; |
| 3090 |
|
| 3091 |
def plrfcn (expr sm) = tplrfcn (sm, default_tension) enddef; |
| 3092 |
vardef tplrfcn (expr sm, tens, tmin, tmax, st) (text ft) = |
| 3093 |
save _fq; vardef _fq (expr t) = (ft) * (dir t) enddef; |
| 3094 |
mkfcn (sm, tens) (tmin, tmax, st) (_fq) |
| 3095 |
enddef; |
| 3096 |
|
| 3097 |
def btwnplrfcn (expr sm) = tbtwnplrfcn (sm, default_tension) enddef; |
| 3098 |
vardef tbtwnplrfcn (expr sm, tn, tlo, thi, st)(text _ft)(text _gt)= |
| 3099 |
tplrfcn (sm, tn, tlo, thi, st) (_ft) -- |
| 3100 |
( reverse tplrfcn (sm, tn, tlo, thi, st) (_gt) ) -- cycle |
| 3101 |
enddef; |
| 3102 |
|
| 3103 |
def plrregion (expr sm) = tplrregion (sm, default_tension) enddef; |
| 3104 |
vardef tplrregion (expr sm, tn, tlo, thi, st) (text _ft) = |
| 3105 |
(0,0)--tplrfcn (sm, tn, tlo, thi, st ) (_ft)--cycle |
| 3106 |
enddef; |
| 3107 |
|
| 3108 |
numeric tolerancefactor; |
| 3109 |
tolerancefactor := .02; |
| 3110 |
|
| 3111 |
vardef mklevelset (expr sm, tens, X, Y, t, a, b, c, d) = |
| 3112 |
save _inside_; |
| 3113 |
vardef _inside_ (expr U, V) = |
| 3114 |
inside_levelset(U, V) and between(a, b)(U) and between(c, d)(V) |
| 3115 |
enddef; |
| 3116 |
if not _inside_ (X, Y): |
| 3117 |
GBwarn "Invalid seed point for levelset."; |
| 3118 |
pairmax((a,c), pairmin((X,Y), (b,d)))&cycle |
| 3119 |
else: |
| 3120 |
save ls, W, A, B, prev, curr, seed; |
| 3121 |
pair ls[], prev, curr, seed; |
| 3122 |
seed := (X,Y); |
| 3123 |
ls := 0; W := 0; |
| 3124 |
|
| 3125 |
save _first_, _next_, get_next; |
| 3126 |
vardef _first_ (expr U) = _inside_ (U, Y) enddef; |
| 3127 |
vardef _next_ (expr ang) = |
| 3128 |
_inside_ (X_curr + t * cosd ang, Y_curr + t * sind ang) |
| 3129 |
enddef; |
| 3130 |
def get_next (expr angA, angB) = |
| 3131 |
X_curr := xpart curr; Y_curr := ypart curr; |
| 3132 |
ls[incr ls] := curr + t * dir (solve _next_ (angA, angB)); |
| 3133 |
prev := curr; curr := ls[ls]; |
| 3134 |
W := W + anglefromto (prev - seed, curr - seed); |
| 3135 |
enddef; |
| 3136 |
|
| 3137 |
interim tolerance := t*tolerancefactor; |
| 3138 |
ls[incr ls] := (solve _first_ (X, b), Y); |
| 3139 |
curr := ls[ls]; |
| 3140 |
interim tolerance := radian*tolerancefactor; |
| 3141 |
get_next (180, 0); |
| 3142 |
for n = 3 upto max_points: |
| 3143 |
A := angle (curr - prev); |
| 3144 |
get_next (A + 120, A - 120); |
| 3145 |
exitif ((abs(W) > 180) or (ls > 10)) |
| 3146 |
and (abs(ls[ls] - ls1) < 1.2t); |
| 3147 |
endfor |
| 3148 |
mkpath (sm, tens, true) (ls) |
| 3149 |
fi |
| 3150 |
enddef; |
| 3151 |
|
| 3152 |
numeric max_points; |
| 3153 |
max_points := 2000; |
| 3154 |
|
| 3155 |
def levelset (expr s) = tlevelset (s, default_tension) enddef; |
| 3156 |
vardef tlevelset (expr smth, tens, seed, seg) (text cond) = |
| 3157 |
save inside_levelset, _t; |
| 3158 |
vardef inside_levelset (expr x, y) = cond enddef; |
| 3159 |
_t := if seg <= 0: emax (xpos-xneg, ypos-yneg)/max_points * 20 |
| 3160 |
else: seg fi; |
| 3161 |
mklevelset (smth, tens, xpart seed, ypart seed, _t) |
| 3162 |
(xneg, xpos, yneg, ypos) |
| 3163 |
enddef; |
| 3164 |
|
| 3165 |
def RKIV (expr sm) = tRKIV (sm, default_tension) |
| 3166 |
enddef; |
| 3167 |
vardef tRKIV (expr sm, tens, zstart, ds, N) (text _RHS_) = |
| 3168 |
save _trj, _ztr, _dz, _ztmp, _ctm; |
| 3169 |
pair _trj[], % The trajectory |
| 3170 |
_ztr, % current point |
| 3171 |
_dz[], % array[4] of displacements |
| 3172 |
_ztmp; % current point for calculating velocity |
| 3173 |
_trj := N+1; % ultimate size of _trj array |
| 3174 |
_trj1 := _ztr := zstart; |
| 3175 |
save _tt, % current time |
| 3176 |
_dt, % current time step |
| 3177 |
_th; % current time plus half a step |
| 3178 |
_tt := 0; |
| 3179 |
for _idx := 2 upto _trj: |
| 3180 |
_dt := ds/emax(1,abs(_RHS_(_tt,_ztr))); |
| 3181 |
_th := _tt + .5_dt; |
| 3182 |
_dz1 := _dt*_RHS_(_tt, _ztr); % displacement for current point |
| 3183 |
_ztmp := _ztr + .5_dz1; % 1st midpoint |
| 3184 |
% use _th instead of twice calculating (_tt + .5_dt) |
| 3185 |
_dz2 := _dt*_RHS_(_th, _ztmp); % displacement for 1st midpoint |
| 3186 |
_ztmp := _ztr + .5_dz2; % 2nd midpoint |
| 3187 |
_dz3 := _dt*_RHS_(_th, _ztmp); % displacement for 2nd midpoint |
| 3188 |
_ztmp := _ztr + _dz3; % temporary end point |
| 3189 |
% get time for next loop now since we need it in the next line: |
| 3190 |
_tt := _tt + _dt; |
| 3191 |
_dz4 := _dt*_RHS_(_tt, _ztmp); % displacement for end point |
| 3192 |
% get next point |
| 3193 |
_ztr := _ztr + (_dz1 + 2_dz2 + 2_dz3 + _dz4)/6; |
| 3194 |
_trj[_idx] := _ztr; |
| 3195 |
endfor |
| 3196 |
mkpath (sm, tens, false, _trj) |
| 3197 |
enddef; |
| 3198 |
|
| 3199 |
def xyRKIV (expr sm) = txyRKIV (sm, default_tension) |
| 3200 |
enddef; |
| 3201 |
vardef txyRKIV (expr sm, tens, zstart, ds, N) (text _RHS_) = |
| 3202 |
save _fgxy, __fgxy; |
| 3203 |
vardef __fgxy (expr t, x, y) = _RHS_ enddef; |
| 3204 |
vardef _fgxy (expr t, Z) = __fgxy(t, xpart Z, ypart Z) enddef; |
| 3205 |
tRKIV (sm, tens, zstart, ds, N) (_fgxy) |
| 3206 |
enddef; |
| 3207 |
|
| 3208 |
def odeRKIV (expr sm) = todeRKIV (sm, default_tension) |
| 3209 |
enddef; |
| 3210 |
vardef todeRKIV (expr sm, tens, xstart, ystart, ds, N) |
| 3211 |
(text _fxy) = |
| 3212 |
txyRKIV (sm, tens, (xstart, ystart), ds, N) ((1, _fxy)) |
| 3213 |
enddef; |
| 3214 |
|
| 3215 |
vardef lclosed expr f = |
| 3216 |
f |
| 3217 |
if not cycle f: |
| 3218 |
if pnt0(f) = pnt[infinity](f): & else: -- fi cycle |
| 3219 |
fi |
| 3220 |
enddef; |
| 3221 |
|
| 3222 |
def sclosed = sclosedt (default_tension) enddef; |
| 3223 |
vardef sclosedt (expr t) expr f = |
| 3224 |
if cycle f: f |
| 3225 |
else: save n; n := length f; |
| 3226 |
if n = 0: f&cycle |
| 3227 |
elseif n = 1: pnt0(f)..tension t..pnt1(f)..tension t..cycle |
| 3228 |
else: |
| 3229 |
(pnt0 (f)) { (pnt1(f)) - (pnt[n] (f)) }..tension t |
| 3230 |
..(subpath (1, n-1) of f)..tension t |
| 3231 |
..(pnt[n](f)) { pnt0(f) - pnt[n-1](f) } |
| 3232 |
..tension t..cycle |
| 3233 |
fi |
| 3234 |
fi |
| 3235 |
enddef; |
| 3236 |
|
| 3237 |
def bclosed = bclosedt (default_tension) enddef; |
| 3238 |
vardef bclosedt (expr t) expr f = |
| 3239 |
f |
| 3240 |
if not cycle f: |
| 3241 |
if pnt0(f) = pnt[infinity](f): & else: ..tension t.. fi cycle |
| 3242 |
fi |
| 3243 |
enddef; |
| 3244 |
|
| 3245 |
def uclosed = bclosed enddef; |
| 3246 |
def uclosedt = bclosedt enddef; |
| 3247 |
|
| 3248 |
def cbcontrols (suffix b, t) = |
| 3249 |
b1 := 2[t3, t2]; |
| 3250 |
b2 := 2[t2, t1]; |
| 3251 |
b3 := 2[b1, b2]; |
| 3252 |
b4 := 2[b2, b3]; |
| 3253 |
enddef; |
| 3254 |
|
| 3255 |
vardef cbclosed expr f = |
| 3256 |
save n; n := length f; |
| 3257 |
if cycle f: f |
| 3258 |
elseif n = 0: f&cycle |
| 3259 |
else: |
| 3260 |
save p, q, t; pair p[], q[], t[]; |
| 3261 |
t1 := pnt0(f); t2 := post0(f); t3 := pre1(f); |
| 3262 |
cbcontrols (p, t); % defines p1 to p4 |
| 3263 |
t1 := pnt[n](f); t2 := pre[n](f); t3 := post[n-1](f); |
| 3264 |
cbcontrols (q, t); % defines q1 to q4 |
| 3265 |
f..controls q2 and q3..opencbs (q1,q4,p4,p1) |
| 3266 |
..controls p3 and p2..cycle |
| 3267 |
fi |
| 3268 |
enddef; |
| 3269 |
|
| 3270 |
vardef qbclosed expr f = |
| 3271 |
if cycle f: f |
| 3272 |
else: save n; n := length f; |
| 3273 |
if n = 0: f&cycle |
| 3274 |
else: |
| 3275 |
save p; pair p[]; p := 4; |
| 3276 |
p1 := (3/2)[pnt[n](f), pre[n](f)]; |
| 3277 |
p2 := 2[p1, pnt[n](f)]; |
| 3278 |
p4 := (3/2)[pnt 0 (f), post0 (f)]; |
| 3279 |
p3 := 2[p4, pnt 0 (f)]; |
| 3280 |
f & mkqbs (p) & cycle |
| 3281 |
fi |
| 3282 |
fi |
| 3283 |
enddef; |
| 3284 |
|
| 3285 |
vardef makesector expr p = (pathcenter p)--p--cycle enddef; |
| 3286 |
|
| 3287 |
vardef arccomplement expr p = |
| 3288 |
if cycle p: onepointpath (false, pnt0(p)) |
| 3289 |
else: |
| 3290 |
setnumeric (nn) length p; |
| 3291 |
setpairs (pp) (pnt0(p), pnt[.5nn](p), pnt[nn](p)); |
| 3292 |
arcpps (pp3,pp1,2*cornerangle(pp2,pp3,pp1)) |
| 3293 |
fi |
| 3294 |
enddef; |
| 3295 |
|
| 3296 |
path cuttings; |
| 3297 |
vardef cutoffbefore (expr b) expr f = |
| 3298 |
save t, n; n := length f; |
| 3299 |
if n > 0: |
| 3300 |
for k = 1 upto n: |
| 3301 |
exitif (subpath (0,k) of f) intersects b; |
| 3302 |
endfor |
| 3303 |
if _Xtime < 0: |
| 3304 |
cuttings := pnt0 (f){0,0}; |
| 3305 |
f |
| 3306 |
else: |
| 3307 |
cuttings := subpath (0,_Xtime) of f; |
| 3308 |
subpath (_Xtime, n) of f |
| 3309 |
fi |
| 3310 |
else: f |
| 3311 |
fi |
| 3312 |
enddef; |
| 3313 |
|
| 3314 |
vardef cutoffafter (expr b) expr f = |
| 3315 |
setpath (g) cutoffbefore (b) reverse f; |
| 3316 |
cuttings := reverse cuttings; |
| 3317 |
reverse g |
| 3318 |
enddef; |
| 3319 |
|
| 3320 |
vardef trimmedpath (expr btrim, etrim) expr f = |
| 3321 |
save g, h; path g, h; |
| 3322 |
g := invvconv (fullcircle scaled 2btrim) shifted pnt0(f); |
| 3323 |
h := invvconv (fullcircle scaled 2etrim) shifted pnt[length f] (f); |
| 3324 |
cutoffafter (h) cutoffbefore (g) f |
| 3325 |
enddef; |
| 3326 |
|
| 3327 |
vardef predirection@# (expr p) = |
| 3328 |
- postdirection[length p - @#] (reverse p) |
| 3329 |
enddef; |
| 3330 |
|
| 3331 |
vardef postdirection@# (expr p) = |
| 3332 |
save _n; _n := length (p); |
| 3333 |
setpair (v) __dir (subpath (@#, @# + _n) of p); |
| 3334 |
if v = origin: |
| 3335 |
v := - __dir (subpath (@#, @# - _n) of p); |
| 3336 |
fi |
| 3337 |
v |
| 3338 |
enddef; |
| 3339 |
|
| 3340 |
vardef __dir (expr p) = |
| 3341 |
save v, w; pair v, w; w := pnt0 (p); |
| 3342 |
v := origin; |
| 3343 |
for n = 1 upto length (p): |
| 3344 |
v := post[n-1] (p) - w; exitif v <> origin; |
| 3345 |
v := pre [ n ] (p) - w; exitif v <> origin; |
| 3346 |
v := pnt [ n ] (p) - w; exitif v <> origin; |
| 3347 |
endfor |
| 3348 |
sgn v |
| 3349 |
enddef; |
| 3350 |
|
| 3351 |
vardef trivial expr p = (__dir (p) = origin) enddef; |
| 3352 |
|
| 3353 |
newinternal hdwdr, hdten; |
| 3354 |
boolean hfilled; |
| 3355 |
|
| 3356 |
def headshape (expr wr, tens, fil) = |
| 3357 |
interim hdwdr := wr; |
| 3358 |
interim hdten := if tens>0: tens else: default_tension fi; |
| 3359 |
if hdten < .75: hdten := .75; fi |
| 3360 |
setboolean (hfilled) fil; |
| 3361 |
mkheadpaths; |
| 3362 |
enddef; |
| 3363 |
def mkheadpaths = |
| 3364 |
save Arrowhead, Leftharpoon, Rightharpoon; |
| 3365 |
path Arrowhead, Leftharpoon, Rightharpoon, |
| 3366 |
Arrowhead.clear, Leftharpoon.clear, Rightharpoon.clear; |
| 3367 |
Rightharpoon := (0,0){down}..tension hdten..(.5hdwdr,-1); |
| 3368 |
Rightharpoon.clear := Rightharpoon--(.5hdwdr,0)--cycle; |
| 3369 |
Leftharpoon := (reverse Rightharpoon) xscaled -1; |
| 3370 |
Leftharpoon.clear := (reverse Rightharpoon.clear) xscaled -1; |
| 3371 |
Arrowhead := Leftharpoon & Rightharpoon; |
| 3372 |
Arrowhead.clear := Leftharpoon.clear & Rightharpoon.clear & cycle; |
| 3373 |
if hfilled: |
| 3374 |
Arrowhead := Arrowhead--cycle; |
| 3375 |
Rightharpoon := Rightharpoon--(0,-1)--cycle; |
| 3376 |
Leftharpoon := Leftharpoon--(0,-1)--cycle; |
| 3377 |
fi |
| 3378 |
enddef; |
| 3379 |
headshape (1,1,false); |
| 3380 |
|
| 3381 |
def head = ahead (headcolor) enddef; |
| 3382 |
|
| 3383 |
vardef ahead (expr clr, front, back, hwr, tens, filled) = |
| 3384 |
settension (_tn) tens; fixtension (_tn); |
| 3385 |
if front <> back: |
| 3386 |
setpair (side) (hwr/2) * ((front-back) rotated 90); |
| 3387 |
setpath (f) (back + side)..tension _tn.. |
| 3388 |
{front-back}front{back-front}..tension _tn..(back - side); |
| 3389 |
if clearhead: |
| 3390 |
safeunfill (back - side)--(front-side)--(front+side)-- |
| 3391 |
(back+side) & f & cycle; |
| 3392 |
colorsafedraw (background) (back - side)--(front-side)-- |
| 3393 |
(front+side)--(back+side) & f & cycle; |
| 3394 |
fi |
| 3395 |
if filled: |
| 3396 |
f := f--cycle; |
| 3397 |
colorsafefill (clr) f; |
| 3398 |
fi |
| 3399 |
colorsafedraw (clr) f; |
| 3400 |
fi |
| 3401 |
enddef; |
| 3402 |
|
| 3403 |
def headpath = Gheadpath (false) (Arrowhead) enddef; |
| 3404 |
def headpathx = Gheadpath (true) (Arrowhead) enddef; |
| 3405 |
|
| 3406 |
def colorheadpath = colorGheadpath (false) (Arrowhead) enddef; |
| 3407 |
def colorheadpathx = colorGheadpath (true) (Arrowhead) enddef; |
| 3408 |
|
| 3409 |
def Gheadpath (expr trim) (suffix ah) = |
| 3410 |
colorGheadpath (trim) (ah) (headcolor) |
| 3411 |
enddef; |
| 3412 |
vardef colorGheadpath |
| 3413 |
(expr trim) (suffix ah) (expr clr, sc, rot, pos) expr f = |
| 3414 |
if (sc <> 0) and (known ah) and (path ah): |
| 3415 |
convertpath (_g) f; |
| 3416 |
setpair (_P) predirection[length _g] (_g); |
| 3417 |
if _P <> origin: |
| 3418 |
_P := _P rotated rot; |
| 3419 |
setnumeric (_ang) anglefromto (up, _P); |
| 3420 |
_P := pnt[length _g] (_g) - pos * _P; |
| 3421 |
setpair (_tip) if known ah.tip: ah.tip else: origin fi; |
| 3422 |
|
| 3423 |
if trim: |
| 3424 |
if known ah.clear: |
| 3425 |
safeunfill (ah.clear shifted - _tip) |
| 3426 |
scaled sc |
| 3427 |
rotated _ang |
| 3428 |
shifted _P; |
| 3429 |
fi |
| 3430 |
setnumeric (_ys) max(bp, penwd, last_dot_size); |
| 3431 |
safeunfill cut_path |
| 3432 |
xscaled ceiling sc yscaled ceiling _ys |
| 3433 |
rotated _ang shifted _P; |
| 3434 |
fi |
| 3435 |
if cycle ah: colorsafefill else: colorsafedraw fi (clr) |
| 3436 |
(ah shifted -_tip) |
| 3437 |
scaled sc |
| 3438 |
rotated _ang |
| 3439 |
shifted _P; |
| 3440 |
fi |
| 3441 |
fi |
| 3442 |
f |
| 3443 |
enddef; |
| 3444 |
|
| 3445 |
path cut_path; |
| 3446 |
cut_path := (.5,0)--(.5,.71)--(-.5,.71)--(-.5,0)--cycle; |
| 3447 |
|
| 3448 |
def tailpath (suffix sh) = colortailpath (sh) (headcolor) enddef; |
| 3449 |
vardef colortailpath (suffix sh) (expr clr, sc, rot, pos) expr f = |
| 3450 |
if (sc <> 0) and (known sh) and (path sh): |
| 3451 |
convertpath (_g) f; |
| 3452 |
setpair(_P) postdirection0 (_g); |
| 3453 |
if _P <> origin: |
| 3454 |
_P := _P rotated rot; |
| 3455 |
if cycle sh: colorsafefill else: colorsafedraw fi (clr) |
| 3456 |
(sh if known sh.tip: shifted -sh.tip fi) |
| 3457 |
scaled sc |
| 3458 |
rotated anglefromto (up, _P) |
| 3459 |
shifted (pnt0 (_g) + pos * _P); |
| 3460 |
fi |
| 3461 |
fi |
| 3462 |
f |
| 3463 |
enddef; |
| 3464 |
|
| 3465 |
def midpath (suffix sh) = colormidpath (sh) (headcolor) enddef; |
| 3466 |
vardef colormidpath (suffix sh) (expr clr, sc, rot, pos) expr f = |
| 3467 |
if (sc <> 0) and (known sh) and (path sh): |
| 3468 |
convertpath (_g) f; |
| 3469 |
setnumeric (_t) pathtime[pos] (_g); |
| 3470 |
setpair (_P) postdirection[_t] (_g); |
| 3471 |
if _P <> origin: |
| 3472 |
_P := _P rotated rot; |
| 3473 |
if cycle sh: colorsafefill else: colorsafedraw fi (clr) |
| 3474 |
sh scaled sc |
| 3475 |
rotated anglefromto (up, _P) |
| 3476 |
shifted (pnt[_t] (_g)); |
| 3477 |
fi |
| 3478 |
fi |
| 3479 |
f |
| 3480 |
enddef; |
| 3481 |
|
| 3482 |
vardef signeddeviate primary X = |
| 3483 |
(uniformdeviate 1)[-X,X] |
| 3484 |
enddef; |
| 3485 |
vardef scaledeviate (expr W, A) = |
| 3486 |
2 ** (signeddeviate W) * dir A |
| 3487 |
enddef; |
| 3488 |
vardef polardeviate primary R = |
| 3489 |
(uniformdeviate abs(R)) * dir uniformdeviate 360 |
| 3490 |
enddef; |
| 3491 |
vardef xydeviate primary Z = |
| 3492 |
(signeddeviate (xpart Z), signeddeviate (ypart Z)) |
| 3493 |
enddef; |
| 3494 |
|
| 3495 |
vardef randompair (expr maxshift) = |
| 3496 |
if numeric maxshift: polardeviate (maxshift) |
| 3497 |
elseif pair maxshift: xydeviate (maxshift) |
| 3498 |
else: (0,0) |
| 3499 |
fi |
| 3500 |
enddef; |
| 3501 |
|
| 3502 |
vardef detrivialized expr f = |
| 3503 |
save g; path p, g[]; g := 0; |
| 3504 |
for k = 1 upto length f: |
| 3505 |
p := subpath (k-1,k) of f; |
| 3506 |
if not trivial p: g[incr g] := p; fi |
| 3507 |
endfor |
| 3508 |
if g = 0: onepointpath (cycle f, pnt0(f)) |
| 3509 |
else: g1 for k = 2 upto g: &g[k] endfor if cycle f: &cycle fi |
| 3510 |
fi |
| 3511 |
enddef; |
| 3512 |
|
| 3513 |
vardef randompath (expr maxshift, weirdness) expr f = |
| 3514 |
save g, n; path g; |
| 3515 |
g := detrivialized f; |
| 3516 |
n := length g; |
| 3517 |
if n = 0: |
| 3518 |
f shifted randompair (maxshift) |
| 3519 |
else: |
| 3520 |
save X, U, V; |
| 3521 |
pair X[], U[], V[]; |
| 3522 |
if cycle g: n := n - 1; fi |
| 3523 |
for k = 0 upto n: |
| 3524 |
X[k] := pnt[k](g); |
| 3525 |
U[k] := X[k] - pre[k](g); |
| 3526 |
V[k] := post[k](g) - X[k]; |
| 3527 |
endfor |
| 3528 |
save A, B; |
| 3529 |
for k := 0 upto n: |
| 3530 |
X[k] := X[k] shifted randompair (maxshift); |
| 3531 |
A := anglefromto (U[k],V[k]); |
| 3532 |
B := signeddeviate (30weirdness); |
| 3533 |
U[k] := X[k] - (U[k] zscaled scaledeviate (weirdness,B)); |
| 3534 |
B := B - A + A * (2 ** signeddeviate weirdness); |
| 3535 |
V[k] := X[k] + (V[k] zscaled scaledeviate (weirdness,B)); |
| 3536 |
endfor |
| 3537 |
X0 for k = 1 upto n: |
| 3538 |
.. controls V[k-1] and U[k] .. X[k] |
| 3539 |
endfor |
| 3540 |
if cycle g: |
| 3541 |
.. controls V[n] and U0 .. cycle |
| 3542 |
fi |
| 3543 |
fi |
| 3544 |
enddef; |
| 3545 |
|
| 3546 |
vardef randomlines (expr maxshift) expr f = |
| 3547 |
save g, n; path g; |
| 3548 |
g := detrivialized f; |
| 3549 |
n := length g; |
| 3550 |
if n = 0: |
| 3551 |
f shifted randompair (maxshift) |
| 3552 |
else: |
| 3553 |
if cycle g: n := n - 1; fi |
| 3554 |
(pnt0(g) shifted randompair (maxshift)) |
| 3555 |
for k = 1 upto n: |
| 3556 |
-- (pnt[k](g) shifted randompair (maxshift)) |
| 3557 |
endfor |
| 3558 |
if cycle g: |
| 3559 |
-- cycle |
| 3560 |
fi |
| 3561 |
fi |
| 3562 |
enddef; |
| 3563 |
|
| 3564 |
vardef interpolatedpath (expr t, P) expr Q = |
| 3565 |
if not path Q: |
| 3566 |
GBerrmsg ("Improper argument to interpolatedpath.") |
| 3567 |
"The last argument to interpolatedpath must be a path."; |
| 3568 |
if pair P: onepointpath(false, P) |
| 3569 |
else: |
| 3570 |
if path P: |
| 3571 |
P |
| 3572 |
else: |
| 3573 |
onepointpath (false, origin) |
| 3574 |
fi |
| 3575 |
fi |
| 3576 |
elseif pair P: |
| 3577 |
interpolated_pair_path (t, cycle Q, P, Q) |
| 3578 |
elseif not path P: |
| 3579 |
GBerrmsg ("Improper argument to interpolatedpath.") |
| 3580 |
"The second argument to interpolatedpath must be a pair " |
| 3581 |
& "or a path."; |
| 3582 |
Q |
| 3583 |
else: |
| 3584 |
if t=0: Q |
| 3585 |
elseif t=1: P |
| 3586 |
else: |
| 3587 |
save P_, Q_; path P_, Q_; |
| 3588 |
P_ := detrivialized P; |
| 3589 |
Q_ := detrivialized Q; |
| 3590 |
if length P_ = 0: |
| 3591 |
interpolated_pair_path (t, cycle Q, pnt0(P_), Q) |
| 3592 |
elseif length Q_ = 0: |
| 3593 |
interpolated_pair_path (t, cycle Q, pnt0(Q_), P) |
| 3594 |
else: |
| 3595 |
save G, H, n, m, k, r; |
| 3596 |
path G[], H[]; |
| 3597 |
G := H := 0; |
| 3598 |
n := length P_; m := length Q_; |
| 3599 |
k := gcd(n, m); |
| 3600 |
r := m/k; |
| 3601 |
for I=0 upto n-1: |
| 3602 |
for J=0 upto r-1: |
| 3603 |
G[incr G] := subpath (I+J/r, I+(J+1)/r) of P_; |
| 3604 |
endfor |
| 3605 |
endfor |
| 3606 |
r := n/k; |
| 3607 |
for I=0 upto m-1: |
| 3608 |
for J=0 upto r-1: |
| 3609 |
H[incr H] := subpath (I+J/r, I+(J+1)/r) of Q_; |
| 3610 |
endfor |
| 3611 |
endfor |
| 3612 |
for N = 1 upto G-1: |
| 3613 |
force_equal_ends(G[N], G[N+1]); |
| 3614 |
force_equal_ends(H[N], H[N+1]); |
| 3615 |
endfor |
| 3616 |
interpolated_segment (t, G1, H1) |
| 3617 |
for N = 2 upto G: & interpolated_segment (t, G[N], H[N]) |
| 3618 |
endfor if (pnt0(G1)=pnt1(G[G])) and (cycle Q): & cycle fi |
| 3619 |
fi |
| 3620 |
fi |
| 3621 |
fi |
| 3622 |
enddef; |
| 3623 |
|
| 3624 |
vardef interpolated_pair_path (expr t, cyclic, P, Q) = |
| 3625 |
save N; N := length Q; |
| 3626 |
if N=0: onepointpath (cyclic, (t)[pnt0(Q),P]) |
| 3627 |
else: |
| 3628 |
(t)[pnt0(Q),P]..controls (t)[post0(Q),P] and |
| 3629 |
for n=1 upto N - 1: |
| 3630 |
(t)[pre[n](Q),P]..(t)[pnt[n](Q),P]..controls (t)[post[n](Q),P] |
| 3631 |
and |
| 3632 |
endfor |
| 3633 |
(t)[pre[N](Q),P].. if cyclic: cycle else: (t)[pnt[N](Q),P] fi |
| 3634 |
fi |
| 3635 |
enddef; |
| 3636 |
|
| 3637 |
vardef interpolated_segment (expr t, S, T) = |
| 3638 |
(t)[ pnt0(S), pnt0(T)]..controls |
| 3639 |
(t)[ post0(S), post0(T)] and (t)[ pre1(S), pre1(T)].. |
| 3640 |
(t)[ pnt1(S), pnt1(T)] |
| 3641 |
enddef; |
| 3642 |
|
| 3643 |
vardef parasegment (expr d, segs, f) = |
| 3644 |
if d = 0: f |
| 3645 |
else: |
| 3646 |
save u, v, t; pair u[], v[]; |
| 3647 |
for n = 0 upto segs: |
| 3648 |
t := n/segs; |
| 3649 |
u[n] := postdirection [t] (f); |
| 3650 |
v[n] := pnt[t] (f) + (u[n] zscaled (0,d)); |
| 3651 |
endfor |
| 3652 |
v0{u0} |
| 3653 |
for n = 1 upto segs: ...v[n]{u[n]} endfor |
| 3654 |
fi |
| 3655 |
enddef; |
| 3656 |
|
| 3657 |
vardef parapath (expr d) expr f = |
| 3658 |
if d = 0: |
| 3659 |
f |
| 3660 |
else: |
| 3661 |
save a, g, h, p, q, s, t, u, v, w; |
| 3662 |
path g[], h, p[], q[]; |
| 3663 |
numeric a, s, t; |
| 3664 |
pair u, v, w, w[]; |
| 3665 |
s := emax(3, emin(segment_split, ceiling(max_points/5/length f))); |
| 3666 |
p := 0; |
| 3667 |
for i = 1 upto length f: |
| 3668 |
h := subpath (i-1, i) of f; |
| 3669 |
if not trivial h: |
| 3670 |
q[incr p] := h; |
| 3671 |
p[p] := parasegment (d, s, h); |
| 3672 |
fi |
| 3673 |
endfor |
| 3674 |
if p = 0: |
| 3675 |
f |
| 3676 |
else: |
| 3677 |
a := if d>0: - fi 180; |
| 3678 |
h := p1; |
| 3679 |
for i = 1 upto p-1: |
| 3680 |
u := predirection 1 (q[i]); |
| 3681 |
v := postdirection 0 (q[i+1]); |
| 3682 |
w1 := pnt 1 (q[i]) - (u zscaled (0,d)); |
| 3683 |
w2 := pnt 0 (q[i+1]) - (v zscaled (0,d)); |
| 3684 |
w3 := pnt [infinity] (h); |
| 3685 |
w4 := pnt 0 (p[i+1]); |
| 3686 |
g0 := arcpps(w3, w1, a); |
| 3687 |
g1 := h & g0; |
| 3688 |
g2 := arcpps(w2, w4, a) & p[i+1]; |
| 3689 |
if (p[i] & g0) intersects reverse g2: |
| 3690 |
s := length g2 - _Ytime; |
| 3691 |
t := length h - length p[i] + _Xtime; |
| 3692 |
g1 := subpath (0, t) of g1; |
| 3693 |
g2 := subpath (s, length g2) of g2; |
| 3694 |
force_equal_ends (g1, g2); |
| 3695 |
h := g1 & g2; |
| 3696 |
else: |
| 3697 |
h := h .. p[i+1]; |
| 3698 |
fi |
| 3699 |
endfor |
| 3700 |
|
| 3701 |
if cycle f: |
| 3702 |
u := predirection 1 (q[p]); |
| 3703 |
v := postdirection 0 (q[1]); |
| 3704 |
w1 := pnt 1 (q[p]) - (u zscaled (0,d)); |
| 3705 |
w2 := pnt 0 (q[1]) - (v zscaled (0,d)); |
| 3706 |
w3 := pnt [infinity] (h); |
| 3707 |
w4 := pnt 0 (p[1]); |
| 3708 |
g3 := arcpps(w3, w1, a); |
| 3709 |
g0 := arcpps(w2, w4, a); |
| 3710 |
g1 := g0 & h & g3; |
| 3711 |
g2 := g0 & p[1]; |
| 3712 |
if (p[p] & g3) intersects reverse g2: |
| 3713 |
s := length g2 - _Ytime; |
| 3714 |
t := length g0 + length h - length p[p] + _Xtime; |
| 3715 |
g1 := subpath (s, t) of g1; |
| 3716 |
force_equal_ends (g1, g1); |
| 3717 |
h := g1 & cycle; |
| 3718 |
else: |
| 3719 |
h := h..cycle; |
| 3720 |
fi |
| 3721 |
fi |
| 3722 |
h |
| 3723 |
fi |
| 3724 |
fi |
| 3725 |
enddef; |
| 3726 |
|
| 3727 |
vardef turnangle@# (expr f) = |
| 3728 |
anglefromto(predirection@# (f), postdirection@#(f)) |
| 3729 |
enddef; |
| 3730 |
|
| 3731 |
def setdatadashes (text lst) = |
| 3732 |
save __type; __type := 0; |
| 3733 |
forsuffixes _itm = lst: |
| 3734 |
if knownnumericarray _itm : |
| 3735 |
copyarray (_itm) (__type[__type]); |
| 3736 |
next __type; |
| 3737 |
else: GBwarn "Improper dash pattern in setdatadashes."; |
| 3738 |
fi |
| 3739 |
endfor |
| 3740 |
if __type > 1: |
| 3741 |
save dashtype; dashtype := __type; |
| 3742 |
for _j = 0 upto dashtype - 1: |
| 3743 |
copyarray (__type[_j]) (dashtype[_j]); |
| 3744 |
endfor |
| 3745 |
else: |
| 3746 |
SetdataWarn "dashes"; |
| 3747 |
fi |
| 3748 |
enddef; |
| 3749 |
def getdashpat expr n = dashtype[n mod dashtype] enddef; |
| 3750 |
|
| 3751 |
def SetdataWarn expr s = |
| 3752 |
GBwarn "command setdata"& s &"() failed. Previous values retained."; |
| 3753 |
enddef; |
| 3754 |
|
| 3755 |
numeric Solid, Simpledash, Simpledot, Dotdash, Dotdashdot, Dotdashdash; |
| 3756 |
dashpat (Solid) (0); |
| 3757 |
dashpat (Simple_dash) (3bp, 4bp); |
| 3758 |
dashpat (Simple_dot) (0, 4bp); |
| 3759 |
dashpat (Dot_dash) (0, 4bp, 3bp, 4bp); |
| 3760 |
dashpat (Dot_dash_dot) (0, 4bp, 3bp, 4bp, 0, 4bp); |
| 3761 |
dashpat (Dot_dash_dash) (0, 4bp, 3bp, 4bp, 3bp, 4bp); |
| 3762 |
|
| 3763 |
numeric dashtype, dashtype[], dashtype[][]; |
| 3764 |
def defaultdashes = |
| 3765 |
setdatadashes (Solid, Simple_dash, Simple_dot, |
| 3766 |
Dot_dash, Dot_dash_dot, Dot_dash_dash); |
| 3767 |
enddef; |
| 3768 |
defaultdashes; |
| 3769 |
|
| 3770 |
def setdatasymbols (text lst) = |
| 3771 |
save __type; path __type[]; |
| 3772 |
__type := 0; |
| 3773 |
for _itm = lst: |
| 3774 |
if (known _itm) and (path _itm): |
| 3775 |
__type[__type] := _itm; |
| 3776 |
next __type; |
| 3777 |
else: |
| 3778 |
GBwarn "Improper symbol in setdatasymbols()."; |
| 3779 |
fi |
| 3780 |
endfor |
| 3781 |
if __type > 1: |
| 3782 |
save pointtype; pointtype := __type; |
| 3783 |
path pointtype[]; |
| 3784 |
for _j = 0 upto pointtype - 1: |
| 3785 |
pointtype[_j] := __type[_j]; |
| 3786 |
endfor |
| 3787 |
else: |
| 3788 |
SetdataWarn "symbols"; |
| 3789 |
fi |
| 3790 |
enddef; |
| 3791 |
def getsymbol expr n := pointtype[n mod pointtype] enddef; |
| 3792 |
|
| 3793 |
def DeclareGBSymbols (text S) = |
| 3794 |
forsuffixes _itm = S: |
| 3795 |
path _itm; |
| 3796 |
path _itm.clear; |
| 3797 |
pair _itm.tip; |
| 3798 |
endfor |
| 3799 |
enddef; |
| 3800 |
DeclareGBSymbols( |
| 3801 |
Triangle, Square, Circle, Diamond, Star, Plus, Cross, |
| 3802 |
Asterisk, Crossbar, Leftbar, Rightbar, Righthook, |
| 3803 |
Lefthook, SolidTriangle, SolidSquare, SolidCircle, |
| 3804 |
SolidDiamond, SolidStar |
| 3805 |
); |
| 3806 |
|
| 3807 |
vardef undo_cycle expr f = subpath (0, length f) of f enddef; |
| 3808 |
|
| 3809 |
SolidTriangle := (up--(dir 210)--(dir -30)--cycle) scaled .78; |
| 3810 |
Triangle := undo_cycle SolidTriangle; |
| 3811 |
|
| 3812 |
Triangle.clear := SolidTriangle.clear := |
| 3813 |
((dir -30)--(cosd 30,1)--(cosd 210,1)--(dir 210)--up--cycle) |
| 3814 |
scaled .78; |
| 3815 |
|
| 3816 |
SolidSquare := (up--(-1,1)--(-1,-1)--(1,-1)--(1,1)--cycle) scaled .443; |
| 3817 |
Square := undo_cycle SolidSquare; |
| 3818 |
|
| 3819 |
SolidCircle := fullcircle rotated 90; |
| 3820 |
Circle := undo_cycle SolidCircle; |
| 3821 |
Circle.clear := SolidCircle.clear := |
| 3822 |
halfcircle--(-.5,.5)--(.5,.5)--cycle; |
| 3823 |
|
| 3824 |
SolidDiamond := (up--left--down--right--cycle) |
| 3825 |
scaled .522 yscaled 1.44; |
| 3826 |
Diamond := undo_cycle SolidDiamond; |
| 3827 |
Diamond.clear := SolidDiamond.clear := |
| 3828 |
(right--(1,1)--(-1,1)--left--up--cycle) scaled .522 yscaled 1.44; |
| 3829 |
|
| 3830 |
Plus := ((0,0)--up--down--(0,0)--left--right) scaled .65; |
| 3831 |
Plus.clear := (right--(1,1)--(-1,1)--(left)--cycle) scaled .65; |
| 3832 |
|
| 3833 |
Cross := ((0,0)--(dir 45)--(dir -135)--(0,0)--(dir -45)--(dir 135)) |
| 3834 |
scaled .65; |
| 3835 |
Cross.clear := |
| 3836 |
((0,0)--(dir -45)--dir(45)--(dir 135)--(dir -135)--cycle) scaled .65; |
| 3837 |
|
| 3838 |
Asterisk := ((0,0)--up--down--(0,0)--(dir 30)--(dir -150) |
| 3839 |
--(0,0)--(dir -30)--(dir 150)) scaled .6; |
| 3840 |
Asterisk.clear := ((0,0)--(dir -30)--(cosd 30,1)--(cosd 150,1) |
| 3841 |
--(dir -150)--cycle) scaled .6; |
| 3842 |
|
| 3843 |
Crossbar := ((0,0)--left--right) scaled .65; |
| 3844 |
Crossbar.clear := rect (right,(-1,.5)) scaled .65; |
| 3845 |
|
| 3846 |
Leftbar := ((0,0)--left); |
| 3847 |
Rightbar := ((0,0)--right); |
| 3848 |
Leftbar.clear := rect((0,0),(-1,.5)); |
| 3849 |
Rightbar.clear := rect((0,0),(1,.5)); |
| 3850 |
|
| 3851 |
Righthook := arcpps((0,0),(1,0),180); |
| 3852 |
Lefthook := Righthook xscaled -1; |
| 3853 |
Righthook.clear := Righthook--cycle; |
| 3854 |
Lefthook.clear := Lefthook--cycle; |
| 3855 |
|
| 3856 |
vardef mkstar (expr n, m) (suffix A) = |
| 3857 |
save ang; ang := 360/n; |
| 3858 |
A1 := up; A3 := up rotated ang; |
| 3859 |
A2 = (whatever)[A1, A1 rotated ( ang*m)]; |
| 3860 |
A2 = (whatever)[A3, A3 rotated (-ang*m)]; |
| 3861 |
for i = 4 upto 2n: |
| 3862 |
A[i] := A[i-2] rotated ang; |
| 3863 |
endfor |
| 3864 |
A := 2n; |
| 3865 |
mkpoly (true, A) |
| 3866 |
enddef; |
| 3867 |
|
| 3868 |
save _A; pair _A[]; |
| 3869 |
SolidStar := mkstar (5, 2, _A) scaled .84; |
| 3870 |
Star := undo_cycle SolidStar; |
| 3871 |
Star.clear := polyline (true) |
| 3872 |
(_A9, _A10, _A1, _A2, _A3, (xpart _A3, 1), (xpart _A9, 1)) |
| 3873 |
scaled .84; |
| 3874 |
SolidStar.clear := Star.clear; |
| 3875 |
|
| 3876 |
forsuffixes S = |
| 3877 |
Triangle, Square, Circle, Diamond, Star, Plus, Cross, |
| 3878 |
Asterisk, Crossbar, Leftbar, Rightbar, Righthook, |
| 3879 |
Lefthook, SolidTriangle, SolidSquare, SolidCircle, |
| 3880 |
SolidDiamond, SolidStar : |
| 3881 |
S.tip := point 0 of S; |
| 3882 |
endfor |
| 3883 |
|
| 3884 |
vardef gcd (expr n, m) = |
| 3885 |
save a, b, r; |
| 3886 |
a := emax (abs(m), abs(n)); |
| 3887 |
b := emin (abs(m), abs(n)); |
| 3888 |
if b > 0: |
| 3889 |
forever: |
| 3890 |
r := a mod b; |
| 3891 |
exitif r < 1; |
| 3892 |
a := b; b := r; |
| 3893 |
endfor |
| 3894 |
b |
| 3895 |
else: |
| 3896 |
a |
| 3897 |
fi |
| 3898 |
enddef; |
| 3899 |
|
| 3900 |
vardef lcm (expr n, m) = |
| 3901 |
n/gcd(n, m)*m |
| 3902 |
enddef; |
| 3903 |
|
| 3904 |
numeric pointtype; path pointtype[]; |
| 3905 |
def defaultsymbols = |
| 3906 |
setdatasymbols( Circle, Cross, SolidDiamond, Square, Plus, |
| 3907 |
Triangle, SolidCircle, Star, SolidTriangle); |
| 3908 |
enddef; |
| 3909 |
defaultsymbols; |
| 3910 |
|
| 3911 |
def computepie (suffix dat) (expr sign, ang, cent, rad) (text data) = |
| 3912 |
begingroup |
| 3913 |
save _tot, _max, _toobig; |
| 3914 |
_max := 0; dat := 0; |
| 3915 |
for _val = data: |
| 3916 |
dat[incr dat] := _val; |
| 3917 |
_max := emax (_max, _val); |
| 3918 |
endfor |
| 3919 |
if dat=0: GBwarn "piechart attempted with empty list."; |
| 3920 |
_toobig := 1; |
| 3921 |
else: |
| 3922 |
_toobig := infinity/dat; |
| 3923 |
fi |
| 3924 |
if _max > _toobig: |
| 3925 |
for _idx = 1 upto dat: |
| 3926 |
dat[_idx] := dat[_idx]/_toobig; |
| 3927 |
endfor |
| 3928 |
fi |
| 3929 |
for _idx = 2 upto dat: |
| 3930 |
dat[_idx] := dat[_idx - 1] + dat[_idx]; |
| 3931 |
endfor |
| 3932 |
_tot := dat[dat]; |
| 3933 |
for _idx = dat downto 2: |
| 3934 |
dat[_idx] := ang + sign*dat[_idx-1]/_tot*360; |
| 3935 |
endfor |
| 3936 |
dat1 := ang; dat[dat + 1] := ang + 360sign; |
| 3937 |
endgroup |
| 3938 |
enddef; |
| 3939 |
|
| 3940 |
def piechart (expr sign, ang, cent, rad) (text data) = |
| 3941 |
save _dat; |
| 3942 |
computepie (_dat) (sign, ang, cent, rad) (data); |
| 3943 |
mkpiewedges (_dat, cent, rad); |
| 3944 |
enddef; |
| 3945 |
|
| 3946 |
def mkpiewedges (suffix dat) (expr cent, rad) = |
| 3947 |
numeric piewedge, piedirection, pieangle, pieangle[]; |
| 3948 |
pair piecenter, piedirection[]; |
| 3949 |
path piewedge[]; |
| 3950 |
piecenter := cent; |
| 3951 |
piedirection := pieangle := piewedge := dat; |
| 3952 |
for _idx = 1 upto dat: |
| 3953 |
pieangle[_idx] := dat[_idx]; |
| 3954 |
piewedge[_idx] := sector (piecenter, rad, dat[_idx], dat[_idx+1]); |
| 3955 |
piedirection[_idx] := dir(0.5[ dat[_idx], dat[_idx+1] ]); |
| 3956 |
endfor |
| 3957 |
enddef; |
| 3958 |
|
| 3959 |
def namedpiechart (suffix nm) (expr sign, ang, cent, rad) (text data) = |
| 3960 |
save _dat; |
| 3961 |
computepie (_dat) (sign, ang, cent, rad) (data); |
| 3962 |
setnumeric (nm) _dat; |
| 3963 |
pair nm.center, nm.direction[]; |
| 3964 |
path nm.wedge[]; |
| 3965 |
nm.center := cent; |
| 3966 |
for _idx = 1 upto _dat: |
| 3967 |
nm.wedge[_idx] := sector (cent, rad, _dat[_idx], _dat[_idx+1]); |
| 3968 |
nm.direction[_idx] := dir(0.5[ _dat[_idx], _dat[_idx+1] ]); |
| 3969 |
endfor |
| 3970 |
enddef; |
| 3971 |
|
| 3972 |
def barchart (expr firstbar, sep, r, vert)(text data) = |
| 3973 |
numeric barbegin, barbegin[], |
| 3974 |
barend, barend[], |
| 3975 |
barlength, barlength[], |
| 3976 |
barstart, barstart[], |
| 3977 |
chartbar, barwd; |
| 3978 |
path chartbar[]; |
| 3979 |
chartbar := 0; barwd := r*sep; |
| 3980 |
for _itm = data: |
| 3981 |
barend[incr chartbar] |
| 3982 |
:= if pair _itm: ypart _itm else: _itm fi; |
| 3983 |
barbegin[chartbar] := if pair _itm: xpart _itm else: 0 fi; |
| 3984 |
endfor |
| 3985 |
barbegin := barend := barlength := barstart := chartbar; |
| 3986 |
for _nn = 1 upto chartbar: |
| 3987 |
barstart[_nn] := firstbar + sep*(_nn-1); |
| 3988 |
barlength[_nn] := barend[_nn]; |
| 3989 |
chartbar[_nn] := rect ((barbegin[_nn], 0), ( barend[_nn], barwd)) |
| 3990 |
shifted (0, barstart[_nn]) if vert: xyswap fi; |
| 3991 |
endfor |
| 3992 |
enddef; |
| 3993 |
|
| 3994 |
def namedbarchart (suffix nm) (expr first, sep, r, vert) (text data) = |
| 3995 |
save nm; |
| 3996 |
begingroup |
| 3997 |
save _bb, _ee, _ww; |
| 3998 |
path nm.bar[]; |
| 3999 |
nm := 0; _ww := r*sep; |
| 4000 |
for _itm = data: |
| 4001 |
_ee := if pair _itm: ypart _itm else: _itm fi; |
| 4002 |
_bb := if pair _itm: xpart _itm else: 0 fi; |
| 4003 |
nm.bar[incr nm] := rect ((_bb, 0), ( _ee, _ww) ) |
| 4004 |
shifted (0, first + sep*(nm-1)) if vert: xyswap fi; |
| 4005 |
endfor |
| 4006 |
endgroup |
| 4007 |
enddef; |
| 4008 |
|
| 4009 |
picture totalpicture; |
| 4010 |
boolean totalnull, currentnull; |
| 4011 |
def clearit = |
| 4012 |
currentpicture := totalpicture := nullpicture; |
| 4013 |
currentnull := totalnull := true; |
| 4014 |
enddef; |
| 4015 |
|
| 4016 |
def keepit = |
| 4017 |
addto totalpicture also currentpicture; |
| 4018 |
mono (totalpicture); |
| 4019 |
currentpicture := nullpicture; |
| 4020 |
totalnull := totalnull or currentnull; |
| 4021 |
currentnull := true; |
| 4022 |
enddef; |
| 4023 |
|
| 4024 |
def addto_currentpicture = |
| 4025 |
currentnull := false; |
| 4026 |
addto currentpicture |
| 4027 |
enddef; |
| 4028 |
|
| 4029 |
def mergeit (text do) = |
| 4030 |
if totalnull: |
| 4031 |
do currentpicture |
| 4032 |
elseif currentnull: |
| 4033 |
do totalpicture |
| 4034 |
else: |
| 4035 |
begingroup |
| 4036 |
save _v_; picture _v_; |
| 4037 |
_v_ := currentpicture; |
| 4038 |
addto _v_ also totalpicture; |
| 4039 |
do _v_ |
| 4040 |
endgroup |
| 4041 |
fi |
| 4042 |
enddef; |
| 4043 |
|
| 4044 |
boolean noship; noship := false; |
| 4045 |
def shipit = if noship: else: mergeit (shipout) fi enddef; |
| 4046 |
|
| 4047 |
def showit_ = mergeit (show_) enddef; |
| 4048 |
def show_ suffix v = display v inwindow currentwindow enddef; |
| 4049 |
|
| 4050 |
numeric gcode; gcode := 0; |
| 4051 |
|
| 4052 |
% end grafbase.mf |
| 4053 |
endinput. |
| 4054 |
%% |
| 4055 |
%% End of file `grafbase.mf'. |