Debian Codegraph @

texmf-dist/doc/latex/circuit-macros/examples/Shapedefs.m4

raw
1 ifdef(`View3d',,`threeD_init')
2 divert(-1)
3 # Shapedefs.m4
4 # 3D structures with complex surfaces and hidden lines are the domain of more
5 # sophisticated software packages, but basic shapes can be drawn with pic.
6
7 #####################################################
8
9 `cylinder3D(xdispl,ydispl,zdispl,diameter,
10 [colored "color"])'
11 define(`cylinder3D',`[ Start: (0,0)
12 xdispl = `$1'; ydispl = `$2'; zdispl = `$3'
13 define(`M4CL',`xdispl,ydispl,zdispl')dnl
14 lenc = length3D(M4CL)
15 diamc = ifelse(`$4',,(lenc/2),`$4') ; radc = diamc/2
16 cs = dot3D(View3D,M4CL)/lenc
17 End: Project(M4CL)
18 az = atan2(ydispl,xdispl)
19 ae = atan2(zdispl,sqrt(xdispl^2+ydispl^2))
20 CL: line invis from Start to End; C: CL.c
21 nls = int(twopi_*radc/lthick)
22 define(`M4PP',`rot3Dz(az,rot3Dy(-ae,rot3Dx(-i/nls*twopi_,0,0,radc)))')dnl
23 for i=0 to nls-1 do { if dot3D(View3D,M4PP) > 0 then {
24 P: Project(M4PP)
25 ql = sqrta(dot3D(Light3D,M4PP)/radc)
26 line from P to End+P outlined rgbstring(ql,ql,ql) `$5' } }
27 Perp: (vperp(CL,radc))
28 if cs > 0 then { T: End; B: Start } else { T: Start; B: End }
29 rpoint_(from Start to Perp)
30 css = sqrta(cs)
31 Top: rotellipse(diamc,diamc*cs,
32 thick linethick/3 shaded rgbstring(css,css,css)) with .c at T
33 W: line thick linethick/3 from T+Perp to B+Perp
34 Bot: ellipsearc(diamc,diamc*cs,pi_,twopi_,rp_ang,cw,
35 thick linethick/3) with .C at B
36 E: line thick linethick/3 from B-Perp to T-Perp
37 rpoint_(from Start to End)
38 `$6']')
39
40 `cone3D(xdispl,ydispl,zdispl,diameter,
41 [colored "color"])'
42 `internal positions:
43 Start (centre of base),
44 End (point),
45 C (axis centre)'
46 define(`cone3D',`[ Start: (0,0)
47 xdispl = `$1'; ydispl = `$2'; zdispl = `$3'
48 define(`M4CL',`xdispl,ydispl,zdispl')dnl
49 lenc = length3D(M4CL)
50 diamc = ifelse(`$4',,(lenc/2),`$4') ; radc = diamc/2
51 End: Project(M4CL)
52 cs = dot3D(View3D,M4CL)/lenc
53 az = atan2(ydispl,xdispl)
54 ae = atan2(zdispl,sqrt(xdispl^2+ydispl^2))
55 CL: line invis from Start to End; C: CL.c
56 nls = int(twopi_*radc/lthick)
57 lt = sqrt(radc^2+lenc^2)
58 b = 0
59 for i=0 to nls-1 do { if dot3D(View3D,
60 rot3Dz(az,rot3Dy(-ae,rot3Dx(-i/nls*twopi_,radc,0,lenc)))) > 0 then {
61 ql = sqrta(dot3D(Light3D,
62 rot3Dz(az,rot3Dy(-ae,rot3Dx(-i/nls*twopi_,radc,0,lenc))))/lt)
63 P: Project(rot3Dz(az,rot3Dy(-ae,rot3Dx(-i/nls*twopi_,0,0,radc))))
64 line from P to End outlined rgbstring(ql,ql,ql) `$5'
65 if !b then { WB: P; b = 1 }
66 } else { if b then { BW: P; b = 0 } } }
67 if vlength(CL.x,CL.y) != 0 then { Perp: (vperp(CL,radc,R)) } \
68 else { Perp: (radc,0) }
69 if cs >= 0 then { T: End; B: Start } else {T: Start; B: End }
70 rpoint_(from Start to Perp)
71 pout = (vlength(End.x,End.y) > abs(radc*cs))
72 if cs < 0 then { css = sqrta(cs)
73 if pout then { line thick linethick/3 from BW to End then to WB }
74 Bot: rotellipse(diamc,diamc*cs,thick linethick/3 \
75 ifelse(`$5',,`shaded rgbstring(css,css,css)',`$5')) with .c at T } \
76 else { if pout then {
77 line thick linethick/3 from BW to End then to WB
78 a = acos((WB.x*Perp.x+WB.y*Perp.y)/radc^2)
79 Bot: ellipsearc(diamc,diamc*cs,a,pi_-a,rp_ang,cw, thick linethick/3) \
80 with .C at B } \
81 else {
82 Bot: rotellipse(diamc,diamc*cs,thick linethick/3) with .c at B } }
83 rpoint_(from Start to End)
84 `$6']')
85 `Arrow3D(xdispl,ydispl,zdispl,
86 diam,arrowwid,arrowht)'
87 define(`Arrow3D',`[ diam_Ar = ifelse(`$4',,linewid/10,`$4')
88 wid_Ar = ifelse(`$5',,diam_Ar*2,`$5')
89 ht_Ar = ifelse(`$6',,wid_Ar*2,`$6')
90 x_Ar = ifelse(`$1',,1,`$1')
91 y_Ar = ifelse(`$2',,1,`$2')
92 z_Ar = ifelse(`$3',,1,`$3')
93 define(`M4Arrow3D',`x_Ar,y_Ar,z_Ar')dnl
94 len_Ar = length3D(M4Arrow3D)
95 len_Shaft = len_Ar - ht_Ar
96 cs_Ar = dot3D(View3D,M4Arrow3D)
97 if cs_Ar >= 0 then {
98 Shaft: cylinder3D(sprod3D(len_Shaft/len_Ar,M4Arrow3D),diam_Ar)
99 Head: cone3D(sprod3D(ht_Ar/len_Ar,M4Arrow3D),wid_Ar) \
100 with .Start at Shaft.End } \
101 else {
102 Head: cone3D(sprod3D(ht_Ar/len_Ar,M4Arrow3D),wid_Ar)
103 Shaft: cylinder3D(sprod3D(len_Shaft/len_Ar,M4Arrow3D),diam_Ar) \
104 with .End at Head.Start }
105 Start: Shaft.Start; End: Head.End
106 `$7']')
107
108 define(`f2xyz3D',``$1'x = `$2'
109 `$1'y = `$3'
110 `$1'z = `$4' ')
111
112 `prism3D(xdispl,ydispl,zdispl,endwid,endht,
113 (rad)rotation)
114 args1-3 are the 3D coords of .End wrt .Start
115 arg6 is rotation about x axis
116 Defined points .Start, .End, .C'
117 define(`prism3D',`[ Start: (0,0)
118 xdispl = ifelse(`$1',,linewid,`$1')
119 ydispl = ifelse(`$2',,linewid,`$2')
120 zdispl = ifelse(`$3',,linewid,`$3')
121 widp = ifelse(`$4',,(linewid/5),`$4')
122 htp = ifelse(`$5',,(linewid/10),`$5')
123 rotp = ifelse(`$6',,0,`$6')
124 End: Project(xdispl,ydispl,zdispl); C: 0.5 between Start and End
125 lenp = length3D(xdispl,ydispl,zdispl)
126 az = atan2(ydispl,xdispl); ae = atan2(zdispl,sqrt(xdispl^2+ydispl^2))
127 wp2 = widp/2; hp2 = htp/2
128 # 3D coords of corners (B=back, F=front)
129 f2xyz3D(nwB,rot3Dz(az,rot3Dy(-ae,rot3Dx(rotp, 0,-wp2, hp2))))
130 f2xyz3D(swB,rot3Dz(az,rot3Dy(-ae,rot3Dx(rotp, 0,-wp2,-hp2))))
131 f2xyz3D(seB,rot3Dz(az,rot3Dy(-ae,rot3Dx(rotp, 0, wp2,-hp2))))
132 f2xyz3D(neB,rot3Dz(az,rot3Dy(-ae,rot3Dx(rotp, 0, wp2, hp2))))
133 f2xyz3D(nwF,rot3Dz(az,rot3Dy(-ae,rot3Dx(rotp,lenp,-wp2, hp2))))
134 f2xyz3D(swF,rot3Dz(az,rot3Dy(-ae,rot3Dx(rotp,lenp,-wp2,-hp2))))
135 f2xyz3D(seF,rot3Dz(az,rot3Dy(-ae,rot3Dx(rotp,lenp, wp2,-hp2))))
136 f2xyz3D(neF,rot3Dz(az,rot3Dy(-ae,rot3Dx(rotp,lenp, wp2, hp2))))
137 # facets
138 M4Facet3D(neF,nwF,swF,seF)
139 M4Facet3D(neB,seB,swB,nwB)
140 M4Facet3D(neF,seF,seB,neB)
141 M4Facet3D(neF,neB,nwB,nwF)
142 M4Facet3D(seF,swF,swB,seB)
143 M4Facet3D(nwF,nwB,swB,swF)
144 `$7']')
145
146 `M4Facet3D( a,b,c,d )'
147 define(`M4Facet3D',`
148 define(`PFacet',`cross3D(diff3D(`$2'x,`$2'y,`$2'z,`$1'x,`$1'y,`$1'z),
149 diff3D(`$3'x,`$3'y,`$3'z,`$2'x,`$2'y,`$2'z))')
150 if dot3D(View3D,PFacet) > 0 then {
151 fFacet = sqrta(dot3D(Light3D,PFacet)/length3D(PFacet))
152 P1: Project(`$1'x,`$1'y,`$1'z)
153 P2: Project(`$2'x,`$2'y,`$2'z)
154 P3: Project(`$3'x,`$3'y,`$3'z)
155 P4: Project(`$4'x,`$4'y,`$4'z)
156 line fill_(fFacet) invis from P1 to P2 then to P3 then to P4 then to P1
157 line from P1 to P2; round
158 line to P3; round
159 line to P4; round
160 line to P1; round } ')
161
162 `M4Pgram3D( c,dne,dnw )'
163 define(`M4Pgram3D',`
164 f2xyz3D(ne,sum3D(`$1'x,`$1'y,`$1'z,`$3'x,`$3'y,`$3'z))
165 f2xyz3D(nw,sum3D(`$1'x,`$1'y,`$1'z,`$2'x,`$2'y,`$2'z))
166 f2xyz3D(sw,diff3D(`$1'x,`$1'y,`$1'z,`$2'x,`$2'y,`$2'z))
167 f2xyz3D(se,diff3D(`$1'x,`$1'y,`$1'z,`$3'x,`$3'y,`$3'z))
168 define(`PPgrapm',`cross3D(
169 diff3D(nwx,nwy,nwz,nex,ney,nez),
170 diff3D(swx,swy,swz,nwx,nwy,nwz))')
171 if dot3D(View3D,PPgram) > 0 then {
172 fPgram = sqrta(dot3D(Light3D,PPgram)/length3D(PPgram))
173 P1: Project(`$1'x,`$1'y,`$1'z)
174 P2: Project(`$2'x,`$2'y,`$2'z)
175 P3: Project(`$3'x,`$3'y,`$3'z)
176 P4: Project(`$4'x,`$4'y,`$4'z)
177 line fill_(fPgram) invis from P1 to P2 then to P3 then to P4 then to P1
178 line from P1 to P2; round
179 line to P3; round
180 line to P4; round
181 line to P1; round } ')
182 `prism3D(
183 xdispl,ydispl,zdispl,endwid,endht,(rad)rotation,
184 index,dataname,r,g,b)
185 Defined points .Start, .End, .C
186 args1-3 are the input 3D coords of .End wrt .Start
187 args4-5 define the end dimensions
188 arg6 is rotation about x axis
189 arg7 is the current depth of the data vector
190 arg8 is the name (e.g. X) of the vector of
191 stored face distance, color, and projected coords:
192 eg X0[n]=[d,r]; X1[n]=[g,b]
193 X2[n]=proj(C); X3[n]=proj(NE); X4[n]=proj(NW)'
194 define(`prism3D',`[ Start: (0,0)
195 xdispl = ifelse(`$1',,linewid,`$1')
196 ydispl = ifelse(`$2',,linewid,`$2')
197 zdispl = ifelse(`$3',,linewid,`$3')
198 widp = ifelse(`$4',,(linewid/5),`$4')
199 htp = ifelse(`$5',,(linewid/10),`$5')
200 rotp = ifelse(`$6',,0,`$6')
201 End: Project(xdispl,ydispl,zdispl); C: 0.5 between Start and End
202 lenp = length3D(xdispl,ydispl,zdispl)
203 az = atan2(ydispl,xdispl); ae = atan2(zdispl,sqrt(xdispl^2+ydispl^2))
204 wp2 = widp/2; hp2 = htp/2
205 # 3D coords of corners (B=back, F=front)
206 f2xyz3D(nwB,rot3Dz(az,rot3Dy(-ae,rot3Dx(rotp, 0,-wp2, hp2))))
207 f2xyz3D(swB,rot3Dz(az,rot3Dy(-ae,rot3Dx(rotp, 0,-wp2,-hp2))))
208 f2xyz3D(seB,rot3Dz(az,rot3Dy(-ae,rot3Dx(rotp, 0, wp2,-hp2))))
209 f2xyz3D(neB,rot3Dz(az,rot3Dy(-ae,rot3Dx(rotp, 0, wp2, hp2))))
210
211 f2xyz3D(nwF,rot3Dz(az,rot3Dy(-ae,rot3Dx(rotp,lenp,-wp2, hp2))))
212 f2xyz3D(swF,rot3Dz(az,rot3Dy(-ae,rot3Dx(rotp,lenp,-wp2,-hp2))))
213 f2xyz3D(seF,rot3Dz(az,rot3Dy(-ae,rot3Dx(rotp,lenp, wp2,-hp2))))
214 f2xyz3D(neF,rot3Dz(az,rot3Dy(-ae,rot3Dx(rotp,lenp, wp2, hp2))))
215 # facets
216 M4Facet3D(neF,nwF,swF,seF)
217 M4Facet3D(neB,seB,swB,nwB)
218 M4Facet3D(neF,seF,seB,neB)
219 M4Facet3D(neF,neB,nwB,nwF)
220 M4Facet3D(seF,swF,swB,seB)
221 M4Facet3D(nwF,nwB,swB,swF)
222 `$7']')
223
224
225 #####################################################
226 divert(0)dnl