Debian Codegraph @

texmf-dist/doc/latex/tkz-elements/TKZdoc-elements-classes-path.tex

raw
1 \newpage
2 \section{Class \tkzClass{path}}
3
4 This class was developed in response to a question posted on \code{tex.stackexchange.com} [\href{https://tex.stackexchange.com/questions/741663/fill-a-space-made-by-three-arcs/741758#741758}{fill-a-space-made-by-three-arcs}].
5
6 The concept was missing from the package, although in some cases it was possible to work around this using \TIKZ{} paths. When creating the \code{conic} class, I frequently had to manipulate tables of coordinates, so it became natural to formalize these as \code{paths}.
7
8 \directlua{
9 z.O = point(0, 0)
10 z.A = point(8, 0)
11 z.E = z.O:rotation(2*math.pi / 3, z.A)
12 z.C = z.O:rotation(-2*math.pi / 3, z.A)
13 z.F, z.D, z.B = z.O:rotation(math.pi / 3, z.A, z.E, z.C)
14 L.AC = line(z.A, z.C)
15 L.OB = line(z.O, z.B)
16 L.OD = line(z.O, z.D)
17 L.CE = line(z.C, z.E)
18 L.AE = line(z.A, z.E)
19 L.OF = line(z.O, z.F)
20 z.G = intersection(L.AC, L.OB)
21 z.H = intersection(L.CE, L.OD)
22 z.I = intersection(L.AE, L.OF)
23 C.GA = circle(z.G, z.A)
24 C.FE = circle(z.F, z.E)
25 C.BA = circle(z.B, z.A)
26 C.IE = circle(z.I, z.E)
27 C.DE = circle(z.D, z.E)
28 C.FA = circle(z.F, z.A)
29 C.HE = circle(z.H, z.E)
30 C.GC = circle(z.G, z.C)
31 _,z.J = intersection(C.GA, C.FE)
32 _,z.K = intersection(C.BA, C.IE)
33 _,z.L = intersection(C.DE, C.IE)
34 _,z.M = intersection(C.FA, C.HE)
35 z.N = intersection(C.BA, C.HE)
36 z.P = intersection(C.DE, C.GC)
37 % the paths
38 local nb = 40
39 PA.th1 = C.IE:path(z.E, z.L, nb) + C.DE:path(z.L, z.O, nb) - C.FA:path(z.E, z.O, nb)
40 PA.th2 = C.IE:path(z.G, z.A, nb) - C.FA:path(z.M, z.A, nb) - C.HE:path(z.G, z.M, nb)
41 PA.th3 = C.IE:path(z.H, z.K, nb) + C.BA:path(z.K, z.C, nb) - C.GC:path(z.H, z.C, nb)
42 PA.th4 = C.GA:path(z.A, z.J, nb) + C.FA:path(z.J, z.O, nb) - C.BA:path(z.A, z.O, nb)
43 PA.th5 = C.HE:path(z.C, z.N, nb) + C.BA:path(z.N, z.O, nb) - C.DE:path(z.C, z.O, nb)
44 PA.th6 = C.HE:path(z.I, z.E, nb) - C.DE:path(z.P, z.E, nb) - C.GA:path(z.I, z.P, nb)}
45 \begin{center}
46 \begin{tikzpicture}[scale = .5, rotate = -30]
47 \tkzGetNodes
48 \tkzFillCircle[fill=yellow!20](O,A)
49 \tkzDrawCircle[ultra thick,black](O,A)
50 \tkzClipCircle(O,A)
51 \tkzDrawCoordinates[draw,ultra thick,fill=green!15](PA.th1)
52 \tkzDrawCoordinates[draw,ultra thick,fill=green!15](PA.th2)
53 \tkzDrawCoordinates[draw,ultra thick,fill=blue!15](PA.th3)
54 \tkzDrawCoordinates[draw,ultra thick,fill=blue!15](PA.th4)
55 \tkzDrawCoordinates[draw,ultra thick,fill=red!15](PA.th5)
56 \tkzDrawCoordinates[draw,ultra thick,fill=red!15](PA.th6)
57 \tkzDrawCircle[ultra thick,black](O,A)
58 \end{tikzpicture}
59 \end{center}
60
61 \begin{verbatim}
62
63 \directlua{
64 z.O = point(0, 0)
65 z.A = point(8, 0)
66 z.E = z.O:rotation(2*math.pi / 3, z.A)
67 z.C = z.O:rotation(-2*math.pi / 3, z.A)
68 z.F, z.D, z.B = z.O:rotation(math.pi / 3, z.A, z.E, z.C)
69 L.AC = line(z.A, z.C)
70 L.OB = line(z.O, z.B)
71 L.OD = line(z.O, z.D)
72 L.CE = line(z.C, z.E)
73 L.AE = line(z.A, z.E)
74 L.OF = line(z.O, z.F)
75 z.G = intersection(L.AC, L.OB)
76 z.H = intersection(L.CE, L.OD)
77 z.I = intersection(L.AE, L.OF)
78 C.GA = circle(z.G, z.A)
79 C.FE = circle(z.F, z.E)
80 C.BA = circle(z.B, z.A)
81 C.IE = circle(z.I, z.E)
82 C.DE = circle(z.D, z.E)
83 C.FA = circle(z.F, z.A)
84 C.HE = circle(z.H, z.E)
85 C.GC = circle(z.G, z.C)
86 _,z.J = intersection(C.GA, C.FE)
87 _,z.K = intersection(C.BA, C.IE)
88 _,z.L = intersection(C.DE, C.IE)
89 _,z.M = intersection(C.FA, C.HE)
90 z.N = intersection(C.BA, C.HE)
91 z.P = intersection(C.DE, C.GC)
92 % the paths
93 local nb = 40
94 PA.th1 = C.IE:path(z.E, z.L, nb) + C.DE:path(z.L, z.O, nb) - C.FA:path(z.E, z.O, nb)
95 PA.th2 = C.IE:path(z.G, z.A, nb) - C.FA:path(z.M, z.A, nb) - C.HE:path(z.G, z.M, nb)
96 PA.th3 = C.IE:path(z.H, z.K, nb) + C.BA:path(z.K, z.C, nb) - C.GC:path(z.H, z.C, nb)
97 PA.th4 = C.GA:path(z.A, z.J, nb) + C.FA:path(z.J, z.O, nb) - C.BA:path(z.A, z.O, nb)
98 PA.th5 = C.HE:path(z.C, z.N, nb) + C.BA:path(z.N, z.O, nb) - C.DE:path(z.C, z.O, nb)
99 PA.th6 = C.HE:path(z.I, z.E, nb) - C.DE:path(z.P, z.E, nb) - C.GA:path(z.I, z.P, nb)}
100 \begin{center}
101 \begin{tikzpicture}[scale = .5, rotate = -30]
102 \tkzGetNodes
103 \tkzFillCircle[fill=yellow!20](O,A)
104 \tkzDrawCircle[ultra thick,black](O,A)
105 \tkzClipCircle(O,A)
106 \tkzDrawCoordinates[draw,ultra thick,fill=green!15](PA.th1)
107 \tkzDrawCoordinates[draw,ultra thick,fill=green!15](PA.th2)
108 \tkzDrawCoordinates[draw,ultra thick,fill=blue!15](PA.th3)
109 \tkzDrawCoordinates[draw,ultra thick,fill=blue!15](PA.th4)
110 \tkzDrawCoordinates[draw,ultra thick,fill=red!15](PA.th5)
111 \tkzDrawCoordinates[draw,ultra thick,fill=red!15](PA.th6)
112 \tkzDrawCircle[ultra thick,black](O,A)
113 \end{tikzpicture}
114 \end{center}
115
116 \end{verbatim}
117
118 \medskip
119
120 In addition to manually constructing paths from a list of points, you can also automatically generate paths using geometric classes. This is particularly useful when drawing arcs, segments, or interpolated curves. Currently, the following classes provide built-in methods to generate path objects:
121
122 \begin{itemize}
123 \item \tkzClass{line} — segment interpolation between two points See [\ref{ssub:line_path}]
124 \item \tkzClass{triangle} — triangle outline as a path. See [\ref{ssub:method_tkzmeth_triangle_path}]
125 \item \tkzClass{circle} — circular arcs between two points on the circumference See [\ref{ssub:circle_path}]
126 \item \tkzClass{conic} — arcs or full curves parameterized. See [\ref{ssub:conic_path}]
127 \end{itemize}
128
129 These methods typically return a \tkzClass{path} object with a default or user-defined number of points. For instance:
130
131 \begin{verbatim}
132 PA.arc = C.OA:path(z.A, z.B, 40) -- circular arc from A to B on circle OA
133 PA.seg = L.AB:path(10) -- line segment from A to B with 10 points
134 PA.tri = T.ABC:path() -- triangle outline path
135 PA.co = CO.EL:points(0, 1, 50) -- conic arc from t=0 to t=1
136 \end{verbatim}
137
138 These generated paths can be combined, reversed, filled, or used for \TIKZ{} decorations.
139
140
141 \subsection{Overview}
142
143 The path object represents an ordered sequence of 2D complex points, stored as TikZ-compatible strings in the form "(x,y)". It supports geometric operations such as translation, homothety, and rotation, and can be combined with other paths using Lua operator overloading.
144
145 The variable \code{PA} holds a table used to store paths. It is optional, and you are free to choose the variable name. However, using \code{PA} is a recommended convention for clarity and consistency. If you use a custom variable (e.g., Paths), you must initialize it manually. The \tkzFct{tkz-elements}{init\_elements()} function reinitializes the \code{PA} table if used.
146
147 When working with path objects—especially when several intermediate curves are involved—it is strongly recommended to use indexed names such as \code{PA.p1}, \code{PA.p2}, etc., rather than a generic name.
148
149 \begin{mybox}
150 \textbf{Rule — Prefer explicit, indexed names:}
151 Using \code{PA.p1}, \code{PA.p2}, \code{PA.result}, etc., improves clarity, avoids accidental overwriting, and ensures that all geometric data can be reset with a single call to \tkzFct{init\_elements()}.
152 \end{mybox}
153
154 This approach aligns with the overall philosophy of \tkzNamePack{tkz-elements}, where named tables like \code{PA}, \code{T}, \code{C}, and \code{L} are used to store structured geometric information. Indexing also helps make diagrams more readable, reproducible, and easier to debug.
155
156
157 It supports arithmetic operations for combining and manipulating paths, as well as geometric transformations such as translation, rotation, homothety, and reversal.
158
159 \subsection{Notes}
160
161 \begin{itemize}
162 \item Points are internally stored as strings like "(x,y)", and parsing is done via a utility function parse\_point.
163
164 \begin{verbatim}
165 local x, y = utils.parse_point("(3.5, -2)")
166 -- x = 3.5, y = -2.0
167 \end{verbatim}
168
169
170 \item Number formatting (e.g., for \TIKZ{} compatibility) should be handled by utils\_checknumber, assumed to be defined globally or in a utility module.
171
172 \item The class is designed to be lightweight and compatible with TikZ/LuaLaTeX workflows.
173
174 \end{itemize}
175
176
177
178
179
180 \subsection{Constructor \tkzMeth{path}{path(<list>)}}
181
182 Creates a new path.
183
184 \medskip
185
186 \texttt{Arguments: }
187 \code{<list>} (optional): a Lua table containing point
188 coordinates given as strings in TikZ format,
189 e.g. \code{"(0,0)"}, \code{"(1,2)"}.
190
191
192 \begin{center}
193 \begin{verbatim}
194 PA.name = path(table_of_points) -- Creates a new path object.
195 \end{verbatim}
196 \end{center}
197
198 \medskip
199
200 If no argument is provided, an empty path is created.
201
202
203 If data is provided, the input should be a table of points written as strings, e.g., |{ "(0,0)", "(1,0)"|, |"(1,1)" }| otherwise creates an empty path.
204
205 Here is a \tkzname{path} representing a simple triangle:
206
207 \begin{mybox}
208 \begin{verbatim}
209 PA.name = path({ "(0, 0)", "(1, 0)","(1, 1)" })
210 tex.print(tostring(PA.name))
211 \end{verbatim}
212 \end{mybox}
213
214
215
216 \subsection{Operator Overloading; metamethods}
217
218 \subsubsection{Table of metamethods}
219 \bgroup
220 \catcode`_=12
221 \small
222 \captionof{table}{Methods of the class vector.}\label{path:metamethods}
223 \begin{tabular}{ll}
224 \toprule
225 \texttt{Metamethods} & \texttt{Reference} \\
226 \midrule
227 \tkzMeta{path}{add(path1,path2)} & [\ref{ssub:metamethod_add}] \\
228 \tkzMeta{path}{sub(path1,path2)} & [\ref{ssub:metamethod_sub}] \\
229 \tkzMeta{path}{unm(path1)} & [\ref{ssub:metamethod_unm}] \\
230 \tkzMeta{vector}{tostring(path1)} & [\ref{ssub:metamethod_tostring}] \\
231 \bottomrule
232 \end{tabular}
233 \egroup
234
235 \subsubsection{Metamethod \tkzMeta{path}{add}}
236 \label{ssub:metamethod_add}
237 If p1 and p2 are two paths, we can obtain a third path with
238 |p3 = p1:add(p2)|
239
240 More easily with an operator
241
242 \code{p3 = p1 + p2} — \textbf{Concatenation} \\
243 Returns a new path by appending \code{p2} after \code{p1}.
244
245 \subsubsection{Metamethod \tkzMeta{path}{unm}}
246 \label{ssub:metamethod_sub}
247 |p2 = p1:unm()|
248
249 More easily with an operator
250
251 \code{-p} — \textbf{Reversal} \\
252 Returns a copy of the path in reverse order.
253
254 \subsubsection{Metamethod \tkzMeta{path}{sub}}
255 \label{ssub:metamethod_unm}
256 If p1 and p2 are two paths, we can obtain a third path with
257 |p3 = p1:sub(p2)|
258
259 More easily with an operator
260
261 \code{p1 - p2} — \textbf{Subtraction} \\
262 Equivalent to \code{p1 + (-p2)} (concatenates \code{p1} with the reversed \code{p2}).
263
264 \subsubsection{Metamethod \tkzMeta{path}{tostring}}
265 \label{ssub:metamethod_tostring}
266 String Representation
267
268 The \code{tostring} metamethod provides a readable form, mainly for debugging:
269
270 \begin{verbatim}
271 tostring(p) --> path: { (x1,y1) , (x2,y2) , ... }
272 \end{verbatim}
273
274
275
276 \subsection{Methods}
277
278 \bgroup
279 \catcode`\_ = 12
280 \small
281 \captionof{table}{Methods of the \texttt{path} class.}\label{path:methods}
282 \begin{tabular}{ll}
283 \toprule
284 \texttt{Method} & \texttt{Reference} \\
285 \midrule
286 \tkzMeth{path}{add\_point(z)} & [\ref{ssub:method_tkzmeth_path_add__point}]\\
287 \tkzMeth{path}{get(i)} & [\ref{ssub:method_tkzmeth_path_get}] \\
288 \tkzMeth{path}{copy()} & [\ref{ssub:method_tkzmeth_path_copy}] \\
289 \tkzMeth{path}{count()} & [\ref{ssub:method_tkzmeth_path_count}] \\
290 \tkzMeth{path}{translate(dx, dy)} & [\ref{ssub:method_tkzmeth_path_translate}] \\
291 \tkzMeth{path}{scale(kx, ky)} & [\ref{ssub:path_scale}] \\
292 \tkzMeth{path}{homothety(pt, k)} & [\ref{ssub:path_homothety}] \\
293 \tkzMeth{path}{map(fx, fy)} & [\ref{ssub:path_map}] \\
294 \tkzMeth{path}{rotate(pt, an)} & [\ref{ssub:method_tkzmeth_path_rotate}] \\
295 \tkzMeth{path}{close()} & [\ref{ssub:method_tkzmeth_path_close}]\\
296 \tkzMeth{path}{sub(i1, i2)} & [\ref{ssub:method_tkzmeth_path_sub_i1_i2}] \\
297 \tkzMeth{path}{show()} & [\ref{ssub:method_tkzmeth_path_show}] \\
298 \tkzMeth{path}{add\_pair\_to\_path(z1, z2, n)} & or add\_pair [\ref{ssub:subsubsection_name}] \\
299 \tkzMeth{path}{concat(sep)} & [\ref{ssub:path_concat}]\\
300 \bottomrule
301 \end{tabular}
302 \egroup
303
304
305
306 \subsubsection{Method \tkzMeth{path}{add\_point(pt,<n>)}}
307 \label{ssub:method_tkzmeth_path_add__point}
308
309 This method appends a point to the path.
310
311 \medskip
312
313 \textbf{Arguments:}
314 \begin{itemize}
315 \item \code{pt}: either
316 \begin{itemize}
317 \item a \code{point} object, or
318 \item a string representing a point coordinate, e.g. \code{"(2,3)"} or
319 \code{"(1e-5,0)"}.
320 \end{itemize}
321 \item \code{n} (optional): number of decimal places used when formatting
322 the coordinates for TikZ output.
323 \end{itemize}
324
325 \medskip
326
327 If \code{pt} is a string, it is parsed using \code{utils.parse\_point}.
328 Then the point is formatted (via \code{utils.format\_point}) and stored
329 in the path.
330
331 \medskip
332
333 If \code{n} is provided, the coordinates are rounded to \code{n}
334 decimal places before being stored in the path.
335 If omitted, a default formatting precision is used.
336
337 \medskip
338
339 \texttt{Note: }
340 The parameter \code{n} affects only the numerical representation in the
341 generated TikZ code. It does not modify the internal geometric
342 computation.
343
344 \medskip
345
346 \textbf{Examples}
347
348 \begin{tkzexample}[latex=.35\textwidth]
349 \directlua{
350 PA.triangle = path()
351 PA.triangle:add_point(point(0,0),0)
352 PA.triangle:add_point(point(1,0),0)
353 PA.triangle:add_point(point(1,1),0)
354 tex.print(tostring(PA.triangle))
355 }
356 \end{tkzexample}
357
358 \begin{tkzexample}[latex=.35\textwidth]
359 \directlua{
360 PA.p = path({ "(0,0)", "(1,0)", "(1,1)" })
361 PA.p:add_point("(2,3)",0)
362 PA.p:add_point("(1e-5,0)",5)
363 tex.print(tostring(PA.p))
364 }
365 \end{tkzexample}
366 \subsubsection{Method \tkzMeth{path}{get(i)}}
367 \label{ssub:method_tkzmeth_path_get}
368
369 The method \code{get(i)} returns the i-th point stored in the path \texttt{PA.p}.
370
371 Paths in tkz-elements store their points as formatted coordinate strings
372 (e.g. "(1.000,2.000)") for compatibility with TikZ and for efficient
373 transfer from Lua to \TeX.
374 When \code{get(i)} is called, these coordinates are automatically converted back
375 into a proper Lua point object.
376
377 \medskip
378 \texttt{Return value: }
379
380 A point object corresponding to the i-th element of the path.
381
382 \medskip
383 \texttt{Errors: }
384
385 If the index is outside the valid range, an error is raised:
386
387
388
389 \subsubsection{Method \tkzMeth{path}{copy()}}
390 \label{ssub:method_tkzmeth_path_copy}
391
392 Returns a deep copy of the path
393
394 \begin{tkzexample}[latex=.3\textwidth]
395 \directlua{
396 PA.p1 = path({ "(0, 0)", "(1, 0)","(1, 1)" })
397 PA.p2 = PA.p1:copy()
398 PA.p2:add_point(point(0, 0),0)}
399 \begin{tikzpicture}
400 \tkzDrawCoordinates(PA.p2)
401 \end{tikzpicture}
402 \end{tkzexample}
403
404 \subsubsection{Method \tkzMeth{path}{count()}}
405 \label{ssub:method_tkzmeth_path_count}
406
407 Returns a deep copy of the path
408
409 \begin{tkzexample}[latex=.3\textwidth]
410 \directlua{
411 PA.p1 = path({ "(0, 0)", "(1, 0)","(1, 1)" })
412 PA.p2 = PA.p1:copy()
413 PA.p2:add_point(point(0, 0),0)
414 tex.print(PA.p2:count())
415 }
416 \end{tkzexample}
417
418
419
420 \subsubsection{Method \tkzMeth{path}{translate(dx,dy)}}
421 \label{ssub:method_tkzmeth_path_translate}
422
423 Translates the path by a given vector \((dx,dy)\)
424
425 \begin{tkzexample}[latex=.3\textwidth]
426 \directlua{
427 PA.p1 = path({ "(0,0)", "(1,0)", "(1,1)" })
428 PA.p2 = PA.p1:translate(2,1)}
429 \begin{tikzpicture}
430 \tkzDrawCoordinates(PA.p2)
431 \end{tikzpicture}
432 \end{tkzexample}
433
434 \subsubsection{Method \tkzMeth{path}{scale(kx,ky)}}
435 \label{ssub:path_scale}
436
437 This method applies an anisotropic scaling to a path with respect to the origin.
438
439 \medskip
440
441 \texttt{Arguments: }
442 \begin{itemize}
443 \item \code{kx} : horizontal scaling factor,
444 \item \code{ky} : vertical scaling factor.
445 \end{itemize}
446
447 \medskip
448
449 \texttt{Description: }
450
451 Each point $P(x,y)$ of the path is transformed into
452 \[
453 P'(kx \cdot x,\; ky \cdot y).
454 \]
455
456 The transformation is performed relative to the origin $(0,0)$.
457
458 \medskip
459
460 \texttt{Returns: }
461
462 A new \code{path} object containing the scaled points.
463 The original path remains unchanged.
464
465 \medskip
466
467 \texttt{Example: }
468
469 \begin{verbatim}
470 PA.scaled = PA.original:scale(2,0.5)
471 \end{verbatim}
472
473 This doubles the $x$-coordinates and halves the $y$-coordinates.
474
475 \subsubsection{Method \tkzMeth{path}{homothety(k,<center>)}}
476 \label{ssub:path_homothety}
477
478 This method applies a uniform scaling (homothety) to a path.
479
480 \medskip
481
482 \textbf{Arguments}
483 \begin{itemize}
484 \item \code{k} : scaling factor,
485 \item \code{center} : optional reference point.
486 \end{itemize}
487
488 \medskip
489
490 \textbf{Change in Version 5}
491
492 The \code{center} parameter is now optional.
493 If omitted, the origin $(0,0)$ is used.
494
495 \medskip
496
497 \textbf{Description}
498
499 Each point $P(x,y)$ is transformed into
500 \[
501 P'(x',y') = (x_c + k(x - x_c),\; y_c + k(y - y_c)).
502 \]
503
504 \medskip
505
506 \textbf{Returns}
507
508 A new \code{path} object.
509
510 \subsubsection{Method \tkzMeth{path}{map(fx,<fy>)}}
511 \label{ssub:path_map}
512
513 This method applies a coordinate transformation to each point of the path.
514
515 \medskip
516
517 \texttt{Arguments: }
518 \begin{itemize}
519 \item \code{fx} : Lua function applied to the $x$-coordinate,
520 \item \code{fy} : optional Lua function applied to the $y$-coordinate.
521 \end{itemize}
522
523 If \code{fy} is omitted, the function \code{fx} is applied to both coordinates.
524
525 \medskip
526
527 \texttt{Description: }
528
529 Each point $P(x,y)$ of the path is transformed into
530 \[
531 P'(x',y')
532 \]
533 according to:
534 \[
535 x' = fx(x),
536 \qquad
537 y' = fy(y).
538 \]
539
540 If only one function is provided:
541 \[
542 (x,y) \mapsto (f(x), f(y)).
543 \]
544
545 \medskip
546
547 \texttt{Returns: }
548
549 A new \code{path} object containing the transformed points.
550 The original path is not modified.
551
552 \medskip
553
554 \texttt{Examples: }
555
556 Apply the same function to both coordinates:
557
558 \begin{verbatim}
559 PA.new = PA.old:map(math.abs)
560 \end{verbatim}
561
562 Apply different transformations:
563
564 \begin{verbatim}
565 PA.new = PA.old:map(
566 function(x) return 2*x end,
567 function(y) return 0.5*y end
568 )
569 \end{verbatim}
570
571 This produces the transformation
572 \[
573 (x,y) \mapsto (2x, 0.5y).
574 \]
575
576
577
578
579 \subsubsection{Method \tkzMeth{path}{rotate(pt, an)}}
580 \label{ssub:method_tkzmeth_path_rotate}
581
582 Rotates the path around \(pt\) by angle \(\theta\) (radians)
583
584 \begin{tkzexample}[latex=.3\textwidth]
585 \directlua{
586 z.O = point(1, 1)
587 PA.base = path({ "(1,0)", "(2,0)", "(2,1)" })
588 PA.rotated = PA.base:rotate(z.O, math.pi / 2)}
589 \begin{tikzpicture}
590 \tkzGetNodes
591 \tkzDrawCoordinates(PA.base)
592 \tkzDrawCoordinates(PA.rotated)
593 \tkzDrawPoint(O)
594 \end{tikzpicture}
595 \end{tkzexample}
596
597 \subsubsection{Method \tkzMeth{path}{close()}}
598 \label{ssub:method_tkzmeth_path_close}
599
600 Closes the path by repeating the first point at the end
601
602 \begin{tkzexample}[latex=.3\textwidth]
603 \directlua{
604 PA.p = path({ "(0,0)", "(1,0)", "(1,1)" })
605 PA.closed = PA.p:close()}
606 \begin{tikzpicture}
607 \tkzDrawCoordinates[fill=green!20](PA.closed)
608 \end{tikzpicture}
609 \end{tkzexample}
610
611 \subsubsection{Method \tkzMeth{path}{sub(i1, i2)}}
612 \label{ssub:method_tkzmeth_path_sub_i1_i2}
613
614 Returns a subpath between two indices
615
616
617 \begin{tkzexample}[latex=.3\textwidth]
618 \directlua{
619 PA.full = path({ "(0,0)", "(1,0)", "(1,1)", "(0,1)", "(0,0)" })
620 PA.part = PA.full:sub(2, 4)}
621 \begin{tikzpicture}
622 \tkzDrawCoordinates[fill=green!20](PA.part)
623 \end{tikzpicture}
624 \end{tkzexample}
625
626
627 \subsubsection{Method \tkzMeth{path}{show()}}
628 \label{ssub:method_tkzmeth_path_show}
629
630 Prints the path in the terminal (for debugging)
631
632 \begin{tkzexample}[latex=.45\textwidth]
633 \directlua{
634 PA.p = path({ "(0,0)", "(1,0)", "(1,1)" })
635 PA.p:show()}
636 \end{tkzexample}
637
638 % ADD(AM, 4.25) some complements
639 \subsubsection{Method \tkzMeth{path}{add\_pair\_to\_path(p, p, n)} or \tkzMeth{path}{add\_pair(p, p, n)}}
640 \label{ssub:subsubsection_name}
641 \begin{verbatim}
642 path:add_pair(z1, z2, decimals)
643 \end{verbatim}
644
645 or equivalently:
646
647 \begin{verbatim}
648 path:add_pair_to_path(z1, z2, decimals)
649 \end{verbatim}
650
651 \medskip
652 \texttt{Purpose: }
653
654 The coordinates of \code{z1} and \code{z2} are extracted, formatted with a fixed number of decimals $n$, and combined into a string \code{"x1/y1/x2/y2"} appended to the path.
655 This string is then inserted into the current path table.This method appends to a \tkzname{path} object. a formatted point pair.
656 It is mainly used to store segments, by keeping together the coordinates of two points.
657
658 \medskip
659 \texttt{Arguments: }
660
661 \begin{itemize}
662 \item \code{z1}, \code{z2} – points given as tables \code{\{re, im\}}.
663 \item \code{decimals} (optional) – number of decimals (default: \code{5}).
664 \end{itemize}
665
666 \medskip
667 \texttt{Returns: }
668
669 Nothing (modifies the path in place).
670
671 \medskip
672 \texttt{Example usage: }
673
674 \begin{verbatim}
675 local PA.p = path() -- empty path
676 local z.A = point(1, 2)
677 local z.B = point(3, 4)
678 PA.p:add_pair(z.A, z.B, 2)
679 -- PA.p = {"1.00/2.00/3.00/4.00"}
680 \end{verbatim}
681
682
683 \subsubsection{Method \tkzMeth{path}{concat(sep)}}
684 \label{ssub:path_concat}
685
686 This method returns a string obtained by concatenating all the coordinates stored
687 in a \tkzClass{path} object. The coordinates are joined in order using a
688 separator.
689
690 \medskip
691 Internally, a \tkzClass{path} contains an ordered Lua table of points written in
692 TikZ syntax, such as \verb|(x,y)|. The \tkzMeth{path}{concat} method simply
693 joins these entries into a single textual path.
694
695 \medskip
696 \texttt{Syntax: }
697 \begin{verbatim}
698 S = PA.myPath:concat(sep)
699 \end{verbatim}
700
701 \medskip
702 \texttt{Arguments: }
703 \begin{itemize}
704 \item \code{sep} — optional string used to separate the coordinates
705 (default: a single space \verb|" "|).
706 \end{itemize}
707
708 \medskip
709 \texttt{Returns: }
710 A Lua string containing all points of the path, separated by \code{sep}.
711 The resulting string is suitable for direct insertion into a TikZ path.
712
713 \texttt{Example: }
714
715 \begin{tkzexample}[code only]
716 \directlua{
717 init_elements()
718 local A = point(0, 0)
719 local B = point(2, 1)
720 local C = point(3, 0)
721 local P = path:new()
722 P:add_point(A)
723 P:add_point(B)
724 P:add_point(C)
725 local S = P:concat(" -- ")
726 }
727 \end{tkzexample}
728
729
730 \texttt{Usefulness: }
731
732 This method is useful when:
733 \begin{itemize}
734 \item building TikZ paths dynamically from Lua;
735 \item exporting or combining geometric paths;
736 \item generating the argument of macros such as \verb|\tkzDrawPath|.
737 \end{itemize}
738
739
740 \subsubsection{Example : Director circle }
741 \label{ssub:director_circle}
742
743
744 Conics are drawn using the \tkzClass{path} tool.
745
746 \begin{minipage}{.55\textwidth}
747 \begin{verbatim}
748 \directlua{
749 init_elements()
750 z.O = point(0, 0)
751 z.F1 = point(4, 1)
752 z.F2 = point(-4, -1)
753 z.H = point(4 * math.sqrt(2), 0)
754 local a = tkz.length(z.O, z.H)
755 CO.EL = conic(EL_bifocal(z.F2, z.F1, a))
756 PA.curve = CO.EL:points(0, 1, 50)
757 z.A = CO.EL.covertex
758 T.HOA = triangle(z.H, z.O, z.A)
759 z.P = T.HOA:parallelogram()
760 C.OP = circle(z.O, z.P)
761 z.L = C.OP:point(0.25)
762 T.LJ, T.LK = CO.EL:tangent_from(z.L)
763 z.J = T.LJ.pb
764 z.K = T.LK.pb}
765 \end{verbatim}
766 \end{minipage}
767 \begin{minipage}{.45\textwidth}
768 \directlua{
769 init_elements()
770 z.O = point(0, 0)
771 z.F1 = point(4, 1)
772 z.F2 = point(-4, -1)
773 z.H = point(4 * math.sqrt(2), 0)
774 local a = tkz.length(z.O, z.H)
775 CO.EL = conic(EL_bifocal(z.F2, z.F1, a))
776 PA.curve = CO.EL:points(0, 1, 50)
777 z.A = CO.EL.covertex
778 T.HOA = triangle(z.H, z.O, z.A)
779 z.P = T.HOA:parallelogram()
780 C.OP = circle(z.O, z.P)
781 z.L = C.OP:point(0.25)
782 T.LJ, T.LK = CO.EL:tangent_from(z.L)
783 z.J = T.LJ.pb
784 z.K = T.LK.pb}
785 \begin{center}
786 \begin{tikzpicture}[scale = .4]
787 \tkzGetNodes
788 \tkzDrawPoints(F1,F2,O)
789 \tkzDrawCircles[teal](O,P)
790 \tkzDrawPolygon(H,O,A,P)
791 \tkzDrawCoordinates[smooth,cyan](PA.curve)
792 \tkzDrawSegments[orange](O,P O,L L,J L,K)
793 \tkzDrawPoints(F1,F2,O,H,A,P,L,J,K)
794 \tkzLabelPoints(F1,F2,O,H,A,P,L,J,K)
795 \end{tikzpicture}
796 \end{center}
797 \end{minipage}
798
799 \begin{verbatim}
800 \begin{tikzpicture}
801 \tkzGetNodes
802 \tkzDrawPoints(F1,F2,O)
803 \tkzDrawCircles[teal](O,P)
804 \tkzDrawPolygon(H,O,A,P)
805 \tkzDrawCoordinates[smooth,cyan](PA.curve)
806 \tkzDrawSegments[orange](O,P O,L L,J L,K)
807 \tkzDrawPoints(F1,F2,O,H,A,P,L,J,K)
808 \tkzLabelPoints(F1,F2,O,H,A,P,L,J,K)
809 \end{tikzpicture}
810 \end{verbatim}
811
812
813
814 \subsubsection{Classic parabola}
815 \label{ssub:classic_parabola}
816
817 This example moves away from the concept of Euclidean geometry, but the method used can be reused.
818 Here we want to draw a parabola whose axis of symmetry is parallel to the y-axis.
819 Knowing three of its points, we can determine the equation. The set of points for tracing the parabola is obtained using the tools provided by the \tkzClass{path} class.
820
821 \begin{minipage}{.55\textwidth}
822 \begin{verbatim}
823 \directlua{
824 init_elements()
825 z.a = point(1, 0)
826 z.b = point(3, 2)
827 z.c = point(0, 2)
828 local A, B, C = parabola(z.a, z.b, z.c)
829
830 function f(t0, t1, n)
831 local PA.tbl = path()
832 for t = t0, t1, (t1 - t0) / n do
833 local y = A * t ^ 2 + B * t + C
834 local pt = point(t, y)
835 PA.tbl:add_point(pt)
836 end
837 return PA.tbl
838 end}
839 \end{verbatim}
840 \end{minipage}
841 \begin{minipage}{.45\textwidth}
842 \directlua{
843 init_elements()
844 z.a = point(1, 0)
845 z.b = point(3, 2)
846 z.c = point(0, 2)
847 local A, B, C = tkz.parabola(z.a, z.b, z.c)
848
849 function def_curve(t0, t1, n)
850 local p = path()
851 for t = t0,t1,(t1-t0)/n do
852 y = A*t^2+B*t +C
853 pt = point(t, y)
854 p:add_point(pt)
855 end
856 return p
857 end
858
859 PA.curve = def_curve(-1,3,100)}
860
861 \begin{center}
862 \begin{tikzpicture}
863 \tkzGetNodes
864 \tkzInit[xmin = -2,xmax=4,ymin =-1,ymax=6]
865 \tkzDrawX\tkzDrawY
866 \tkzDrawPoints[red,size=4pt](a,b,c)
867 \tkzDrawCoordinates[smooth,purple](PA.curve)
868 \end{tikzpicture}
869 \end{center}
870 \end{minipage}
871
872
873 \begin{verbatim}
874 \begin{tikzpicture}
875 \tkzGetNodes
876 \tkzInit[xmin = -2,xmax=4,ymin =-1,ymax=6]
877 \tkzDrawX\tkzDrawY
878 \tkzDrawPoints[red,size=4pt](a,b,c)
879 \draw[smooth] plot coordinates {%
880 \directlua{tex.print(f(-1,3,100))}};
881 \end{tikzpicture}
882 \end{verbatim}
883
884 \subsection{Example with several paths}
885
886 \begin{tkzexample}[latex=.45\textwidth]
887 \directlua{
888 z.O = point(0, 0)
889 z.A = point(5, 0)
890 C.OA = circle(z.O, z.A)
891 z.S = C.OA.south
892 C.SO = circle(z.S, z.O)
893 z.B, z.C = intersection(C.OA, C.SO)
894 C.BC = circle(z.B, z.C)
895 z.D = intersection(C.OA, C.BC)
896 C.CD = circle(z.C, z.D)
897
898 local p1 = C.SO:path(z.C, z.B, 50)
899 local p2 = C.BC:path(z.C, z.D, 50)
900 local p3 = C.CD:path(z.D, z.B, 50)
901 thepath = (-p1) + p2 + p3 }
902 \begin{tikzpicture}[scale=.5]
903 \tkzGetNodes
904 \tkzDrawCircles(O,A S,O)
905 \tkzDrawArc(B,C)(D)
906 \tkzDrawArc(C,D)(B)
907 \tkzDrawCoordinates[fill = purple!20,
908 opacity=.4](thepath)
909 \tkzDrawCoordinates[smooth,red,thick](thepath)
910 \tkzDrawPoints(A,O,B,C,S,D)
911 \tkzLabelPoints(A,B,C)
912 \end{tikzpicture}
913 \end{tkzexample}
914 \endinput