Debian Codegraph @

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

raw
1 -- File: tkz_elements_line.lua
2 -- Copyright (c) 2026 Alain Matthes
3 -- SPDX-License-Identifier: LPPL-1.3c
4 -- Maintainer: Alain Matthes
5 -- -------------------------------------------------------------------------
6 -- Lines
7 -- -------------------------------------------------------------------------
8 line = {}
9 line.__index = line
10 -- Function to create a new line object with two points za and zb.
11 function line:new(za, zb)
12 local type = "line"
13 local mid = (za + zb) / 2
14 local north_pa = rotation_(za, math.pi / 2, zb)
15 local south_pa = rotation_(za, -math.pi / 2, zb)
16 local north_pb = rotation_(zb, -math.pi / 2, za)
17 local south_pb = rotation_(zb, math.pi / 2, za)
18 local west = rotation_(za, math.pi / 2, north_pa)
19 local east = rotation_(zb, math.pi / 2, south_pb)
20 local slope = angle_normalize_(point.arg(zb - za))
21 local length = point.mod(zb - za)
22 local vec = vector:new(za, zb)
23 local li = {
24 pa = za,
25 pb = zb,
26 north_pa = north_pa,
27 south_pa = south_pa,
28 west = west,
29 east = east,
30 north_pb = north_pb,
31 south_pb = south_pb,
32 slope = slope,
33 mid = mid,
34 type = type,
35 vec = vec,
36 length = length,
37 }
38 setmetatable(li, self)
39 return li
40 end
41
42 setmetatable(line, {
43 __call = function(cls, ...)
44 return cls:new(...)
45 end,
46 })
47
48 function line:get(i)
49 if i == nil then
50 return self.pa, self.pb
51 elseif i == 1 then
52 return self.pa
53 elseif i == 2 then
54 return self.pb
55 else
56 return nil
57 end
58 end
59
60
61 -------------------
62 -- Result -> real
63 -------------------
64
65 -- Calculate the distance between a point and a line
66 function line:distance(pt)
67 return distance_(self.pa, self.pb, pt)
68 end
69
70 function line:side_line(pt)
71 local det = (self.pa - self.pb) ^ (self.pa - pt)
72 if math.abs(det) < tkz.epsilon then return 0 end
73 return (det > 0) and 1 or -1
74 end
75 -------------------
76 -- Result -> boolean
77 -------------------
78
79
80
81 -- --- new canonical API --------------------------------------------
82 -- Returns "on" or "outside"
83 -- function line:position(p, EPS)
84 -- return line_on_(self.pa, self.pb, p, EPS) and "ON" or "OUT"
85 -- end
86 --
87 -- -- --- legacy API (compat)
88 function line:position(obj, EPS)
89 EPS = EPS or tkz.epsilon
90
91 local mt = getmetatable(obj)
92
93 if mt == point then
94 return line_on_(self.pa, self.pb, obj, EPS) and "ON" or "OUT"
95
96 elseif mt == line then
97 return line_line_position_(self, obj, EPS)
98
99 elseif mt == circle then
100 return line_circle_position_(self, obj, EPS)
101 end
102
103 error("Unsupported object in line:position()")
104 end
105
106 ------------------------------------------
107 -- Checks whether a point is on the (infinite) line
108 function line:in_out(pt, EPS)
109 return line_on_(self.pa, self.pb, pt, EPS)
110 end
111
112 -- Keep the alias, but route through the EPS-aware method
113 line.on_line = line.in_out
114 line.is_on_line = line.in_out
115
116 -- Checks if a point is on the line segment
117
118 function line:position_segment(pt, EPS)
119 EPS = EPS or tkz.epsilon
120 if self:position(pt, EPS) ~= "ON" then
121 return "OUT"
122 end
123 return on_segment_(self.pa, self.pb, pt, EPS) and "ON" or "OUT"
124 end
125
126
127 function line:on_segment(pt, EPS)
128 return on_segment_(self.pa, self.pb, pt, EPS)
129 end
130 line.in_out_segment = line.on_segment
131 line.is_on_segment = line.on_segment
132
133 function line:is_equidistant(p)
134 return is_equidistant_(self.pa, self.pb, p)
135 end
136
137
138 -- Classification along the oriented segment [pa,pb], but only if pt is on the line
139 -- Returns -1 (before pa), 0 (between), +1 (after pb), or nil if pt is not on the line.
140 function line:where_on_line(pt, EPS)
141 EPS = EPS or tkz.epsilon
142
143 if self:position(pt, EPS) ~= "ON" then
144 return nil
145 end
146
147 local t = self:abscissa_(pt, EPS)
148 if t == nil then return nil end
149
150 if t < -EPS then return "BEFORE" end
151 if t > 1+EPS then return "AFTER" end
152 return "BETWEEN"
153 end
154
155
156 function line:is_parallel(L)
157 local pa, pb = self:get()
158 local pc, pd = L:get()
159 return is_parallel_(pa, pb, pc, pd)
160 end
161
162 function line:is_orthogonal(L)
163 local pa, pb = self:get()
164 local pc, pd = L:get()
165 return is_orthogonal_(pa, pb, pc, pd)
166 end
167
168 -- ------------------------------------------------------------
169 -- line / line relative position
170 -- returns: "IDENTICAL" | "PARALLEL" | "SECANT"
171 -- ------------------------------------------------------------
172 function line_line_position_(L1, L2, EPS)
173 EPS = EPS or tkz.epsilon
174
175 if is_parallel_(L1.pa, L1.pb, L2.pa, L2.pb) then
176 -- parallèles ou identiques
177 if line_on_(L1.pa, L1.pb, L2.pa, EPS) then
178 return "IDENTICAL"
179 else
180 return "PARALLEL"
181 end
182 else
183 return "INTERSECT"
184 end
185 end
186
187
188 -------------------
189 -- Result -> point
190 -------------------
191
192 -- Returns a point on the line to a distance d from pa (optionally modified by pt).
193 function line:report(d, pt)
194 if not self.length or math.abs(self.length) < tkz.epsilon then
195 tex.error("self.length must be non-zero")
196 end
197 local t = d / self.length
198 local result = barycenter_({ self.pa, 1 - t }, { self.pb, t })
199 if pt then
200 return result + pt - self.pa
201 else
202 return result
203 end
204 end
205 line.point_at_distance = line.report
206
207 -- Calculates the barycenter of two points on the line with weights ka and kb
208 function line:barycenter(ka, kb)
209 return barycenter_({ self.pa, ka }, { self.pb, kb })
210 end
211
212 function line:point(t)
213 return barycenter_({ self.pa, 1 - t }, { self.pb, t })
214 end
215
216 function line:midpoint()
217 return (self.pa + self.pb) / 2
218 end
219
220 function line:harmonic_int(pt)
221 return div_harmonic_int_(self.pa, self.pb, pt)
222 end
223
224 function line:harmonic_ext(pt)
225 return div_harmonic_ext_(self.pa, self.pb, pt)
226 end
227
228 function line:harmonic_both(k)
229 return div_harmonic_both_(self.pa, self.pb, k)
230 end
231
232 -- Unified harmonic division interface
233 -- Usage:
234 -- L:harmonic(k) --> "both" mode, ratio k (internal, external)
235 -- L:harmonic("both", k) --> idem
236 -- L:harmonic("int", pt) --> internal harmonic conjugate of pt
237 -- L:harmonic("ext", pt) --> external harmonic conjugate of pt
238 function line:harmonic(mode, arg)
239 -- si le premier argument n'est pas une chaîne, on suppose mode = "both"
240 if type(mode) ~= "string" then
241 if mode == "int" then mode = "internal" end
242 if mode == "ext" then mode = "external" end
243 arg = mode
244 mode = "both"
245 end
246
247
248 if mode == "internal" then
249 return div_harmonic_int_(self.pa, self.pb, arg)
250
251 elseif mode == "external" then
252 return div_harmonic_ext_(self.pa, self.pb, arg)
253
254 elseif mode == "both" then
255 return div_harmonic_both_(self.pa, self.pb, arg)
256
257 else
258 tex.error("line:harmonic -> invalid mode: use 'internal', 'external' or 'both'")
259 return
260 end
261 end
262
263
264
265
266 -- Returns the point corresponding to the gold ratio on the line
267 function line:gold_ratio()
268 return self.pa + (self.pb - self.pa) * tkz.invphi
269 end
270
271 -- Normalize line (point at one unit distance)
272 function line:normalize()
273 return self.pa + (self.pb - self.pa) / self.length
274 end
275
276
277 -- Reverse normalizes the line
278 function line:normalize_inv()
279 return normalize_(self.pb, self.pa)
280 end
281
282 function line:collinear_at(pt, k)
283 if type(k) == "table" then
284 local L = k.length or k.len
285 if not L then
286 -- pas de longueur fournie : on retombe sur le comportement par défaut
287 return collinear_at_(self.pa, self.pb, pt, 1)
288 end
289 return collinear_at_(self.pa, self.pb, pt, L / self.length)
290 else
291 if k == nil then
292 return collinear_at_(self.pa, self.pb, pt, 1)
293 else
294 return collinear_at_(self.pa, self.pb, pt, k)
295 end
296 end
297 end
298 line.colinear_at = line.collinear_at
299
300 function line:orthogonal_at(pt, k)
301 if type(k) == "table" then
302 local L = k.length or k.len
303 if not L then
304 -- pas de longueur fournie : on retombe sur le comportement par défaut
305 return orthogonal_at_(self.pa, self.pb, pt, 1)
306 end
307 return orthogonal_at_(self.pa, self.pb, pt, L / self.length)
308 else
309 if k == nil then
310 return orthogonal_at_(self.pa, self.pb, pt, 1)
311 else
312 return orthogonal_at_(self.pa, self.pb, pt, k)
313 end
314 end
315 end
316
317
318 function line:random()
319 return self:point(math.random())
320 end
321
322 -------------------
323 -- Result -> line
324 -------------------
325
326 function line:ll_from(pt)
327 return line:new(pt, pt + self.pb - self.pa)
328 end
329
330 line.parallel_from = line.ll_from
331
332 function line:ortho_from(pt)
333 local v = (self.pb - self.pa) * point(0, 1)
334 return line:new(pt - v, pt + v)
335 end
336
337 line.orthogonal_from = line.ortho_from
338
339 function line:mediator()
340 local m = midpoint_(self.pa, self.pb)
341 return line:new(rotation_(m, -math.pi / 2, self.pb), rotation_(m, math.pi / 2, self.pb))
342 end
343 line.perpendicular_bisector = line.mediator
344 line.bisector = line.mediator
345
346 function line:swap_line()
347 self.pa, self.pb = self.pb, self.pa
348 return self
349 end
350
351 -- d : signed distance from the line (positive / negative side)
352 function line:collinear_at_distance(d)
353 local a, b = self.pa, self.pb
354 local u = orthogonal_at_(a, b, a, d / self.length)
355 local v = collinear_at_(a, b, u)
356 return line:new(u, v)
357 end
358
359
360 function line:equidistant_with(L2, choice)
361 return lines_equidistant_(self, L2, choice)
362 end
363 -------------------
364 -- Result -> circle
365 -------------------
366
367 function line:circle(swap)
368 swap = (swap == "swap")
369 if swap then
370 return circle:new(self.pb, self.pa)
371 else
372 return circle:new(self.pa, self.pb)
373 end
374 end
375
376 function line:circle_swap()
377 return circle:new(self.pb, self.pa)
378 end
379
380 function line:diameter()
381 local c = midpoint_(self.pa, self.pb)
382 return circle:new(c, self.pb)
383 end
384
385 function line:apollonius(k)
386 local z1, z2, c
387 z1 = barycenter_({ self.pa, 1 }, { self.pb, k })
388 z2 = barycenter_({ self.pa, 1 }, { self.pb, -k })
389 c = midpoint_(z1, z2)
390 return circle:new(c, z2)
391 end
392
393
394 ----------------------
395 -- Result -> triangle
396 ----------------------
397 function line:equilateral(swap)
398 swap = (swap == "swap")
399 if swap then
400 return triangle:new(self.pa, self.pb, rotation_(self.pa, -math.pi / 3, self.pb))
401 else
402 return triangle:new(self.pa, self.pb, rotation_(self.pa, math.pi / 3, self.pb))
403 end
404 end
405
406 function line:isosceles(a, swap)
407 swap = (swap == "swap")
408 local c1, c2, pta, ptb, pt1, pt2
409 -- Create circles with radius 'a' centered on self.pa and self.pb
410 c1 = circle:radius(self.pa, a)
411 c2 = circle:radius(self.pb, a)
412 -- Calculating the points of intersection of the two circles
413 pta, ptb = intersection_cc(c1, c2)
414 if get_angle_normalize_(self.pa, self.pb, pta) < get_angle_normalize_(self.pa, self.pb, ptb) then
415 pt1 = pta
416 pt2 = ptb
417 else
418 pt1 = ptb
419 pt2 = pta
420 end
421 if swap then
422 return triangle:new(self.pa, self.pb, pt2)
423 else
424 return triangle:new(self.pa, self.pb, pt1)
425 end
426 end
427
428 function line:school(swap)
429 local pta, ptb, pt
430 swap = (swap == "swap")
431
432 if swap then
433 pta = rotation_(self.pa, math.pi / 3, self.pb)
434 ptb = rotation_(self.pb, -math.pi / 6, self.pa)
435 else
436 pta = rotation_(self.pa, math.pi / 6, self.pb)
437 ptb = rotation_(self.pb, -math.pi / 3, self.pa)
438 end
439
440 pt = intersection_ll_(self.pa, pta, self.pb, ptb)
441 return triangle:new(self.pa, self.pb, pt)
442 end
443
444 function line:half(swap)
445 local x, pt
446 x = midpoint_(self.pa, self.pb)
447 swap = (swap == "swap")
448
449 if swap then
450 pt = rotation_(self.pa, math.pi / 2, x)
451 else
452 pt = rotation_(self.pb, -math.pi / 2, x)
453 end
454
455 return triangle:new(self.pa, self.pb, pt)
456 end
457
458 function line:two_angles(alpha, beta, swap)
459 local pta, ptb, pt
460 swap = (swap == "swap")
461
462 if swap then
463 pta = rotation_(self.pa, -alpha, self.pb)
464 ptb = rotation_(self.pb, beta, self.pa)
465 else
466 pta = rotation_(self.pa, alpha, self.pb)
467 ptb = rotation_(self.pb, -beta, self.pa)
468 end
469
470 pt = intersection_ll_(self.pa, pta, self.pb, ptb)
471 return triangle:new(self.pa, self.pb, pt)
472 end
473
474 line.asa = line.two_angles
475 line.a_a = line.two_angles
476
477 function line:s_s(a, b, swap)
478 local pta, ptb
479 swap = (swap == "swap")
480
481 pta = self.pa + point(a, 0)
482 ptb = self.pb + point(-b, 0)
483
484 local i, j = intersection_cc_(self.pa, pta, self.pb, ptb)
485 local an = get_angle_normalize_(self.pa, self.pb, i)
486 if an < 0 then
487 i, j = j, i
488 end
489 if swap then
490 return triangle:new(self.pa, self.pb, j)
491 else
492 return triangle:new(self.pa, self.pb, i)
493 end
494 end
495 line.sss = line.s_s
496
497 function line:s_a(a, phi, swap)
498 local x = rotation_(self.pb, -phi, self.pa)
499 local y = report_(self.pa, self.pb, a)
500 local u, v = intersection_lc_(self.pb, x, self.pa, y)
501
502 if not u then -- Pas d'intersection
503 tex.error("Invalid configuration. No solution found.")
504 return nil
505 end
506
507 if point.mod(u - v) < tkz.epsilon then -- Solution unique
508 return triangle:new(self.pa, self.pb, u)
509 end
510
511 -- Deux solutions
512 if length_(self.pb, u) < length_(self.pb, v) then
513 u, v = v, u
514 end
515
516 if swap == "swap" then
517 if point.mod(v - self.pb) < tkz.epsilon then
518 tex.error("Degenerated triangle.")
519 return nil
520 else
521 return triangle:new(self.pa, self.pb, v)
522 end
523 else
524 return triangle:new(self.pa, self.pb, u)
525 end
526 end
527 line.ssa = line.s_a
528
529 function line:a_s(a, phi, swap)
530 local x = rotation_(self.pa, phi, self.pb)
531 local y = report_(self.pb, self.pa, a)
532 local u, v = intersection_lc_(self.pa, x, self.pb, y)
533
534 if not u then -- No intersection
535 tex.error("Invalid configuration. No solution found.")
536 return nil
537 end
538
539 if point.mod(u - v) < tkz.epsilon then -- One-stop solution
540 return triangle:new(self.pa, self.pb, u)
541 end
542
543 -- Two solutions
544 if length_(self.pa, u) < length_(self.pa, v) then
545 u, v = v, u
546 end
547
548 if swap == "swap" then
549 if point.mod(v - self.pa) < tkz.epsilon then
550 tex.error("Degenerated triangle.")
551 return nil
552 else
553 return triangle:new(self.pa, self.pb, v)
554 end
555 else
556 return triangle:new(self.pa, self.pb, u)
557 end
558 end
559
560 function line:sa_(a, phi, swap)
561 local x, pt
562 swap = (swap == "swap")
563 x = report_(self.pa, self.pb, a)
564 if swap then
565 pt = rotation_(self.pa, -phi, x)
566 else
567 pt = rotation_(self.pa, phi, x)
568 end
569 return triangle:new(self.pa, self.pb, pt)
570 end
571
572 function line:_as(a, phi, swap)
573 local x, pt
574 swap = (swap == "swap")
575 x = report_(self.pb, self.pa, a)
576 if swap then
577 pt = rotation_(self.pb, phi, x)
578 else
579 pt = rotation_(self.pb, -phi, x)
580 end
581 return triangle:new(self.pa, self.pb, pt)
582 end
583
584 --------------------------
585 -- Result --> sacred triangles
586 --------------------------
587
588 function line:gold(swap)
589 local pt
590 swap = (swap == "swap")
591
592 if swap then
593 pt = rotation_(self.pa, -math.pi / 2, self.pb)
594 else
595 pt = rotation_(self.pa, math.pi / 2, self.pb)
596 end
597
598 return triangle:new(self.pa, self.pb, self.pa + (pt - self.pa) * tkz.invphi)
599 end
600
601 function line:golden(swap)
602 local pta, ptb, pt
603 swap = (swap == "swap")
604 local angle = tkz.tau / 5
605
606 if swap then
607 pta = rotation_(self.pa, -angle, self.pb)
608 ptb = rotation_(self.pb, angle, self.pa)
609 else
610 pta = rotation_(self.pa, angle, self.pb)
611 ptb = rotation_(self.pb, -angle, self.pa)
612 end
613
614 pt = intersection_ll_(self.pa, pta, self.pb, ptb)
615 return triangle:new(self.pa, self.pb, pt)
616 end
617 line.sublime = line.golden
618 line.euclid = line.golden
619
620 function line:divine(swap)
621 swap = (swap == "swap")
622 local angle = math.pi / 5
623 local pta = rotation_(self.pa, swap and -angle or angle, self.pb)
624 local ptb = rotation_(self.pb, swap and angle or -angle, self.pa)
625 local pt = intersection_ll_(self.pa, pta, self.pb, ptb)
626 return triangle:new(self.pa, self.pb, pt)
627 end
628
629 function line:egyptian(swap)
630 swap = (swap == "swap")
631 local n = rotation_(self.pb, swap and math.pi / 2 or -math.pi / 2, self.pa)
632 local pt = self.pb + (n - self.pb) / point.mod(n - self.pb) * self.length * 0.75
633 return triangle:new(self.pa, self.pb, pt)
634 end
635
636 line.pythagoras = line.egyptian
637 line.isis = line.egyptian
638
639 line.golden_gnomon = line.divine
640
641 ------------------------------
642 -- Résultat -> carré
643 ------------------------------
644 function line:square(swap)
645 return square_from_side_(self.pa, self.pb, swap)
646 end
647
648 -------------------
649 -- Transformations
650 -------------------
651
652 -- Translation of a given point in the direction of the line (pb - pa)
653 function line:translation_pt(pt)
654 return translation_(self.pb - self.pa, pt)
655 end
656
657 -- Translation of a circle object in line direction
658 function line:translation_C(obj)
659 local pa = obj.center
660 local pb = obj.through
661 local x, y = set_translation_(self.pb - self.pa, pa, pb)
662 return circle:new(x, y)
663 end
664
665 --Translation of a triangle object in line direction
666 function line:translation_T(obj)
667 local pa = obj.pa
668 local pb = obj.pb
669 local pc = obj.pc
670 local x, y, z = set_translation_(self.pb - self.pa, pa, pb, pc)
671 return triangle:new(x, y, z)
672 end
673
674 -- Translation of a line object along the line direction
675 function line:translation_L(obj)
676 local pa = obj.pa
677 local pb = obj.pb
678 local x, y = set_translation_(self.pb - self.pa, pa, pb)
679 return line:new(x, y)
680 end
681
682 -- General function for translating one or more objects
683 function line:translation(...)
684 local tp = table.pack(...) -- Groups all arguments in a table
685 local obj = tp[1] -- The first object is retrieved
686 local nb = tp.n -- Number of objects to be processed
687
688 --If only one object is passed
689 if nb == 1 then
690 if obj.type == "point" then
691 return translation_(self.pb - self.pa, obj)
692 elseif obj.type == "line" then
693 return self:translation_L(obj)
694 elseif obj.type == "triangle" then
695 return self:translation_T(obj)
696 elseif obj.type == "circle" then
697 return self:translation_C(obj)
698 else
699 tex.error("Unsupported object type for translation")
700 end
701 else
702 -- If several objects are passed, they are translated one by one
703 local t = {}
704 for i = 1, nb do
705 -- Translation of each object with the translation vector
706 table.insert(t, translation_(self.pb - self.pa, tp[i]))
707 end
708 return table.unpack(t) --Returns translated objects
709 end
710 end
711
712 -- Function for translating an object using line direction
713 function line:set_translation(...)
714 return set_translation_(self.pb - self.pa, ...)
715 end
716
717 function line:projection_ll(...)
718 local tp = table.pack(...)
719 local c, d = tp[1].pa, tp[1].pb -- c and d are the two points that define the projection line
720 local obj = tp[2] -- The object to be projected
721 local nb = tp.n -- Number of objects passed as parameters
722
723 if nb == 2 then
724 --Projection of line onto object
725 return projection_ll_(self.pa, self.pb, c, d, obj)
726 else
727 local t = {}
728 for i = 2, tp.n do
729 -- Projection of each object on the line
730 table.insert(t, projection_ll_(self.pa, self.pb, c, d, tp[i]))
731 end
732 return table.unpack(t) -- Retourne les résultats de la projection pour tous les objets
733 end
734 end
735
736 function line:set_projection_ll(...)
737 local tp = table.pack(...)
738 local c, d = tp[1].pa, tp[1].pb -- c and d are the two points defining the projection line
739 local t = {}
740
741 for i = 2, tp.n do
742 -- Projection de chaque objet sur la ligne
743 table.insert(t, projection_ll_(self.pa, self.pb, c, d, tp[i]))
744 end
745 return table.unpack(t) -- Returns projection results for all objects
746 end
747
748 function line:projection(...)
749 local tp = table.pack(...) -- Groups arguments in a table
750 local obj = tp[1] -- Retrieves the first object
751 local nb = tp.n -- Number of objects to be processed
752
753 -- If only one object is passed
754 if nb == 1 then
755 return projection_(self.pa, self.pb, obj) -- Projects object onto line
756 else
757 local t = {}
758 -- If several objects are passed, each is projected onto the line
759 for i = 1, tp.n do
760 table.insert(t, projection_(self.pa, self.pb, tp[i])) -- Projection of each object
761 end
762 return table.unpack(t) --Return projections
763 end
764 end
765
766 function line:set_projection(...)
767 local tp = table.pack(...) -- Groups arguments in a table
768 local t = {}
769 -- Projects each object onto the line
770 for i = 1, tp.n do
771 table.insert(t, projection_(self.pa, self.pb, tp[i])) --Projection of each object
772 end
773 return table.unpack(t) -- Return projections
774 end
775
776 function line:symmetry_axial_L(obj)
777 local pa = obj.pa
778 local pb = obj.pb
779 local x, y = self:set_reflection(pa, pb)
780 return line:new(x, y)
781 end
782
783 function line:symmetry_axial_T(obj)
784 local pa = obj.pa
785 local pb = obj.pb
786 local pc = obj.pc
787 local x, y, z = self:set_reflection(pa, pb, pc)
788 return triangle:new(x, y, z)
789 end
790
791 function line:symmetry_axial_C(obj)
792 local pa = obj.center
793 local pb = obj.through
794 local x, y = self:set_reflection(pa, pb)
795 return circle:new(x, y)
796 end
797
798 function line:reflection(...)
799 local tp = table.pack(...)
800 local obj = tp[1]
801 local nb = tp.n
802
803 if nb == 1 then
804 if obj.type == "point" then
805 return symmetry_axial_(self.pa, self.pb, obj)
806 elseif obj.type == "line" then
807 return self:symmetry_axial_L(obj)
808 elseif obj.type == "triangle" then
809 return self:symmetry_axial_T(obj)
810 elseif obj.type == "circle" then
811 return self:symmetry_axial_C(obj)
812 else
813 tex.error("Unsupported object type for reflection")
814 end
815 else
816 local t = {}
817 for i = 1, tp.n do
818 table.insert(t, symmetry_axial_(self.pa, self.pb, tp[i]))
819 end
820 return table.unpack(t)
821 end
822 end
823
824 function line:set_reflection(...)
825 return set_symmetry_axial_(self.pa, self.pb, ...)
826 end
827
828 function line:affinity(...)
829 local tp = table.pack(...)
830 local c, d = tp[1].pa, tp[1].pb
831 local k = tp[2]
832 local obj = tp[3]
833 local nb = tp.n
834 if nb == 3 then
835 return affinity_(self.pa, self.pb, c, d, k, obj)
836 else
837 local t = {}
838 for i = 3, tp.n do
839 table.insert(t, affinity_(self.pa, self.pb, c, d, k, tp[i]))
840 end
841 return table.unpack(t)
842 end
843 end
844
845 function line:set_affinity(...)
846 local tp = table.pack(...) -- Gather all arguments passed to table tp
847 local c, d = tp[1].pa, tp[1].pb --Extract the two points of the reference line (tp[1])
848 local k = tp[2] -- The affinity coefficient
849 local t = {}
850
851 -- Apply affinity for each object passed in parameter (from 3rd object upwards)
852 for i = 3, tp.n do
853 table.insert(t, affinity_(self.pa, self.pb, c, d, k, tp[i])) --Apply affinity to each object
854 end
855
856 return table.unpack(t) --Returns transformed objects
857 end
858
859 function line:path(nb)
860 nb = nb or 1
861 local list = {}
862 for i = 0, nb do
863 local t = i / nb
864 local P = self:point(t)
865 table.insert(list, "(" .. checknumber_(P.re) .. "," .. checknumber_(P.im) .. ")")
866 end
867 return path:new(list)
868 end
869
870 --- ===== CONTACT PROBLEM APOLLONIUS ===---
871 --- LLL LLP LPP
872
873 -- which ∈ {"all","in","pa","pb","pc"} -- comme triangle:c_lll
874 -- "all" (défaut) : renvoie 4 cercles (inscrit + 3 exinscrits)
875 -- line: LLL — circles tangent to three lines (self, L1, L2)
876 -- Return value (like CLL): centers_path, through_path, n
877 function line:LLL(L1, L2, which)
878 -- --- outputs (always returned)
879 local pa_center = path:new()
880 local pa_through = path:new()
881 local n = 0
882
883 -- --- unpack
884 local a0, b0 = self:get()
885 local a1, b1 = L1:get()
886 local a2, b2 = L2:get()
887
888 -- --- small helper: push one solution (center o, through = tangency on self)
889 local function push(o)
890 if not o then return end
891 local t = projection_(a0, b0, o) -- tangency on the receiver line (self)
892 if t then
893 pa_center:add_point(o)
894 pa_through:add_point(t)
895 n = n + 1
896 end
897 end
898
899 -- --- parallel flags
900 local p01 = is_parallel_(a0, b0, a1, b1)
901 local p12 = is_parallel_(a1, b1, a2, b2)
902 local p20 = is_parallel_(a2, b2, a0, b0)
903 local npar = (p01 and 1 or 0) + (p12 and 1 or 0) + (p20 and 1 or 0)
904
905 -- === Case 1: triangle (no parallel pairs) -> incircle + 3 excircles
906 if npar == 0 then
907 -- A = L1 ∩ L2 ; B = L2 ∩ self ; C = self ∩ L1
908 local A = intersection_ll_(a1, b1, a2, b2)
909 local B = intersection_ll_(a2, b2, a0, b0)
910 local C = intersection_ll_(a0, b0, a1, b1)
911 if not (A and B and C) then
912 return pa_center, pa_through, 0
913 end
914
915 local T = triangle:new(A, B, C)
916
917 -- which = "all" | "in" | "pa" | "pb" | "pc" | 1..4
918 local sel = which or "all"
919 local function normalize_selector(w)
920 if type(w) == "number" then
921 return (w == 1 and "in")
922 or (w == 2 and "pa")
923 or (w == 3 and "pb")
924 or (w == 4 and "pc")
925 or "all"
926 end
927 return w
928 end
929 sel = normalize_selector(sel)
930
931 if sel == "all" then
932 local c1, c2, c3, c4 = T:c_lll("all") -- four circles
933 local list = {c1, c2, c3, c4}
934 for _, Ck in ipairs(list) do
935 if Ck and Ck.center then push(Ck.center) end
936 end
937 return pa_center, pa_through, n
938 elseif sel == "in" or sel == "pa" or sel == "pb" or sel == "pc" then
939 local Ck = T:c_lll(sel) -- one circle
940 if Ck and Ck.center then push(Ck.center) end
941 return pa_center, pa_through, n
942 else
943 return pa_center, pa_through, 0
944 end
945 end
946
947 -- === Case 2: exactly one parallel pair -> 2 solutions
948 if npar == 1 then
949 -- Identify the parallel pair (P // Q) and the remaining line R
950 local P, Q, R
951 if p01 then P, Q, R = self, L1, L2
952 elseif p12 then P, Q, R = L1, L2, self
953 else P, Q, R = L2, self, L1
954 end
955
956 -- distance between the parallels (P, Q)
957 local D = distance_(P.pa, P.pb, Q.pa)
958 if not D or D <= tkz.epsilon then
959 return pa_center, pa_through, 0
960 end
961 local r = 0.5 * D
962
963 -- Midline M between P and Q (same direction as P/Q)
964 local h = Q.pa
965 local hproj = projection_(P.pa, P.pb, h)
966 if not hproj then
967 return pa_center, pa_through, 0
968 end
969 local mpt = (h + hproj) * 0.5
970 local dir = P.pb - P.pa
971 local M = line:new(mpt, mpt + dir)
972
973 -- Offsets of R at ±r
974 local Rp = R:collinear_at_distance( r)
975 local Rm = R:collinear_at_distance(-r)
976
977 -- Centers are intersections of M with these offsets
978 local W1 = Rp and intersection_ll_(M.pa, M.pb, Rp.pa, Rp.pb) or nil
979 local W2 = Rm and intersection_ll_(M.pa, M.pb, Rm.pa, Rm.pb) or nil
980
981 -- Always push with respect to self (the receiver), not P/Q/R arbitrarily
982 push(W1)
983 push(W2)
984
985 return pa_center, pa_through, n
986 end
987
988 -- === Case 3: degenerate (≥ 2 parallel pairs) -> no unique circle
989 return pa_center, pa_through, 0
990 end
991 --- === END ===
992
993
994 --===================================================
995 -- LPP (paths) — Circle tangent to self (a line) and
996 -- passing through two points a, b
997 -- Sortie unifiée : PA.center, PA.through, n
998 --===================================================
999 function line:LPP(a, b)
1000 -- Paths de sortie
1001 local pa_center = path:new()
1002 local pa_through = path:new()
1003 local n = 0
1004 local function push(o, t)
1005 if o and t then
1006 pa_center:add_point(o)
1007 pa_through:add_point(t)
1008 n = n + 1
1009 end
1010 end
1011
1012 -- Données de base
1013 local lab = line:new(a, b) -- droite (a,b)
1014 local Cab = circle:diameter(a, b) -- cercle de diamètre [a,b]
1015 local i = intersection(lab, self) -- intersection (ab) ∩ self
1016
1017
1018 -- 1) Un des points est sur la droite self (un seul cercle)
1019 -- NB: on conserve ta logique avec in_out() telle quelle.
1020 if (self:on_line(a)) and (not self:on_line(b) )then
1021 local lmed = lab:mediator()
1022 local laperp = self:ortho_from(a)
1023 local o = intersection(lmed, laperp)
1024 push(o, a)
1025 return pa_center, pa_through, n
1026 end
1027 if (self:on_line(b)) and (not self:on_line(a) )then
1028 local lmed = lab:mediator()
1029 local laperp = self:ortho_from(b)
1030 local o = intersection(lmed, laperp)
1031 push(o, b)
1032 return pa_center, pa_through, n
1033 end
1034
1035 -- 2) (ab) ⟂ self
1036 if self:is_orthogonal(lab) and i then
1037 local m = lab.mid
1038 local r = length_(m, i)
1039 local t1 = lab:isosceles(r).pc
1040 local t2 = lab:isosceles(r, "swap").pc
1041 push(t1, a)
1042 push(t2, a)
1043 return pa_center, pa_through, n
1044 end
1045
1046 -- 3) (ab) ∥ self
1047 if lab:is_parallel(self) then
1048 local mid = midpoint_(a, b)
1049 local proj = self:projection(mid)
1050 local o = circum_center_(a, b, proj)
1051 push(o, proj) -- une seule solution (centre sur médiatrice, rayon = proj→a)
1052 return pa_center, pa_through, n
1053 end
1054
1055 -- Cas dégénéré : i existe et est sur le segment [a,b] → aucune solution
1056 if i and lab:on_segment(i) then
1057 return pa_center, pa_through, n
1058 end
1059
1060 -- 4) Cas général (deux solutions en général)
1061 if i then
1062 local tpt = Cab:tangent_from(i).pb -- point de tangence sur Cab
1063 local circ = circle:new(i, tpt) -- cercle (i, tpt)
1064 local x, y = intersection(self, circ) -- tangences sur self
1065
1066 if x then
1067 local o1 = intersection(self:ortho_from(x), lab:mediator())
1068 push(o1, x)
1069 end
1070 if y and (not x or (x.identity and not x:identity(y)) or (not x.identity and (x ~= y))) then
1071 local o2 = intersection(self:ortho_from(y), lab:mediator())
1072 push(o2, y)
1073 end
1074 end
1075
1076 return pa_center, pa_through, n
1077 end
1078
1079 line.c_l_pp = line.LPP
1080
1081 --========= END of LPP =====================
1082 --=============================================
1083
1084 --===================== c_LLP (paths) =================--
1085 -- Cercles tangents à self et L, passant par p
1086 -- Retour : PA.center, PA.through, n
1087 -- (port fidèle de ton ancien code, sans changer l'algo)
1088 --=====================================================--
1089 function line:LLP(L, p)
1090 -- Conteneurs de solutions (paths)
1091 local pa_center = path:new()
1092 local pa_through = path:new()
1093 local n = 0
1094
1095 -- ===== Helpers, version CLP =====
1096 local function push(o, t)
1097 if o and t then
1098 pa_center:add_point(o)
1099 pa_through:add_point(t)
1100 n = n + 1
1101 end
1102 end
1103
1104 local function push_from_paths(centers, throughs, m)
1105 if (not centers) or (not throughs) or (not m) then return end
1106 for i = 1, m do
1107 push(centers:get(i), throughs:get(i))
1108 end
1109 end
1110 -- =================================
1111
1112 -- ======= CAS PARALLÈLES =======
1113 if self:is_parallel(L) then
1114 local x = self.pa
1115 local d = L:distance(x)
1116 local ds = self:distance(p)
1117 local dl = L:distance(p)
1118
1119 if ds + dl > d then
1120 return pa_center, pa_through, 0
1121 elseif utils.almost_equal(ds, 0) then
1122 local q = L:projection(p)
1123 push(midpoint_(p, q), p)
1124 return pa_center, pa_through, n
1125 elseif utils.almost_equal(dl, 0) then
1126 local q = self:projection(p)
1127 push(midpoint_(p, q), p)
1128 return pa_center, pa_through, n
1129 else
1130 local u = self:projection(p)
1131 local v = L:projection(p)
1132 local ma, mb = mediator_(u, v)
1133 local ci = circle:new(through(p, d / 2))
1134 local c1, c2 = intersection_lc_(ma, mb, p, ci.through)
1135 push(c1, p)
1136 push(c2, p)
1137 return pa_center, pa_through, n
1138 end
1139 end
1140 -- ======= FIN PARALLÈLES =======
1141
1142 -- ======= CAS SÉCANTS =======
1143 local s = intersection_ll_(self.pa, self.pb, L.pa, L.pb)
1144 local caux = circle:new(through(s, 2))
1145 local a, b = intersection_lc_(self.pa, self.pb, s, caux.through)
1146 local c, d = intersection_lc_(L.pa, L.pb, s, caux.through)
1147 local lbi = tkz.bisector(s, a, c)
1148 local lbj = tkz.bisector(s, a, d)
1149
1150 -- ======= CAS SPÉCIAUX : p sur une des droites =======
1151 if self:on_line(p) or L:on_line(p) then
1152 if self:on_line(p) and L:on_line(p) then
1153 return pa_center, pa_through, 0
1154 end
1155
1156 local N = self:on_line(p) and self:ortho_from(p) or L:ortho_from(p)
1157
1158 local O1 = intersection_ll_(N.pa, N.pb, lbi.pa, lbi.pb)
1159 local O2 = intersection_ll_(N.pa, N.pb, lbj.pa, lbj.pb)
1160
1161 if O1 and O2 then
1162 if point.abs(O2 - O1) > tkz.epsilon then
1163 push(O1, p)
1164 push(O2, p)
1165 else
1166 push(O1, p)
1167 end
1168 return pa_center, pa_through, n
1169 elseif O1 then
1170 push(O1, p)
1171 return pa_center, pa_through, n
1172 elseif O2 then
1173 push(O2, p)
1174 return pa_center, pa_through, n
1175 else
1176 return pa_center, pa_through, n
1177 end
1178 end
1179 -- ======= FIN CAS SPÉCIAUX =======
1180
1181 -- --- p sur une bissectrice ---
1182 if lbi:on_line(p) or lbj:on_line(p) then
1183 local bis = lbi:on_line(p) and lbi or lbj
1184 local lp = bis:ortho_from(p)
1185 local pi = intersection_ll_(lp.pa, lp.pb, self.pa, self.pb)
1186
1187 local t1, t2 = intersection_lc_(self.pa, self.pb, pi, p)
1188 if t1 then
1189 local x = self:ortho_from(t1).pb
1190 local u = intersection_ll_(x, t1, s, p)
1191 if u then push(u, t1) end
1192 end
1193 if t2 then
1194 local y = self:ortho_from(t2).pb
1195 local v = intersection_ll_(y, t2, s, p)
1196 if v then push(v, t2) end
1197 end
1198 return pa_center, pa_through, n
1199 end
1200
1201 -- --- cas général ---
1202 local u = lbi:reflection(p)
1203 local v = lbj:reflection(p)
1204 local sp = self:side_line(p)
1205 local su = self:side_line(u)
1206 local pt = (sp == su) and u or v
1207
1208 local centers, throughs, m = self:c_l_pp(p, pt)
1209 push_from_paths(centers, throughs, m)
1210
1211 return pa_center, pa_through, n
1212 -- ======= FIN SÉCANTS =======
1213 end
1214
1215 -- alias conservé
1216 line.c_ll_p = line.LLP
1217
1218
1219
1220 return line