Debian Codegraph @

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

raw
1 .PS
2 # graysurf.m4
3 threeD_init
4 NeedDpicTools
5
6 Overall: [
7
8 Surface:[
9 # This just draws the facets back to front
10
11 setview(20,30)
12 dimen = 1.5
13
14 xmax = 2*dimen
15 ymax = 1.5*dimen
16 zmax = 1*dimen
17
18 define(`fn',
19 `expe(-(`$1')/tt)*cos((`$2')/xmax*1.75*twopi_)*cos((`$1')/ymax*twopi_)')
20
21 nx = 24; dx = xmax/nx
22 ny = 30; dy = ymax/ny
23 tt = 1.5
24 fnmax = fn(0,0,0)
25
26 Orig: (0,0)
27 linethick_(1.2)
28 arrow from Orig to 1.2 <Orig,(Project(xmax,0,0))> ; "$x$" below
29 arrow from Orig to 1.2 <Orig,(Project(0,ymax,0))> ; "$y$" ljust
30 arrow from Orig to 1.2 <Orig,(Project(0,0,zmax))> ; "$z$" rjust
31
32 linethick_(0)
33 for i = 0 to nx-1 do {
34 x = xmax*i/nx
35 for j = 0 to ny-1 do {
36 y = ymax*j/ny
37 fnxy = fn(x,y)
38 shade((fnmax+fnxy)/fnmax/2,
39 line from (Project(x,y,fnxy)) to (Project(x,(y+dy),`fn(x,(y+dy))'))\
40 then to (Project((x+dx),(y+dy),`fn((x+dx),(y+dy))'))\
41 then to (Project((x+dx),y,`fn((x+dx),y)'))\
42 then to (Project(x,y,fnxy))
43 ) } }
44 ] #with .w at last [].e+(-0.2,0)
45
46 Torus: [
47 # Calculate all the facet centres but draw
48 # only a subset of them after sorting
49
50 viewazimuth = 20 # Set view angles in degrees
51 viewelevation = 30
52
53 setview(viewazimuth,viewelevation,0)
54
55 sfactor = 0.7
56 tradius = 2*sfactor
57 sradius = 0.75*sfactor
58
59 define(`torus',`rot3Dz((`$1')*dtor_,
60 sum3D(tradius,0,0,rot3Dy((`$2')*dtor_,sradius,0,0)))')
61
62 define(`tvisible',
63 `dot3D(View3D, rot3Dz((`$1')*dtor_,rot3Dy((`$2')*dtor_,1,0,0)))')
64
65 Orig: (0,0)
66 X: arrow from Orig to (Project(tradius+sradius*4,0,0)); "x" rjust
67 Y: arrow from Orig to (Project(0,tradius+sradius*2,0)); "y" ljust
68
69 dt = 10 # major angle degrees per facet
70 ds = 10 # minor angle degrees per facet
71
72 # create the arrays and sort
73 n = 0
74 for t = dt to 360+dt/2 by dt do {
75 for s=ds/2 to 360.1 by ds do {
76 if tvisible(t,s) >= 0 then {
77 n +=1
78 t[n] = t
79 s[n] = s
80 d[n] = dot3D(torus(t,s),view3D1,view3D2,view3D3) # view distance
81 ix[n] = n
82 } } }
83 #prval(n)
84 dpquicksort(d,1,n,ix)
85
86 # draw the facets
87 thinlines_
88 for i = 1 to n do {
89 tc = t[ix[i]]; sc = s[ix[i]]
90 SE: (Project(torus(tc+dt/2,sc-ds/2)))
91 SW: (Project(torus(tc-dt/2,sc-ds/2)))
92 NW: (Project(torus(tc-dt/2,sc+ds/2)))
93 NE: (Project(torus(tc+dt/2,sc+ds/2)))
94 f = ((dcosine3D(3,torus(tc,sc))/sradius+1)/2)^2
95 g = min(f,0.9)
96 setrgb(g,g,g)
97 line fill f ifpdf( invis ) \
98 from SE to SW then to NW then to NE then to SE
99 # line invis shaded rgbstring(f,f^3,f^3) \
100 # f from SE to SW then to NW then to NE then to SE
101 resetrgb
102 }
103
104 thicklines_
105 arrow from Orig to (Project(0,0,tradius*1.4)); "z" above
106 dashline(from Orig to (Project(torus(0,0))),,,,G)
107 arrow to X.end
108 dashline(from Orig to (Project(torus(90,0))),,,,G)
109 arrow to Y.end
110
111 ] with .sw at last [].se+(0.2,0)
112
113 Hat: [
114 # Sort as for the torus but draw edges too
115
116 azimuth = 25 # View angles in degrees
117 elevation = 20
118 setview(azimuth,elevation)
119 # the 3D object: `hat(t,y)'
120 define(`hat',`cosd($1)*($2),sind($1)*($2),(($2)^2-1)^2')
121 define(`gradient',`(-4*($2)*(($2)^2-1)*cosd(`$1')),
122 (-4*($2)*(($2)^2-1)*sind(`$1')),1')
123 define edge {$2=dot3D(View3D,gradient($1,y))}
124 define(`hatvis',
125 `(($2 > 0.6) || (dot3D(View3D,gradient($1,$2)) > 0))')
126
127 # grid parameters
128 dang = 09
129 maxy = 1.25
130 dy = 0.05
131
132 Orig: (0,0)
133 X: arrow from Orig to (Project(maxy*1.2,0,0)); "x" rjust
134 Y: arrow from Orig to (Project(0,maxy*1.2,0)); "y" ljust
135 Xv: (Project(1,0,0))
136 Yv: (Project(0,1,0))
137 Zv: (Project(0,0,1))
138 # create the arrays and sort
139 n = 0
140 for t = dang/2 to 360.1 by dang do {
141 for y=dy/2 to maxy by dy do {
142 if hatvis(t,y) then {
143 n +=1
144 t[n] = t
145 y[n] = y
146 d[n] = dot3D(hat(t,y),View3D) #distance toward the front
147 ix[n] = n
148 } } }
149 #prval(n)
150 dpquicksort(d,1,n,ix)
151 # draw the facets
152 ellipse wid maxy*2 ht maxy*2*sind(elevation) \
153 with .c at (Project(0,0,(maxy^2-1)^2)+(0,linethick bp__/2))
154 thinlines_
155 for i = 1 to n do {
156 tc = t[ix[i]]; yc = y[ix[i]]
157 SE: (Project(hat(tc+dang/2,yc-dy/2)))
158 SW: (Project(hat(tc-dang/2,yc-dy/2)))
159 NW: (Project(hat(tc-dang/2,yc+dy/2)))
160 NE: (Project(hat(tc+dang/2,yc+dy/2)))
161 f = abs(yc^2-1)
162 line invis fill f from SE to SW then to NW then to NE then to SE
163 }
164
165 # Outline where the surface goes invisible
166 mm = int(maxy/dy)
167 nr = -1
168 for i=1 to mm do { y = i/(mm+3)
169 findroot(edge,(azimuth+2),(azimuth+180-2),1e-8,t)
170 nr +=1
171 R[mm+nr]: (Project(hat(t,y)))
172 R[mm-1-nr]: (-R[mm+nr].x,R[mm+nr].y)
173 }
174 fitcurve(R,mm*2-1)
175
176 # Z axis
177 thicklines_
178 Z: arrow from Zv to (Project(0,0,1.2)); "z" ljust
179 line dashed from Orig to Zv chop 0 chop dashwid/2
180 line dashed from Orig to Xv
181 line dashed from Orig to Yv
182
183 # Partial rim
184 ellipsearc(maxy*2,maxy*2*sind(elevation),-pi_*1.4,pi_/3) \
185 with .C at (Project(0,0,(maxy^2-1)^2)+(0,linethick bp__/2))
186 ] scaled 1.25 with .s at 1st [].ne+(0,-0.75)
187 ] scaled 0.85
188
189 .PE