Merge branch 'ht/graphs'
[shapes.git] / examples / features / shapesarrow-parameters.shape
blob76dc4cf76fc3d9a384ee72d9774a64d5207585dc
1 /** This file is part of Shapes.
2  **
3  ** Shapes is free software: you can redistribute it and/or modify
4  ** it under the terms of the GNU General Public License as published by
5  ** the Free Software Foundation, either version 3 of the License, or
6  ** any later version.
7  **
8  ** Shapes is distributed in the hope that it will be useful,
9  ** but WITHOUT ANY WARRANTY; without even the implied warranty of
10  ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  ** GNU General Public License for more details.
12  **
13  ** You should have received a copy of the GNU General Public License
14  ** along with Shapes.  If not, see <http://www.gnu.org/licenses/>.
15  **
16  ** Copyright 2009, 2010, 2014 Henrik Tidefelt
17  **/
19 ##needs ..Applications..Blockdraw
20 ##needs ..Shapes..Geometry / pathmapping
21 ##preamble \usepackage{amsmath}
23 ##lookin ..Shapes
24 ##lookin ..Shapes..Geometry
25 ##lookin ..Applications..Blockdraw
27 lengthMarks: \ n p r:void a:75° →
29   the_r: [if [typeof r]=Data..Type..§Void Traits..@width*10 r]
30   d: 0.5 * the_r
31   rot: [rotate a]
32   l_p: (|p|)
33   (>
34      picture:
35   [[Data..range '0 n-'1].foldl
36    \ pile e →
37      pile &
38      {
39        sl: [p 0.5*l_p + ( e*1 - 0.5*(n-'1) ) * d]
40        [shift sl.p]*rot [] [Graphics..stroke the_r*sl.T--~the_r*sl.T]
41      }
42    Graphics..null]
43      cut:0m
44    <)
47 IO..•page << [shift (~4cm,~1cm)][][Layout..center [Graphics..TeX `
48 \begin{minipage}{6cm}
49 \begin{align*}
50 2\, \alpha &= \text{\texttt{frontAngle}}
52 2\, \beta &= \text{\texttt{rearAngle}}
54 2\, w &=
55 \begin{cases}
56 \text{\texttt{width}}, & \text{if \texttt{width} is a Data..Type..§Length}
58 \max \left\{\, 3\,\mathrm{bp},\, \text{\texttt{width}}\, \text{\texttt{Traits..@width}} \,\right\}, & \text{if \texttt{width} is a Data..Type..§Float}
59 \end{cases}
60 \end{align*}
61 \end{minipage}
62 ´] (~1,~1)]
65 Traits..@width:1cm
68   pth: { c: [Geometry..circle 5cm]  [c 2.3]--[c 3.3] }
70   /** First we set up variables that mirror the formal parameters of ShapesArrow.
71    **/
72   p: [Geometry..reverse pth]
73   width:3
74   frontAngle:40°
75   rearAngle:150°
77   /** Show the arrowhead.
78    **/
79   IO..•page << Traits..@stroking:[Traits..gray 0.7] | [Graphics..stroke pth head:[Graphics..ShapesArrow width:width frontAngle:frontAngle rearAngle:rearAngle ...]]
81   /** Then we paste the implementation of ShapesArrow...
82    **/
83   z: p.begin.p
84   lMax: 0.99*[Numeric..Math..abs p]
85   w: 0.5*[if [typeof width]=Data..Type..§Float Traits..@width*width width]
86   el: [Numeric..Math..min w/[Numeric..Math..sin 0.5*frontAngle] lMax]
87   l: [Numeric..Math..min w * ( [Numeric..Math..cot 0.5*frontAngle] - [Numeric..Math..cot 0.5*rearAngle] ) lMax]
88   e: [p 0]--[p el] >> [shift ~z]
89   sl: [p l]
90   pr: sl.p
91   dr: e.end.p
92   dra: [angle dr]
93   drl: (|dr|)
94   r: w / [Numeric..Math..sin 0.5*rearAngle]
95   d1: sl.p + [[rotate 0.5*rearAngle] (r*sl.T)] - z
96   d2: sl.p + [[rotate ~0.5*rearAngle] (r*sl.T)] - z
98   /** The following paths are not bound to variables in the implementation, but we need them here.
99    **/
100   e1: [[shift z]*[rotate [angle d1]-dra]*[scale (|d1|)/drl] e]
101   e2: [[shift z]*[rotate [angle d2]-dra]*[scale (|d2|)/drl] e]
103   /** ... so that we can annotate the result!
104    **/
106   IO..•page << Traits..@width:3bp & Traits..@stroking:[Traits..gray 0.8] | [Graphics..stroke pth]
108   Traits..@width:1.5bp & Traits..@stroking:Traits..RGB..BLUE
109   |
110   {
111     IO..•page << [Graphics..stroke [shift z][]e]
112           << [Graphics..stroke e1]
113           << [Graphics..stroke e2]
114   }
116   Traits..@width:5bp & Traits..@stroking:Traits..RGB..RED
117   |
118   {
119     IO..•page << [Graphics..spot pr] << [Graphics..spot z] << [Graphics..spot e1.end.p] << [Graphics..spot e2.end.p]
120   }
122   hStyle: Traits..@width:0.5bp & Traits..@stroking:[Traits..gray 0.2]
123   angleHead: [Graphics..ShapesArrow width:2bp ...]
125   hStyle
126   |
127   {
128     r0: 1cm
129     r1: 2.5cm
130     r2: 3.5cm
131     IO..•page << [Graphics..stroke [shift z][]( e1.begin.T*r0--e1.begin.T*~r1 )]
132     IO..•page << [Graphics..stroke [shift z][]( e2.begin.T*r0--e2.begin.T*~r1 )]
133     {
134       rAngle: 0.8*r1
135       arc: [shift z] [] [Geometry..ccw_arc (0m,0m) [rotate ~(5mm/rAngle)][]e2.begin.T*~r1 e2.begin.T*~r1 rAngle]
136       IO..•page << [Graphics..stroke [shift z] [] [Geometry..ccw_arc (0m,0m) e2.begin.T*~r1 e1.begin.T*~r1 rAngle]]
137             << [Graphics..stroke [shift z] [] [Geometry..cw_arc (0m,0m) [rotate (5mm/rAngle)][]e1.begin.T*~r1 e1.begin.T*~r1 rAngle]
138                  head:angleHead]
139             << [Graphics..stroke arc head:angleHead]
140             << [putlabelRight [Graphics..TeX `$\approx 2\, \alpha$´] arc.begin.p ~1]
141     }
142     {
143       arc1: [shift z] [] [Geometry..cw_arc (0m,0m) e1.begin.T*~r1 p.begin.T*~r1 0.5*(r1+r2)]
144       arc2: [shift z] [] [Geometry..ccw_arc (0m,0m) e2.begin.T*~r1 p.begin.T*~r1 0.5*(r1+r2)]
145       IO..•page << [Graphics..stroke z--(+~r2*p.begin.T)]
146             << [Graphics..stroke [[shift z]*[rotate 0.5*frontAngle] ~(r1+1bp)*p.begin.T--(+(~(r2-r1)*p.begin.T))]]
147             << [Graphics..stroke [[shift z]*[rotate ~0.5*frontAngle] ~(r1+1bp)*p.begin.T--(+(~(r2-r1)*p.begin.T))]]
148             << [Graphics..stroke arc1 head:angleHead tail:angleHead]
149             << [Graphics..stroke arc2 head:angleHead tail:angleHead]
150             << [putlabelRight [Graphics..TeX `$\alpha$´] [Layout..mspoint arc1 0.5] ~1]
151             << [putlabelRight [Graphics..TeX `$\alpha$´] [Layout..mspoint arc2 0.5] ~1]
152     }
153     {
154       p: e1.end.p--pr
155       IO..•page << [Graphics..stroke p]
156             << [putlabelRight [Graphics..TeX `$r$´] [Layout..mspoint p 0.5] 0]
157     }
158     {
159       p: pr--e2.end.p
160       IO..•page << [Graphics..stroke p]
161             << [putlabelRight [Graphics..TeX `$r$´] [Layout..mspoint p 0.5] 0]
162     }
163     {
164       pw: pr + [Numeric..Math..cos 0.5*rearAngle]*r*sl.T
165       p1: pw--e1.end.p
166       p2: pw--e2.end.p
167       IO..•page << [Graphics..stroke p1 head:angleHead tail:angleHead]
168             << [putlabelLeft [Graphics..TeX `$w$´] [Layout..mspoint p1 0.5] 1]
169             << [Graphics..stroke p2 head:angleHead tail:angleHead]
170             << [putlabelLeft [Graphics..TeX `$w$´] [Layout..mspoint p2 0.5] ~1]
171     }
172     {
173       hLine: pr--(+(1.2*r*sl.T))
174       arc1: [Geometry..ccw_arc pr hLine.end.p e1.end.p 0.9*r]
175       arc2: [Geometry..cw_arc pr hLine.end.p e2.end.p 0.9*r]
176       IO..•page << [Graphics..stroke hLine]
177             << [Graphics..stroke arc1 head:angleHead tail:angleHead]
178             << [putlabelLeft [Graphics..TeX `$\beta$´] [Layout..mspoint arc1 0.5] 1]
179             << [Graphics..stroke arc2 head:angleHead tail:angleHead]
180             << [putlabelLeft [Graphics..TeX `$\beta$´] [Layout..mspoint arc2 0.5] ~1]
181     }
183     {
184       offset: 5bp
185       arc0: p.begin--[p l]
186       arc: [Geometry..sidepath2 [Geometry..upsample_bends 20° arc0] offset]
187       IO..•page << [Graphics..stroke arc head:angleHead tail:angleHead]
188             << [Graphics..stroke arc0.begin.p--(+arc0.begin.N*1.5*offset)]
189             << [Graphics..stroke arc0.end.p--(+arc0.end.rN*~2*offset)]
190             << [putlabelBelow [Graphics..TeX `$l$´] [Layout..mspoint arc 0.8] 0]
191     }
192     {
193       offset: ~5bp
194       arc0: [shift z][]e
195       arc: [Geometry..sidepath2 [Geometry..upsample_bends 20° arc0] offset]
196       IO..•page << [Graphics..stroke arc head:angleHead tail:angleHead]
197             << [Graphics..stroke arc0.begin.p--(+arc0.begin.N*1.5*offset)]
198             << [Graphics..stroke arc0.end.p--(+arc0.end.rN*~2*offset)]
199             << [putlabelAbove [Graphics..TeX `$h$´] [Layout..mspoint arc 0.7] 0]
200     }
202   }
207 Traits..@width:1cm
210   pth: (~5cm,~10cm)--(3cm,(+0cm))
212   /** First we set up variables that mirror the formal parameters of ShapesArrow.
213    **/
214   p: [Geometry..reverse pth]
215   width:3
216   frontAngle:40°
217   rearAngle:150°
219   /** Show the arrowhead.
220    **/
221   IO..•page << Traits..@stroking:[Traits..gray 0.7] | [Graphics..stroke pth head:[Graphics..ShapesArrow width:width frontAngle:frontAngle rearAngle:rearAngle ...]]
223   /** Then we paste the implementation of ShapesArrow...
224    **/
225   z: p.begin.p
226     lMax: 0.99*[Numeric..Math..abs p]
227     w: 0.5*[if [typeof width]=Data..Type..§Float Traits..@width*width width]
228     el: [Numeric..Math..min w/[Numeric..Math..sin 0.5*frontAngle] lMax]
229     l: [Numeric..Math..min w * ( [Numeric..Math..cot 0.5*frontAngle] - [Numeric..Math..cot 0.5*rearAngle] ) lMax]
230     e: [p 0]--[p el] >> [shift ~z]
231     sl: [p l]
232     pr: sl.p
233     dr: e.end.p
234     dra: [angle dr]
235     drl: (|dr|)
236     r: w / [Numeric..Math..sin 0.5*rearAngle]
237   d1: sl.p + [[rotate 0.5*rearAngle] (r*sl.T)] - z
238   d2: sl.p + [[rotate ~0.5*rearAngle] (r*sl.T)] - z
240   /** The following paths are not bound to variables in the implementation, but we need them here.
241    **/
242   e1: [[shift z]*[rotate [angle d1]-dra]*[scale (|d1|)/drl] e]
243   e2: [[shift z]*[rotate [angle d2]-dra]*[scale (|d2|)/drl] e]
245   /** ... so that we can annotate the result!
246    **/
248   IO..•page << Traits..@width:3bp & Traits..@stroking:[Traits..gray 0.8] | [Graphics..stroke pth]
250   Traits..@width:1.5bp & Traits..@stroking:Traits..RGB..BLUE
251   |
252   {
253     IO..•page << [Graphics..stroke [shift z][]e]
254           << [Graphics..stroke e1]
255           << [Graphics..stroke e2]
256   }
258   Traits..@width:5bp & Traits..@stroking:Traits..RGB..RED
259   |
260   {
261     IO..•page << [Graphics..spot pr] << [Graphics..spot z] << [Graphics..spot e1.end.p] << [Graphics..spot e2.end.p]
262   }
264   hStyle: Traits..@width:0.5bp & Traits..@stroking:[Traits..gray 0.2]
265   angleHead: [Graphics..ShapesArrow width:2bp ...]
267   hStyle
268   |
269   {
270     {
271       rLbl: 2cm
272       arc1: [Geometry..ccw_arc z pr e1.end.p rLbl]
273       arc2: [Geometry..cw_arc z pr e2.end.p rLbl]
274       IO..•page << [Graphics..stroke arc1 head:angleHead tail:angleHead]
275             << [putlabelLeft [Graphics..TeX `$\alpha$´] [Layout..mspoint arc1 0.5] 0]
276             << [Graphics..stroke arc2 head:angleHead tail:angleHead]
277             << [putlabelLeft [Graphics..TeX `$\alpha$´] [Layout..mspoint arc2 0.5] ~0.5]
278     }
279     {
280       p: e1.end.p--pr
281       IO..•page << [Graphics..stroke p]
282             << [putlabelRight [Graphics..TeX `$r$´] [Layout..mspoint p 0.5] 0]
283     }
284     {
285       p: pr--e2.end.p
286       IO..•page << [Graphics..stroke p]
287             << [putlabelRight [Graphics..TeX `$r$´] [Layout..mspoint p 0.5] 0]
288     }
289     {
290       pw: pr + [Numeric..Math..cos 0.5*rearAngle]*r*sl.T
291       p1: pw--e1.end.p
292       p2: pw--e2.end.p
293       IO..•page << [Graphics..stroke p1 head:angleHead tail:angleHead]
294             << [putlabelLeft [Graphics..TeX `$w$´] [Layout..mspoint p1 0.5] 1]
295             << [Graphics..stroke p2 head:angleHead tail:angleHead]
296             << [putlabelLeft [Graphics..TeX `$w$´] [Layout..mspoint p2 0.5] ~1]
297     }
298     {
299       hLine: pr--(+(1.2*r*sl.T))
300       arc1: [Geometry..ccw_arc pr hLine.end.p e1.end.p 0.9*r]
301       arc2: [Geometry..cw_arc pr hLine.end.p e2.end.p 0.9*r]
302       IO..•page << [Graphics..stroke hLine]
303             << [Graphics..stroke arc1 head:angleHead tail:angleHead]
304             << [putlabelLeft [Graphics..TeX `$\beta$´] [Layout..mspoint arc1 0.5] 1]
305             << [Graphics..stroke arc2 head:angleHead tail:angleHead]
306             << [putlabelLeft [Graphics..TeX `$\beta$´] [Layout..mspoint arc2 0.5] ~1]
307     }
309     {
310       offset: 5bp
311       arc0: p.begin--[p l]
312       arc: [Geometry..sidepath [Geometry..upsample_bends 20° arc0] offset]
313       IO..•page << [Graphics..stroke arc head:angleHead tail:angleHead]
314             << [Graphics..stroke arc0.begin.p--(+arc0.begin.N*1.5*offset)]
315             << [Graphics..stroke arc0.end.p--(+arc0.end.rN*~2*offset)]
316             << [putlabelBelow [Graphics..TeX `$l$´] [Layout..mspoint arc 0.8] 0]
317     }
318     {
319       offset: ~5bp
320       arc0: [shift z][]e
321       arc: [Geometry..sidepath [Geometry..upsample_bends 20° arc0] offset]
322       IO..•page << [Graphics..stroke arc head:angleHead tail:angleHead]
323             << [Graphics..stroke arc0.begin.p--(+arc0.begin.N*1.5*offset)]
324             << [Graphics..stroke arc0.end.p--(+arc0.end.rN*~2*offset)]
325             << [putlabelAbove [Graphics..TeX `$h$´] [Layout..mspoint arc 0.7] 0]
326             << [putlabelBelow [Graphics..TeX `$h$´] [Layout..mspoint e1 0.7] 0]
327             << [putlabelAbove [Graphics..TeX `$h$´] [Layout..mspoint e2 0.7] 0]
328     }
330   }