Debian Codegraph @

texmf-dist/doc/latex/circuit-macros/dpictools.pic

raw
1 # dpictools.pic
2 # General-purpose pic macros. Input this file using the Circuit_macros
3 # m4 macro `NeedDpicTools' or the pic statement copy "HOMELIB_/dpictools.pic"
4 # when HOMELIB_ is defined or, generically, copy "<path>dpictools.pic"
5
6 # Circuit_macros Version 11.0, copyright (c) 2026 J. D. Aplevich under #
7 # the LaTeX Project Public Licence in file Licence.txt. The files of #
8 # this distribution may be redistributed or modified provided that this #
9 # copyright notice is included and provided that modifications are clearly #
10 # marked to distinguish them from this distribution. There is no warranty #
11 # whatsoever for these files. #
12
13 # findroot(function,left bound,right bound,tolerance,var name)
14 # Solve function(x)=0 by the method of bisection
15 # e.g. define parabola { $2 = ($1)^2 - 1 }
16 # findroot( parabola, 0, 2, 1e-8, x )
17 define findroot {$5 = 0; [ x_m = $2; x_M = $3
18 loop( $1(x_m,f_m);, abs(x_M-x_m)>$4,,
19 x_c = (x_m+x_M)/2
20 $1(x_c,f_c)
21 if sign(f_c)==sign(f_m) then {x_m=x_c} else {x_M=x_c};)
22 $5 := (x_m+x_M)/2 ] ; }
23
24 # bisect(function,left bound,right bound, tolerance, var name)
25 # Like findroot but uses recursion and without a [] box
26 define bisect { x_m_$1 = $2; x_M_$1 = $3
27 x_c_$1 = (x_m_$1+x_M_$1)/2
28 if (abs(x_m_$1-x_M_$1) <= $4) then { $5 = x_c_$1 } else {
29 $1(x_m_$1,f_m_$1)
30 $1(x_c_$1,f_c_$1)
31 if (sign(f_c_$1)==sign(f_m_$1)) then { bisect($1,x_c_$1,x_M_$1,$4,$5) } \
32 else { bisect($1,x_m_$1,x_c_$1,$4,$5) } } }
33
34 # case(i, alt1, alt2, ... ),
35 # Case statement: execute alternative i
36 # e.g., case(2, x=5, x=10, x=15) sets x to 10
37 define case { exec sprintf("$%g",floor($1+0.5)+1); }
38
39 # testexpr(i, expr1, expr2, ... )
40 # Set i to index of the first true alternative
41 # in a sequence of logical expressions, e.g.,
42 # testexpr(i, 1>2, 1<2 ) sets i to 2; to 0
43 # if no test is true.
44 define testexpr { $1 = 0; [for i_testexpr=2 to $+ do {
45 exec sprintf("if $%g then {$1 := i_testexpr-1; i_testexpr=$+}",i_testexpr)
46 }] ; }
47
48 # loop(initial,test,loopend,statements)
49 # C-like loop. Commas in arg3 and arg4 must
50 # be in quotes or parentheses, e.g.,
51 # loop(i=1, i<=3, i+=1, print i) prints 1, 2, 3
52 define loop {ld__+=1
53 $1
54 for lx__[ld__]=0 to 1 do {
55 if $2 then { lx__[ld__]=0; $4; $3; } else { lx__[ld__]=1 }}
56 ld__-=1; }
57
58 # array(var,expr1,expr2,...)
59 # var[1]=expr1; var[2]=expr2,...
60 define array {
61 for i_array=2 to $+ do { exec sprintf("$1[%g] = $%g",i_array-1,i_array); }}
62
63 # array2(var,expr1,expr2,...)
64 # var[expr1,1]=expr2; var[expr1,2]=expr3,...
65 define array2 { for i_array=3 to $+ do {
66 exec sprintf("$1[%g,%g]=$%g",$2,i_array-2,i_array);}}
67
68 # posarray(Name,Position1,Position2,...)
69 # Name[1]:Position1; Name[2]:Position2,...
70 define posarray {
71 for i_array=2 to $+ do { exec sprintf("$1[%g] : $%g",i_array-1,i_array); }}
72
73 # posarray2(Var,expr,Position1,Position2,...)
74 # Var[expr,1]:position1; Var[expr,2]:Position2,...
75 define posarray2 { for i_array=3 to $+ do {
76 exec sprintf("$1[%g,%g] : $%g",$2,i_array-2,i_array); }}
77
78 # Operations on 3D vectors (could be generalized
79 # except for cross):
80 # $3 = $1 + $2
81 define sum3 {
82 $3[1]=$1[1]+$2[1]
83 $3[2]=$1[2]+$2[2]
84 $3[3]=$1[3]+$2[3]}
85
86 # $3 = $1 - $2
87 define diff3 {
88 $3[1]=$1[1]-$2[1]
89 $3[2]=$1[2]-$2[2]
90 $3[3]=$1[3]-$2[3]}
91
92 # $3 = $1 X $2
93 define cross3 {
94 $3[1]=$1[2]*$2[3]-$1[3]*$2[2]
95 $3[2]=$1[3]*$2[1]-$1[1]*$2[3]
96 $3[3]=$1[1]*$2[2]-$1[2]*$2[1]}
97
98 # $1 . $2
99 define dot3 {($1[1]*$2[1]+$1[2]*$2[2]+$1[3]*$2[3])}
100
101 # $3 = $1 * $2
102 define sprod3 {
103 $3[1]=($1)*$2[1]
104 $3[2]=($1)*$2[2]
105 $3[3]=($1)*$2[3]}
106
107 # |$1|
108 define length3 { sqrt($1[1]^2+$1[2]^2+$1[3]^2) }
109
110 # Expand a vector
111 define vec3 {$1[1],$1[2],$1[3]}
112
113 # $2 = $1
114 define copy3 {
115 $2[1] = $1[1]
116 $2[2] = $1[2]
117 $2[3] = $1[3] }
118
119 # slantbox(wid,ht,xslant,yslant,attributes)
120 define slantbox { [
121 if "$1"=="" then { w = boxwid } else { w = $1 }
122 if "$2"=="" then { h = boxht } else { h = $2 }
123 if "$3"=="" then { xs = 0 } else { xs = $3 }
124 if "$4"=="" then { ys = 0 } else { ys = $4 }
125 NE: (w+xs,h+ys)/2 ; SE: (w-xs,-h+ys)/2
126 SW: (-w-xs,-h-ys)/2 ; NW: (-w+xs,h-ys)/2
127 N: 0.5 between NW and NE ; E: 0.5 between NE and SE
128 S: 0.5 between SE and SW ; W: 0.5 between SW and NW
129 C: 0.5 between SW and NE
130 line from N to NE then to SE then to SW then to NW then to N $5
131 ] }
132 # arraymax( data array, n, index name, value)
133 # Find the index in array[1:n] of the first
134 # occurrence of the max value. The value is
135 # assigned if arg4 is non-blank. eg.,
136 # array(x,4,9,8,6); arraymax( x,4,i )
137 # assigns 2 to i, and arraymax( x,4,i,m )
138 # assigns 2 to i and 9 to m
139 define arraymax { { $3 = -1; if "$4" != "" then { $4 = 0 }; m_arrm = -1e25
140 for i_arrm=1 to $2 do { if $1[i_arrm] > m_arrm then {
141 $3 := i_arrm; m_arrm = $1[i_arrm] }}
142 if "$4" != "" then { $4 := m_arrm } } }
143
144 # arraymin( data array, n, index name, value)
145 # Like arraymax
146 define arraymin { { $3 = -1; if "$4" != "" then { $4 = 0 }; m_arrm = 1e25
147 for i_arrm=1 to $2 do { if $1[i_arrm] < m_arrm then {
148 $3 := i_arrm; m_arrm = $1[i_arrm] }}
149 if "$4" != "" then { $4 := m_arrm } } }
150
151 # copythru(macro_name,"datafile")
152 # See the GNU pic manual
153 # Implements "copy datafile thru macro_name"
154 # for data separated by comma, spaces, or tabs
155 define copythru {
156 sh "sed -e 's/^[ ]*/$1(/' -e 's/[ ]*$/)/' -e 's/[, ][ ]*/,/g' $2 \
157 > copythru_tmp__"
158 copy "copythru_tmp__"
159 sh "rm -f copythru_tmp__";}
160
161 # randn(array_name,n,mean,stddev)
162 # Assign n Gaussian random numbers
163 # in array_name[1] ... array_name[n]
164 define randn {
165 if "$2"=="" then { n_randn = 1 } else { n_randn = $2 }
166 if "$3"=="" then { m_randn = 0 } else { m_randn = $3 }
167 if "$4"=="" then { s_randn = 1 } else { s_randn = $4 }
168 for i_randn=1 to n_randn by 2 do {
169 for done=0 to 1 do { u_randn=2*rand()-1; v_randn=2*rand()-1
170 t_randn = u_randn^2+v_randn^2; done=(t_randn<1) }
171 t_randn = sqrt( -2*loge(t_randn)/t_randn )
172 $1[i_randn] = u_randn*t_randn*s_randn+m_randn
173 if i_randn < n_randn then {
174 $1[i_randn+1] = v_randn*t_randn*s_randn+m_randn }
175 }
176 }
177
178 # dfitpoints(V,n,m,P,mP)
179 # Compute the controls in P[mP], P[mP+1]... for
180 # the spline passing throught points V[m]...V[n]
181 define dfitpoints {
182 if "$3"=="" then { m_dfit=0 } else { m_dfit=$3 }
183 if "$5"=="" then { mP_dfit=0 } else { mP_dfit=$5 }
184 n_dfit = $2; np_dfit = n_dfit-m_dfit
185 $4[mP_dfit]: $1[m_dfit]
186 for i_dfit=m_dfit+1 to n_dfit-1 do {
187 $4[mP_dfit+i_dfit-m_dfit]: $1[i_dfit]*(4/3) }
188 $4[mP_dfit+np_dfit]: $1[n_dfit]
189 $4[mP_dfit+1]: $4[mP_dfit+1]-$4[mP_dfit+0]/6 # forward substitution
190 d_dfit[1] = 1
191 for i_dfit = 2 to np_dfit-1 do { $4[mP_dfit+i_dfit]: \
192 $4[mP_dfit+i_dfit]-$4[mP_dfit+i_dfit-1]/d_dfit[i_dfit-1]/6
193 d_dfit[i_dfit] = 1-1/d_dfit[i_dfit-1]/36 }
194 for i_dfit= np_dfit-1 to 1 by -1 do { # backward substitution
195 $4[mP_dfit+i_dfit]: \
196 ($4[mP_dfit+i_dfit]-$4[mP_dfit+i_dfit+1]/6)/d_dfit[i_dfit] } }
197
198 # dfitcurve(V,n,linetype,m (default 0))
199 # Draw a spline through V[m],...V[n]
200 # linetype=eg dotted. Works only with dpic.
201 # The calculated control points P[i] satisfy
202 # approximately:
203 # P[0] = V[0]
204 # P[i-1]/8 + P[i]*3/4 + P[i+1]/8 = V[i]
205 # P[n] = V[n]
206 # Like m4 macro fitcurve
207 define dfitcurve { if "$4"=="" then { m_dfit=0 } else { m_dfit=$4 }
208 n_dfit = $2; np_dfit = n_dfit-m_dfit
209 M4P_[0]: $1[m_dfit]
210 case( min(max(np_dfit,-1),3)+1,
211 spline 0.551784 $3 from M4P_[0] to M4P_[0],
212 spline 0.551784 $3 from M4P_[0] to $1[n_dfit],
213 M4P_[3]: $1[n_dfit]; Q_dfit: (M4P_[3]-M4P_[0])/4
214 M4P_[1]: $1[m_dfit+1]-Q_dfit; M4P_[2]: $1[m_dfit+1]+Q_dfit
215 spline 0.551784 $3 from M4P_[0] to M4P_[1] then to M4P_[2] then to M4P_[3],
216 dfitpoints($1,$2,$4,M4P_,0) # draw using computed control points
217 spline 0.551784 $3 from M4P_[0] to 11/32 between M4P_[0] and M4P_[1] \
218 then to 5/32 between M4P_[1] and M4P_[2]
219 for i_dfit=2 to np_dfit-2 do { continue to M4P_[i_dfit] }
220 continue to 27/32 between M4P_[np_dfit-2] and M4P_[np_dfit-1] \
221 then to 21/32 between M4P_[np_dfit-1] and M4P_[np_dfit] \
222 then to M4P_[np_dfit]) }
223
224 # histbins { data array name, n, [min], [max],
225 # nbins, bin array name )
226 # Generate the distribution of n values in
227 # dataarray. If given, arg3 and arg4 specify
228 # maximum and minimum data values, otherwise they
229 # are calculated. Bins have index 0 to arg5-1
230 define histbins { # dataarray, n, [min], [max], nbins, binarray
231 { if "$3" == "" then { arraymin($1,$2,mn_histb,n_histb)} else { n_histb = $3 }
232 if "$4" == "" then { arraymax($1,$2,mx_histb,m_histb)} else { m_histb = $4 }
233 f_histb = ($5-0.001)/(m_histb-n_histb)
234 for i_histb=0 to $5-1 do { $6[i_histb] = 0 }
235 for i_histb=1 to $2 do {
236 x_histb = floor(($1[i_histb]-n_histb)*f_histb)
237 if (x_histb >= 0) && (x_histb < $5) then { $6[x_histb] += 1 } }
238 } }
239
240 # dpquicksort(a,lo,hi,ix)
241 # Given array a[lo:hi] and index
242 # array ix[lo:hi] = lo,lo+1,lo+2,...hi,
243 # sort a[lo:hi] and do identical exchanges on ix
244 define dpquicksort { [ if $3 > $2 then {
245 pivot = $1[($2+($3))/2]
246 loop(lo = $2; hi = $3, lo <= hi,
247 loop(,$1[lo] < pivot, lo += 1 )
248 loop(,$1[hi] > pivot, hi -= 1 )
249 if lo < hi then {
250 tmp = $1[lo]; $1[lo] := $1[hi]; $1[hi] := tmp
251 tmp = $4[lo]; $4[lo] := $4[hi]; $4[hi] := tmp }
252 if lo <= hi then { lo += 1; hi -= 1 } )
253 if hi > $2 then { exec sprintf("dpquicksort($1,%g,%g,$4)",$2,hi) }
254 if lo < $3 then { exec sprintf("dpquicksort($1,%g,%g,$4)",lo,$3) }
255 } ] }
256
257 # dprot(radians,x,y)
258 # Evaluates to a rotated pair (like m4 rot_ )
259 define dprot { cos($1)*($2)-sin($1)*($3),sin($1)*($2)+cos($1)*($3) }
260
261 # dprtext(degrees,text)
262 # Rotated pstricks or pgf text in a [] box
263 define dprtext {[ if "$1"=="" then { a = 90 } else { a = $1 }
264 if dpicopt==optPSTricks then {
265 sprintf("\rput[c]{%g}(0,0)",a)+"{$2}"} else {
266 if dpicopt==optPGF then {
267 sprintf("\pgftext[rotate=%g]",a)+"{$2}" } else { "$2" }}
268 ]}
269
270 # rgbtohsv(r,g,b,h,s,v)
271 # rgb color triple to hsv with h range 0 to 360
272 define rgbtohsv { $4 = 0; $5 = 0; $6 = 0
273 [r = $1; g = $2; b = $3
274 maxc = max(max(r,g),b)
275 minc = min(min(r,g),b)
276 if maxc==minc then { $4 := 0 } \
277 else {if maxc == r then {
278 $4 := pmod(60*((g-b)/(maxc-minc)),360) } \
279 else {if maxc == g then {
280 $4 := 60*((b-r)/(maxc-minc)) + 120 } \
281 else { $4 := 60*((r-g)/(maxc-minc)) + 240 }}}
282 if maxc == 0 then { $5 := 0 } else { $5 := 1 - (minc/maxc) }
283 $6 := maxc
284 ] }
285
286 # hsvtorgb(h,s,v,r,g,b)
287 # hsv color triple to rgb, h has range 0 to 360
288 define hsvtorgb { $4 = 0; $5 = 0; $6 = 0
289 [h = pmod($1,360)/60; s = $2; v = $3
290 i = floor(h)
291 f = h-i
292 m = v*(1-s)
293 n = v*(1-s*f)
294 k = v*(1-s*(1-f))
295 case(i+1,
296 $4 := v; $5 := k; $6 := m,
297 $4 := n; $5 := v; $6 := m,
298 $4 := m; $5 := v; $6 := k,
299 $4 := m; $5 := n; $6 := v,
300 $4 := k; $5 := m; $6 := v,
301 $4 := v; $5 := m; $6 := n)
302 ] }
303
304 # cmyktorgb(c,m,y,k,r,g,b)
305 # cmyk colors in percent to rgb
306 define cmyktorgb {
307 $5 = 1-min(1,($1+$4)/100)
308 $6 = 1-min(1,($2+$4)/100)
309 $7 = 1-min(1,($3+$4)/100)
310 }
311
312 # rgbtocmyk(r,g,b,c,m,y,k)
313 # rgb to cmyk colors out of 100
314 define rgbtocmyk {
315 $7 = min(1-$1,min(1-$2,1-$3))*100
316 $4 = (1-$7-$1)/(1-$7)*100
317 $5 = (1-$7-$2)/(1-$7)*100
318 $6 = (1-$7-$3)/(1-$7)*100 }
319
320 # DefineRGBColor(colorname,r,g,b)
321 # Arguments are in the range 0 to 1
322 # Define dpic macro colorname according to the
323 # postprocessor specified by dpic command-line
324 # option; colorname then evaluates to a string
325 # See m4 macro definergbcolor which gives a
326 # color name
327 define DefineRGBColor {
328 case(abs(dpicopt), # The order of the following is defined in dpic source:
329 # MFpic:
330 command sprintf("\mfpdefinecolor{_$1__}{rgb}{%g,%g,%g}",$2,$3,$4)
331 define $1 {"_$1__"} ,
332 # Mpost:
333 define $1 {sprintf("(%g,%g,%g)",$2,$3,$4)} ,
334 # PDF:
335 define $1 {sprintf("%g %g %g",$2,$3,$4)} ,
336 # PGF:
337 command sprintf("\definecolor{_$1__}{rgb}{%g,%g,%g}",$2,$3,$4)
338 define $1 {"_$1__"} ,
339 # Pict2e:
340 command sprintf("\definecolor{_$1__}{rgb}{%g,%g,%g}",$2,$3,$4)
341 define $1 {"_$1__"} ,
342 # PS:
343 define $1 {sprintf("%g %g %g",$2,$3,$4)} ,
344 # PSfrag:
345 define $1 {sprintf("%g %g %g",$2,$3,$4)} ,
346 # PSTricks:
347 command sprintf("\definecolor{_$1__}{rgb}{%g,%g,%g}",$2,$3,$4)
348 define $1 {"_$1__"} ,
349 # SVG:
350 define $1 {sprintf("rgb(%g,%g,%g)",int($2*255),int($3*255),int($4*255))} ,
351 # TeX:
352 command sprintf("\definecolor{_$1__}{rgb}{%g,%g,%g}",$2,$3,$4)
353 define $1 {"_$1__"} ,
354 # tTeX:
355 command sprintf("\definecolor{_$1__}{rgb}{%g,%g,%g}",$2,$3,$4)
356 define $1 {"_$1__"} ,
357 # xfig:
358 define $1 {"black"}
359 ) }
360
361 # DefineHSVColor(colorname,h,s,v)
362 # Like DefineRGBColor but takes arguments
363 # h in [0,360], s in [0,1], and v in [0,1]
364 define DefineHSVColor { hsvtorgb($2,$3,$4,r_HSVRGB,g_HSVRGB,b_HSVRGB)
365 DefineRGBColor($1,r_HSVRGB,g_HSVRGB,b_HSVRGB) }
366
367 # DefineCMYKColor(colorname,c,m,y,k)
368 # Like DefineRGBColor but arguments in percent
369 define DefineCMYKColor { cmyktorgb($2,$3,$4,r_CMYKRGB,g_CMYKRGB,b_CMYKRGB)
370 DefineRGBColor($1,r_CMYKRGB,g_CMYKRGB,b_CMYKRGB) }
371
372 # ShadeObject(DrawRoutineName, n, colorseq)
373 # colorseq = frac0,r0,g0,b0,
374 # frac1,r1,g1,b1,
375 # ...
376 # fracm,rm,gm,bm
377 # with 0 <= frac0 < frac1 < ... < fracm <= 1
378 # (Often frac0 = 0 and fracm = 1)
379 #
380 # calls DrawRoutineName(frac,r,g,b)
381 # n+1 times for frac =
382 # frac0, frac0+1/n*(fracm-frac0),
383 # frac0+2/n*(fracm-frac0), ... fracm
384 # (i.e., often frac = 0, 1/n, 2/n, ... 1)
385 # with rgb args interpolated in hsv space
386 # between colorseq points
387 # eg B: box
388 # define HorizShade { line right B.wid thick B.ht/100/(1bp__) \
389 # from (0,-($1)*B.ht) outlined rgbstring($2,$3,$4) }
390 # ShadeObject(HorizShade, 100, 0,1,0,0, 1,0,0,1) at B
391 # i.e., 101 lines, starting at red, finishing at blue, overlaying box B
392 #
393 define ShadeObject { [ Origin: Here; nSteps = max(abs($2),1)
394 nextF = $3; nextR = $4; nextG = $5; nextB = $6; lastF = nextF; frac0 = nextF
395 fracn = frac0; nextarg = 3; for done = 0 to 1 do { if nextarg < $+ then \
396 { exec sprintf("fracn = $%g",nextarg); nextarg +=4; done = 0 } }
397 if frac0 <= fracn then { $1(frac0,nextR,nextG,nextB) }
398 nextarg = 7
399 if $2 < 0 then { rgbtohsv(nextR,nextG,nextB, nextH,nextS,nextV) } \
400 else { rgbtohsv(nextR^2,nextG^2,nextB^2, nextH,nextS,nextV) }
401 for stepnum = 0 to nSteps do {
402 currF = frac0+stepnum/nSteps*(fracn-frac0)
403 if currF >= nextF then {
404 if nextarg >= $+ then { stepnum = nSteps } \
405 else { lastF = nextF; lastH = nextH; lastS = nextS; lastV = nextV
406 exec sprintf("nextF = $%g; nextR = $%g; nextG = $%g; nextB = $%g",\
407 nextarg,nextarg+1,nextarg+2,nextarg+3);
408 nextarg +=4 } }
409 if stepnum <= nSteps then {
410 if nextF != lastF then {
411 rgbtohsv(nextR^2,nextG^2,nextB^2,nextH,nextS,nextV)
412 if lastS == 0 then { lastH = nextH }
413 if nextS == 0 then { nextH = lastH }
414 if lastH-nextH > 180 then { nextH += 360 } \
415 else { if nextH-lastH > 180 then { lastH +=360 } } }
416 if nextF > lastF then {
417 x = (currF-lastF)/(nextF-lastF)
418 currH = lastH*(1-x) + nextH*x
419 currS = lastS*(1-x) + nextS*x
420 currV = lastV*(1-x) + nextV*x
421 hsvtorgb(currH,currS,currV,cRsq,cGsq,cBsq)
422 if (currF >= frac0) && (currF <= fracn) then {
423 if $2 < 0 then { $1(currF,cRsq,cGsq,cBsq) } \
424 else { $1(currF,sqrt(abs(cRsq)),sqrt(abs(cGsq)),sqrt(abs(cBsq))) } } }
425 } }
426 exec sprintf("$%g",nextarg)
427 ] }
428
429 # Useful for debugging:
430 # Print Pos:(Pos.x,Pos.y)
431 define prpos { { print sprintf("$1:(%g,%g)",($1).x,($1).y) } }
432
433 define prval { print sprintf("$1=%g",$1) }
434 define prval2 { print sprintf("$1=%g, $2=%g",$1,$2) }
435 define prval3 { print sprintf("$1=%g, $2=%g, $3=%g",$1,$2,$3) }
436
437 # prow(array name,rowno,lo,hi)
438 # print array[rowno,lo:hi] as a row
439 # rowno can be omitted, e.g.,
440 # array(x,6,4,5); prow(x,1,3)
441 define prow {
442 sh "echo -n \"print \\"\" > $1_prow"
443 if ($+ < 4) || ("$2"=="") then {
444 for i_prow=$2 to $3-1 do {
445 sh sprintf("echo -n \"%g \" >> $1_prow", $1[i_prow]) }
446 sh sprintf("echo \"%g\\"\" >> $1_prow", $1[$3])
447 } \
448 else {
449 for i_prow=$2 to $3-1 do {
450 sh sprintf("echo -n \"%g \" >> $1_prow", $1[($4,i_prow)]) }
451 sh sprintf("echo \"%g\\"\" >> $1_prow", $1[($4,$3)])
452 }
453 copy "$1_prow"
454 sh "rm $1_prow"
455 }
456
457 define rnd {int($1+sign($1)/2)} # round function
458
459 # Operations on complex numbers (x,y)
460 define Zsum {($1+($2))}
461 define Zdiff{($1-($2))}
462 define Zprod{(($1).x*($2).x-($1).y*($2).y,($1).y*($2).x+($1).x*($2).y)}
463 define Zinv {(($1).x/(($1).x^2+($1).y^2),-($1).y/(($1).x^2+($1).y^2))}
464 define Zexp {((cos(($1).y),sin(($1).y))*expe(($1).x))}
465 define Zcos {(cos(($1).x)*cosh(($1).y),-sin(($1).x)*sinh(($1).y))}
466 define Zsin {(sin(($1).x)*cosh(($1).y), cos(($1).x)*sinh(($1).y))}
467 define zabs {sqrt(($1).x^2+($1).y^2)}
468 define zarg {atan2(($1).y,($1).x)}
469 # Trig functions if undefined
470 if "cosh"=="co"+"sh" then {
471 define cosh {((expe($1) + expe(-($1)))/2)}
472 define sinh {((expe($1) - expe(-($1)))/2)}
473 }
474
475 ## dpic equivalents or almost equivalents to libgen.m4 routines ########
476 ## Including them here has to be regarded as experimental for now ######
477
478 define cosd {cos(($1)*dtor_)}
479 define sind {sin(($1)*dtor_)}
480
481 define ceiling {(-floor(-($1)))}
482 define round_ {int($1+sign($1)/2)}
483
484 define bp__ {*(scale/72)} # Absolute Adobe point
485 define pt__ {*(scale/72.27)} # Absolute TeX point
486 define pc__ {*(12*scale/72.27)} # Absolute Pica
487 define in__ {*scale} # Absolute inch
488 define cm__ {*(scale/2.54)} # Absolute cm
489 define mm__ {*(scale/25.4)} # Absolute mm
490 define lthick {(linethick bp__)}
491 if dpicopt==optSVG then { define px__ {*(scale/dpPPI)} } \
492 else { define px__ {*(scale/96)} } # Absolute pixels
493
494 define assign_dpicvars {
495 # print " *** dpic: dpictools.pic processed"
496 define dpictools_ {1}
497 ld__ = 0
498 rtod_ = 57.295779513082323
499 dtor_ = 0.017453292519943295
500 twopi_ = 6.2831853071795862
501 pi_ = twopi_/2
502 $1
503 }
504 # Polar to rectangular conversion
505 define Rect_ {($1)*cos(($2)*dtor_),($1)*sin(($2)*dtor_)}
506
507 # intersect_(Start1,End1,Start2,End2)
508 # Intersection of lines joining named positions
509 define intersect_ {((($3.x-$1.x)*($3.y-$4.y)-($3.y-$1.y)*($3.x-$4.x))/\
510 (($2.x-$1.x)*($3.y-$4.y)-($2.y-$1.y)*($3.x-$4.x)) \
511 between $1 and $2) }
512 # Intersect_(Name1,Name2)
513 # Intersection of named lines
514 define Intersect_ {intersect_($1.start,$1.end,$2.start,$2.end)}
515
516 # drawdir_(degrees)
517 # Nearest multiple of 90
518 define drawdir_ {(int(pmod($1+45,360)/90)*90)}
519
520 # vlength(x,y) 2-D length
521 define vlength {sqrt(abs(($1)^2+($2)^2))}
522
523 # distance(Pos1,Pos2) distance between positions
524 define distance {vlength(($1).x-($2).x,($1).y-($2).y)}
525
526 # linang(linear obj) angle of linear object
527 define linang {atan2($1.end.y-$1.start.y,$1.end.x-$1.start.x)}
528
529 # posang(Position) angle of position wrt (0,0)
530 define posang {atan2(($1).y,($1).x)}
531
532 # cangle(A,B,C) angle at B of line A to B to C
533 define cangle { (posang(($1-($2)))-posang(($3-($2)))) }
534
535 # For PGF, PSTricks, or SVG only:
536 define dpshade { beginshade($1); $2; endshade } # like libgen shade()
537
538 # beginshade
539 if dpicopt==optPGF then {
540 define beginshade { if "$1"!="" then { dpshade_=$1 } else { dpshade_=0.5 }
541 command "\global\let\dpicshdraw=\dpicdraw\global\def\dpicdraw{}"
542 command "\global\def\dpicstop{--}"
543 command sprintf("\dpicshdraw[fill=white!%g!black]",dpshade_*100) } } \
544 else { if dpicopt==optPSTricks then {
545 define beginshade { if "$1"!="" then { dpshade_=$1 } else { dpshade_=0.5 }
546 command sprintf("\newgray{m4fillv}{%g}",dpshade_)
547 command sprintf("\pscustom[fillstyle=solid,fillcolor=m4fillv]{%%") } } \
548 else { if dpicopt==optSVG then {
549 define beginshade { if "$1"!="" then { dpshade_=$1 } else { dpshade_=0.5 }
550 command sprintf("<g fill=\"rgb(%g,%g,%g)\">",int(dpshade_*255+0.5),\
551 int(dpshade_*255+0.5),int(dpshade_*255+0.5))} } }}
552
553 # endshade
554 if dpicopt==optPGF then {
555 define endshade {command "cycle; \
556 \global\let\dpicdraw=\dpicshdraw\global\def\dpicstop{;}"} } \
557 else { if dpicopt==optPSTricks then {
558 define endshade {command "}%"} } \
559 else { if dpicopt==optSVG then {
560 define endshade { command "</g>"} } }}
561
562 # rgbstring
563 if dpicopt==optPGF then {
564 define rgbstring \
565 {sprintf("{rgb,1:red,%7.5f;green,%7.5f;blue,%7.5f}",$1,$2,$3)} } \
566 else { if dpicopt==optPSTricks then {
567 define rgbstring \
568 {sprintf("{rgb,1:red,%7.5f;green,%7.5f;blue,%7.5f}",$1,$2,$3)} } \
569 else { if dpicopt==optSVG then {
570 define rgbstring {sprintf("rgb(%g,%g,%g)",\
571 int(($1)*255+0.5),int(($2)*255+0.5),int(($3)*255+0.5))} } }}
572
573 #######################################################################
574
575 assign_dpicvars()
576
577 # dpictools end