Debian Codegraph @

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

raw
1 -- File: tkz_elements_functions_conics.lua
2 -- Copyright (c) 2026 Alain Matthes
3 -- SPDX-License-Identifier: LPPL-1.3c
4 -- Maintainer: Alain Matthes
5
6 function get_a(h, e)
7 if math.abs(e - 1) < tkz.epsilon then
8 return nil
9 elseif e < 1 then
10 return e * h / (1 - e ^ 2)
11 elseif e > 1 then
12 return e * h / (e ^ 2 - 1)
13 end
14 end
15
16 function get_b(h, e)
17 if math.abs(e - 1) < tkz.epsilon then
18 return nil
19 elseif e < 1 then
20 return e * h / math.sqrt(1 - e ^ 2)
21 elseif e > 1 then
22 return e * h / math.sqrt(e ^ 2 - 1)
23 end
24 end
25
26 function get_c(h, e)
27 if math.abs(e - 1) < tkz.epsilon then
28 return nil
29 elseif e < 1 then
30 return e ^ 2 * h / (1 - e ^ 2)
31 elseif e > 1 then
32 return e ^ 2 * h / (e ^ 2 - 1)
33 end
34 end
35
36 function next_focus(F, K, e, h)
37 if math.abs(e - 1) < tkz.epsilon then
38 return nil
39 elseif e > 1 then
40 return report_(F, K, 2 * get_c(h, e))
41 elseif e < 1 then
42 return report_(F, K, -2 * get_c(h, e))
43 end
44 end
45
46 function conic_center(F, K, e, h)
47 if math.abs(e - 1) < tkz.epsilon then
48 return nil
49 elseif e > 1 then
50 return report_(F, K, get_c(h, e))
51 elseif e < 1 then
52 return report_(F, K, -get_c(h, e))
53 end
54 end
55
56 function get_subtype(e)
57 if math.abs(e - 1) < tkz.epsilon then
58 return "parabola"
59 elseif e < 1 then
60 return "ellipse"
61 elseif e > 1 then
62 return "hyperbola"
63 end
64 end
65
66 function get_vertex(F, K, e, h)
67 if math.abs(e - 1) < tkz.epsilon then
68 return report_(K, F, h / 2)
69 else
70 local center = conic_center(F, K, e, h)
71 return report_(center, F, get_a(h, e))
72 end
73 end
74
75 function get_covertex(F, K, e, h)
76 if math.abs(e - 1) < tkz.epsilon then
77 return nil
78 else
79 local center = conic_center(F, K, e, h)
80 return report_(center, ortho_from_(center, center, F), get_b(h, e))
81 end
82 end
83
84 function get_minor_axis(F, K, e, h)
85 if math.abs(e - 1) < tkz.epsilon then
86 return nil
87 else
88 local center = conic_center(F, K, e, h)
89 local pa = get_covertex(F, K, e, h)
90 local pb = symmetry_(center, pa)
91 return line:new(pa, pb)
92 end
93 end
94
95 ---------------------------------------------------------------------------
96 ---------------------------------------------------------------------------
97
98 function get_points_conic_(Co, ta, tb, nb)
99 if math.abs(Co.e - 1) < tkz.epsilon then
100 return get_points_parabola(Co, ta, tb, nb)
101 elseif Co.e > 1 then
102 return get_points_hyperbola(Co, ta, tb, nb)
103 elseif Co.e < 1 then
104 return get_points_ellipse(Co, ta, tb, nb)
105 end
106 end
107
108 function get_points_sym_conic_(Co, ta, tb, nb)
109 return get_points_hyperbola_sym(Co, ta, tb, nb)
110 end
111
112 function get_points_parabola(C, ta, tb, nb)
113 local points = {}
114 --Function to add points in a given range
115 for t = ta, tb, 1 / nb do
116 local T = C.directrix:report(t, C.K)
117 local LL = C.major_axis:ll_from(T)
118 local x, y = mediator_(C.Fa, T)
119 local pt = intersection_ll_(x, y, LL.pa, LL.pb)
120 table.insert(points, "(" .. checknumber_(pt.re) .. "," .. checknumber_(pt.im) .. ")")
121 end
122 return points
123 end
124
125 function get_points_hyperbola(C, ta, tb, nb)
126 local points = {}
127 local LC = C.minor_axis
128 local LS = LC:ll_from(C.vertex)
129 for t = ta, tb, 1 / nb do
130 local T = C.directrix:report(t, C.K)
131 local LT = C.major_axis:ll_from(T)
132 local D = intersection_ll_(LC.pa, LC.pb, C.Fa, T)
133 local E = intersection_ll_(LS.pa, LS.pb, C.Fa, T)
134 local P, Q = intersection_lc_(LT.pa, LT.pb, D, E)
135 if length_(P, C.Fa) > length_(Q, C.Fa) then
136 P, Q = Q, P
137 end
138 table.insert(points, "(" .. checknumber_(P.re) .. "," .. checknumber_(P.im) .. ")")
139 end
140 return points
141 end
142
143 function get_points_hyperbola_sym(C, ta, tb, nb)
144 local points = {}
145 local LC = C.minor_axis
146 local LS = LC:ll_from(C.vertex)
147 for t = ta, tb, 1 / nb do
148 local T = C.directrix:report(t, C.K)
149 local LT = C.major_axis:ll_from(T)
150 local D = intersection_ll_(LC.pa, LC.pb, C.Fa, T)
151 local E = intersection_ll_(LS.pa, LS.pb, C.Fa, T)
152 local M, N = intersection_lc_(LT.pa, LT.pb, D, E)
153 local P = symmetry_(C.center, M)
154 local Q = symmetry_(C.center, N)
155 if length_(P, C.Fb) > length_(Q, C.Fb) then
156 P, Q = Q, P
157 end
158 table.insert(points, "(" .. checknumber_(P.re) .. "," .. checknumber_(P.im) .. ")")
159 end
160 return points
161 end
162
163 function get_points_ellipse(C, x, y, nb)
164 local points = {}
165 for t = x, y + 1 / nb, 1 / nb do
166 -- point on circle
167 CI = circle:new(C.center, C.vertex)
168 local M = CI:point(t)
169 -- point on ellipse
170 local P = affinity_(C.Fa, C.Fb, C.center, C.covertex, C.b / C.a, M)
171 table.insert(points, "(" .. checknumber_(P.re) .. "," .. checknumber_(P.im) .. ")")
172 end
173 return points
174 end
175 ---------------------------------------------------------------------------
176 ---------------------------------------------------------------------------
177
178 function get_one_point_conic_(C, t)
179 if C.e < 1 then
180 return get_one_point_ellipse(C, t)
181 else
182 local a, b = C.directrix.pa, C.directrix.pb
183 local c, d = C.Fa, C.K
184 local angle = tkz_angle_between_vectors_(a, b, c, d)
185 local newDir = (angle > 0) and line:new(b, a) or line:new(a, b)
186
187 local T = newDir:report(t, C.K)
188 local LT = C.major_axis:ll_from(T)
189
190 if math.abs(C.e - 1) < tkz.epsilon then
191 return get_one_point_parabola(C, T, LT)
192 else
193 return get_one_point_hyperbola(C, T, LT)
194 end
195 end
196 end
197
198 function get_one_point_parabola(C, T, LT)
199 local x, y = mediator_(C.Fa, T)
200 return intersection_ll_(x, y, LT.pa, LT.pb)
201 end
202
203 function get_one_point_hyperbola(C, T, LT)
204 local LC = C.minor_axis
205 local LS = LC:ll_from(C.vertex)
206 local D = intersection_ll_(LC.pa, LC.pb, C.Fa, T)
207 local E = intersection_ll_(LS.pa, LS.pb, C.Fa, T)
208 local P, Q = intersection_lc_(LT.pa, LT.pb, D, E)
209 if length_(P, C.Fa) > length_(Q, C.Fa) then
210 P, Q = Q, P
211 end
212 return P
213 end
214
215 function get_one_point_hyperbola_ii(C, t)
216 local T = C.directrix:report(t, C.K)
217 local LT = C.major_axis:ll_from(T)
218 local p = get_one_point_hyperbola(C, T, LT)
219 local D = C.minor_axis
220 return symmetry_axial_(D.pa, D.pb, p)
221 end
222
223 function get_one_point_ellipse(C, t)
224 -- point on circle
225 CI = circle:new(C.center, C.vertex)
226 local M = CI:point(t)
227 return affinity_(C.Fa, C.Fb, C.center, C.covertex, C.b / C.a, M)
228 end
229
230 function EL_in_out(CO, pt)
231 local d = point.abs(pt - CO.center)
232 local L = line:new(CO.center, pt)
233 local x, y = intersection(L, CO)
234 local dx = point.abs(x - CO.center)
235 if d < dx then
236 return true
237 else
238 return false
239 end
240 end
241
242 function PA_in_out(PA, pt)
243 local D = PA.major_axis
244 local Dp = D:ortho_from(pt)
245 local x, y = intersection(Dp, PA)
246 if x == false then
247 return false
248 else
249 local L = line:new(x, y)
250 return L:on_segment(pt)
251 end
252 end
253
254 function HY_in_out(HY, pt)
255 local D = HY.major_axis
256 local Dp = D:ortho_from(pt)
257 local x, y = intersection(Dp, HY)
258 if x == false then
259 return false
260 else
261 local L = line:new(x, y)
262 return L:on_segment(pt)
263 end
264 end
265
266 function EL_points(center, vertex, covertex)
267 local a = length_(center, vertex)
268 local b = length_(center, covertex)
269 local c = math.sqrt(a ^ 2 - b ^ 2)
270 local F = report_(center, vertex, c)
271 local e = c / a
272 local h = b ^ 2 / c
273 local K = report_(center, F, b ^ 2 / c, F)
274 local axis = line:new(vertex, center)
275 local L = axis:ortho_from(K)
276 return F, L, e
277 end
278
279 function EL_bifocal(Fa, Fb, x)
280 local a
281 if type(x) == "number" then
282 a = x
283 else -- x is a point
284 a = (length_(Fa, x) + length_(Fb, x)) / 2
285 end
286 local center = midpoint_(Fa, Fb)
287 local c = length_(center, Fa)
288 local e = c / a
289 local b = math.sqrt(a ^ 2 - c ^ 2)
290 local h = b ^ 2 / c
291 local K = report_(center, Fa, b ^ 2 / c, Fa)
292 local vertex = report_(center, Fa, a)
293 local axis = line:new(vertex, center)
294 local L = axis:ortho_from(K)
295 return Fa, L, e
296 end
297
298 function HY_bifocal(Fa, Fb, x)
299 local a
300 if type(x) == "number" then
301 a = x
302 else -- x is a point
303 a = math.abs((length_(Fa, x) - length_(Fb, x))) / 2
304 end
305 local center = midpoint_(Fa, Fb)
306 local c = length_(center, Fa)
307 local e = c / a
308 local b = math.sqrt(c ^ 2 - a ^ 2)
309 local h = b ^ 2 / c
310 local K = report_(center, Fa, -b ^ 2 / c, Fa)
311 local vertex = report_(center, Fa, a)
312 local axis = line:new(vertex, center)
313 local L = axis:ortho_from(K)
314 return Fa, L, e
315 end
316
317 function PA_dir(F, A, B)
318 local CA = circle:new(A, F)
319 local CB = circle:new(B, F)
320 return CA:common_tangent(CB)
321 end
322
323 function PA_focus(D, pA, pB)
324 local HA = D:projection(pA)
325 local HB = D:projection(pB)
326 local x, y = intersection_cc_(pA, HA, pB, HB)
327 if x == false then
328 tex.error("An error has occurred. Bad configuration")
329 return
330 else
331 return x, y
332 end
333 end
334
335 function search_center_ellipse(coefficients)
336 local A, B, C, D, E, F =
337 coefficients.A, coefficients.B, coefficients.C, coefficients.D, coefficients.E, coefficients.F
338 local delta = B ^ 2 - 4 * A * C
339 local xc = (2 * C * D - B * E) / delta
340 local yc = (2 * A * E - B * D) / delta
341 return point:new(xc, yc)
342 end
343
344 function matrix_conic(coefficients)
345 local A, B, C, D, E, F =
346 coefficients.A, coefficients.B, coefficients.C, coefficients.D, coefficients.E, coefficients.F
347 return matrix.square(3, A, B, D, B, C, E, D, E, F)
348 end
349
350 function search_ellipse(...)
351 local function create_coordinates_table(...)
352 local T = {}
353 local pointNames = { ... }
354
355 for i, name in ipairs(pointNames) do
356 T[i] = { z[name].re, z[name].im }
357 end
358 return T
359 end
360 Names = { ... }
361 points = create_coordinates_table(table.unpack(Names))
362 local ma = matrix.create(5, 6)
363 for i, point in ipairs(points) do
364 local x, y = point[1], point[2]
365 ma.set[i][1] = x * x
366 ma.set[i][2] = x * y
367 ma.set[i][3] = y * y
368 ma.set[i][4] = x
369 ma.set[i][5] = y
370 ma.set[i][6] = 1
371 end
372 local mm = ma:gauss_jordan()
373 local coefficients = {}
374 for i = 1, 5 do
375 coefficients[i] = -mm.set[i][6]
376 end
377 table.insert(coefficients, 1)
378 local A = coefficients[1]
379 local B = coefficients[2]
380 local C = coefficients[3]
381 local D = coefficients[4]
382 local E = coefficients[5]
383 local F = coefficients[6]
384 return { A = A, B = B, C = C, D = D, E = E, F = F }
385 end
386
387 function search_ellipse_(...)
388 local function create_coordinates_table_(...)
389 local T = {}
390 local pointNames = { ... }
391
392 for i, name in pairs(pointNames) do
393 T[i] = { name.re, name.im }
394 end
395 return T
396 end
397 Names = { ... }
398 points = create_coordinates_table_(table.unpack(Names))
399 local ma = matrix.create(5, 6)
400 for i, point in ipairs(points) do
401 local x, y = point[1], point[2]
402 ma.set[i][1] = x * x
403 ma.set[i][2] = x * y
404 ma.set[i][3] = y * y
405 ma.set[i][4] = x
406 ma.set[i][5] = y
407 ma.set[i][6] = 1
408 end
409 local mm = ma:gauss_jordan()
410 local coefficients = {}
411 for i = 1, 5 do
412 coefficients[i] = -mm.set[i][6]
413 end
414 table.insert(coefficients, 1)
415 local A = coefficients[1]
416 local B = coefficients[2]
417 local C = coefficients[3]
418 local D = coefficients[4]
419 local E = coefficients[5]
420 local F = coefficients[6]
421 return { A = A, B = B, C = C, D = D, E = E, F = F }
422 end
423
424 function test_angle_major(Center, radius, temp_angle, A, B, C, D, E, F)
425 local new_angle = 0
426 local V = Center + point(math.cos(temp_angle), math.sin(temp_angle))
427 local test_point = report_(Center, V, radius)
428 if math.abs(test_ellipse(test_point.re, test_point.im, A, B, C, D, E, F)) < tkz.epsilon then
429 new_angle = temp_angle
430 else
431 new_angle = temp_angle + math.pi / 2
432 end
433 return new_angle
434 end
435
436 -- Utility function to calculate ellipse axes and angles
437 function ellipse_axes_angle(coefficients)
438 local A, B, C, D, E, F =
439 coefficients.A, coefficients.B, coefficients.C, coefficients.D, coefficients.E, coefficients.F
440
441 local discriminant = 4 * A * C - B * B
442 if math.abs(discriminant) < tkz.epsilon then
443 tex.error("Degenerate ellipse (discriminant close to zero)")
444 end
445
446 local radicand_ra = 2
447 * (A * E ^ 2 + C * D ^ 2 - B * D * E + (B ^ 2 - 4 * A * C) * F)
448 * ((A + C) + math.sqrt((A - C) ^ 2 + B ^ 2))
449 local radicand_rb = 2
450 * (A * E ^ 2 + C * D ^ 2 - B * D * E + (B ^ 2 - 4 * A * C) * F)
451 * ((A + C) - math.sqrt((A - C) ^ 2 + B ^ 2))
452
453 if radicand_ra < 0 or radicand_rb < 0 then
454 tex.error("Negative square root in ellipse axis calculation")
455 end
456
457 local ra = math.sqrt(radicand_ra) / discriminant
458 local rb = math.sqrt(radicand_rb) / discriminant
459 local center = search_center_ellipse(coefficients)
460 local angle = 0
461 if math.abs(A - C) < tkz.epsilon then
462 angle = math.pi / 4
463 else
464 local temp_angle = 0.5 * math.atan(B / (A - C))
465 local V = center + point(math.cos(temp_angle), math.sin(temp_angle))
466 local test_point = report_(center, V, ra)
467 if math.abs(test_ellipse(test_point, coefficients)) < tkz.epsilon then
468 angle = temp_angle
469 else
470 angle = temp_angle + math.pi / 2
471 end
472 end
473 return center, ra, rb, angle
474 end
475
476 function define_ellipse(coefficients)
477 local center, ra, rb, angle = ellipse_axes_angle(coefficients)
478 local vertex, covertex = EL_radii(center, ra, rb, angle)
479 return EL_points(center, vertex, covertex)
480 end
481
482 -- Utility function for testing a point relative to the ellipselocal
483 function test_ellipse(pt, coefficients)
484 local x, y = pt:get()
485 local A, B, C, D, E, F =
486 coefficients.A, coefficients.B, coefficients.C, coefficients.D, coefficients.E, coefficients.F
487 return A * x ^ 2 + B * x * y + C * y ^ 2 + D * x + E * y + F
488 end
489
490 -- Function for creating ellipses from points and axes.
491 function EL_radii(center_point, ra, rb, angle)
492 local P = center_point + point(math.cos(angle), math.sin(angle))
493 local Q = center_point + point(-math.sin(angle), math.cos(angle))
494 -- get the conic with center, vertex and covertex
495 -- P and Q with good directions
496 local V, CV = report_(center_point, P, ra), report_(center_point, Q, rb)
497 return EL_points(center_point, V, CV)
498 end
499
500 function inverse_affinity_ellipse(C, z)
501 local ortho = C.major_axis:ortho_from(z)
502 local CI = circle:new(C.center, C.vertex)
503 return intersection(CI, ortho, { near = z })
504 end
505
506
507 function EL_five_points(z1,z2,z3,z4,z5)
508 local coefficients = search_ellipse_(z1,z2,z3,z4,z5)
509 if coefficients == nil then
510 tex.error("ellipse_inscribed: unable to determine the conic.")
511 return nil
512 end
513
514 local center, ra, rb, angle = ellipse_axes_angle(coefficients)
515 if not center then
516 tex.error("EL_ellipse_five_points: impossible to determine ellipse data.")
517 return nil
518 end
519
520 return EL_radii(center, ra, rb, angle)
521 end