Debian Codegraph @

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

raw
1 -- File: tkz_elements_functions_triangles.lua
2 -- Copyright (c) 2026 Alain Matthes
3 -- SPDX-License-Identifier: LPPL-1.3c
4 -- Maintainer: Alain Matthes
5
6 ---------------------------------------------------------------------------
7 -- triangle center with circle
8 ---------------------------------------------------------------------------
9 ----------------------------
10 ---- boolean ---------------
11 ----------------------------
12 function in_out_(a, b, c, pt, EPS)-- nil (absent or fals or true)
13 EPS = EPS or tkz.epsilon
14 local x, y, z = barycentric_coordinates_(a, b, c, pt)
15 return (x >= -EPS and y >= -EPS and z >= -EPS)
16 end
17
18
19 function in_out_strict_(a, b, c, pt, EPS)-- nil (absent or fals or true)
20 EPS = EPS or tkz.epsilon
21 local x, y, z = barycentric_coordinates_(a, b, c, pt)
22 return (x > EPS and y > EPS and z > EPS)
23 end
24
25 function check_equilateral_(A, B, C, EPS)
26 EPS = EPS or tkz.epsilon
27 local a, b, c = length_(B, C), length_(A, C), length_(A, B)
28
29 -- Vérifie que les trois longueurs sont égales à epsilon près
30 if math.abs(a - b) < EPS and math.abs(a - c) < EPS and math.abs(b - c) < EPS then
31 return true
32 else
33 return false
34 end
35 end
36
37 -- acutangle : triangle with three acute angles.
38 function check_acutangle_(A, B, C, EPS)
39 EPS = EPS or tkz.epsilon
40 local a, b, c = length_(B, C), length_(A, C), length_(A, B)
41
42 local asq = a * a
43 local bsq = b * b
44 local csq = c * c
45 if asq + bsq > csq and bsq + csq > asq and csq + asq > bsq then
46 return true
47 else
48 return false
49 end
50 end
51
52 function check_isosceles_(A, B, C, EPS)
53 EPS = EPS or tkz.epsilon
54 local a, b, c = length_(B, C), length_(A, C), length_(A, B)
55
56 -- si déjà équilatéral
57 if check_equilateral_(A, B, C, EPS) then
58 return true
59 end
60
61 if math.abs(a - b) <= EPS then
62 return true -- sommet C au-dessus de AB
63 elseif math.abs(b - c) <= EPS then
64 return true -- sommet A au-dessus de BC
65 elseif math.abs(c - a) <= EPS then
66 return true -- sommet B au-dessus de CA
67 else
68 return false
69 end
70 end
71 ------------------------
72 ---- string ------------
73 -------------------------
74
75 function find_orientation_(za, zb, zc)
76 local d = oriented_area2_(za.re, za.im, zb.re, zb.im, zc.re, zc.im)
77 if is_zero_(d) then
78 return "aligned"
79 elseif d > 0 then
80 return "direct"
81 else
82 return "indirect"
83 end
84 end
85
86 ------------------------
87 ---- reals ------------
88 -------------------------
89
90 function area_(pa, pb, pc)
91 return point.mod(pa - projection_(pb, pc, pa)) * point.mod(pb - pc) / 2
92 end
93
94 function area_heron_(a, b, c)
95 local s = (a + b + c) / 2
96 return math.sqrt(s * (s - a) * (s - b) * (s - c))
97 end
98
99 function trilinear_to_d_(x, y, z, a, b, c)
100 local k = area_heron_(a, b, c)
101 -- Calculating side distances
102 local p = k * x / a
103 local q = k * y / b
104 local r = k * z / c
105 return p, q, r
106 end
107
108 function barycentric_coordinates_(a, b, c, pt)
109 local xA,yA,xB,yB,xC,yC = a.re,a.im,b.re,b.im,c.re,c.im
110 local xP,yP = pt.re, pt.im
111 local orient2d_ABC = oriented_area2_(xA,yA,xB,yB,xC,yC)
112
113 if is_zero_(orient2d_ABC) then
114 tex.error("Degenerate triangle: vertices aligned (barycentric).")
115 return
116 end
117
118 local l_A = oriented_area2_(xP,yP,xB,yB,xC,yC) / orient2d_ABC
119 local l_B = oriented_area2_(xA,yA,xP,yP,xC,yC) / orient2d_ABC
120 local l_C = oriented_area2_(xA,yA,xB,yB,xP,yP) / orient2d_ABC
121 return l_A, l_B, l_C
122 end
123
124
125 function trilinear_coordinates_(a, b, c, pt)
126 local la, lb, lc = barycentric_coordinates_(a, b, c, pt)
127 local ca, cb, cc = length_(b, c), length_(a, c), length_(a, b)
128 local ta, tb, tc = la / ca, lb / cb, lc / cc
129 -- normalize
130 local N = ta + tb + tc
131 return ta / N, tb / N, tc / N
132 end
133
134 function radius_excircle_(a, b, c)
135 local ea = trilinear_(a, b, c, -1, 1, 1)
136 local ra = length_(ea, projection_(a, c, ea))
137 local eb = trilinear_(a, b, c, 1, -1, 1)
138 local rb = length_(eb, projection_(c, a, eb))
139 local ec = trilinear_(a, b, c, 1, 1, -1)
140 local rc = length_(ec, projection_(a, b, ec))
141 return ra, rb, rc
142 end
143
144 function find_angle_(a, b, c)
145 if det3pt_(a, b, c) > 0 then
146 return angle_normalize_(point.arg((c - a) / (b - a)))
147 else
148 return angle_normalize_(point.arg((b - a) / (c - a)))
149 end
150 end
151
152 ------------------------
153 -- Points --
154 ------------------------
155
156 function find_foot_bisector_(a, b, c)
157 local i = in_center_(a, b, c)
158 return intersection_ll_(a,i,b,c)
159 end
160
161 function barycentric_(pa, pb, pc, x, y, z)
162 local a = length_(pb, pc)
163 local b = length_(pa, pc)
164 local c = length_(pa, pb)
165 return barycenter_({ pa, x }, { pb, y }, { pc, z })
166 end
167
168 function trilinear_(pa, pb, pc, x, y, z)
169 local a = length_(pb, pc)
170 local b = length_(pa, pc)
171 local c = length_(pa, pb)
172 return barycenter_({ pa, x * a }, { pb, y * b }, { pc, z * c })
173 end
174
175 function circum_center_(a, b, c)
176 local ka = math.sin(2 * get_angle_(a, b, c))
177 local kb = math.sin(2 * get_angle_(b, c, a))
178 local kc = math.sin(2 * get_angle_(c, a, b))
179 return barycenter_({ a, ka }, { b, kb }, { c, kc })
180 end
181
182 function in_center_(a, b, c)
183 local ka = point.abs(b - c)
184 local kc = point.abs(b - a)
185 local kb = point.abs(c - a)
186 return barycenter_({ a, ka }, { b, kb }, { c, kc })
187 end
188
189 function ex_center_(a, b, c)
190 local ka = point.abs(b - c)
191 local kc = point.abs(b - a)
192 local kb = point.abs(c - a)
193 return barycenter_({ a, -ka }, { b, kb }, { c, kc })
194 end
195
196 function centroid_(a, b, c)
197 return barycenter_({ a, 1 }, { b, 1 }, { c, 1 })
198 end
199 centroid_center_ = centroid_
200
201 function ortho_center_(a, b, c)
202 local ka = math.tan(get_angle_(a, b, c))
203 local kb = math.tan(get_angle_(b, c, a))
204 local kc = math.tan(get_angle_(c, a, b))
205 return barycenter_({ a, ka }, { b, kb }, { c, kc })
206 end
207
208 function euler_center_(a, b, c)
209 local ma, mb, mc = medial_tr_(a, b, c)
210 return circum_center_(ma, mb, mc)
211 end
212
213 function gergonne_point_(a, b, c)
214 local u, v, w
215 u, v, w = intouch_tr_(a, b, c)
216 return intersection_ll_(a, u, b, v)
217 end
218
219 function lemoine_point_(a, b, c)
220 local ma, mb, mc, ha, hb, hc, u, v, w
221 u = point.abs(c - b)
222 v = point.abs(a - c)
223 w = point.abs(b - a)
224 return barycenter_({ a, u * u }, { b, v * v }, { c, w * w })
225 end
226
227 function nagel_point_(a, b, c)
228 -- Calculate the excircle tangency points (u, v, w)
229 local u, v, w = extouch_tr_(a, b, c)
230
231 -- Find the intersection of lines through a and u, and through b and v
232 return intersection_ll_(a, u, b, v)
233 end
234
235 function feuerbach_point_(a, b, c)
236 local i, h, e, ma
237
238 -- Calculate the incenter and some related point (likely the orthocenter or another center)
239 i, h = in_circle_(a, b, c)
240
241 -- Calculate the Euler center (center of the nine-point circle)
242 e = euler_center_(a, b, c)
243
244 -- Calculate the midpoint of side BC
245 ma = (b + c) / 2
246
247 -- Find the intersection of the circles at (i, h) and (e, ma), which gives the Feuerbach point
248 return intersection_cc_(i, h, e, ma)
249 end
250
251 function spieker_center_(a, b, c)
252 return in_center_(medial_tr_(a, b, c))
253 end
254
255 function euler_points_(a, b, c)
256 local H
257 H = ortho_center_(a, b, c)
258 return midpoint_(H, a), midpoint_(H, b), midpoint_(H, c)
259 end
260
261 function parallelogram_(a, b, c)
262 local x = c + a - b
263 return x
264 end
265
266 function soddy_center_(a, b, c)
267 -- Step 1: Compute the incenter and excircle centers
268 local i, e, f, g = in_circle_(a, b, c)
269 local ha, hb, hc = orthic_tr_(a, b, c)
270
271 -- Step 2: Find the intersection points for the tangent lines
272 local x, xp = intersection_lc_(a, ha, a, g)
273 if point.mod(ha - x) < point.mod(ha - xp) then
274 else
275 x, xp = xp, x
276 end
277
278 local y, yp = intersection_lc_(b, hb, b, e)
279 if point.mod(hb - y) < point.mod(hb - yp) then
280 else
281 y, yp = yp, y
282 end
283
284 local z, zp = intersection_lc_(c, hc, c, f)
285 if point.mod(hc - z) < point.mod(hc - zp) then
286 else
287 z, zp = zp, z
288 end
289
290 -- Step 3: Calculate the intersections with the opposite triangle sides
291 local xi, t = intersection_lc_(xp, e, a, g)
292 if in_out_(a, b, c, xi) then
293 else
294 xi, t = t, xi
295 end
296
297 local yi, t = intersection_lc_(yp, f, b, e)
298 if in_out_(a, b, c, yi) then
299 else
300 yi, t = t, yi
301 end
302
303 local zi, t = intersection_lc_(zp, g, c, f)
304 if in_out_(a, b, c, zi) then
305 else
306 zi, t = t, zi
307 end
308
309 -- Step 4: Calculate the circumcenter of the triangle formed by the tangent points
310 local s = circum_center_(xi, yi, zi)
311
312 return s, xi, yi, zi -- Return the Soddy center and the tangent points
313 end
314 --------------------
315 -- lines --
316 --------------------
317 -- N,G,H,O
318 function euler_line_(a, b, c)
319 -- ---- equilateral: all centers coincide, Euler line degenerates to a point
320 if check_equilateral_(a, b, c, EPS) then
321 local g = centroid_(a, b, c)
322 return g, g, g, g
323 end
324
325 local A = math.tan(get_angle_(a, b, c))
326 local B = math.tan(get_angle_(b, c, a))
327 local C = math.tan(get_angle_(c, a, b))
328
329 return euler_center_(a, b, c),
330 barycenter_({ a, 1 }, { b, 1 }, { c, 1 }),
331 barycenter_({ a, A }, { b, B }, { c, C }),
332 barycenter_({ a, B + C }, { b, A + C }, { c, A + B })
333 end
334
335 function bisector_(a, b, c) -- possible intersection bisector with side
336 return in_center_(a, b, c)
337 end
338
339 function bisector_ext_(a, b, c)
340 local i
341 i = in_center_(a, b, c)
342 return rotation_(a, math.pi / 2, i)
343 end
344
345 function mediators_(a, b, c)
346 local o = circum_center_(a, b, c)
347 return o, projection_(b, c, o), projection_(a, c, o), projection_(a, b, o)
348 end
349
350 --------------------
351 -- circles --
352 --------------------
353 function circum_circle_(a, b, c)
354 local ka = math.sin(2 * get_angle_(a, b, c))
355 local kb = math.sin(2 * get_angle_(b, c, a))
356 local kc = math.sin(2 * get_angle_(c, a, b))
357 return barycenter_({ a, ka }, { b, kb }, { c, kc }), a
358 end
359
360 function in_circle_(a, b, c)
361 local ka, kb, kc, o
362 ka = point.abs(b - c)
363 kc = point.abs(b - a)
364 kb = point.abs(c - a)
365 o = barycenter_({ a, ka }, { b, kb }, { c, kc })
366 return o, projection_(b, c, o), projection_(a, c, o), projection_(a, b, o)
367 end
368
369 function ex_circle_(a, b, c)
370 local ka, kb, kc, o
371 ka = point.abs(b - c)
372 kc = point.abs(b - a)
373 kb = point.abs(c - a)
374 o = barycenter_({ a, -ka }, { b, kb }, { c, kc })
375 return o, projection_(b, c, o), projection_(a, c, o), projection_(b, a, o)
376 end
377
378 function euler_circle_(a, b, c)
379 local o, ma, mb, mc, H, ha, hb, hc
380 -- Compute the Euler center (center of the nine-point circle)
381 o = euler_center_(a, b, c)
382
383 -- Calculate the medial triangle (midpoints of the sides)
384 ma, mb, mc = medial_tr_(a, b, c)
385
386 -- Calculate the orthic triangle (feet of the altitudes)
387 ha, hb, hc = orthic_tr_(a, b, c)
388
389 -- Get the Euler line and midpoint (H) on the Euler line
390 _, _, H, _ = euler_line_(a, b, c)
391
392 -- Return all relevant geometric elements
393 return o,
394 ma,
395 mb,
396 mc,
397 ha,
398 hb,
399 hc,
400 midpoint_(H, a), -- Midpoint between H and vertex a
401 midpoint_(H, b), -- Midpoint between H and vertex b
402 midpoint_(H, c) -- Midpoint between H and vertex c
403 end
404
405 --------------------
406 -- triangles --
407 --------------------
408 function orthic_tr_(a, b, c)
409 local o = ortho_center_(a, b, c)
410 return projection_(b, c, o), projection_(a, c, o), projection_(b, a, o)
411 end
412
413 function medial_tr_(a, b, c)
414 return barycenter_({ a, 0 }, { b, 1 }, { c, 1 }),
415 barycenter_({ a, 1 }, { b, 0 }, { c, 1 }),
416 barycenter_({ a, 1 }, { b, 1 }, { c, 0 })
417 end
418
419 function anti_tr_(a, b, c)
420 return barycenter_({ a, -1 }, { b, 1 }, { c, 1 }),
421 barycenter_({ a, 1 }, { b, -1 }, { c, 1 }),
422 barycenter_({ a, 1 }, { b, 1 }, { c, -1 })
423 end
424
425 function incentral_tr_(a, b, c)
426 local i, r, s, t
427 -- Compute the incenter (center of the incircle)
428 i = in_center_(a, b, c)
429
430 -- Calculate the points of tangency where the incircle touches the sides
431 r = intersection_ll_(a, i, b, c) -- Intersection of lines a-i and b-c
432 s = intersection_ll_(b, i, a, c) -- Intersection of lines b-i and a-c
433 t = intersection_ll_(c, i, a, b) -- Intersection of lines c-i and a-b
434
435 -- Return the points of tangency that form the incentral triangle
436 return r, s, t
437 end
438
439 function excentral_tr_(a, b, c)
440 local r = trilinear_(a, b, c, -1, 1, 1)
441 local s = trilinear_(a, b, c, 1, -1, 1)
442 local t = trilinear_(a, b, c, 1, 1, -1)
443 return r, s, t
444 end
445
446 function intouch_tr_(a, b, c)
447 local i
448 i = in_center_(a, b, c)
449 return projection_(b, c, i), projection_(a, c, i), projection_(a, b, i)
450 end
451
452 function cevian_(a, b, c, p)
453 return intersection_ll_(a, p, b, c), intersection_ll_(b, p, a, c), intersection_ll_(c, p, a, b)
454 end
455
456 function extouch_tr_(a, b, c)
457 local u, v, w
458 u, v, w = excentral_tr_(a, b, c)
459 return projection_(b, c, u), projection_(a, c, v), projection_(a, b, w)
460 end
461
462 function tangential_tr_(a, b, c)
463 local u, v, w, x, y, z, xx, yy, zz
464 u, v, w = orthic_tr_(a, b, c)
465 x = ll_from_(a, v, w)
466 y = ll_from_(b, u, w)
467 z = ll_from_(c, u, v)
468 xx = intersection_ll_(c, z, b, y)
469 yy = intersection_ll_(a, x, c, z)
470 zz = intersection_ll_(a, x, b, y)
471 return xx, yy, zz
472 end
473
474 function feuerbach_tr_(a, b, c)
475 local e, m, ja, ha, jb, hb, jc, hc
476 e = euler_center_(a, b, c)
477 m = midpoint_(b, c)
478 ja, ha = ex_circle_(a, b, c)
479 jb, hb = ex_circle_(b, c, a)
480 jc, hc = ex_circle_(c, a, b)
481 return intersection_cc_(e, m, ja, ha), intersection_cc_(e, m, jb, hb), intersection_cc_(e, m, jc, hc)
482 end
483
484 function orthic_axis_(a, b, c)
485 local ha, hb, hc = orthic_tr_(a, b, c)
486 local z = intersection_ll_(ha, hb, a, b)
487 local y = intersection_ll_(ha, hc, a, c)
488 local x = intersection_ll_(hb, hc, b, c)
489 return x, y, z
490 end
491 --------------------
492 -- ellipse --
493 --------------------
494 function steiner_(a, b, c)
495 local g = centroid_(a, b, c)
496 local delta = a * a + b * b + c * c - a * b - a * c - b * c
497 local fa = (a + b + c - point.sqrt(delta)) / 3
498 local fb = (a + b + c + point.sqrt(delta)) / 3
499 local m = midpoint_(b, c)
500 local r = (length_(fa, m) + length_(fb, m)) / 2
501 return conic:new(EL_bifocal(fb, fa, r))
502 end
503 --------------------
504 -- miscellanous --
505 --------------------
506
507 function square_inscribed_(a, b, c)
508 local d, e = square_(c, b)
509 local m = intersection_ll_(a, d, b, c)
510 local n = intersection_ll_(a, e, b, c)
511 local o, p = square_(m, n)
512 return m, n, o, p
513 end
514
515 function resolve_triangle_index(self, arg)
516 local a, b, c = self.pa, self.pb, self.pc
517
518 if arg == nil or arg == 0 then
519 return 0
520 elseif type(arg) == "number" then
521 return arg % 3
522 elseif type(arg) == "table" and arg.re and arg.im then
523 if rawequal(arg, a) then
524 return 0
525 elseif rawequal(arg, b) then
526 return 1
527 elseif rawequal(arg, c) then
528 return 2
529 else
530 tex.error("Invalid vertex (not the stored triangle vertex object)")
531 end
532 else
533 tex.error("Invalid argument for triangle index")
534 end
535 end
536