| 1 |
-- File: tkz_elements_conic.lua |
| 2 |
-- Copyright (c) 2026 Alain Matthes |
| 3 |
-- SPDX-License-Identifier: LPPL-1.3c |
| 4 |
-- Maintainer: Alain Matthes |
| 5 |
|
| 6 |
--------------------------------------------------------------------------- |
| 7 |
-- conics |
| 8 |
--------------------------------------------------------------------------- |
| 9 |
----------------------------- |
| 10 |
----- Conic parameters |
| 11 |
----------------------------- |
| 12 |
|
| 13 |
-- foci, directrix and eccentricity are the main parameters and are used to define all conics. |
| 14 |
|
| 15 |
-------------------- General parameters common to all conics ------------- |
| 16 |
-- (e) is eccentricity e = 1 for parabola; e = p / h |
| 17 |
|
| 18 |
-- (h) distance between the focus and the directrix (sometime called f) ; h = p / e ellipse and hyperbola and h = p for parabola |
| 19 |
|
| 20 |
-- (p) semi-latus rectum (The latus rectum is the chord parallel to the directrix and passing through a focus) It's also called, the focal parameter (distance from a focus to the corresponding directrix.) |
| 21 |
|
| 22 |
-- vertex We can define the distance between the vertex and the directrix or the distance between the vertex and the focus p / (e + 1) so p / 2 for parabola; (e * h) / (e + 1) = h * e / (e + 1) |
| 23 |
|
| 24 |
-- major_axis : The principal axis is the line joining the foci of an ellipse or hyperbola, and its midpoint is the curve's center. A parabola has no center, in this case, the major_axis is the line joining the focus and the vertex. |
| 25 |
|
| 26 |
-- parabola has no center and one focus. c/a for hyperbola and ellipse |
| 27 |
-- (c) is the distance between the center and a focus. (line eccentricity) |
| 28 |
|
| 29 |
-- for ellipse |
| 30 |
-- a semi_major_awis distance between center and vertex |
| 31 |
-- b semi_mminor_axis distance between center and covertex |
| 32 |
-- c = math.sqrt(a^2 - b^2) and p = b^2 / a |
| 33 |
-- h = b^2 / c |
| 34 |
|
| 35 |
-- for hyperbola |
| 36 |
-- a semi_major_awis distance between center and vertex |
| 37 |
-- b semi_mminor_axis distance between center and covertex b/a slope of asymptote |
| 38 |
-- c = math.sqrt(a^2 - b^2) and p = b^2 / a |
| 39 |
-- h = b^2 / c |
| 40 |
--------------------------------------------------------------------------- |
| 41 |
conic = {} |
| 42 |
conic.__index = conic |
| 43 |
function conic:new(Fa, Di, ecc) -- focus, directrix, eccentricity |
| 44 |
local type = "conic" |
| 45 |
local e = ecc |
| 46 |
local subtype = get_subtype(e) |
| 47 |
local K = projection_(Di.pa, Di.pb, Fa) |
| 48 |
local h = length_(K, Fa) |
| 49 |
local a = get_a(h, e) |
| 50 |
local b = get_b(h, e) -- only hy and el |
| 51 |
local c = get_c(h, e) |
| 52 |
local p = e * h -- demi latus rectum if hy and el b^2/a |
| 53 |
local slope = slope_(K, Fa) |
| 54 |
local major_axis = line:new(K, Fa) |
| 55 |
local directrix = Di |
| 56 |
local vertex = get_vertex(Fa, K, e, h) |
| 57 |
-- pb No center, no covertex, no Fb with parabola |
| 58 |
local center = conic_center(Fa, K, e, h) |
| 59 |
local Fb = next_focus(Fa, K, e, h) |
| 60 |
local covertex = get_covertex(Fa, K, e, h) |
| 61 |
local minor_axis = get_minor_axis(Fa, K, e, h) |
| 62 |
local o = { |
| 63 |
type = type, |
| 64 |
subtype = subtype, |
| 65 |
K = K, |
| 66 |
e = e, |
| 67 |
h = h, |
| 68 |
a = a, |
| 69 |
b = b, |
| 70 |
c = c, |
| 71 |
p = p, |
| 72 |
Rx = a, |
| 73 |
Ry = b, |
| 74 |
Fa = Fa, |
| 75 |
Fb = Fb, |
| 76 |
center = center, |
| 77 |
vertex = vertex, |
| 78 |
covertex = covertex, |
| 79 |
major_axis = major_axis, |
| 80 |
minor_axis = minor_axis, |
| 81 |
directrix = directrix, |
| 82 |
slope = slope, |
| 83 |
} |
| 84 |
setmetatable(o, self) |
| 85 |
return o |
| 86 |
end |
| 87 |
|
| 88 |
setmetatable(conic, { |
| 89 |
__call = function(cls, ...) |
| 90 |
return cls:new(...) |
| 91 |
end, |
| 92 |
}) |
| 93 |
|
| 94 |
function conic:get(i) |
| 95 |
local p1, p2, p3 |
| 96 |
|
| 97 |
if self.subtype == "parabola" then |
| 98 |
p1 = self.vertex |
| 99 |
p2 = self.Fa |
| 100 |
p3 = nil |
| 101 |
else |
| 102 |
p1 = self.center |
| 103 |
p2 = self.Fa |
| 104 |
p3 = self.Fb |
| 105 |
end |
| 106 |
|
| 107 |
if i == nil then |
| 108 |
return p1, p2, p3 |
| 109 |
elseif i == 1 then |
| 110 |
return p1 |
| 111 |
elseif i == 2 then |
| 112 |
return p2 |
| 113 |
elseif i == 3 then |
| 114 |
return p3 |
| 115 |
else |
| 116 |
return nil |
| 117 |
end |
| 118 |
end |
| 119 |
|
| 120 |
|
| 121 |
function conic:points(ta, tb, nb, swap) |
| 122 |
swap = (swap == "swap") |
| 123 |
if not swap then |
| 124 |
return path(get_points_conic_(self, ta, tb, nb)) |
| 125 |
else |
| 126 |
return path(get_points_sym_conic_(self, ta, tb, nb)) |
| 127 |
end |
| 128 |
end |
| 129 |
|
| 130 |
function conic:point(t, swap) |
| 131 |
swap = (swap == "swap") |
| 132 |
if not swap then |
| 133 |
return get_one_point_conic_(self, t) |
| 134 |
else |
| 135 |
return get_one_point_hyperbola_ii(self, t) |
| 136 |
end |
| 137 |
end |
| 138 |
|
| 139 |
function conic:antipode(pt) |
| 140 |
local e = self.e |
| 141 |
if math.abs(e - 1) < tkz.epsilon then |
| 142 |
tex.error("Undefined antipode for a parabola.") |
| 143 |
else |
| 144 |
return 2 * self.center - pt |
| 145 |
end |
| 146 |
end |
| 147 |
|
| 148 |
function conic:tangent_at(pt) -- actually only parabola |
| 149 |
local u, v |
| 150 |
local e = self.e |
| 151 |
if math.abs(e - 1) < tkz.epsilon then -- Parabola |
| 152 |
local h = self.directrix:projection(pt) |
| 153 |
u = self.vertex:identity(pt) and ll_from_(pt, self.directrix.pa, self.directrix.pb) |
| 154 |
or in_center_(pt, h, self.Fa) |
| 155 |
elseif self.e > 1 then -- Hyperbola |
| 156 |
u = self.vertex:identity(pt) and ll_from_(pt, self.directrix.pa, self.directrix.pb) |
| 157 |
or in_center_(pt, self.Fb, self.Fa) |
| 158 |
elseif self.e < 1 then -- Ellipse |
| 159 |
local zi = in_center_(self.Fa, pt, self.Fb) |
| 160 |
u = pt + (zi - pt) * point(0, 1) |
| 161 |
end |
| 162 |
|
| 163 |
u = normalize_(pt, u) |
| 164 |
v = pt:symmetry(u) |
| 165 |
|
| 166 |
return line:new(u, v) |
| 167 |
end |
| 168 |
|
| 169 |
-- Intersection entre une parabole et une droite |
| 170 |
function conic:inter_Pa_line(pa, pb) |
| 171 |
local function solve_para_line(p, m, n) |
| 172 |
return tkz.solve_quadratic_(1, -2 * p * m, -2 * p * n) |
| 173 |
end |
| 174 |
|
| 175 |
-- Crée un système de coordonnées basé sur l'axe majeur et le sommet de la parabole |
| 176 |
local sys = occs:new(self.major_axis, self.vertex) |
| 177 |
|
| 178 |
-- Calcule les coordonnées des points pa et pb dans ce système |
| 179 |
local Xa, Ya = sys:coordinates(pa) |
| 180 |
local Xb, Yb = sys:coordinates(pb) |
| 181 |
|
| 182 |
-- Résout l'intersection entre la parabole et la droite définie par les points pa et pb |
| 183 |
local r1, r2 = solve_para_line(self.h, tkz.line_coefficients(Xa, Ya, Xb, Yb)) |
| 184 |
-- Si les résultats de l'intersection sont invalides (false), retourne deux faux |
| 185 |
if r1 == false then |
| 186 |
local s1 = false |
| 187 |
local s2 = false |
| 188 |
return s1, s2 |
| 189 |
else |
| 190 |
-- Sinon, calcule les points d'intersection |
| 191 |
local s1, s2 = self:point(r1), self:point(r2) |
| 192 |
|
| 193 |
-- Retourne les points d'intersection dans l'ordre croissant de distance par rapport à pa |
| 194 |
if length_(pa, s1) < length_(pa, s2) then |
| 195 |
return s1, s2 |
| 196 |
else |
| 197 |
return s2, s1 |
| 198 |
end |
| 199 |
end |
| 200 |
end |
| 201 |
|
| 202 |
function conic:tangent_from(pt) |
| 203 |
if math.abs(self.e - 1) < tkz.epsilon then -- |
| 204 |
local sys = occs:new(self.major_axis, self.vertex) |
| 205 |
local Xb, Yb = sys:coordinates(pt) |
| 206 |
local p1, p2 = tkz.solve_quadratic_(self.h, -2 * Xb, 2 * Yb) |
| 207 |
local s1 = self:point(self.h * p1) |
| 208 |
local s2 = self:point(self.h * p2) |
| 209 |
return line:new(pt, s1), line:new(pt, s2) |
| 210 |
elseif self.e > 1 then -- Hyperbola |
| 211 |
local C = circle:radius(self.Fb, 2 * self.a) |
| 212 |
local m, n = intersection_cc_(pt, self.Fa, self.Fb, C.through) |
| 213 |
local u, v = mediator_(m, self.Fa) |
| 214 |
local x, y = mediator_(n, self.Fa) |
| 215 |
local T1, T2 = line:new(u, v), line:new(x, y) |
| 216 |
local d1, d2 = T1:distance(self.Fa), T2:distance(self.Fa) |
| 217 |
if d2 < d1 then |
| 218 |
T2, T1 = T1, T2 |
| 219 |
end |
| 220 |
local Fbsym = symmetry_axial_(T1.pa, T1.pb, self.Fb) |
| 221 |
local t1 = intersection_ll_(Fbsym, self.Fa, T1.pa, T1.pb) |
| 222 |
local Fasym = symmetry_axial_(T2.pa, T2.pb, self.Fa) |
| 223 |
local t2 = intersection_ll_(Fasym, self.Fb, T2.pa, T2.pb) |
| 224 |
return line:new(pt, t1), line:new(pt, t2) |
| 225 |
elseif self.e < 1 then -- Ellipse |
| 226 |
local w = report_(self.Fb, self.Fa, 2 * self.a) |
| 227 |
local s1, s2 = intersection_cc_(pt, self.Fa, self.Fb, w) |
| 228 |
local u, v = mediator_(s1, self.Fa) |
| 229 |
local U = intersection_ll_(u, v, self.Fb, s1) |
| 230 |
u, v = mediator_(s2, self.Fa) |
| 231 |
local V = intersection_ll_(u, v, self.Fb, s2) |
| 232 |
return line:new(pt, U), line:new(pt, V) |
| 233 |
end |
| 234 |
end |
| 235 |
|
| 236 |
-- intersection line hyperbola |
| 237 |
function conic:inter_Hy_line(pa, pb) |
| 238 |
local function hyp_fct(x) |
| 239 |
return self.a * math.sqrt(1 + (x ^ 2) / self.b ^ 2) |
| 240 |
end |
| 241 |
|
| 242 |
local function solve_hyper_line(a, b, m, p) |
| 243 |
local A = a ^ 2 / b ^ 2 - m ^ 2 |
| 244 |
local B = -2 * m * p |
| 245 |
local C = a ^ 2 - p ^ 2 |
| 246 |
return tkz.solve_quadratic_(A, B, C) |
| 247 |
end |
| 248 |
|
| 249 |
local sys = occs:new(self.major_axis, self.center) |
| 250 |
local XA, YA = sys:coordinates(pa) |
| 251 |
local XB, YB = sys:coordinates(pb) |
| 252 |
if math.abs(XA - XB) < tkz.epsilon then |
| 253 |
local xs = XA |
| 254 |
local fa, c = self.Fa, self.center |
| 255 |
local s1, s2 = hyp_fct(xs), -hyp_fct(xs) |
| 256 |
local wx = report_(self.directrix.pa, self.directrix.pb, xs, report_(c, fa, s1)) |
| 257 |
local wy = report_(self.directrix.pa, self.directrix.pb, xs, report_(c, fa, s2)) |
| 258 |
|
| 259 |
return wx, wy |
| 260 |
else |
| 261 |
local r, s = tkz.line_coefficients(XA, YA, XB, YB) |
| 262 |
local t1, t2 = solve_hyper_line(self.a, self.b, r, s) |
| 263 |
if t1 == false then |
| 264 |
return pa, pb |
| 265 |
else |
| 266 |
local s1, s2 = self:point(t1), self:point(t2) |
| 267 |
|
| 268 |
if r * t2 + s < 0 then |
| 269 |
s2 = self:point(t2, "swap") |
| 270 |
end |
| 271 |
if r * t1 + s < 0 then |
| 272 |
s1 = self:point(t1, "swap") |
| 273 |
end |
| 274 |
|
| 275 |
if length_(pa, s1) < length_(pa, s2) then |
| 276 |
return s1, s2 |
| 277 |
else |
| 278 |
return s2, s1 |
| 279 |
end |
| 280 |
end |
| 281 |
end |
| 282 |
end |
| 283 |
|
| 284 |
-- Fonction pour déterminer si un point est à l'intérieur ou à l'extérieur de la conique |
| 285 |
-- ============================================================ |
| 286 |
-- 1) API COMPATIBLE : in_out -> boolean (historique) |
| 287 |
-- ============================================================ |
| 288 |
function conic:in_out(pt, EPS) |
| 289 |
EPS = EPS or tkz.epsilon |
| 290 |
local e = self.e |
| 291 |
if math.abs(e - 1) <= EPS then -- parabola |
| 292 |
return PA_in_out(self, pt, EPS) -- boolean |
| 293 |
elseif e > 1 then -- hyperbola |
| 294 |
return HY_in_out(self, pt, EPS) -- boolean |
| 295 |
else -- ellipse |
| 296 |
return EL_in_out(self, pt, EPS) -- boolean |
| 297 |
end |
| 298 |
end |
| 299 |
|
| 300 |
|
| 301 |
-- ============================================================ |
| 302 |
-- 2) NOUVELLE API : position -> "IN" | "ON" | "OUT" |
| 303 |
-- ============================================================ |
| 304 |
function conic:position(pt, EPS) |
| 305 |
EPS = EPS or tkz.epsilon |
| 306 |
local e = self.e |
| 307 |
if math.abs(e - 1) <= EPS then |
| 308 |
return PA_position_(self, pt, EPS) |
| 309 |
elseif e > 1 then |
| 310 |
return HY_position_(self, pt, EPS) |
| 311 |
else |
| 312 |
return EL_position_(self, pt, EPS) |
| 313 |
end |
| 314 |
end |
| 315 |
|
| 316 |
|
| 317 |
-- ============================================================ |
| 318 |
-- Ellipse : position |
| 319 |
-- (on utilise le même principe que ton EL_in_out, mais avec ON) |
| 320 |
-- ============================================================ |
| 321 |
function EL_position_(CO, pt, EPS) |
| 322 |
EPS = EPS or tkz.epsilon |
| 323 |
|
| 324 |
local d = point.abs(pt - CO.center) |
| 325 |
local L = line(CO.center, pt) |
| 326 |
local x, y = intersection(L, CO) |
| 327 |
if x == false then return "OUT" end |
| 328 |
|
| 329 |
local dx = point.abs(x - CO.center) |
| 330 |
if y ~= false then |
| 331 |
local dy = point.abs(y - CO.center) |
| 332 |
if dy < dx then dx = dy end |
| 333 |
end |
| 334 |
|
| 335 |
local diff = d - dx |
| 336 |
if math.abs(diff) <= EPS then return "ON" end |
| 337 |
if diff < -EPS then return "IN" else return "OUT" end |
| 338 |
end |
| 339 |
|
| 340 |
|
| 341 |
-- ============================================================ |
| 342 |
-- Parabola/Hyperbola : position |
| 343 |
-- Convention : "IN" si pt est entre les 2 intersections sur la corde |
| 344 |
-- ============================================================ |
| 345 |
local function AX_position_(CO, pt, EPS) |
| 346 |
EPS = EPS or tkz.epsilon |
| 347 |
|
| 348 |
local D = CO.major_axis |
| 349 |
local Dp = D:ortho_from(pt) |
| 350 |
local x, y = intersection(Dp, CO) |
| 351 |
|
| 352 |
if x == false then return "OUT" end |
| 353 |
if y == false then |
| 354 |
return (point.abs(pt - x) <= EPS) and "ON" or "OUT" |
| 355 |
end |
| 356 |
|
| 357 |
-- test ON (utile si pt est calculé et tombe sur la conique) |
| 358 |
if point.abs(pt - x) <= EPS or point.abs(pt - y) <= EPS then |
| 359 |
return "ON" |
| 360 |
end |
| 361 |
|
| 362 |
local chord = line(x, y) |
| 363 |
return chord:on_segment(pt, EPS) and "IN" or "OUT" |
| 364 |
end |
| 365 |
|
| 366 |
function PA_position_(PA, pt, EPS) return AX_position_(PA, pt, EPS) end |
| 367 |
function HY_position_(HY, pt, EPS) return AX_position_(HY, pt, EPS) end |
| 368 |
|
| 369 |
|
| 370 |
-- ============================================================ |
| 371 |
-- 3) (Option) rendre "ON" compatible avec l'ancien booléen |
| 372 |
-- Si tu veux que ON compte comme IN pour l'ancien monde : |
| 373 |
-- ============================================================ |
| 374 |
-- function conic:on_conic(pt, EPS) |
| 375 |
-- return self:position(pt, EPS) == "ON" |
| 376 |
-- end |
| 377 |
|
| 378 |
-- Fonction pour obtenir l'orthoptique de la conique |
| 379 |
function conic:orthoptic() |
| 380 |
local e = self.e |
| 381 |
if math.abs(e - 1) < tkz.epsilon then -- Parabole |
| 382 |
return self.directrix |
| 383 |
elseif self.e > 1 and self.e < math.sqrt(2) then -- Hyperbole |
| 384 |
local r = math.sqrt(self.a * self.a - self.b * self.b) |
| 385 |
local th = report_(self.center, self.vertex, r) |
| 386 |
return circle:new(self.center, th) |
| 387 |
elseif self.e < 1 then -- Ellipse |
| 388 |
local r = math.sqrt(self.a * self.a + self.b * self.b) |
| 389 |
local th = report_(self.center, self.vertex, r) |
| 390 |
return circle:new(self.center, th) |
| 391 |
end |
| 392 |
end |
| 393 |
|
| 394 |
-- Fonction pour calculer les asymptotes de la conique |
| 395 |
function conic:asymptotes() |
| 396 |
if self.subtype == "hyperbola" then -- Hyperbole |
| 397 |
-- Calcul du point sur les foyers et des asymptotes |
| 398 |
local pa = report_(self.Fa, self.Fb, self.a, self.center) |
| 399 |
local p1 = (pa - self.center):orthogonal(self.b):at(pa) |
| 400 |
local p2 = (pa - self.center):orthogonal(-self.b):at(pa) |
| 401 |
|
| 402 |
-- Symétrie des points d'asymptotes par rapport au centre |
| 403 |
local q1 = symmetry_(self.center, p1) |
| 404 |
local q2 = symmetry_(self.center, p2) |
| 405 |
|
| 406 |
-- Retourne les deux asymptotes |
| 407 |
return line:new(p1, q1), line:new(p2, q2) |
| 408 |
else |
| 409 |
-- If this is not an hyperbola, return an error |
| 410 |
tex.error("An error has occurred", { "It's not an hyperbola" }) |
| 411 |
return |
| 412 |
end |
| 413 |
end |
| 414 |
|
| 415 |
-- conic:path(za, zb, nb, mode, dir) |
| 416 |
-- mode : nil/"direct" (défaut) → arc direct (court) |
| 417 |
-- "swap" → arc complémentaire (l'autre partie) |
| 418 |
-- dir : "ccw" | "cw" (optionnel, utilisé pour lever l'ambiguïté à dt=±0.5 |
| 419 |
-- ou quand za==zb → tour complet) |
| 420 |
function conic:path(za, zb, nb, mode, dir) |
| 421 |
nb = nb or 20 |
| 422 |
mode = (mode or "direct"):lower() |
| 423 |
|
| 424 |
local ta = self:get_t_from_point(za) |
| 425 |
local tb = self:get_t_from_point(zb) |
| 426 |
local dt = tb - ta |
| 427 |
|
| 428 |
local function sgn(x) return (x < 0) and -1 or 1 end |
| 429 |
local function frac1(x) |
| 430 |
x = x % 1 |
| 431 |
if x < 0 then x = x + 1 end |
| 432 |
return x |
| 433 |
end |
| 434 |
|
| 435 |
-- Ellipse uniquement : paramètre périodique sur [0,1) |
| 436 |
if self.e and self.e < 1 then |
| 437 |
ta = frac1(ta) |
| 438 |
|
| 439 |
-- Normaliser dt dans (-0.5, 0.5] |
| 440 |
local eps = 1e-12 |
| 441 |
dt = ((dt + 0.5) % 1) - 0.5 |
| 442 |
|
| 443 |
-- Antipodes : trancher proprement |
| 444 |
if math.abs(math.abs(dt) - 0.5) < eps then |
| 445 |
-- choix déterministe selon dir (par défaut "cw") |
| 446 |
dt = (dir == "ccw") and 0.5 or -0.5 |
| 447 |
end |
| 448 |
|
| 449 |
if mode == "swap" then |
| 450 |
-- Prendre l'autre partie (arc complémentaire) |
| 451 |
if math.abs(dt) < eps then |
| 452 |
-- mêmes points : faire un tour complet |
| 453 |
dt = (dir == "cw") and -1 or 1 |
| 454 |
else |
| 455 |
-- décale de ±1 pour obtenir l'arc > 0.5 en valeur absolue |
| 456 |
dt = dt - sgn(dt) |
| 457 |
end |
| 458 |
else |
| 459 |
-- "direct" (défaut) : on garde dt dans (-0.5, 0.5] |
| 460 |
end |
| 461 |
else |
| 462 |
-- Parabole/hyperbole : pas de périodicité ; on ne modifie pas dt |
| 463 |
end |
| 464 |
|
| 465 |
-- Échantillonnage |
| 466 |
local P = path() |
| 467 |
for i = 0, nb do |
| 468 |
local t = i / nb |
| 469 |
local t_interp |
| 470 |
if self.e and self.e < 1 then |
| 471 |
t_interp = (ta + t * dt) % 1 |
| 472 |
if t_interp < 0 then t_interp = t_interp + 1 end |
| 473 |
else |
| 474 |
t_interp = ta + t * dt |
| 475 |
end |
| 476 |
local pt = self:point(t_interp) |
| 477 |
if not pt or not pt.re or not pt.im then |
| 478 |
tex.error("Invalid point at t = " .. tostring(t_interp)) |
| 479 |
end |
| 480 |
P:add_point(pt) |
| 481 |
end |
| 482 |
return P |
| 483 |
end |
| 484 |
|
| 485 |
|
| 486 |
function conic:get_t_from_point(z) |
| 487 |
local e = self.e |
| 488 |
if e == 1 then |
| 489 |
return self:get_t_parabola(z) |
| 490 |
elseif e < 1 then |
| 491 |
return self:get_t_ellipse(z) |
| 492 |
else |
| 493 |
return self:get_t_hyperbola(z) |
| 494 |
end |
| 495 |
end |
| 496 |
|
| 497 |
function conic:get_t_parabola(z) |
| 498 |
local H = self.directrix:projection(z) |
| 499 |
local d = length_(H, self.K) |
| 500 |
local u = self.directrix.pb - self.directrix.pa |
| 501 |
local v = H - self.K |
| 502 |
|
| 503 |
if v .. u >= 0 then |
| 504 |
return d |
| 505 |
else |
| 506 |
return -d |
| 507 |
end |
| 508 |
end |
| 509 |
|
| 510 |
function conic:get_t_hyperbola(z) |
| 511 |
local H = self.directrix:projection(z) |
| 512 |
local d = length_(H, self.K) |
| 513 |
local u = self.directrix.pb - self.directrix.pa |
| 514 |
local v = H - self.K |
| 515 |
|
| 516 |
if v .. u >= 0 then |
| 517 |
return d |
| 518 |
else |
| 519 |
return -d |
| 520 |
end |
| 521 |
end |
| 522 |
|
| 523 |
function conic:get_t_ellipse(z) |
| 524 |
local M = inverse_affinity_ellipse(self, z) |
| 525 |
local a = get_angle_normalize_(self.center, self.vertex, M) |
| 526 |
if a < 0 then |
| 527 |
a = a + tkz.tau |
| 528 |
end |
| 529 |
return a / tkz.tau |
| 530 |
end |
| 531 |
|
| 532 |
-- Tangentes communes externes ellipse–ellipse. |
| 533 |
-- Retour : L1, L2 |
| 534 |
function conic:common_tangent(E2, which) |
| 535 |
which = which or "external" -- "external" (défaut) ou "internal" |
| 536 |
if self.subtype ~= "ellipse" or E2.subtype ~= "ellipse" then |
| 537 |
return nil, nil |
| 538 |
end |
| 539 |
|
| 540 |
local E1 = self |
| 541 |
local E2 = E2 |
| 542 |
local C1 = E1.center |
| 543 |
local C2 = E2.center |
| 544 |
local A1 = E1.a |
| 545 |
local B1 = E1.b |
| 546 |
|
| 547 |
local a, b = E1.directrix:get() |
| 548 |
local ll = line(b, a) |
| 549 |
local sys = occs(ll, C1) |
| 550 |
local X2, Y2 = sys:coordinates(E2.center) |
| 551 |
local c2_loc = point(X2, Y2) |
| 552 |
local u2_loc, v2_loc |
| 553 |
do |
| 554 |
local vx, vy = sys:coordinates(E2.vertex) |
| 555 |
local cvx, cvy = vx - c2_loc.re, vy - c2_loc.im |
| 556 |
local nv = math.sqrt(cvx * cvx + cvy * cvy) |
| 557 |
if nv > 0 then cvx, cvy = cvx / nv, cvy / nv end |
| 558 |
u2_loc = point(cvx, cvy) |
| 559 |
|
| 560 |
local wx, wy = sys:coordinates(E2.covertex) |
| 561 |
local cwx, cwy = wx - c2_loc.re, wy - c2_loc.im |
| 562 |
local nw = math.sqrt(cwx * cwx + cwy * cwy) |
| 563 |
if nw > 0 then cwx, cwy = cwx / nw, cwy / nw end |
| 564 |
v2_loc = point(cwx, cwy) |
| 565 |
end |
| 566 |
|
| 567 |
local A2 = E2.a |
| 568 |
local B2 = E2.b |
| 569 |
|
| 570 |
-- paramétrisation locale de E2 (dans le repère sys) |
| 571 |
local function pE2_loc(t) |
| 572 |
return { |
| 573 |
x = c2_loc.re + A2 * math.cos(t) * u2_loc.re + B2 * math.sin(t) * v2_loc.re, |
| 574 |
y = c2_loc.im + A2 * math.cos(t) * u2_loc.im + B2 * math.sin(t) * v2_loc.im |
| 575 |
} |
| 576 |
end |
| 577 |
local function dE2_loc(t) |
| 578 |
return { |
| 579 |
x = -A2 * math.sin(t) * u2_loc.re + B2 * math.cos(t) * v2_loc.re, |
| 580 |
y = -A2 * math.sin(t) * u2_loc.im + B2 * math.cos(t) * v2_loc.im |
| 581 |
} |
| 582 |
end |
| 583 |
|
| 584 |
-- produit scalaire elliptique (E1) & normes associées |
| 585 |
local function ps_loc(z1, z2) |
| 586 |
return (z1.x * z2.x) / (A1 * A1) + (z1.y * z2.y) / (B1 * B1) |
| 587 |
end |
| 588 |
local function N2_loc(z) |
| 589 |
return ps_loc(z, z) |
| 590 |
end |
| 591 |
|
| 592 |
-- discriminant de tangence |
| 593 |
local function D_of_t(t) |
| 594 |
local p = pE2_loc(t) |
| 595 |
local dp = dE2_loc(t) |
| 596 |
return ps_loc(p, dp) ^ 2 - N2_loc(dp) * (N2_loc(p) - 1) |
| 597 |
end |
| 598 |
|
| 599 |
local function is_external(A, u) |
| 600 |
local px, py = -u.y, u.x |
| 601 |
local s1 = A.x * px + A.y * py |
| 602 |
local dx, dy = A.x - c2_loc.re, A.y - c2_loc.im |
| 603 |
local s2 = dx * px + dy * py |
| 604 |
return s1 * s2 >= 0 |
| 605 |
end |
| 606 |
|
| 607 |
local function is_internal(A, u) |
| 608 |
local px, py = -u.y, u.x |
| 609 |
local s1 = A.x * px + A.y * py |
| 610 |
local dx, dy = A.x - c2_loc.re, A.y - c2_loc.im |
| 611 |
local s2 = dx * px + dy * py |
| 612 |
return s1 * s2 <= 0 |
| 613 |
end |
| 614 |
|
| 615 |
local function to_global(O, origin, a, b) |
| 616 |
local ox, oy = origin.re, origin.im |
| 617 |
local ux, uy = O.x.re - ox, O.x.im - oy |
| 618 |
local vx, vy = O.y.re - ox, O.y.im - oy |
| 619 |
return point(ox + a * ux + b * vx, oy + a * uy + b * vy) |
| 620 |
end |
| 621 |
|
| 622 |
local function almost_eq(P, Q, eps) |
| 623 |
if (not P) or (not Q) then return false end |
| 624 |
eps = eps or 1e-4 |
| 625 |
return (math.abs(P.x - Q.x) < eps) and (math.abs(P.y - Q.y) < eps) |
| 626 |
end |
| 627 |
-- ===== Fin du bloc “préambule/repère” adapté ===== |
| 628 |
|
| 629 |
local E1_raw, E2_raw = {}, {} |
| 630 |
local T = tkz.fsolve(D_of_t, -math.pi, math.pi, 720) |
| 631 |
if T == nil then T = {} end |
| 632 |
if type(T) ~= "table" then T = { T } end |
| 633 |
|
| 634 |
for _, t in ipairs(T) do |
| 635 |
local A = pE2_loc(t) |
| 636 |
local u = dE2_loc(t) |
| 637 |
if A and u then |
| 638 |
local uu = ps_loc(u, u) |
| 639 |
if uu and uu > 0 then |
| 640 |
local lam = -ps_loc(A, u) / uu |
| 641 |
local B = { x = A.x + lam * u.x, y = A.y + lam * u.y } |
| 642 |
if which == "external" then |
| 643 |
is_which=is_external |
| 644 |
else |
| 645 |
is_which=is_internal |
| 646 |
end |
| 647 |
if is_which(A, u) then |
| 648 |
local n = utils.table_getn(E1_raw) |
| 649 |
local dup = false |
| 650 |
for i = 1, n do |
| 651 |
if almost_eq(B, E1_raw[i]) and almost_eq(A, E2_raw[i]) then |
| 652 |
dup = true |
| 653 |
break |
| 654 |
end |
| 655 |
end |
| 656 |
if not dup then |
| 657 |
E1_raw[n + 1] = B |
| 658 |
E2_raw[n + 1] = A |
| 659 |
if n + 1 >= 2 then break end |
| 660 |
end |
| 661 |
end |
| 662 |
end |
| 663 |
end |
| 664 |
end |
| 665 |
|
| 666 |
-- projection finale en global via le repère sys centré en C1 |
| 667 |
local a = E1_raw[1] and to_global(sys, C1, E1_raw[1].x, E1_raw[1].y) or nil |
| 668 |
local b = E1_raw[2] and to_global(sys, C1, E1_raw[2].x, E1_raw[2].y) or nil |
| 669 |
local c = E2_raw[1] and to_global(sys, C1, E2_raw[1].x, E2_raw[1].y) or nil |
| 670 |
local d = E2_raw[2] and to_global(sys, C1, E2_raw[2].x, E2_raw[2].y) or nil |
| 671 |
return line:new(a, b), line:new(c, d) |
| 672 |
end |
| 673 |
|
| 674 |
return conic |