Debian Codegraph @

texmf-dist/doc/generic/pgf/pgfmanual-en-tutorial-Euclid.tex

raw
1 % Copyright 2019 by Till Tantau
2 %
3 % This file may be distributed and/or modified
4 %
5 % 1. under the LaTeX Project Public License and/or
6 % 2. under the GNU Free Documentation License.
7 %
8 % See the file doc/generic/pgf/licenses/LICENSE for more details.
9
10
11 \section{Tutorial: Euclid's Amber Version of the \emph{Elements}}
12
13 In this third tutorial we have a look at how \tikzname\ can be used to draw
14 geometric constructions.
15
16 Euclid is currently quite busy writing his new book series, whose working title
17 is ``Elements'' (Euclid is not quite sure whether this title will convey the
18 message of the series to future generations correctly, but he intends to change
19 the title before it goes to the publisher). Up to now, he wrote down his text
20 and graphics on papyrus, but his publisher suddenly insists that he must submit
21 in electronic form. Euclid tries to argue with the publisher that electronics
22 will only be discovered thousands of years later, but the publisher informs him
23 that the use of papyrus is no longer cutting edge technology and Euclid will
24 just have to keep up with modern tools.
25
26 Slightly disgruntled, Euclid starts converting his papyrus entitled ``Book I,
27 Proposition I'' to an amber version.
28
29
30 \subsection{Book I, Proposition I}
31
32 The drawing on his papyrus looks like this:\footnote{The text is taken from the
33 wonderful interactive version of Euclid's Elements by David E. Joyce, to be
34 found on his website at Clark University.}
35
36 \bigskip
37 \noindent
38 \begin{tikzpicture}[thick,help lines/.style={thin,draw=black!50}]
39 \pgfmathsetseed{1}
40 \def\A{\textcolor{input}{$A$}}
41 \def\B{\textcolor{input}{$B$}}
42 \def\C{\textcolor{output}{$C$}}
43 \def\D{$D$}
44 \def\E{$E$}
45
46 \colorlet{input}{blue!80!black}
47 \colorlet{output}{red!70!black}
48 \colorlet{triangle}{orange}
49
50 \coordinate [label=left:\A]
51 (A) at ($ (0,0) + .1*(rand,rand) $);
52 \coordinate [label=right:\B]
53 (B) at ($ (1.25,0.25) + .1*(rand,rand) $);
54
55 \draw [input] (A) -- (B);
56
57 \node [name path=D,help lines,draw,label=left:\D] (D) at (A) [circle through=(B)] {};
58 \node [name path=E,help lines,draw,label=right:\E] (E) at (B) [circle through=(A)] {};
59
60 \path [name intersections={of=D and E,by={[label=above:\C]C}}];
61
62 \draw [output] (A) -- (C);
63 \draw [output] (B) -- (C);
64
65 \foreach \point in {A,B,C}
66 \fill [black,opacity=.5] (\point) circle (2pt);
67
68 \begin{pgfonlayer}{background}
69 \fill[triangle!80] (A) -- (C) -- (B) -- cycle;
70 \end{pgfonlayer}
71
72 \node [below right,text width=10cm,align=justify] at (4,3)
73 {
74 \small
75 \textbf{Proposition I}\par
76 \emph{To construct an \textcolor{triangle}{equilateral triangle}
77 on a given \textcolor{input}{finite straight line}.}
78 \par
79 \vskip1em
80 Let \A\B\ be the given \textcolor{input}{finite straight line}. It
81 is required to construct an \textcolor{triangle}{equilateral
82 triangle} on the \textcolor{input}{straight line}~\A\B.
83
84 Describe the circle \B\C\D\ with center~\A\ and radius \A\B. Again
85 describe the circle \A\C\E\ with center~\B\ and radius \B\A. Join the
86 \textcolor{output}{straight lines} \C\A\ and \C\B\ from the
87 point~\C\ at which the circles cut one another to the points~\A\ and~\B.
88
89 Now, since the point~\A\ is the center of the circle \C\D\B,
90 therefore \A\C\ equals \A\B. Again, since the point \B\ is the
91 center of the circle \C\A\E, therefore \B\C\ equals \B\A. But
92 \A\C\ was proved equal to \A\B, therefore each of the straight
93 lines \A\C\ and \B\C\ equals \A\B. And
94 things which equal the same thing also equal one another,
95 therefore \A\C\ also equals \B\C. Therefore the three straight
96 lines \A\C, \A\B, and \B\C\ equal one another.
97 Therefore the \textcolor{triangle}{triangle} \A\B\C\ is
98 equilateral, and it has been constructed on the given finite
99 \textcolor{input}{straight line}~\A\B.
100 };
101 \end{tikzpicture}
102 \bigskip
103
104 Let us have a look at how Euclid can turn this into \tikzname\ code.
105
106
107 \subsubsection{Setting up the Environment}
108
109 As in the previous tutorials, Euclid needs to load \tikzname, together with
110 some libraries. These libraries are |calc|, |intersections|, |through|, and
111 |backgrounds|. Depending on which format he uses, Euclid would use one of the
112 following in the preamble:
113 %
114 \begin{codeexample}[code only]
115 % For LaTeX:
116 \usepackage{tikz}
117 \usetikzlibrary{calc,intersections,through,backgrounds}
118 \end{codeexample}
119
120 \begin{codeexample}[code only]
121 % For plain TeX:
122 \input tikz.tex
123 \usetikzlibrary{calc,intersections,through,backgrounds}
124 \end{codeexample}
125
126 \begin{codeexample}[code only]
127 % For ConTeXt:
128 \usemodule[tikz]
129 \usetikzlibrary[calc,intersections,through,backgrounds]
130 \end{codeexample}
131
132
133 \subsubsection{The Line \emph{AB}}
134
135 The first part of the picture that Euclid wishes to draw is the line $AB$. That
136 is easy enough, something like |\draw (0,0) -- (2,1);| might do. However,
137 Euclid does not wish to reference the two points $A$ and $B$ as $(0,0)$ and
138 $(2,1)$ subsequently. Rather, he wishes to just write |A| and |B|. Indeed, the
139 whole point of his book is that the points $A$ and $B$ can be arbitrary and all
140 other points (like $C$) are constructed in terms of their positions. It would
141 not do if Euclid were to write down the coordinates of $C$ explicitly.
142
143 So, Euclid starts with defining two coordinates using the |\coordinate|
144 command:
145 %
146 \begin{codeexample}[]
147 \begin{tikzpicture}
148 \coordinate (A) at (0,0);
149 \coordinate (B) at (1.25,0.25);
150
151 \draw[blue] (A) -- (B);
152 \end{tikzpicture}
153 \end{codeexample}
154
155 That was easy enough. What is missing at this point are the labels for the
156 coordinates. Euclid does not want them \emph{on} the points, but next to them.
157 He decides to use the |label| option:
158 %
159 \begin{codeexample}[]
160 \begin{tikzpicture}
161 \coordinate [label=left:\textcolor{blue}{$A$}] (A) at (0,0);
162 \coordinate [label=right:\textcolor{blue}{$B$}] (B) at (1.25,0.25);
163
164 \draw[blue] (A) -- (B);
165 \end{tikzpicture}
166 \end{codeexample}
167
168 At this point, Euclid decides that it would be even nicer if the points $A$ and
169 $B$ were in some sense ``random''. Then, neither Euclid nor the reader can make
170 the mistake of taking ``anything for granted'' concerning these position of
171 these points. Euclid is pleased to learn that there is a |rand| function in
172 \tikzname\ that does exactly what he needs: It produces a number between $-1$
173 and $1$. Since \tikzname\ can do a bit of math, Euclid can change the
174 coordinates of the points as follows:
175 %
176 \begin{codeexample}[code only]
177 \coordinate [...] (A) at (0+0.1*rand,0+0.1*rand);
178 \coordinate [...] (B) at (1.25+0.1*rand,0.25+0.1*rand);
179 \end{codeexample}
180
181 This works fine. However, Euclid is not quite satisfied since he would prefer
182 that the ``main coordinates'' $(0,0)$ and $(1.25,0.25)$ are ``kept separate''
183 from the perturbation $0.1(\mathit{rand},\mathit{rand})$. This means, he would
184 like to specify that coordinate $A$ as ``the point that is at $(0,0)$ plus one
185 tenth of the vector $(\mathit{rand},\mathit{rand})$''.
186
187 It turns out that the |calc| library allows him to do exactly this kind of
188 computation. When this library is loaded, you can use special coordinates that
189 start with |($| and end with |$)| rather than just |(| and~|)|. Inside these
190 special coordinates you can give a linear combination of coordinates. (Note
191 that the dollar signs are only intended to signal that a ``computation'' is
192 going on; no mathematical typesetting is done.)
193
194 The new code for the coordinates is the following:
195 %
196 \begin{codeexample}[code only]
197 \coordinate [...] (A) at ($ (0,0) + .1*(rand,rand) $);
198 \coordinate [...] (B) at ($ (1.25,0.25) + .1*(rand,rand) $);
199 \end{codeexample}
200
201 Note that if a coordinate in such a computation has a factor (like |.1|), you
202 must place a |*| directly before the opening parenthesis of the coordinate. You
203 can nest such computations.
204
205
206 \subsubsection{The Circle Around \emph{A}}
207
208 The first tricky construction is the circle around~$A$. We will see later how
209 to do this in a very simple manner, but first let us do it the ``hard'' way.
210
211 The idea is the following: We draw a circle around the point $A$ whose radius
212 is given by the length of the line $AB$. The difficulty lies in computing the
213 length of this line.
214
215 Two ideas ``nearly'' solve this problem: First, we can write |($ (A) - (B) $)|
216 for the vector that is the difference between $A$ and~$B$. All we need is the
217 length of this vector. Second, given two numbers $x$ and $y$, one can write
218 |veclen(|$x$|,|$y$|)| inside a mathematical expression. This gives the value
219 $\sqrt{x^2+y^2}$, which is exactly the desired length.
220
221 The only remaining problem is to access the $x$- and $y$-coordinate of the
222 vector~$AB$. For this, we need a new concept: the \emph{let operation}. A let
223 operation can be given anywhere on a path where a normal path operation like a
224 line-to or a move-to is expected. The effect of a let operation is to evaluate
225 some coordinates and to assign the results to special macros. These macros make
226 it easy to access the $x$- and $y$-coordinates of the coordinates.
227
228 Euclid would write the following:
229 %
230 \begin{codeexample}[preamble={\usetikzlibrary{calc}}]
231 \begin{tikzpicture}
232 \coordinate [label=left:$A$] (A) at (0,0);
233 \coordinate [label=right:$B$] (B) at (1.25,0.25);
234 \draw (A) -- (B);
235
236 \draw (A) let
237 \p1 = ($ (B) - (A) $)
238 in
239 circle ({veclen(\x1,\y1)});
240 \end{tikzpicture}
241 \end{codeexample}
242
243 Each assignment in a let operation starts with |\p|, usually followed by a
244 \meta{digit}. Then comes an equal sign and a coordinate. The coordinate is
245 evaluated and the result is stored internally. From then on you can use the
246 following expressions:
247 %
248 \begin{enumerate}
249 \item |\x|\meta{digit} yields the $x$-coordinate of the resulting point.
250 \item |\y|\meta{digit} yields the $y$-coordinate of the resulting point.
251 \item |\p|\meta{digit} yields the same as
252 |\x|\meta{digit}|,\y|\meta{digit}.
253 \end{enumerate}
254 %
255 You can have multiple assignments in a let operation, just separate them with
256 commas. In later assignments you can already use the results of earlier
257 assignments.
258
259 Note that |\p1| is not a coordinate in the usual sense. Rather, it just expands
260 to a string like |10pt,20pt|. So, you cannot write, for instance,
261 |(\p1.center)| since this would just expand to |(10pt,20pt.center)|, which
262 makes no sense.
263
264 Next, we want to draw both circles at the same time. Each time the radius is
265 |veclen(\x1,\y1)|. It seems natural to compute this radius only once. For this,
266 we can also use a let operation: Instead of writing |\p1 = ...|, we write
267 |\n2 = ...|. Here, ``n'' stands for ``number'' (while ``p'' stands for
268 ``point''). The assignment of a number should be followed by a number in curly
269 braces.
270 %
271 \begin{codeexample}[preamble={\usetikzlibrary{calc}}]
272 \begin{tikzpicture}
273 \coordinate [label=left:$A$] (A) at (0,0);
274 \coordinate [label=right:$B$] (B) at (1.25,0.25);
275 \draw (A) -- (B);
276
277 \draw let \p1 = ($ (B) - (A) $),
278 \n2 = {veclen(\x1,\y1)}
279 in
280 (A) circle (\n2)
281 (B) circle (\n2);
282 \end{tikzpicture}
283 \end{codeexample}
284 %
285 In the above example, you may wonder, what |\n1| would yield? The answer is
286 that it would be undefined -- the |\p|, |\x|, and |\y| macros refer to the same
287 logical point, while the |\n| macro has ``its own namespace''. We could even
288 have replaced |\n2| in the example by |\n1| and it would still work. Indeed,
289 the digits following these macros are just normal \TeX\ parameters. We could
290 also use a longer name, but then we have to use curly braces:
291 %
292 \begin{codeexample}[preamble={\usetikzlibrary{calc}}]
293 \begin{tikzpicture}
294 \coordinate [label=left:$A$] (A) at (0,0);
295 \coordinate [label=right:$B$] (B) at (1.25,0.25);
296 \draw (A) -- (B);
297
298 \draw let \p1 = ($ (B) - (A) $),
299 \n{radius} = {veclen(\x1,\y1)}
300 in
301 (A) circle (\n{radius})
302 (B) circle (\n{radius});
303 \end{tikzpicture}
304 \end{codeexample}
305
306 At the beginning of this section it was promised that there is an easier way to
307 create the desired circle. The trick is to use the |through| library. As the
308 name suggests, it contains code for creating shapes that go through a given
309 point.
310
311 The option that we are looking for is |circle through|. This option is given to
312 a \emph{node} and has the following effects: First, it causes the node's inner
313 and outer separations to be set to zero. Then it sets the shape of the node to
314 |circle|. Finally, it sets the radius of the node such that it goes through the
315 parameter given to |circle through|. This radius is computed in essentially the
316 same way as above.
317 %
318 \begin{codeexample}[preamble={\usetikzlibrary{through}}]
319 \begin{tikzpicture}
320 \coordinate [label=left:$A$] (A) at (0,0);
321 \coordinate [label=right:$B$] (B) at (1.25,0.25);
322 \draw (A) -- (B);
323
324 \node [draw,circle through=(B),label=left:$D$] at (A) {};
325 \end{tikzpicture}
326 \end{codeexample}
327
328
329 \subsubsection{The Intersection of the Circles}
330
331 Euclid can now draw the line and the circles. The final problem is to compute
332 the intersection of the two circles. This computation is a bit involved if you
333 want to do it ``by hand''. Fortunately, the |intersections| library allows us
334 to compute the intersection of arbitrary paths.
335
336 The idea is simple: First, you ``name'' two paths using the |name path| option.
337 Then, at some later point, you can use the option |name intersections|, which
338 creates coordinates called |intersection-1|, |intersection-2|, and so on at all
339 intersections of the paths. Euclid assigns the names |D| and |E| to the paths
340 of the two circles (which happen to be the same names as the nodes themselves,
341 but nodes and their paths live in different ``namespaces'').
342 %
343 \begin{codeexample}[preamble={\usetikzlibrary{intersections,through}}]
344 \begin{tikzpicture}
345 \coordinate [label=left:$A$] (A) at (0,0);
346 \coordinate [label=right:$B$] (B) at (1.25,0.25);
347 \draw (A) -- (B);
348
349 \node (D) [name path=D,draw,circle through=(B),label=left:$D$] at (A) {};
350 \node (E) [name path=E,draw,circle through=(A),label=right:$E$] at (B) {};
351
352 % Name the coordinates, but do not draw anything:
353 \path [name intersections={of=D and E}];
354
355 \coordinate [label=above:$C$] (C) at (intersection-1);
356
357 \draw [red] (A) -- (C);
358 \draw [red] (B) -- (C);
359 \end{tikzpicture}
360 \end{codeexample}
361
362 It turns out that this can be further shortened: The |name intersections| takes
363 an optional argument |by|, which lets you specify names for the coordinates and
364 options for them. This creates more compact code. Although Euclid does not need
365 it for the current picture, it is just a small step to computing the bisection
366 of the line $AB$:
367 %
368 \begin{codeexample}[preamble={\usetikzlibrary{intersections,through}}]
369 \begin{tikzpicture}
370 \coordinate [label=left:$A$] (A) at (0,0);
371 \coordinate [label=right:$B$] (B) at (1.25,0.25);
372 \draw [name path=A--B] (A) -- (B);
373
374 \node (D) [name path=D,draw,circle through=(B),label=left:$D$] at (A) {};
375 \node (E) [name path=E,draw,circle through=(A),label=right:$E$] at (B) {};
376
377 \path [name intersections={of=D and E, by={[label=above:$C$]C, [label=below:$C'$]C'}}];
378
379 \draw [name path=C--C',red] (C) -- (C');
380
381 \path [name intersections={of=A--B and C--C',by=F}];
382 \node [fill=red,inner sep=1pt,label=-45:$F$] at (F) {};
383 \end{tikzpicture}
384 \end{codeexample}
385
386
387 \subsubsection{The Complete Code}
388
389 Back to Euclid's code. He introduces a few macros to make life simpler, like a
390 |\A| macro for typesetting a blue $A$. He also uses the |background| layer for
391 drawing the triangle behind everything at the end.
392 %
393 \begin{codeexample}[pre={\pgfmathsetseed{1}},preamble={\usetikzlibrary{backgrounds,calc,intersections,through}}]
394 \begin{tikzpicture}[thick,help lines/.style={thin,draw=black!50}]
395 \def\A{\textcolor{input}{$A$}} \def\B{\textcolor{input}{$B$}}
396 \def\C{\textcolor{output}{$C$}} \def\D{$D$}
397 \def\E{$E$}
398
399 \colorlet{input}{blue!80!black} \colorlet{output}{red!70!black}
400 \colorlet{triangle}{orange}
401
402 \coordinate [label=left:\A] (A) at ($ (0,0) + .1*(rand,rand) $);
403 \coordinate [label=right:\B] (B) at ($ (1.25,0.25) + .1*(rand,rand) $);
404
405 \draw [input] (A) -- (B);
406
407 \node [name path=D,help lines,draw,label=left:\D] (D) at (A) [circle through=(B)] {};
408 \node [name path=E,help lines,draw,label=right:\E] (E) at (B) [circle through=(A)] {};
409
410 \path [name intersections={of=D and E,by={[label=above:\C]C}}];
411
412 \draw [output] (A) -- (C) -- (B);
413
414 \foreach \point in {A,B,C}
415 \fill [black,opacity=.5] (\point) circle (2pt);
416
417 \begin{pgfonlayer}{background}
418 \fill[triangle!80] (A) -- (C) -- (B) -- cycle;
419 \end{pgfonlayer}
420
421 \node [below right, text width=10cm,align=justify] at (4,3) {
422 \small\textbf{Proposition I}\par
423 \emph{To construct an \textcolor{triangle}{equilateral triangle}
424 on a given \textcolor{input}{finite straight line}.}
425 \par\vskip1em
426 Let \A\B\ be the given \textcolor{input}{finite straight line}. \dots
427 };
428 \end{tikzpicture}
429 \end{codeexample}
430
431
432 \subsection{Book I, Proposition II}
433
434 The second proposition in the Elements is the following:
435
436 \bigskip\noindent
437 \begin{tikzpicture}[thick,help lines/.style={thin,draw=black!50}]
438 \pgfmathsetseed{1}
439 \def\A{\textcolor{orange}{$A$}} \def\B{\textcolor{input}{$B$}}
440 \def\C{\textcolor{input}{$C$}} \def\D{$D$}
441 \def\E{$E$} \def\F{$F$}
442 \def\G{$G$} \def\H{$H$}
443 \def\K{$K$} \def\L{\textcolor{output}{$L$}}
444
445 \colorlet{input}{blue!80!black} \colorlet{output}{red!70!black}
446
447 \coordinate [label=left:\A] (A) at ($ (0,0) + .1*(rand,rand) $);
448 \coordinate [label=right:\B] (B) at ($ (1,0.2) + .1*(rand,rand) $);
449 \coordinate [label=above:\C] (C) at ($ (1,2) + .1*(rand,rand) $);
450
451 \draw [input] (B) -- (C);
452 \draw [help lines] (A) -- (B);
453
454 \coordinate [label=above:\D] (D) at ($ (A)!.5!(B) ! {sin(60)*2} ! 90:(B) $);
455
456 \draw [help lines] (D) -- ($ (D)!3.75!(A) $) coordinate [label=-135:\E] (E);
457 \draw [help lines] (D) -- ($ (D)!3.75!(B) $) coordinate [label=-45:\F] (F);
458
459 \node (H) at (B) [name path=H,help lines,circle through=(C),draw,label=135:\H] {};
460 \path [name path=B--F] (B) -- (F);
461 \path [name intersections={of=H and B--F}]
462 coordinate [label=right:\G] (G) at (intersection-1);
463
464 \node (K) at (D) [name path=K,help lines,circle through=(G),draw,label=135:\K] {};
465
466 \path [name path=A to E line] (A) -- (E);
467 \path [name intersections={of=K and A to E line}]
468 coordinate [label=below:\L] (L) at (intersection-1);
469
470 \draw [output] (A) -- (L);
471
472 \foreach \point in {A,B,C,D,G,L}
473 \fill [black,opacity=.5] (\point) circle (2pt);
474
475 \node [below right, text width=9cm,align=justify] at (4,4) {
476 \small\textbf{Proposition II}\par
477 \emph{To place a \textcolor{output}{straight line} equal to a
478 given \textcolor{input}{straight line} with
479 one end at a \textcolor{orange}{given point}.}
480 \par\vskip1em
481 Let \A\ be the given point, and \B\C\ the given
482 \textcolor{input}{straight line}.
483 It is required to place a \textcolor{output}{straight line} equal
484 to the given \textcolor{input}{straight line} \B\C\ with one end
485 at the point~\A.
486
487 Join the straight line \A\B\ from the point \A\ to the point \B, and
488 construct the equilateral triangle \D\A\B\ on it.
489
490 Produce the straight lines \A\E\ and \B\F\ in a straight line with
491 \D\A\ and \D\B. Describe the circle \C\G\H\ with center \B\ and
492 radius \B\C, and again, describe the circle \G\K\L\ with center
493 \D\ and radius \D\G.
494
495 Since the point \B\ is the center of the circle \C\G\H, therefore
496 \B\C\ equals \B\G. Again, since the point \D\ is the center of the
497 circle \G\K\L, therefore \D\L\ equals \D\G. And in these \D\A\
498 equals \D\B, therefore the remainder \A\L\ equals the remainder
499 \B\G. But \B\C\ was also proved equal to \B\G, therefore each of
500 the straight lines \A\L\ and \B\C\ equals \B\G. And things which
501 equal the same thing also equal one another, therefore \A\L\ also
502 equals \B\C.
503
504 Therefore the \textcolor{output}{straight line} \A\L\ equal to the
505 given \textcolor{input}{straight line} \B\C\ has been placed with
506 one end at the \textcolor{orange}{given point}~\A.
507 };
508 \end{tikzpicture}
509
510
511 \subsubsection{Using Partway Calculations for the Construction of \emph{D}}
512
513 Euclid's construction starts with ``referencing'' Proposition~I for the
514 construction of the point~$D$. Now, while we could simply repeat the
515 construction, it seems a bit bothersome that one has to draw all these circles
516 and do all these complicated constructions.
517
518 For this reason, \tikzname\ supports some simplifications. First, there is a
519 simple syntax for computing a point that is ``partway'' on a line from $p$
520 to~$q$: You place these two points in a coordinate calculation -- remember,
521 they start with |($| and end with |$)| -- and then combine them using
522 |!|\meta{part}|!|. A \meta{part} of |0| refers to the \emph{first} coordinate,
523 a \meta{part} of |1| refers to the second coordinate, and a value in between
524 refers to a point on the line from $p$ to~$q$. Thus, the syntax is similar to
525 the |xcolor| syntax for mixing colors.
526
527 Here is the computation of the point in the middle of the line $AB$:
528 %
529 \begin{codeexample}[preamble={\usetikzlibrary{calc}}]
530 \begin{tikzpicture}
531 \coordinate [label=left:$A$] (A) at (0,0);
532 \coordinate [label=right:$B$] (B) at (1.25,0.25);
533 \draw (A) -- (B);
534 \node [fill=red,inner sep=1pt,label=below:$X$] (X) at ($ (A)!.5!(B) $) {};
535 \end{tikzpicture}
536 \end{codeexample}
537
538 The computation of the point $D$ in Euclid's second proposition is a bit more
539 complicated. It can be expressed as follows: Consider the line from $X$ to $B$.
540 Suppose we rotate this line around $X$ for 90$^\circ$ and then stretch it by a
541 factor of $\sin(60^\circ) \cdot 2$. This yields the desired point~$D$. We can
542 do the stretching using the partway modifier above, for the rotation we need a
543 new modifier: the rotation modifier. The idea is that the second coordinate in
544 a partway computation can be prefixed by an angle. Then the partway point is
545 computed normally (as if no angle were given), but the resulting point is
546 rotated by this angle around the first point.
547 %
548 \begin{codeexample}[preamble={\usetikzlibrary{calc}}]
549 \begin{tikzpicture}
550 \coordinate [label=left:$A$] (A) at (0,0);
551 \coordinate [label=right:$B$] (B) at (1.25,0.25);
552 \draw (A) -- (B);
553 \node [fill=red,inner sep=1pt,label=below:$X$] (X) at ($ (A)!.5!(B) $) {};
554 \node [fill=red,inner sep=1pt,label=above:$D$] (D) at
555 ($ (X) ! {sin(60)*2} ! 90:(B) $) {};
556 \draw (A) -- (D) -- (B);
557 \end{tikzpicture}
558 \end{codeexample}
559
560 Finally, it is not necessary to explicitly name the point $X$. Rather, again
561 like in the |xcolor| package, it is possible to chain partway modifiers:
562 %
563 \begin{codeexample}[preamble={\usetikzlibrary{calc}}]
564 \begin{tikzpicture}
565 \coordinate [label=left:$A$] (A) at (0,0);
566 \coordinate [label=right:$B$] (B) at (1.25,0.25);
567 \draw (A) -- (B);
568 \node [fill=red,inner sep=1pt,label=above:$D$] (D) at
569 ($ (A) ! .5 ! (B) ! {sin(60)*2} ! 90:(B) $) {};
570 \draw (A) -- (D) -- (B);
571 \end{tikzpicture}
572 \end{codeexample}
573
574
575 \subsubsection{Intersecting a Line and a Circle}
576
577 The next step in the construction is to draw a circle around $B$ through $C$,
578 which is easy enough to do using the |circle through| option. Extending the
579 lines $DA$ and $DB$ can be done using partway calculations, but this time with
580 a part value outside the range $[0,1]$:
581 %
582 \begin{codeexample}[preamble={\usetikzlibrary{calc,through}}]
583 \begin{tikzpicture}
584 \coordinate [label=left:$A$] (A) at (0,0);
585 \coordinate [label=right:$B$] (B) at (0.75,0.25);
586 \coordinate [label=above:$C$] (C) at (1,1.5);
587 \draw (A) -- (B) -- (C);
588 \coordinate [label=above:$D$] (D) at
589 ($ (A) ! .5 ! (B) ! {sin(60)*2} ! 90:(B) $) {};
590 \node (H) [label=135:$H$,draw,circle through=(C)] at (B) {};
591 \draw (D) -- ($ (D) ! 3.5 ! (B) $) coordinate [label=below:$F$] (F);
592 \draw (D) -- ($ (D) ! 2.5 ! (A) $) coordinate [label=below:$E$] (E);
593 \end{tikzpicture}
594 \end{codeexample}
595
596 We now face the problem of finding the point $G$, which is the intersection of
597 the line $BF$ and the circle $H$. One way is to use yet another variant of the
598 partway computation: Normally, a partway computation has the form
599 \meta{p}|!|\meta{factor}|!|\meta{q}, resulting in the point
600 $(1-\meta{factor})\meta{p} + \meta{factor}\meta{q}$. Alternatively, instead of
601 \meta{factor} you can also use a \meta{dimension} between the points. In this
602 case, you get the point that is \meta{dimension} away from \meta{p} on the
603 straight line to \meta{q}.
604
605 We know that the point $G$ is on the way from $B$ to $F$. The distance is given
606 by the radius of the circle~$H$. Here is the code for computing $H$:
607 %
608 {\ifpgfmanualexternalize\tikzexternaldisable\fi
609 \begin{codeexample}[
610 preamble={\usetikzlibrary{calc,through}},
611 pre={\begin{tikzpicture}
612 \coordinate [label=left:$A$] (A) at (0,0);
613 \coordinate [label=right:$B$] (B) at (0.75,0.25);
614 \coordinate [label=above:$C$] (C) at (1,1.5);
615 \draw (A) -- (B) -- (C);
616 \coordinate [label=above:$D$] (D) at
617 ($ (A) ! .5 ! (B) ! {sin(60)*2} ! 90:(B) $) {};
618 \draw (D) -- ($ (D) ! 3.5 ! (B) $) coordinate [label=below:$F$] (F);
619 \draw (D) -- ($ (D) ! 2.5 ! (A) $) coordinate [label=below:$E$] (E);},
620 post={\end{tikzpicture}},
621 ]
622 \node (H) [label=135:$H$,draw,circle through=(C)] at (B) {};
623 \path let \p1 = ($ (B) - (C) $) in
624 coordinate [label=left:$G$] (G) at ($ (B) ! veclen(\x1,\y1) ! (F) $);
625 \fill[red,opacity=.5] (G) circle (2pt);
626 \end{codeexample}
627
628 However, there is a simpler way: We can simply name the path of the circle and
629 of the line in question and then use |name intersections| to compute the
630 intersections.
631 %
632 \begin{codeexample}[
633 preamble={\usetikzlibrary{calc,intersections,through}},
634 pre={\begin{tikzpicture}
635 \coordinate [label=left:$A$] (A) at (0,0);
636 \coordinate [label=right:$B$] (B) at (0.75,0.25);
637 \coordinate [label=above:$C$] (C) at (1,1.5);
638 \draw (A) -- (B) -- (C);
639 \coordinate [label=above:$D$] (D) at
640 ($ (A) ! .5 ! (B) ! {sin(60)*2} ! 90:(B) $) {};
641 \draw (D) -- ($ (D) ! 3.5 ! (B) $) coordinate [label=below:$F$] (F);
642 \draw (D) -- ($ (D) ! 2.5 ! (A) $) coordinate [label=below:$E$] (E);},
643 post={\end{tikzpicture}},
644 ]
645 \node (H) [name path=H,label=135:$H$,draw,circle through=(C)] at (B) {};
646 \path [name path=B--F] (B) -- (F);
647 \path [name intersections={of=H and B--F,by={[label=left:$G$]G}}];
648 \fill[red,opacity=.5] (G) circle (2pt);
649 \end{codeexample}
650 }%
651
652
653 \subsubsection{The Complete Code}
654
655 \begin{codeexample}[pre={\pgfmathsetseed{1}},preamble={\usetikzlibrary{calc,intersections,through}}]
656 \begin{tikzpicture}[thick,help lines/.style={thin,draw=black!50}]
657 \def\A{\textcolor{orange}{$A$}} \def\B{\textcolor{input}{$B$}}
658 \def\C{\textcolor{input}{$C$}} \def\D{$D$}
659 \def\E{$E$} \def\F{$F$}
660 \def\G{$G$} \def\H{$H$}
661 \def\K{$K$} \def\L{\textcolor{output}{$L$}}
662
663 \colorlet{input}{blue!80!black} \colorlet{output}{red!70!black}
664
665 \coordinate [label=left:\A] (A) at ($ (0,0) + .1*(rand,rand) $);
666 \coordinate [label=right:\B] (B) at ($ (1,0.2) + .1*(rand,rand) $);
667 \coordinate [label=above:\C] (C) at ($ (1,2) + .1*(rand,rand) $);
668
669 \draw [input] (B) -- (C);
670 \draw [help lines] (A) -- (B);
671
672 \coordinate [label=above:\D] (D) at ($ (A)!.5!(B) ! {sin(60)*2} ! 90:(B) $);
673
674 \draw [help lines] (D) -- ($ (D)!3.75!(A) $) coordinate [label=-135:\E] (E);
675 \draw [help lines] (D) -- ($ (D)!3.75!(B) $) coordinate [label=-45:\F] (F);
676
677 \node (H) at (B) [name path=H,help lines,circle through=(C),draw,label=135:\H] {};
678 \path [name path=B--F] (B) -- (F);
679 \path [name intersections={of=H and B--F,by={[label=right:\G]G}}];
680
681 \node (K) at (D) [name path=K,help lines,circle through=(G),draw,label=135:\K] {};
682 \path [name path=A--E] (A) -- (E);
683 \path [name intersections={of=K and A--E,by={[label=below:\L]L}}];
684
685 \draw [output] (A) -- (L);
686
687 \foreach \point in {A,B,C,D,G,L}
688 \fill [black,opacity=.5] (\point) circle (2pt);
689
690 % \node ...
691 \end{tikzpicture}
692 \end{codeexample}