Debian Codegraph @

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

raw
1 \newpage
2
3 \section{Class \tkzClass{vector}}
4 \label{sec:class_vector}
5
6 A \tkzname{vector} object represents an oriented segment from a tail point $A$ to a head point $B$.
7 Internally, it is built from two points \tkzVar{za} and \tkzVar{zb}, interpreted as complex numbers.
8 The vector stores both its geometric endpoints and its analytic data (components, length, direction).
9
10
11
12 The variable \tkzVar{vector}{V} holds a table used to store vectors. It is optional, and you are free to choose the variable name. However, using \code{V} is a recommended convention for clarity and consistency. If you use a custom variable (e.g., Vectors), you must initialize it manually. The \code{init\_elements()} function reinitializes the \code{V} table if used.
13
14 \vspace{1em}
15 In fact, they are more a class of oriented segments than vectors in the strict mathematical sense.
16
17 A vector is defined by giving two points (i.e. two affixes).
18 |V.AB = vector(z.A, z.B)| creates the vector $(\overrightarrow{AB})$, i.e. the oriented segment with origin $A$ representing a vector. A few rudimentary operations are defined, such as sum, subtraction and multiplication by a scalar.
19
20
21 \subsection{Creating a vector}
22 \label{sub:creating_a_vector}
23
24 The \tkzClass{vector} class represents a vector between two points or a free vector with given coordinates.
25
26 \medskip
27 The result is usually stored in \tkzVar{vector}{V}.
28
29 \medskip
30 \textbf{Short form: }
31 \begin{mybox}
32 \begin{verbatim}
33 V.v1 = vector:new(z.A, z.B) -- from A to B
34 The short form is equivalent:
35 V.v1 = vector(z.A, z.B)
36 \end{verbatim}
37 \end{mybox}
38
39
40
41 \subsection{Attributes of a vector}
42
43 \vspace{1em}
44
45 \bgroup
46 \small
47 \catcode`_=12
48 \captionof{table}{Vector attributes.}\label{vector:attributes}
49 \begin{tabular}{ll}
50 \toprule
51 \texttt{Attributes} & \texttt{Reference}\\
52 \midrule
53 \tkzAttr{vector}{tail} & \\
54 \tkzAttr{vector}{head} & [\ref{ssub:attributes_vector_head}] \\
55 \tkzAttr{vector}{type} & [\ref{ssub:attributes_vector_type}] \\
56 \tkzAttr{vector}{slope} & [\ref{ssub:attributes_vector_slope}] \\
57 \tkzAttr{vector}{z} & [\ref{ssub:attributes_vector_z}] \\
58 \tkzAttr{vector}{dx} & [\ref{ssub:attributes_vector_d}] \\
59 \tkzAttr{vector}{dy} & [\ref{ssub:attributes_vector_d}]\\
60 \tkzAttr{vector}{norm} & [\ref{ssub:attributes_vector_norm}]\\
61 \tkzAttr{vector}{mtx} & [\ref{ssub:attributes_vector_mtx}] \\
62 \bottomrule
63 \end{tabular}
64 \egroup
65
66
67 \subsubsection{Attribute \tkzAttr{vector}{head}}
68 \label{ssub:attributes_vector_head}
69
70 \begin{minipage}{.5\textwidth}
71 \directlua{
72 init_elements()
73 z.O = point(0, 0)
74 z.A = point(0, 1)
75 z.B = point(3, 4)
76 z.C = point(1, 2)
77 z.D = point(2, 1)
78 V.u = vector(z.A, z.B)
79 V.v = vector(z.C, z.D)
80 V.w = V.u + V.v
81 z.E = V.w.head}
82 \begin{tikzpicture}[gridded]
83 \tkzGetNodes
84 \tkzLabelPoints(A,B,C,D,O,E)
85 \tkzDrawSegments[->,red](A,B)
86 \tkzDrawSegments[->,cyan](A,E)
87 \tkzDrawSegments[->,blue](C,D B,E)
88 \tkzLabelSegment(A,B){$\overrightarrow{u}$}
89 \tkzLabelSegment(C,D){$\overrightarrow{v}$}
90 \tkzLabelSegment(A,E){$\overrightarrow{w}$}
91 \end{tikzpicture}
92 \end{minipage}
93 \begin{minipage}{.5\textwidth}
94 \begin{tkzexample}[code only]
95 \directlua{
96 init_elements()
97 z.O = point(0, 0)
98 z.A = point(0, 1)
99 z.B = point(3, 4)
100 z.C = point(1, 2)
101 z.D = point(2, 1)
102 V.u = vector(z.A, z.B)
103 V.v = vector(z.C, z.D)
104 V.w = V.u + V.v
105 z.E = V.w.head}
106 \end{tkzexample}
107 \end{minipage}
108
109
110 \subsubsection{Attributes \tkzAttr{vector}{type}}
111 \label{ssub:attributes_vector_type}
112 With previous data:
113
114 \begin{mybox}
115 \code{|V.u.type = 'vector'|}
116 \end{mybox}
117
118 \subsubsection{Attribute \tkzAttr{vector}{slope}}
119 \label{ssub:attributes_vector_slope}
120 The attribute gives the slope of the line supporting a vector representative.
121
122 \begin{tkzexample}[latex = .5\textwidth]
123 \directlua{
124 z.A = point(-1, 0)
125 z.B = point (3, 3)
126 V.u = vector(z.A, z.B)
127 local d = math.deg(V.u.slope)
128 tex.print(utils.format_number(d,3))}
129 \end{tkzexample}
130
131 \subsubsection{Attributes \tkzAttr{vector}{dx}, \tkzAttr{vector}{dy}}
132 \label{ssub:attributes_vector_d}
133
134 These attributes give the coordinates of the vectors in the reference base.
135
136 \begin{tkzexample}[latex=.5\textwidth]
137 \directlua{
138 z.A = point(0, 1)
139 z.B = point(3, 4)
140 z.C = point(3, 2)
141 z.D = point(4, 1)
142 V.u = vector(z.A, z.B)
143 V.v = vector(z.C, z.D)
144 V.w = V.u + V.v
145 z.E = V.w.head
146 local pc = string.char(37)
147 local format = "("..pc.."g,"..pc.."g)"
148 tex.print("(dx,dy) =
149 ",string.format(format, V.w.dx, V.w.dy))
150 }
151 \begin{center}
152 \begin{tikzpicture}[gridded]
153 \tkzGetNodes
154 \tkzLabelPoints(A,B,C,D,E)
155 \tkzDrawSegments[->,red](A,B C,D)
156 \tkzDrawSegments[->,blue](A,E B,E)
157 \tkzDrawPoints(A,C)
158 \end{tikzpicture}
159 \end{center}
160 \end{tkzexample}
161
162
163 \subsubsection{Attribute \tkzAttr{vector}{norm}}
164 \label{ssub:attributes_vector_norm}
165 This attribute gives the length of the segment between the two ends.
166
167 \begin{tkzexample}[latex = .5\textwidth]
168 \directlua{
169 z.A = point(-1, 0)
170 z.B = point(3, 3)
171 V.u = vector(z.A, z.B)
172 V.d = V.u.norm
173 tex.print("norm = ",V.d)}
174 \end{tkzexample}
175
176 \subsubsection{Attribute \tkzAttr{vector}{mtx}}
177 \label{ssub:attributes_vector_mtx}
178 This involves associating a matrix in the form of a \code{column vector} with the vector under consideration.
179
180 \begin{tkzexample}[latex = .5\textwidth]
181 \directlua{
182 z.O = point(1, 0)
183 z.I = point(2, 1)
184 V.u = vector(z.O, z.I)
185 V.u.mtx:print()
186 V.v = V.u.mtx:transpose()
187 V.v:print()}
188 \end{tkzexample}
189
190 \subsubsection{Attribute \tkzAttr{vector}{z}}
191 \label{ssub:attributes_vector_z}
192 This attribute is very useful for working with the \code{\^{}} and \code{..} metamethods.
193
194 \begin{tkzexample}[latex = .5\textwidth]
195 \directlua{
196 z.A = point(1, 1)
197 z.B = point(2, -1)
198 z.C = point(0, 1)
199 z.D = point(3, 2)
200 V.u = vector(z.A, z.B)
201 V.v = vector(z.C, z.D)
202 n = V.u.z ^ V.v.z
203 m = V.u.z .. V.v.z
204 tex.print("determinant(u,v) = "..tostring(n))
205 tex.print('\\\\')
206 tex.print("dot product(u,v) = "..tostring(m))
207 }
208 \end{tkzexample}
209
210
211 \newpage
212
213 \subsection{Metamethods overview of the class vector}
214
215 \vspace{1em}
216 \bgroup
217 \catcode`_=12
218 \small
219 \captionof{table}{Methods of the class vector.}\label{vector:metamethods}
220 \begin{tabular}{ll}
221 \toprule
222 \texttt{Metamethods} & \texttt{Reference} \\
223 \midrule
224 \tkzMeta{vector}{add(u,v)} & [\ref{ssub:method_vector_add}] \\
225 \tkzMeta{vector}{sub(u,v)} & [\ref{ssub:method_vector_sub}] \\
226 \tkzMeta{vector}{unm(u)} & [\ref{ssub:method_vector_unm}] \\
227 \tkzMeta{vector}{mul(k,u)} & [\ref{ssub:method_vector_mul}] \\
228 \tkzMeta{vector}{pow(k,u)} & [\ref{ssub:method_vector_det}] \\
229 \tkzMeta{vector}{concat(k,u)}& [\ref{ssub:method_vector_dot}] \\
230 \bottomrule
231 \end{tabular}
232 \egroup
233
234
235
236 \subsection{Example of metamethods}
237
238 \subsubsection{Method \tkzMeth{vector}{add}}
239 \label{ssub:method_vector_add}
240
241 The sum is defined as follows:
242
243 Let V.AB + V.CD result in a vector V.AE defined as follows
244
245 If $\overrightarrow{CD} = \overrightarrow{BE} $ then $\overrightarrow{AB} + \overrightarrow{CD} = \overrightarrow{AB} + \overrightarrow{BE} =\overrightarrow{AE}$
246
247 \begin{verbatim}
248 z.A = point(0, 1)
249 z.B = point(3, 4)
250 z.C = point(3, 2)
251 z.D = point(4, 1)
252 V.AB = vector(z.A, z.B)
253 V.CD = vector(z.C, z.D)
254 V.AE = V.AB + V.CD % possible V.AB:add(V.CD)
255 z.E = V.AE.head % we recover the final point (head)
256 \end{verbatim}
257
258
259 \begin{tkzexample}[latex=.5\textwidth]
260 \directlua{
261 init_elements()
262 z.A = point(0, 1)
263 z.B = point(3, 4)
264 z.C = point(3, 2)
265 z.D = point(4, 1)
266 V.u = vector(z.A, z.B)
267 V.v = vector(z.C, z.D)
268 V.w = V.u + V.v % or V.u:add(V.v)
269 z.E = V.w.head}
270 \begin{tikzpicture}[gridded]
271 \tkzGetNodes
272 \tkzLabelPoints(A,B,C,D,E)
273 \tkzDrawSegments[->,red](A,B)
274 \tkzDrawSegments[->,cyan](A,E)
275 \tkzDrawSegments[->,blue](C,D B,E)
276 \tkzLabelSegment(A,B){$\overrightarrow{u}$}
277 \tkzLabelSegment(C,D){$\overrightarrow{v}$}
278 \tkzLabelSegment(A,E){$\overrightarrow{w}$}
279 \end{tikzpicture}
280 \end{tkzexample}
281
282
283 \subsubsection{Method \tkzMeth{vector}{sub}}
284 \label{ssub:method_vector_sub}
285
286 \begin{tkzexample}[latex=.5\textwidth]
287 \directlua{
288 z.A = point(0, 1)
289 z.B = point(3, 4)
290 z.C = point(3, 2)
291 z.D = point(4, 1)
292 V.u = vector(z.A, z.B)
293 V.v = vector(z.C, z.D)
294 V.w = V.u - V.v
295 z.E = V.w.head}
296
297 \begin{tikzpicture}[gridded]
298 \tkzGetNodes
299 \tkzLabelPoints(A,B,C,D,E)
300 \tkzDrawSegments[->,red](A,B)
301 \tkzDrawSegments[->,cyan](A,E)
302 \tkzDrawSegments[->,blue](C,D B,E)
303 \tkzLabelSegment(A,B){$\overrightarrow{u}$}
304 \tkzLabelSegment(C,D){$\overrightarrow{v}$}
305 \tkzLabelSegment(A,E){$\overrightarrow{w}$}
306 \end{tikzpicture}
307 \end{tkzexample}
308
309
310 \subsubsection{Method \tkzMeth{vector}{mul}}
311 \label{ssub:method_vector_mul}
312 This is, of course, multiplication by a scalar.
313
314 \begin{tkzexample}[latex=.5\textwidth]
315 \directlua{
316 init_elements()
317 z.A = point(0, 1)
318 z.B = point(3, 4)
319 z.C = point(3, 2)
320 z.D = point(4, 1)
321 V.u = vector(z.A, z.B)
322 V.v = vector(z.C, z.D)
323 V.w = V.u + 2 * V.v
324 z.E = V.w.head}
325
326 \begin{tikzpicture}[gridded]
327 \tkzGetNodes
328 \tkzLabelPoints(A,B,C,D,E)
329 \tkzDrawSegments[->,red](A,B)
330 \tkzDrawSegments[->,cyan](A,E)
331 \tkzDrawSegments[->,blue](C,D B,E)
332 \tkzLabelSegment(A,B){$\overrightarrow{u}$}
333 \tkzLabelSegment(C,D){$\overrightarrow{v}$}
334 \tkzLabelSegment(A,E){$\overrightarrow{w}$}
335 \end{tikzpicture}
336 \end{tkzexample}
337
338
339 \subsubsection{Method \tkzMeth{vector}{unm}}
340 \label{ssub:method_vector_unm}
341
342 Cette méthode vous permet d'écrire \code{V.w = -V.v}
343 \begin{tkzexample}[latex=.5\textwidth]
344 \directlua{
345 z.A = point(0, 1)
346 z.B = point(3, 3)
347 z.C = point(3, 2)
348 z.D = point(4, 1)
349 V.u = vector(z.A, z.B)
350 V.v = vector(z.C, z.D)
351 V.v = -V.v
352 V.w = V.u + V.v
353 z.E = V.w.head}
354 \begin{center}
355 \begin{tikzpicture}[gridded]
356 \tkzGetNodes
357 \tkzLabelPoints(A,B,C,D,E)
358 \tkzDrawSegments[->,red](A,B)
359 \tkzDrawSegments[->,cyan](A,E)
360 \tkzDrawSegments[->,blue](D,C B,E)
361 \end{tikzpicture}
362 \end{center}
363 \end{tkzexample}
364
365 \subsubsection{Method \tkzMeth{vector}{\textasciicircum{}}}
366 \label{ssub:method_vector_det}
367 Instead of the power, which wouldn't make sense here, we're talking about the determinant of two vectors. Note: the reference frame used is orthonormal and direct.
368
369 \begin{mybox}
370 |V.u = vector(z.A, z.B) with z.A = point(xa, ya) and z.B = point(xb, yb)|
371 |V.v = vector(z.C, z.D) with z.C = point(xc, yc) and z.D = point(xd, yd)|
372 then |V.u ^ V.v = xa * yb - xb * ya|
373 \end{mybox}
374
375 remark: |u ^ v = u.norm * v.norm * sin(u,v)|
376
377 \subsubsection{Method \tkzMeth{vector}{..}}
378 \label{ssub:method_vector_dot}
379 Instead of the concatenation, which wouldn't make sense here, we're talking about the dot product of two vectors. Note: the reference frame used is orthonormal and direct.
380
381 \begin{mybox}
382 |V.u = vector(z.A, z.B) with z.A = point(xa, ya) and z.B = point(xb, yb)|
383 |V.v = vector(z.C, z.D) with z.C = point(xc, yc) and z.D = point(xd, yd)|
384 then |V.u .. V.v = xa * xb + ya * yb|
385 \end{mybox}
386
387 remark: |V.u .. V.v = V.u.norm * V.v.norm * cos(V.u,V.v)|
388
389
390 We're going to use the two last methods . We can determine the cosine and sine of the angle using the dot product and determinant expressed in the direct orthonormal frame used by the package.
391
392 \begin{minipage}{.5\textwidth}
393 \directlua{
394 z.O = point(0, 0)
395 z.A = point(5, 0)
396 z.B = point(1, -4)
397 V.u = vector(z.O, z.A)
398 V.v = vector(z.O, z.B)
399 local dp = V.u .. V.v % dot product
400 local d = V.u ^ V.v % determinant
401 % costheta = dp / (u.norm * v.norm)
402 % sintheta = d / (u.norm * v.norm)
403 an = math.atan(d / dp)}
404 \begin{tikzpicture}
405 \tkzGetNodes
406 \tkzDrawSegments[>=stealth,->](O,A O,B)
407 \tkzDrawPoints(O)
408 \tkzLabelPoints(A,B)
409 \tkzLabelPoints[left](O)
410 \tkzMarkAngle[<-,>=stealth](B,O,A)
411 \tkzLabelAngle[pos=2](B,O,A){$\directlua{
412 local format = string.char(37) .. ".2f"
413 tex.print(string.format( format , math.deg(an)))}$ deg}
414 \end{tikzpicture}
415 \end{minipage}
416 \begin{minipage}{.5\textwidth}
417 \begin{tkzexample}[code only]
418 \directlua{
419 z.O = point(0, 0)
420 z.A = point(5, 0)
421 z.B = point(1, -4)
422 V.u = vector(z.O, z.A)
423 V.v = vector(z.O, z.B)
424 local dp = V.u .. V.v % dot product
425 local d = V.u ^ V.v % determinant
426 % costheta = dp / (u.norm * v.norm)
427 % sintheta = d / (u.norm * v.norm)
428 an = math.atan(d / dp)}
429 \end{tkzexample}
430 \end{minipage}
431
432 The code required to display the angle measurement is as follows:
433
434 \begin{tkzexample}[code only]
435 \tkzLabelAngle[pos=2](B,O,A){$\directlua{
436 local format = string.char(37) .. ".2f"
437 tex.print(string.format( format , math.deg(an)))}$ deg}
438 \end{tkzexample}
439
440 % subsubsection method_vector_dot (end)`
441
442
443
444 \smallskip
445 The main methods provided by the \tkzClass{vector} class are summarised below.
446
447 \medskip
448 \small
449 \captionof{table}{Methods of the class vector.}\label{vector:methods}
450 \begin{tabular}{ll}
451 \toprule
452 \texttt{Methods} & \texttt{Reference} \\
453 \midrule
454 \texttt{Constructor} & \\
455 \midrule
456 \tkzMeth{vector}{new(pt, pt)} & \ref{sub:creating_a_vector} \\
457 \midrule
458 \texttt{Methods Returning a Boolean} & \\
459 \midrule
460 \tkzMeth{vector}{is\_zero([EPS])} & \\
461 \tkzMeth{vector}{is\_parallel(v,[EPS])} & \\
462 \tkzMeth{vector}{is\_orthogonal(v,[EPS])} & \\
463 \midrule
464 \texttt{Methods Returning a Point} & \\
465 \midrule
466 \tkzMeth{vector}{get()} & \\
467 \midrule
468 \texttt{Methods Returning a Vector} & \\
469 \tkzMeth{vector}{add(v)} & \\
470 \tkzMeth{vector}{scale(d)} & \\
471 \tkzMeth{vector}{dot(v)} &\\
472 \tkzMeth{vector}{cross(v)} &\\
473 \tkzMeth{vector}{normalize()} & [\ref{ssub:method_vector_normalize}] \\
474 \tkzMeth{vector}{angle\_to(v)} & \\
475 \tkzMeth{vector}{at (pt)} & [\ref{ssub:method_vector_at}] \\
476 \tkzMeth{vector}{rotate(\tkzVar{$\theta$})} & \\
477 \tkzMeth{vector}{orthogonal([side],[length])} & \\
478 \bottomrule
479 \end{tabular}
480
481
482
483 \subsection{Returns a boolean}
484
485 \subsubsection{Predicates: \tkzMeth{vector}{is\_zero}, \tkzMeth{vector}{is\_parallel}, \tkzMeth{vector}{is\_orthogonal}}
486
487 \begin{verbatim}
488 if V:is_zero() then ... end
489 if V:is_parallel(W) then ... end
490 if V:is_orthogonal(W) then ... end
491 \end{verbatim}
492
493 These methods test basic geometric relations between vectors, using an optional
494 tolerance \tkzVar{EPS}, which defaults to \tkzVar{tkz.epsilon}. They are convenient
495 when deciding whether two directions should be treated as parallel or orthogonal
496 in numerical computations.
497
498 \subsection{Returns a vector}
499
500
501 \subsubsection{Method \tkzMeth{vector}{normalize()}}
502 \label{ssub:method_vector_normalize}
503 This method produces a normalized vector that is collinear with the initial vector.
504
505 \begin{tkzexample}[latex = .5\textwidth]
506 \directlua{
507 init_elements()
508 z.A = point(0, 1)
509 z.B = point(3, 4)
510 V.AB = vector(z.A, z.B)
511 V.AN = V.AB:normalize()
512 z.N = V.AN.head}
513 \begin{tikzpicture}
514 \tkzGetNodes
515 \tkzDrawSegments(A,B)
516 \tkzDrawPoints(A,B,N)
517 \tkzLabelPoints(A,B,N)
518 \end{tikzpicture}
519 \end{tkzexample}
520
521 \subsubsection{Method \tkzMeth{vector}{at()}}
522 \label{ssub:method_vector_at}
523
524 \begin{tkzexample}[latex = .5\textwidth]
525 \directlua{
526 init_elements()
527 z.A = point(0, 1)
528 z.B = point(3, 4)
529 z.O = point(0, 0)
530 V.AB = vector(z.A, z.B)
531 V.OC = V.AB:at(z.O)
532 z.C = V.OC.head}
533 \begin{tikzpicture}
534 \tkzGetNodes
535 \tkzDrawSegments(A,B O,C)
536 \tkzDrawPoints(A,B,O,C)
537 \tkzLabelPoints(A,B,O,C)
538 \end{tikzpicture}
539 \end{tkzexample}
540
541 \subsubsection{Method \tkzMeth{vector}{orthogonal([side],[length])}}
542 \label{ssub:method_vector_orthogonal_r}
543
544 \begin{verbatim}
545 Vperp = V:orthogonal() -- default: "ccw"
546 VperpC = V:orthogonal("cw") -- clockwise orthogonal
547 Vlen = V:orthogonal("ccw", 2.0) -- orthogonal of prescribed length
548 \end{verbatim}
549
550 The \tkzMeth{vector}{orthogonal} method constructs an orthogonal vector to \code{V},
551 with the same tail:
552 \begin{itemize}
553 \item the optional argument \tkzVar{side} may be \code{"ccw"} (counter-clockwise,
554 the default) or \code{"cw"} (clockwise),
555 \item the optional argument \tkzVar{length} may be used to prescribe the norm
556 of the resulting vector; if omitted, the length is determined by a rotation
557 of the current vector.
558 \end{itemize}
559
560
561 \begin{tkzexample}[latex=.5\textwidth]
562 \directlua{
563 init_elements()
564 z.A = point(0, 1)
565 z.B = point(3, 4)
566 V.AB = vector(z.A, z.B)
567 V.AR = V.AB:orthogonal(2 * math.sqrt(2))
568 z.R = V.AR.head
569 V.AS = V.AB:orthogonal("cw",2)
570 z.S = V.AS.head}
571 \begin{center}
572 \begin{tikzpicture}[gridded]
573 \tkzGetNodes
574 \tkzDrawSegments[>=stealth,->,
575 red](A,B A,R A,S)
576 \tkzLabelPoints(A,B,R,S)
577 \end{tikzpicture}
578 \end{center}
579 \end{tkzexample}
580
581
582
583 \endinput