Debian Codegraph @

texmf-dist/tex/latex/tkz-elements/tkz_elements_vector.lua

raw
1 -- File: tkz_elements_vector.lua
2 -- Copyright (c) 2026 Alain Matthes
3 -- SPDX-License-Identifier: LPPL-1.3c
4 -- Maintainer: Alain Matthes
5
6 -- ----------------------------------------------------------------------------
7 vector = {}
8 vector.__index = vector
9 -- Constructor for creating a new vector
10 function vector:new(za, zb)
11 local type = "vector"
12 local mtx = matrix:new({ { za }, { zb } })
13 local z = zb - za
14 local dx = z.re
15 local dy = z.im
16 local norm = point.mod(z)
17 local slope = 0
18 if norm > (tkz.epsilon or 0) then
19 slope = angle_normalize_(point.arg(z))
20 end
21 local vect = {
22 type = type,
23 tail = za,
24 head = zb,
25 z = z,
26 dx = dx,
27 dy = dy,
28 norm = norm,
29 slope = slope,
30 mtx = mtx,
31 }
32
33 setmetatable(vect, self)
34 return vect
35 end
36
37 setmetatable(vector, {
38 __call = function(cls, ...)
39 return cls:new(...)
40 end,
41 })
42
43 function vector.__add(v1, v2)
44 return v1:add(v2)
45 end
46
47 function vector.__sub(v1, v2)
48 return v1:add(v2:scale(-1))
49 end
50
51 function vector.__unm(v)
52 return v:scale(-1)
53 end
54
55 -- function vector.__mul(r, v)
56 -- return v:scale(r)
57 -- end
58
59 function vector.__mul(a, b)
60 local ta, tb = type(a), type(b)
61
62 if ta == "number" and getmetatable(b) == vector then
63 return b:scale(a)
64 elseif tb == "number" and getmetatable(a) == vector then
65 return a:scale(b)
66 else
67 error("vector.__mul: expected scalar * vector or vector * scalar")
68 end
69 end
70
71
72 function vector.__pow(v1, v2)
73 local z
74 z = point.conj(v1.head - v1.tail) * (v2.head - v2.tail)
75 return z.im
76 end
77
78 function vector.__concat(v1, v2)
79 local z
80 z = (v1.head - v1.tail) * point.conj((v2.head - v2.tail))
81 return z.re
82 end
83
84 function vector:get()
85 return self.tail, self.head
86 end
87
88 function vector:is_zero(EPS)
89 EPS = EPS or tkz.epsilon
90 return self.norm <= EPS
91 end
92
93 function vector:is_parallel(v, EPS)
94 EPS = EPS or tkz.epsilon
95 if self:is_zero(EPS) or v:is_zero(EPS) then
96 return false
97 end
98 return math.abs(self:cross(v)) <= EPS * self.norm * v.norm
99 end
100
101 function vector:is_orthogonal(v, EPS)
102 EPS = EPS or tkz.epsilon
103 if self:is_zero(EPS) or v:is_zero(EPS) then
104 return false
105 end
106 return math.abs(self:dot(v)) <= EPS * self.norm * v.norm
107 end
108
109
110 -- Normalize the vector (unit vector)
111 -- function vector:normalize()
112 -- local z = self.head - self.tail
113 -- local d = point.abs(z)
114 -- local nz = point(z.re / d, z.im / d)
115 -- return vector:new(self.tail, nz + self.tail)
116 -- end
117
118 function vector:dot(v)
119 local z = point.conj(self.z) * v.z
120 return z.re
121 end
122
123 function vector:cross(v)
124 local z = point.conj(self.z) * v.z
125 return z.im
126 end
127
128
129
130 function vector:normalize()
131 local EPS = tkz.epsilon
132 if self.norm <= EPS then
133 tex.error("Cannot normalize a near-zero vector.")
134 return self
135 end
136 local u = point(self.z.re / self.norm, self.z.im / self.norm)
137 return vector:new(self.tail, self.tail + u)
138 end
139
140
141 function vector:add(ve)
142 return vector:new(self.tail, self.head + ve.head - ve.tail)
143 end
144
145
146
147 -- --Scale the vector by a scalar
148 -- function vector:scale(d)
149 -- local z = scale_(self, d)
150 -- return vector:new(self.tail, z)
151 -- end
152
153 function vector:scale(d)
154 local new_head = scaled_head_(self, d)
155 return vector:new(self.tail, new_head)
156 end
157
158
159 function vector:at(zc)
160 return vector:new(zc, zc + self.head - self.tail)
161 end
162
163 function vector:angle_to(v)
164 -- angle entre self et v (orienté)
165 return angle_normalize_(v.slope - self.slope)
166 end
167
168 function vector:rotate(theta)
169 return vector:new(self.tail, rotation_(self.tail, theta, self.head))
170 end
171
172 -- side = "ccw" (par défaut) ou "cw"
173 -- length optionnel pour imposer la norme du vecteur orthogonal
174 function vector:orthogonal(side, length)
175 local theta = (side == "cw") and -math.pi / 2 or math.pi / 2
176
177 if length then
178 -- on part du vecteur normalisé, on impose sa norme, puis on tourne
179 local u = self:normalize()
180 local tip = self.tail + point(length * math.cos(u.slope), length * math.sin(u.slope))
181 return vector:new(self.tail, rotation_(self.tail, theta, tip))
182 else
183 return self:rotate(theta)
184 end
185 end
186
187 return vector