Debian Codegraph @

texmf-dist/doc/latex/circuit-macros/examples/extras/Clocks.m4

raw
1 .PS
2 # AntiqueClock.m4
3 gen_init
4 NeedDpicTools
5 # https://tex.stackexchange.com/questions/236923/generate-analog-clock-with-numbered-face-add-seconds-roman-numerals
6
7 # `hms2deg(hr,min,sec) hr:min:sec to degrees
8 # blank arg1: degrees for minute hand
9 # blank arg1 and arg2: degrees for second hand'
10 define(`hms2deg',`ifelse(`$1',,
11 `ifelse(`$2',,
12 `(90-pmod(ifelse(`$3',,0,`$3'),60)/60*360)',
13 `(90-(pmod(`$2',60)/60 + pmod(ifelse(`$3',,0,`$3'),60)/60/60)*360)')',
14 `(90-(pmod(`$1',12) + pmod(ifelse(`$2',,0,`$2'),60)/60 + dnl
15 pmod(ifelse(`$3',,0,`$3'),60)/3600)/12*360)')')
16
17 # `SecondHand(length,hr,min,sec)'
18 define(`SecondHand',`[ shsf = (`$1')/3.2
19 C: Here
20 { L: rpoint_(to (Rect_(`$1',hms2deg(,,`$4')))) }
21 line thick 8*shsf to rvec_(`$1',0)
22 ]')
23
24 # `AntiqueMinuteHand(length,hr,min,sec)'
25 define(`AntiqueMinuteHand',`[ mhsf = (`$1')/2.84
26 L: rpoint_(to (Rect_(`$1',hms2deg(,`$3',`$4'))))
27 C: circle fill_(0) diam 0.47*mhsf at L.start
28 move to C
29 spline from rvec_(0,0.047*mhsf) \
30 to rvec_(1.65*mhsf,0.11*mhsf) \
31 then to rvec_(`$1',0.018*mhsf) \
32 then to rvec_(`$1',-0.018*mhsf) \
33 then to rvec_(1.65*mhsf,-0.11*mhsf) \
34 then to rvec_(0,-0.047*mhsf) \
35 shaded rgbstring(0,0,0)
36 ]')
37
38 # `AntiqueHourHand(length,hr,min,sec)'
39 define(`AntiqueHourHand',`[ hhsf = (`$1')/2.2
40 L: rpoint_(to (Rect_(`$1',hms2deg(`$2',`$3',`$4'))))
41 C: circle fill_(0) diam 0.6*hhsf at L.start
42 move to C
43 v = 0.25*hhsf
44 { line to rvec_(1.1*hhsf,0) thick 0.15/(1bp__)*hhsf }
45 { C1: circle rad v at rvec_(1.25*hhsf,0) fill_(0) }
46 d = `$1'-1.25*hhsf
47 q = 1bp__*hhsf
48 r1 = (d^2 + q^2 - v^2)/(v-q)/2
49 h = r1+v
50 shade(0,
51 arc ccw from C1+vec_(d/h*v,(r1+q)/h*v) to \
52 C1+vec_(d,q) rad r1 with .c at C1+vec_(d,r1+q)
53 arc ccw from C1+vec_(d,-q) to C1+vec_(d/h*v,-(r1+q)/h*v) rad r1 \
54 with .c at C1+vec_(d,-r1-q))
55 ]')
56
57 define(`AntiqueClock',`[ # h,m,s,diam
58 # Clock size parameters
59 # outer radius
60 hour = ifelse(`$1',,3,`$1')
61 minute = ifelse(`$2',,41,`$2')
62 second = ifelse(`$3',,51,`$3')
63 ifelse(`$4',,`skale=0.5; r1=2',`r1=(`$4')/2; skale=r1/4')
64 r2 = r1-0.5*skale
65 r3 = r2-0.14*skale
66 r4 = r3 - 0.35*skale
67 r5 = r4 - 0.17*skale
68 r6 = r5 - 0.63*skale
69 r7 = r6 - 0.17*skale
70 shadethick = 1.0
71
72 C: circle thick 0.2 rad r1
73
74 define shadeline {
75 s = 1-($`'1)*2
76 v = r*s
77 h = sqrt(r^2-v^2)
78 t = 1-abs(s)
79 line from (vrot_(-h,v,cost,sint)) to (vrot_(h,v,cost,sint)) \
80 thick shadethick outlined rgbstring(t,t,t)
81 }
82
83 r = r1 # Bezel outer
84 nlines = int(2*r/(shadethick pt__)*1.1)
85 cost = cosd(10); sint = sind(10);
86 ShadeObject(shadeline,nlines, 0, 0,0,0,
87 0.5, 1,1,1,
88 1, 0,0,0 ) at C
89
90 r = r2 # Bezel inner
91 nlines = int(2*r/(shadethick pt__)*1.1)
92 cost = cosd(-10); sint = sind(-10);
93 ShadeObject(shadeline,nlines, 0, 0,0,0,
94 0.25, 0.8,0.8,0.8,
95 0.5, 1,1,1,
96 0.75, 0.8,0.8,0.8,
97 1, 0,0,0 ) at C
98
99 # Clock face
100 Face: circle thick 0 fill_(1) rad r3 at C
101 circle rad r4 at C
102 circle rad r5 at C
103 circle rad r6 at C
104 circle rad r7 at C
105
106 define rotext {
107 sprintf("`$'1 ifpstricks(\rput[C]{%g}(0,0){%g},
108 ifpgf(\rotatebox{%g}{%g}))",`$'2,`$'3) }
109 # Outer numbers
110 command sprintf("\font\outerfont=cmss12 at %4.2fin",r3-r4)
111 for mn = -15 to 15 by 5 do { rotext(\outerfont,-mn/60*360,pmod(mn,60)) \
112 at C+(Rect_((r3+r4)/2,90-mn/60*360)) }
113 for mn = 20 to 40 by 5 do { rotext(\outerfont,180-mn/60*360,mn) \
114 at C+(Rect_((r3+r4)/2,90-mn/60*360)) }
115 # Outer tics
116 for mn = 1 to 60 do { t = 90-mn/60*360
117 line from C+(Rect_(r5,t)) to C+(Rect_(r4,t)) }
118
119 # Inner numbers
120 command sprintf("\font\innerfont=cmss12 at %4.2fin",r5-r6)
121 Loopover_(`mx',`t = (m4Lx-4)/12*360;
122 sprintf("\innerfont`'ifpstricks(`\rput[C]{%g}(0,0)',
123 `\rotatebox{%g}'){\scalebox{0.7}[1.0]{mx}}",-t) \
124 at C+(Rect_((r5+r6)/2,90-t))', IX,X,XI,XII,I,II,III)
125 Loopover_(`mx',`t = (m4Lx+3)/12*360;
126 sprintf("\innerfont`'ifpstricks(`\rput[C]{%g}(0,0)',
127 `\rotatebox{%g}'){\scalebox{.7}[1.0]{mx}}",-t+180) \
128 at C+(Rect_((r5+r6)/2,90-t))', IV,V,VI,VII,VIII)
129 # Inner tics
130 for mn = 5 to 60 by 5 do { t = 90-mn/60*360
131 line from C+(Rect_(r7,t)) to C+(Rect_(r6,t)) }
132
133 # Hands
134 AntiqueHourHand(r6,hour,minute,second) with .C at C
135 AntiqueMinuteHand(r5,hour,minute,second) with .C at C
136 SecondHand((r3+r4)/2,hour,minute,second) with .C at C
137
138 # Center
139 dot(at C,0.1/4*r1,1)
140 ]')
141
142 Clock1: AntiqueClock
143 # Clock2: AntiqueClock(4,50,07,2) at Clock1.e+(1.5,0)
144 Clock2: AntiqueClock(4,50,07,2) with .nw at Clock1.ne+(0.5,0)
145 Clock3: [
146 linethick = 1.5
147 arrowwid = 5 pt__
148
149 rgbfill(255/255,250/255,205/255,
150 Clock: circle rad 0.75 at (0,0) )
151
152 command "\newcounter{hour}"
153 for time=1 to 12 do {
154 sprintf("\setcounter{hour}{%g}\Roman{hour}",time) \
155 at Rect_(Clock.rad*0.85,90-time*30)
156 }
157 line <-> from Rect_(Clock.rad*0.95,90-8.5*30) to Clock \
158 then to Rect_(Clock.rad*0.7,90-3.5*30)
159 line thick 1 outline "red" from Clock to Rect_(Clock.rad*0.95,90-0.75*30)
160 ] with .s at (Clock2,Clock1.s)
161
162 define(`htod',`(90-(`$1')/12*360)')
163
164 # clock1(h:m[:s],diam,color)
165 define(`ClockA',`[
166 clockdiam = ifelse(`$2',,`40 mm__',`$2')
167 clockrad = clockdiam/2
168 stacksplit_(`HMS',ifelse(`$1',,10:10,`$1'),:)
169 s = HMS; popdef(`HMS')
170 m = HMS; popdef(`HMS')
171 ifdef(`HMS',`h = HMS',`h = m; m = s')
172 C: circle rad clockrad thick 2 shaded ifelse(`$3',,"cyan",`$3') at (0,0)
173 for t = 1 to 12 do {
174 if pmod(t,3)==0 then { ticlen = clockrad/5; ticthk = 2 } \
175 else { ticlen = clockrad/10; ticthk = 1 }
176 line thick ticthk from Rect_(clockrad,htod(t)) \
177 to Rect_(clockrad-ticlen,htod(t))
178 sprintf("\large\sf %g",t) at Rect_(clockrad*3.5/5,htod(t))
179 }
180 line thick 3 from C to Rect_(clockrad*2/5,htod(h+m/60))
181 line thick 3 from C to Rect_(clockrad*3/5,htod(m/60*12))
182 round(at C, 3)
183 ifdef(`HMS',`line thick 0.7 outlined "red" from C \
184 to Rect_(clockrad*3.5/5,htod(s/60*12)) chop -5bp__ chop 0')
185 popdef(`HMS') ]')
186
187 [
188 CA: ClockA(11:10:35,,"blue!20")
189 ClockA(10:10,,"blue!20!black!30")
190 ClockA(10:10:23,,"blue!20!black!30!green!40")
191 ClockA(10:10,,rgbstring(0.2,0.2,0.7))
192
193 # calls DrawRoutineName(frac,r,g,b)
194 # clock2(h:m[:s],diam,color,rim thickness)
195 define(`ClockB',`[
196 clockdiam = ifelse(`$2',,`60 mm__',`$2')
197 clockrad = clockdiam/2
198 stacksplit_(`HMS',ifelse(`$1',,10:10,`$1'),:)
199 s = HMS; popdef(`HMS')
200 m = HMS; popdef(`HMS')
201 ifdef(`HMS',`h = HMS',`h = m; m = s')
202 rimwd = ifelse(`$4',,9 pt__,`$4')
203 rimwid = rimwd/(1 pt__)
204 n = 4
205 C: (0,0)
206 ClockA(`$1',clockdiam-rimwd*2,"green!20") at C
207 Q:circle diam clockdiam-rimwd thick rimwid \
208 outlined rgbstring(0.7,0.7,0.7) at C
209 Nx: Q.n+(0,Q.thick pt__/2)
210
211 define rimcirc {
212 #print sprintf("%g: %g,%g,%g",$`'1,$`'2,$`'3,$`'4)
213 circle diam clockdiam-rimwd thick rimwid/(n+1) \
214 outlined rgbstring($`'2,$`'3,$`'4) with .n at Nx-(0,rimwd/(n+1)/2)
215 Nx : last circle.n+(0,last circle.thick pt__/2) }
216
217 v = 0.1
218 ShadeObject(rimcirc, n,
219 0,v,v,v,
220 1/(n-1),v,v,v,
221 1,1,1,1) at C
222 popdef(`HMS') ]')
223
224 # ClockA(10:10:35,,"green!20")
225 w = last [].wid
226 ClockB(10:10:35,w,) with .nw at CA.sw+(10bp__,-0.2)
227 ] with .nw at 1st [].sw+(0,-0.2)
228
229 .PE