Debian Codegraph @

texmf-dist/doc/latex/tkz-elements/TKZdoc-elements-why.tex

raw
1 \newpage
2 \section{Why tkz-elements?}
3
4 The \tkzNamePack{tkz-elements} package was created to address two key challenges in geometric work with TeX. First, TeX alone lacks the numerical precision and flexibility required for complex geometric calculations. By leveraging \tkzname{Lua}, \tkzNamePack{tkz-elements} provides accurate and stable computations for operations such as intersections, projections, and transformations.
5
6 Second, the package introduces an object-oriented approach to geometry. Geometric entities—such as points, vectors, lines, circles, and conics—are modeled as structured objects with associated methods. This allows users to express geometric relationships in a more natural and readable way, similar to mathematical language.
7
8 Beyond these two core ideas, \tkzNamePack{tkz-elements} offers several additional benefits:
9
10 \begin{itemize}
11
12 \item Simplified computations: \tkzname{Lua}'s floating-point arithmetic and expressive syntax make geometric calculations much easier to write and understand than in pure TeX.
13
14 \item Extensibility: Thanks to its modular design and use of classes, new geometric objects and methods can be added with minimal effort, making the package scalable and adaptable.
15
16 \item Interoperability: Calculated points and data integrate smoothly with \TIKZ{} or other TeX drawing tools, allowing users to combine computation and visualization effectively.
17
18 \item Pedagogical clarity: With its structured and expressive syntax, \tkzNamePack{tkz-elements} is well-suited for educational purposes, where clarity of construction and notation is essential.
19 \end{itemize}
20
21 Altogether, \tkzNamePack{tkz-elements} provides a modern, object-oriented, and Lua-powered framework for geometric constructions within the TeX ecosystem.
22
23 \subsection{Calculation accuracy}
24 \subsubsection{Calculation accuracy in \TIKZ}
25 With \TIKZ, the expression \tkzimp{|veclen(x,y)|} calculates the expression $\sqrt{x^2+y^2}$.
26 This calculation is achieved through a polynomial approximation, drawing inspiration from the ideas of \tkzimp{Rouben Rostamian}.
27
28 \pgfkeys{/pgf/number format/.cd,std,precision=5} \pgfmathparse{veclen(65,72)}
29 \begin{mybox}{}
30 \begin{verbatim}
31 pgfmathparse{veclen(65,72)} \pgfmathresult
32 \end{verbatim}
33 \end{mybox}
34
35 \tkzHand $\sqrt{65^2+72^2} \approx \pmpn{\pgfmathresult} $ .
36
37 \subsubsection{Calculation accuracy in Lua}
38 A |luaveclen| macro can be defined as follows:
39
40 \begin{mybox}{}
41 \begin{verbatim}
42 \def\luaveclen#1#2{\directlua{tex.print(string.format(
43 '\percentchar.5f',math.sqrt((#1)*(#1)+(#2)*(#2))))}}
44 \end{verbatim}
45 \end{mybox}
46
47 and
48
49 \begin{mybox}
50 \begin{verbatim}
51 \luaveclen{65}{72}
52 \end{verbatim}
53 \end{mybox}
54
55 gives: \tkzHand $\sqrt{65^2+72^2} = \pmpn{\luaveclen{65}{72}} $ {\color{red}!!}
56
57 \medskip
58 The error, though insignificant when it comes to the placement of an object on a page by a hundredth of a point, becomes problematic for the results of mathematical demonstrations. Moreover, these inaccuracies can accumulate and lead to erroneous constructions.
59
60 \vspace{.5em}
61 To address this lack of precision, I initially introduced the \tkzNamePack{fp}, followed by the package \tkzNamePack{xfp}. More recently, with the emergence of Lua\LATEX{}, I incorporated a \tkzname{Lua} option aimed at performing calculations with \tkzname{Lua}.
62
63 This was the primary motivation behind creating the package, with the secondary goal being the introduction of object-oriented programming (OOP) and simplifying programming with Lua. The concept of OOP persuaded me to explore its various possibilities further.
64
65 At that time, I had received some Lua programming examples from {\tkzimpbf{Nicolas Kisselhoff}}, but I struggled to understand the code initially, so I dedicated time to studying Lua patiently. Eventually, I was able to develop \tkzname{\tkznameofpack}, incorporating many of his ideas that I adapted for the package.
66
67 \subsubsection{Using objects}
68 Subsequently, I came across an article by \tkzimp{Roberto Giacomelli}\footnote{\href{https://www.guitex.org/home/images/meeting2012/slides/presentazione_giacomell_guitmeeting_2012.pdf}{Grafica ad oggetti con LuaTEX}} on object-oriented programming using \tkzname{Lua} and \tkzNamePack{\TIKZ} tools. This served as my second source of inspiration. Not only did this approach enable programming to be executed step-by-step, but the introduction of objects facilitated a direct link between the code and geometry. As a result, the code became more readable, explicit, and better structured.
69
70
71 \subsubsection{Example: Apollonius circle (new version 2025/05/12)}
72 \label{ssub:example_apollonius_circle}
73
74 \begin{mybox}{Problem:}
75 The objective is to identify an inner tangent circle to the three exinscribed circles of a triangle.\end{mybox}
76
77
78 For additional information, you can consult the corresponding entry on \href{https://mathworld.wolfram.com/ApolloniusCircle.html}{MathWorld}.
79 This example was used as a reference to test the \tkzNamePack{tkz-euclide} package. Initially, the results obtained with basic methods and available tools lacked precision. Thanks to \tkzNamePack{tkz-elements}, more powerful and accurate tools are now available — and they are also easier to use.
80 The core principles of figure construction with \tkzNamePack{tkz-euclide} remain unchanged: definitions, calculations, drawings, and labels, all following a step-by-step process that mirrors classical compass-and-straightedge constructions.
81 This version takes advantage of the simplest construction method enabled by \tkzname{Lua}.
82
83
84 \begin{mybox}
85 \begin{verbatim}
86 \directlua{
87 init_elements()
88 z.A = point(0, 0)
89 z.B = point(6, 0)
90 z.C = point(0.8, 4)
91 T.ABC = triangle(z.A, z.B, z.C)
92 z.N = T.ABC.eulercenter
93 z.S = T.ABC.spiekercenter
94 T.feuerbach = T.ABC:feuerbach()
95 z.Ea, z.Eb, z.Ec = T.feuerbach:get()
96 T.excentral = T.ABC:excentral()
97 z.Ja, z.Jb, z.Jc = T.excentral:get()
98 C.JaEa = circle(z.Ja, z.Ea)
99 % C.ortho = circle:radius(z.S, math.sqrt(C.JaEa:power(z.S)))
100 C.ortho = C.JaEa:orthogonal_from(z.S) % 2025/05/12
101 z.a = C.ortho.through
102 C.euler = T.ABC:euler_circle()
103 C.apo = C.ortho:inversion(C.euler)
104 z.O = C.apo.center
105 z.xa, z.xb, z.xc = C.ortho:inversion(z.Ea, z.Eb, z.Ec)}
106 \end{verbatim}
107 \end{mybox}
108
109 Creating an object involves defining its attributes and methods — that is, its properties and the actions it can perform. Once created, the object is associated with a name (or reference) by storing it in a table. This table acts as an associative array, linking a \code{key} (the reference) to a \code{value} (the object itself). These concepts will be explored in more detail later.
110
111 For instance, suppose \code{T} is a table that associates the triangle object with the key \code{ABC}. Then \code{T.ABC} refers to another table containing the attributes of the triangle — such as its vertices, sides, or angles — each accessible via a specific key. These attributes are predefined within the package to support geometric operations.
112
113 \vspace{1em}
114 \begin{mybox}
115 \begin{verbatim}
116 z.N = T.ABC.eulercenter
117 \end{verbatim}
118 \end{mybox}
119
120 \code{N} is the name of the point, \code{eulercenter} is an attribute of the triangle.
121 \footnote{ The center of the Euler circle, or center of the nine-point circle, is a characteristic of every triangle.}
122
123 \begin{mybox}
124 \begin{verbatim}
125 T.excentral = T.ABC:excentral()
126 \end{verbatim}
127 \end{mybox}
128
129 In this context, \tkzname{excentral} is a method associated with the \tkzname{T.ABC }object. It defines the triangle formed by the centers of the exinscribed circles.
130
131
132 \begin{tcolorbox}[
133 title=Deprecated Code,
134 colframe=red!50,
135 colback=red!5,
136 coltitle=black,
137 sharp corners,
138 fonttitle=\bfseries,
139 leftrule=1mm,
140 rightrule=1mm,
141 toprule=0.5mm,
142 bottomrule=0.5mm
143 ]
144 \texttt{Note: } This code has been replaced by a more elegant version.
145
146 Two lines were particularly noteworthy. The first demonstrates how the exceptional precision of \tkzname{Lua} allows a radius to be defined through a complex computation.
147
148 The radius of the radical circle is given by:
149 \[
150 \sqrt{\Pi(S,\mathcal{C}(Ja,Ea))}
151 \]
152 (the square root of the power of the point $S$ with respect to the exinscribed circle centered at |Ja| and passing through |Ea|).
153
154 \begin{mybox}{}
155 \begin{verbatim}
156 C.ortho = circle: radius (z.S,math.sqrt(C.JaEa: power(z.S)))
157 \end{verbatim}
158 \end{mybox}
159 \end{tcolorbox}
160
161 \begin{tcolorbox}[
162 title=Revised Code,
163 colframe=cyan!60!black,
164 colback=cyan!5,
165 coltitle=white,
166 sharp corners,
167 fonttitle=\bfseries,
168 leftrule=1mm,
169 rightrule=1mm,
170 toprule=0.5mm,
171 bottomrule=0.5mm
172 ]
173 \texttt{Note: } The following code replaces the previous version with a more elegant and object-oriented approach.
174 The previous code demonstrated the kinds of calculations that can be performed manually. However, \tkzNamePack{tkz-elements} offers a wide range of methods associated with the various geometric objects. Among the methods related to circles is one that allows you to define a circle orthogonal to another, given a center point.
175
176 We want to obtain the circle orthogonal to the three exinscribed circles of the triangle. Its center ($S$) is the radical center of these three circles. It suffices to compute a circle orthogonal to one of them \footnote{Given a main circle and two secondary circles, a specific method exists for defining the radical circle orthogonal to all three.}, taking as center the point $S$.
177
178 The second important line performs an inversion with respect to this orthogonal circle.
179
180 \begin{mybox}{}
181 \begin{verbatim}
182 C.ortho = C.JaEa:orthogonal_from(z.S)
183 \end{verbatim}
184 \end{mybox}
185 \end{tcolorbox}
186
187 \vspace{1em}
188
189 Lastly, it's worth noting that the inversion of the Euler circle with respect to the radical circle yields the Apollonius circle.\footnote{The nine-point circle, also known as Euler's circle, is externally tangent to the three exinscribed circles. The points of tangency form the Feuerbach triangle.}
190
191 This transformation requires an object as a parameter. The method automatically detects the object type (as all objects in the package are typed) and selects the appropriate algorithm accordingly.
192
193 \begin{mybox}{}
194 \begin{verbatim}
195 C.apo = C.ortho:inversion(C.euler)
196 \end{verbatim}
197 \end{mybox}
198
199 Now that all relevant points have been defined, it is time to begin drawing the geometric paths. To do this, the corresponding nodes must first be created. This is precisely the role of the macro \tkzMacro{tkz-elements}{tkzGetNodes}.
200
201 The subsequent section exclusively deals with drawings, and is managed by \tkzNamePack{tkz-euclide}.
202
203 \begin{verbatim}
204 \begin{tikzpicture}
205 \tkzGetNodes
206 \tkzFillCircles[green!30](O,xa)
207 \tkzFillCircles[teal!30](Ja,Ea Jb,Eb Jc,Ec)
208 \tkzFillCircles[lightgray](S,a)
209 \tkzFillCircles[green!30](N,Ea)
210 \tkzDrawPoints(xa,xb,xc)
211 \tkzDrawCircles(Ja,Ea Jb,Eb Jc,Ec S,a O,xa N,Ea)
212 \tkzClipCircle(O,xa)
213 \tkzDrawLines[add=3 and 3](A,B A,C B,C)
214 \tkzDrawPoints(O,A,B,C,S,Ea,Eb,Ec,N)
215 \tkzDrawSegments[dashed](S,xa S,xb S,xc)
216 \tkzLabelPoints(O,N,A,B)
217 \tkzLabelPoints[right](S,C)
218 \end{tikzpicture}
219 \end{verbatim}
220
221 \vspace{1em}
222 \directlua{
223 init_elements()
224 z.A = point(0, 0)
225 z.B = point(6, 0)
226 z.C = point(0.8, 4)
227 T.ABC = triangle(z.A, z.B, z.C)
228 z.N = T.ABC.eulercenter
229 z.S = T.ABC.spiekercenter
230 T.feuerbach = T.ABC:feuerbach()
231 z.Ea, z.Eb, z.Ec = T.feuerbach:get()
232 T.excentral = T.ABC:excentral()
233 z.Ja, z.Jb, z.Jc = T.excentral:get()
234 C.JaEa = circle(z.Ja, z.Ea)
235 C.ortho = C.JaEa:orthogonal_from(z.S)
236 z.a = C.ortho.through
237 C.euler = T.ABC:euler_circle()
238 C.apo = C.ortho:inversion(C.euler)
239 z.O = C.apo.center
240 z.xa, z.xb, z.xc = C.ortho:inversion(z.Ea, z.Eb, z.Ec)
241 }
242 \begin{minipage}{\textwidth}
243
244 \vspace{1em}
245 \begin{center}
246 \begin{tikzpicture}[ scale = .6]
247 \tkzGetNodes
248 \tkzFillCircles[purple!50](O,xa)
249 \tkzFillCircles[orange!50](Ja,Ea Jb,Eb Jc,Ec)
250 \tkzFillCircles[yellow](S,a)
251 \tkzFillCircles[purple!30](N,Ea)
252 \tkzDrawPoints(xa,xb,xc)
253 \tkzDrawCircles(Ja,Ea Jb,Eb Jc,Ec S,a O,xa N,Ea)
254 \tkzClipCircle(O,xa)
255 \tkzDrawLines[add=3 and 3](A,B A,C B,C)
256 \tkzDrawPoints[red](O,A,B,C,S,Ea,Eb,Ec,N)
257 \tkzDrawSegments[dashed](S,xa S,xb S,xc)
258 \tkzLabelPoints(O,N,A,B)
259 \tkzLabelPoints[right](S,C)
260 \end{tikzpicture}
261 \end{center}
262 \end{minipage}
263
264
265 \endinput