Debian Codegraph @

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

raw
1 -- File: tkz_elements_point.lua
2 -- Copyright (c) 2026 Alain Matthes
3 -- SPDX-License-Identifier: LPPL-1.3c
4 -- Maintainer: Alain Matthes
5
6 -- point.lua
7 -- Définition locale de class
8
9 local function class(init, base)
10 local cls = {}
11 cls.__index = cls
12
13 if base then
14 setmetatable(cls, { __index = base })
15 cls._base = base
16 end
17
18 setmetatable(cls, {
19 __call = function(class_tbl, ...)
20 local self = setmetatable({}, class_tbl)
21 if init then
22 init(self, ...)
23 elseif base and base.init then
24 base.init(self, ...)
25 end
26 return self
27 end,
28 })
29
30 cls.init = init
31
32 function cls:is_a(klass)
33 local m = getmetatable(self)
34 while m do
35 if m == klass then
36 return true
37 end
38 m = m._base
39 end
40 return false
41 end
42
43 return cls
44 end
45
46 point = class(function(p, re, im)
47 -- Cas 1 : appel standard point(x, y)
48 if type(re) == "number" and type(im) == "number" then
49 p.re = re
50 p.im = im
51
52 -- Cas 2 : appel interne point({re=..., im=...})
53 elseif type(re) == "table" and im == nil and type(re.re) == "number" and type(re.im) == "number" then
54 p.re = re.re
55 p.im = re.im
56
57 -- Cas invalide
58 else
59 tex.error(
60 "Invalid point: expected two numbers or a complex {re,im}, got re = "
61 .. tostring(re)
62 .. ", im = "
63 .. tostring(im)
64 )
65 end
66
67 p.type = "point"
68 p.argument = math.atan(p.im, p.re)
69 p.modulus = math.sqrt(p.re * p.re + p.im * p.im)
70 p.mtx = matrix:new({ { p.re }, { p.im } })
71 end)
72
73 local sqrt = math.sqrt
74 local cos = math.cos
75 local sin = math.sin
76 local exp = math.exp
77 local atan = math.atan
78 local min = math.min
79 local max = math.max
80 local abs = math.abs
81
82
83 local function is_point(obj)
84 if type(obj) ~= "table" then
85 return false
86 end
87 local mt = getmetatable(obj)
88 while mt do
89 if mt == point then
90 return true
91 end
92 mt = mt._base
93 end
94 return false
95 end
96
97 local function topoint(z)
98 if is_point(z) then
99 return z
100 elseif type(z) == "number" then
101 return point(z, 0)
102 elseif type(z) == "table" and type(z.re) == "number" and type(z.im) == "number" then
103 return point(z) -- It is managed in the constructor.
104 else
105 tex.error("topoint: cannot convert value to point")
106 end
107 end
108
109
110 local function check(z1, z2)
111 local p1, p2 = topoint(z1), topoint(z2)
112 if not p1 or not p2 then
113 tex.error("check: expected point or number")
114 end
115 return p1, p2
116 end
117 -- -------------------------------------------------------------------
118 -- metamethods
119 -- -------------------------------------------------------------------
120 -- redefine arithmetic operators!
121 function point.__add(z1, z2)
122 local c1, c2 = check(z1, z2)
123 return point(c1.re + c2.re, c1.im + c2.im)
124 end
125
126 function point.__sub(z1, z2)
127 local c1, c2 = check(z1, z2)
128 return point(c1.re - c2.re, c1.im - c2.im)
129 end
130
131 function point.__unm(z)
132 local z = topoint(z)
133 return point(-z.re, -z.im)
134 end
135
136 function point.__mul(z1, z2)
137 local c1, c2 = check(z1, z2)
138 return point(c1.re * c2.re - c1.im * c2.im, c1.im * c2.re + c1.re * c2.im)
139 end
140
141 -- dot product is '..' (a+ib) (c-id) = ac+bd + i(bc-ad)
142 function point.__concat(z1, z2)
143 local c1, c2 = check(z1, z2)
144 local z = c1 * point.conj(c2)
145 return z.re
146 end
147
148 -- determinant is '^' (a-ib) (c+id) = ac+bd + i(ad - bc)
149 function point.__pow(z1, z2)
150 local c1, c2 = check(z1, z2)
151 local z = point.conj(c1) * c2
152 return z.im
153 end
154
155 function point.__div(z1, z2)
156 local c1, c2 = check(z1, z2)
157 local den = c2.re * c2.re + c2.im * c2.im
158 if den == 0 then
159 tex.error("Division by zero in point.__div")
160 end
161 return point((c1.re * c2.re + c1.im * c2.im) / den, (c1.im * c2.re - c1.re * c2.im) / den)
162 end
163
164 function point.__tostring(z)
165 local real = z.re
166 local imag = z.im
167 local tolerance = 1e-10 -- Définir une tolérance
168
169 local function format_real(r)
170 if math.abs(r - math.round(r)) < tolerance then
171 return tostring(math.round(r)) -- Utiliser un entier
172 else
173 return string.format("%." .. tkz.dc .. "f", r) -- Utiliser un flottant
174 end
175 end
176
177 if real == 0 then
178 if imag == 0 then
179 return "0"
180 else
181 if imag == 1 then
182 return "i"
183 elseif imag == -1 then
184 return "-i"
185 else
186 return string.format("%." .. tkz.dc .. "f", imag) .. "i"
187 end
188 end
189 else
190 if imag > 0 then
191 if imag == 1 then
192 return format_real(real) .. "+i"
193 else
194 return format_real(real) .. "+" .. string.format("%." .. tkz.dc .. "f", imag) .. "i"
195 end
196 elseif imag < 0 then
197 if imag == -1 then
198 return format_real(real) .. "-i"
199 else
200 return format_real(real) .. string.format("%." .. tkz.dc .. "f", imag) .. "i"
201 end
202 else
203 return format_real(real)
204 end
205 end
206 end
207
208 function point.__tonumber(z)
209 if z.im == 0 then
210 return z.re
211 else
212 return nil
213 end
214 end
215
216 function point.__eq(z1, z2)
217 local epsilon = 1e-10
218 return math.abs(z1.re - z2.re) < epsilon and math.abs(z1.im - z2.im) < epsilon
219 end
220
221 function point:identity(pt)
222 return point.abs(self - pt) < tkz.epsilon
223 end
224
225 -- -------------------------------------------------------------------
226 local function pyth(a, b)
227 if a == 0 and b == 0 then
228 return 0
229 end
230 a, b = abs(a), abs(b)
231 a, b = max(a, b), min(a, b)
232 return a * sqrt(1 + (b / a) ^ 2)
233 end
234
235 function point.conj(z)
236 local cx = topoint(z)
237 return point(cx.re, -cx.im)
238 end
239
240 function point.mod(z)
241 local cx = topoint(z)
242 local function sqr(x)
243 return x * x
244 end
245 return pyth(cx.re, cx.im)
246 end
247
248 function point.abs(z)
249 local cx = topoint(z)
250 local function sqr(x)
251 return x * x
252 end
253 return sqrt(sqr(cx.re) + sqr(cx.im))
254 end
255
256 function point.norm(z)
257 local cx = topoint(z)
258 local function sqr(x)
259 return x * x
260 end
261 return (sqr(cx.re) + sqr(cx.im))
262 end
263
264 function point.power(z, n)
265 if type(z) == "number" then
266 return z ^ n
267 else
268 local m = z.modulus ^ n
269 local a = angle_normalize_(z.argument * n)
270 return polar_(m, a)
271 end
272 end
273
274 function point.arg(z)
275 local cx = topoint(z)
276 return math.atan(cx.im, cx.re)
277 end
278
279 function point.get(z)
280 return z.re, z.im
281 end
282
283 function point.sqrt(z)
284 local cx = topoint(z)
285 local len = math.sqrt(cx.re ^ 2 + cx.im ^ 2)
286 local sign = (cx.im < 0 and -1) or 1
287 return point(math.sqrt((cx.re + len) / 2), sign * math.sqrt((len - cx.re) / 2))
288 end
289
290 -- ============= methods =========--
291 -- the result is a point
292 function point:new(a, b)
293 return point(a, b)
294 end
295
296 function point:polar(radius, phi)
297 return point:new(radius * math.cos(phi), radius * math.sin(phi))
298 end
299
300 function point:polar_deg(radius, phi)
301 return polar_(radius, phi * math.pi / 180)
302 end
303
304 function point:north(d)
305 local d = d or 1
306 return self + polar_(d, math.pi / 2)
307 end
308
309 function point:south(d)
310 local d = d or 1
311 return self + polar_(d, 3 * math.pi / 2)
312 end
313
314 function point:east(d)
315 local d = d or 1
316 return self + polar_(d, 0)
317 end
318
319 function point:west(d)
320 local d = d or 1
321 return self + polar_(d, math.pi)
322 end
323
324 function point:length(p)
325 return length_(self, p)
326 end
327
328 function point:normalize()
329 local d = point.abs(self)
330 if d < tkz.epsilon then
331 tex.error("division by zero" )
332 else
333 return point(self.re / d, self.im / d)
334 end
335 end
336
337 function point:normalize_from(p)
338 local u = (self - p)
339 local d = point.abs(u)
340 if d < tkz.epsilon then
341 tex.error("division by zero" )
342 else
343 return point(u.re / d, u.im / d) + p
344 end
345 end
346
347 -- function point:identity(pt)
348 -- return point.abs(self - pt) < tkz.epsilon
349 -- end
350
351 function point:orthogonal(d)
352 local m
353 if d == nil then
354 -- If no scaling factor d is provided, return the point rotated 90 degrees counterclockwise
355 return point(-self.im, self.re)
356 else
357 m = point.mod(self) -- Get the modulus (magnitude) of the current point
358 return point(-self.im * d / m, self.re * d / m) -- Return the scaled orthogonal point
359 end
360 end
361
362 function point:at(z)
363 return point(self.re + z.re, self.im + z.im)
364 end
365
366 function point:print()
367 tex.print(tostring(self))
368 end
369
370 -- Shift the current point A orthogonally to the direction (AB)
371 -- by a distance "dist"
372 function point:shift_orthogonal_to(B, dist)
373 local v = B - self -- vecteur AB
374 local n = v:orthogonal(dist) -- vecteur normal de norme "dist"
375 return n:at(self) -- applique le décalage à A
376 end
377
378 -- Shift the current point A along the direction (AB)
379 -- by a distance "dist"
380 function point:shift_collinear_to(B, dist)
381 local v = (B - self):normalize()
382 return self + v*dist
383 end
384
385 function point:scale(kx, ky)
386 ky = ky or kx
387 return point(kx * self.re, ky * self.im)
388 end
389 -- =====================
390 -- the result is a circle
391 -- =====================
392 -- Circle through three points: self, a, b
393 -- point:PPP — cercle passant par trois points (circonscrit)
394 -- Retourne :
395 -- PA_center : path des centres
396 -- PA_through: path des points "through"
397 -- n : nombre de cercles trouvés (0 si dégénéré, 1 sinon)
398 function point:PPP(a, b)
399 -- paths de sortie (toujours créés, même si n = 0)
400 local PA_center = path()
401 local PA_through = path()
402
403 -- cas dégénéré : points alignés ou deux points confondus
404 if tkz.aligned(self, a, b)
405 or self:identity(a) or self:identity(b) or a:identity(b) then
406 return PA_center, PA_through, 0
407 end
408
409 -- centre du cercle circonscrit
410 local o = circum_center_(self, a, b)
411
412 -- on choisit "self" comme point-through (n'importe lequel des 3 convient)
413 PA_center:add_point(o)
414 PA_through:add_point(self)
415
416 return PA_center, PA_through, 1
417 end
418
419 -- ----------------------------------------------------------------
420 -- transformations
421 -- ----------------------------------------------------------------
422 -- function point: symmetry(pt)
423 -- return symmetry_ (self ,pt)
424 -- end
425
426 function point:symmetry(...)
427 local tp = table.pack(...) -- Pack arguments into a table
428 local nb = tp.n -- Number of arguments
429 local obj = tp[1] -- The first object in the arguments
430
431 if nb == 1 then -- If there's only one argument
432 if obj.type == "point" then
433 return symmetry_(self, obj) -- Apply symmetry on the point
434 elseif obj.type == "line" then
435 return line:new(set_symmetry_(self, obj.pa, obj.pb)) -- Create a new line
436 elseif obj.type == "circle" then
437 return circle:new(set_symmetry_(self, obj.center, obj.through)) -- Create a new circle
438 else
439 return triangle:new(set_symmetry_(self, obj.pa, obj.pb, obj.pc)) -- Create a new triangle
440 end
441 else -- If there are multiple arguments
442 local results = {} -- Initialize a table to store results
443 for i = 1, nb do
444 table.insert(results, symmetry_(self, tp[i])) -- Apply symmetry on each object
445 end
446 return table.unpack(results) -- Return the results as separate values
447 end
448 end
449
450 function point:set_symmetry(...)
451 return set_symmetry_(self, ...)
452 end
453
454 function point:rotation_pt(angle, pt)
455 return rotation_(self, angle, pt)
456 end
457
458 function point:set_rotation(angle, ...)
459 return set_rotation_(self, angle, ...)
460 end
461
462 function point:rotation(angle, ...)
463 local tp = table.pack(...) -- Pack arguments into a table
464 local nb = tp.n -- Number of arguments
465 local obj = tp[1] -- The first object in the arguments
466
467 if nb == 1 then -- If there's only one argument
468 if obj.type == "point" then
469 return rotation_(self, angle, obj) -- Rotate the point
470 elseif obj.type == "line" then
471 return line:new(set_rotation_(self, angle, obj.pa, obj.pb)) -- Rotate the line
472 elseif obj.type == "triangle" then
473 return triangle:new(set_rotation_(self, angle, obj.pa, obj.pb, obj.pc)) -- Rotate the triangle
474 elseif obj.type == "circle" then
475 return circle:new(set_rotation_(self, angle, obj.center, obj.through)) -- Rotate the circle
476 else -- For other shapes like square
477 return square:new(set_rotation_(self, angle, obj.pa, obj.pb, obj.pc, obj.pd)) -- Rotate the square
478 end
479 else -- If there are multiple arguments
480 local results = {} -- Initialize a table to store results
481 for i = 1, nb do
482 table.insert(results, rotation_(self, angle, tp[i])) -- Rotate each object
483 end
484 return table.unpack(results) -- Return the results as separate values
485 end
486 end
487
488 function point:homothety(coeff, ...)
489 local tp = table.pack(...) -- Pack arguments into a table
490 local nb = tp.n -- Number of arguments
491 local obj = tp[1] -- The first object in the arguments
492 local t = {} -- Initialize a table to store results
493
494 if nb == 1 then -- If there's only one argument
495 if obj.type == "point" then
496 return homothety_(self, coeff, obj) -- Apply homothety to the point
497 elseif obj.type == "line" then
498 return line:new(set_homothety_(self, coeff, obj.pa, obj.pb)) -- Apply homothety to the line
499 elseif obj.type == "triangle" then
500 return triangle:new(set_homothety_(self, coeff, obj.pa, obj.pb, obj.pc)) -- Apply homothety to the triangle
501 elseif obj.type == "circle" then
502 return circle:new(set_homothety_(self, coeff, obj.center, obj.through)) -- Apply homothety to the circle
503 else -- For other shapes like square
504 return square:new(set_homothety_(self, coeff, obj.pa, obj.pb, obj.pc, obj.pd)) -- Apply homothety to the square
505 end
506 else -- If there are multiple arguments
507 for i = 1, nb do
508 table.insert(t, homothety_(self, coeff, tp[i])) -- Apply homothety to each object
509 end
510 return table.unpack(t) -- Return the results as separate values
511 end
512 end
513