Debian Codegraph @

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

raw
1 \newpage
2
3 \section{Class and Object}
4 \label{sec:class_and_object}
5
6 \subsection{Class}
7
8 Object-Oriented Programming (OOP) is a programming paradigm based on the concept of objects. An object is a data structure that contains both attributes (data) and methods (operations), which together define its behavior.
9
10 \vspace{1em}
11 A class is a user-defined data type that serves as a blueprint for creating objects. It specifies the structure and shared behavior of a category of objects, including default values for attributes and common implementations of methods\footnote{An action that an object can perform.}.
12
13 \subsection{Object}
14 An object is an instance of a class. Each object encapsulates attributes and methods. Attributes store information or properties specific to the object (typically as fields in a data table), while methods define how the object behaves or interacts with other objects.
15
16
17 \vspace{1em}
18 All objects in the package are typed. The currently defined and used types are: \tkzNameObj{point}, \tkzNameObj{line}, \tkzNameObj{circle}, \tkzNameObj{triangle}, \tkzNameObj{conic}, \tkzNameObj{quadrilateral}, \tkzNameObj{square}, \tkzNameObj{rectangle}, \tkzNameObj{parallelogram}, \tkzNameObj{regular\_polygon}, \tkzNameObj{occs} and \tkzNameObj{path}.
19
20
21 \subsubsection{Creating an object}
22 Objects are generally created using the method \tkzMeth{obj}{new}, by providing points as arguments.
23
24 \begin{itemize}
25 \item The \tkzClass{point} class requires two real numbers (coordinates),
26 \item The \tkzClass{regular\_polygon} class requires two points and an integer (the number of sides),
27 \item The \tkzClass{occs} class requires a line and a point,
28 \item The \tkzClass{path} class requires a table of points written as strings.
29 \end{itemize}
30
31 Each object is usually assigned a name and stored in a table according to its type. For example:
32 \begin{itemize}
33 \item points are stored in the global table \code{z},
34 \item lines in \code{L}, circles in \code{C}, triangles in \code{T}, and so on.
35 \end{itemize}
36
37 This convention allows easy access and reusability across computations and drawings. For example:
38
39 \begin{mybox}
40 \begin{verbatim}
41 z.A = point(1, 2)
42 z.B = point(4, 5)
43 L.AB = line(z.A, z.B)
44 \end{verbatim}
45 \end{mybox}
46
47
48 Here, \code{z.A} and \code{z.B} store points in table \code{z}, while the line defined by these points is stored as \code{L.AB} in table \code{L}.
49
50 \vspace{1em}
51 \tkzRHand{} \textbf{Note:} \\
52 From version 4 onwards, object creation has been streamlined. Instead of calling \code{object:new(arguments)}, you can simply use \code{object(arguments)} — the shorter form is equivalent.
53
54 \begin{mybox}
55 \begin{verbatim}
56 z.A = point(1, 2) -- short form
57 -- equivalent to:
58 z.A = point:new(1, 2)
59 \end{verbatim}
60 \end{mybox}
61
62 Objects can also be generated by applying methods to existing objects. For instance, \code{T.ABC:circum\_circle()} produces a new \tkzClass{circle} object. Some object attributes are themselves objects: \code{T.ABC.bc} returns a \tkzClass{line} representing side BC of triangle ABC.
63
64 \vspace{1em}
65 \tkzRHand{} \textbf{Important:} \\
66 All these named objects are stored in global tables. To avoid conflicts or residual data between figures, it is strongly recommended to call the function \tkzFct{init\_elements()} at the beginning of each construction. This resets the environment and ensures a clean setup. See the next section
67
68 \subsubsection{Initialization: \tkzFct{tkz-elements}{init\_elements}}
69
70 Before performing geometric constructions or calculations, it is important to initialize the system. The function \tkzFct{tkz-elements}{init\_elements()} resets internal tables and parameters to prepare for a new figure. This step ensures a clean environment and avoids interference from previously defined objects.
71
72 \begin{mybox}
73 \begin{verbatim}
74 init_elements()
75 \end{verbatim}
76 \end{mybox}
77
78 \textbf{Purpose.}
79 The function \code{init\_elements} clears global tables such as:
80 \begin{itemize}
81 \item \tkzname{z} — for storing points,
82 \item \tkzname{L}, \tkzname{C}, \tkzname{T}, etc. — for lines, circles, triangles,
83 \item and other geometric structures.
84 \end{itemize}
85 It also (re)sets default values for internal constants such as the number of decimal digits and the floating-point tolerance.
86
87 \medskip
88 \texttt{When to use it:}
89
90 This function should be called:
91 \begin{itemize}
92 \item at the beginning of each new TikZ figure using Lua,
93 \item or any time you need to reset the environment manually.
94 \end{itemize}
95
96 \medskip
97 \texttt{Example usage.}
98
99 Here is a typical usage:
100
101 \begin{verbatim}
102 \directlua{
103 init_elements()
104 z.A = point(0, 0)
105 z.B = point(3, 4)
106 L.AB = line(z.A, z.B)}
107 \end{verbatim}
108
109
110 \medskip
111 \texttt{Note.}
112
113 Calling \code{init\_elements} is optional if you manage object names carefully, but it is highly recommended in iterative workflows or automated figure generation to avoid unwanted data persistence.
114
115 \subsubsection{Attributes}
116 \label{ssub:attributes}
117
118 Attributes are accessed using the standard method. For example, |T.pc| retrieves the third point of the triangle, and |C.OH.center| retrieves the center of the circle. Additionally, I have added a method \tkzMeth{obj}{get()} that returns the points of an object. This method applies to straight lines (pa and pc), triangles (pa, pb, and pc), and circles (center and through).
119
120 \vspace{1em}
121
122 \texttt{Example usage: }: |z.O, z.T = C.OT:get()| retrieves the center and a point of the circle.
123
124 \subsubsection{Methods}
125 \label{ssub:methods}
126
127 A method is an operation (function or procedure) associated (linked) with an object.
128
129 Example: The point object is used to vertically determine a new point object located at a certain distance from it (here 2). Then it is possible to rotate objects around it.
130
131 \begin{mbox}{}
132 \begin{verbatim}
133 \directlua{
134 init_elements()
135 z.A = point(1, 0)
136 z.B = z.A:north(2)
137 z.C = z.A:rotation (math.pi / 3, z.B)
138 tex.print(tostring(z.C))
139 }
140 \end{verbatim}
141 \end{mbox}
142
143 \directlua{
144 init_elements()
145 z.A = point(1, 0)
146 z.B = z.A:north(2)
147 z.C = z.A:rotation(math.pi / 3, z.B)
148 tex.print(tostring("The coordinates of $C$ are: " .. z.C.re .." and "..z.C.im))
149 }
150 \endinput