| 1 |
-- File: tkz_elements_intersections.lua |
| 2 |
-- Copyright (c) 2026 Alain Matthes |
| 3 |
-- SPDX-License-Identifier: LPPL-1.3c |
| 4 |
-- Maintainer: Alain Matthes |
| 5 |
|
| 6 |
------------------------------------------------------------------------- |
| 7 |
-- intersection of lines |
| 8 |
------------------------------------------------------------------------- |
| 9 |
function intersection_ll(la, lb ,EPS) |
| 10 |
return intersection_ll_(la.pa, la.pb, lb.pa, lb.pb, EPS) |
| 11 |
end |
| 12 |
--------------------------------------------------------------------------- |
| 13 |
-- intersection of a line and a circle |
| 14 |
--------------------------------------------------------------------------- |
| 15 |
function intersection_lc(D, C, EPS) |
| 16 |
return intersection_lc_(D.pa, D.pb, C.center, C.through, EPS) |
| 17 |
end -- function |
| 18 |
--------------------------------------------------------------------------- |
| 19 |
-- intersection of two circles |
| 20 |
--------------------------------------------------------------------------- |
| 21 |
function intersection_cc(Ca, Cb, EPS) |
| 22 |
return intersection_cc_(Ca.center, Ca.through, Cb.center, Cb.through, EPS) |
| 23 |
end -- function |
| 24 |
|
| 25 |
-- line ellipse |
| 26 |
function intersection_le(L, E, EPS) |
| 27 |
EPS = EPS or tkz.epsilon |
| 28 |
local a, b, c, d, t1, t2, z1, z2, A, B, Bx, By, Ax, Ay, Rx, Ry, sd |
| 29 |
A = (L.pa - E.center) * (point(math.cos(E.slope), -math.sin(E.slope))) |
| 30 |
B = (L.pb - E.center) * (point(math.cos(E.slope), -math.sin(E.slope))) |
| 31 |
Rx = E.Rx |
| 32 |
Ry = E.Ry |
| 33 |
Ax = A.re |
| 34 |
Ay = A.im |
| 35 |
Bx = B.re |
| 36 |
By = B.im |
| 37 |
a = Rx ^ 2 * (By - Ay) ^ 2 + Ry ^ 2 * (Bx - Ax) ^ 2 |
| 38 |
b = 2 * Rx ^ 2 * Ay * (By - Ay) + 2 * Ry ^ 2 * Ax * (Bx - Ax) |
| 39 |
c = Rx ^ 2 * Ay ^ 2 + Ry ^ 2 * Ax ^ 2 - Rx ^ 2 * Ry ^ 2 |
| 40 |
d = b ^ 2 - 4 * a * c |
| 41 |
|
| 42 |
if d > 0 then |
| 43 |
sd = math.sqrt(d) |
| 44 |
t1 = (-b + sd) / (2 * a) |
| 45 |
t2 = (-b - sd) / (2 * a) |
| 46 |
z1 = point(Ax + (Bx - Ax) * t1, Ay + (By - Ay) * t1) |
| 47 |
z2 = point(Ax + (Bx - Ax) * t2, Ay + (By - Ay) * t2) |
| 48 |
if angle_normalize_(point.arg(z1)) < angle_normalize_(point.arg(z2)) then |
| 49 |
return z1 * (point(math.cos(E.slope), math.sin(E.slope))) + E.center, |
| 50 |
z2 * (point(math.cos(E.slope), math.sin(E.slope))) + E.center |
| 51 |
else |
| 52 |
return z2 * (point(math.cos(E.slope), math.sin(E.slope))) + E.center, |
| 53 |
z1 * (point(math.cos(E.slope), math.sin(E.slope))) + E.center |
| 54 |
end -- if |
| 55 |
elseif math.abs(d) < EPS then |
| 56 |
t1 = -b / (2 * a) |
| 57 |
z1 = point(Ax + (Bx - Ax) * t1, Ay + (By - Ay) * t1) |
| 58 |
return z1 * (point(math.cos(E.slope), math.sin(E.slope))) + E.center, |
| 59 |
z1 * (point(math.cos(E.slope), math.sin(E.slope))) + E.center |
| 60 |
else |
| 61 |
return false, false |
| 62 |
end |
| 63 |
end |
| 64 |
|
| 65 |
function intersection_ll_(a, b, c, d, EPS) |
| 66 |
local x1, y1 = a.re, a.im |
| 67 |
local x2, y2 = b.re, b.im |
| 68 |
local x3, y3 = c.re, c.im |
| 69 |
local x4, y4 = d.re, d.im |
| 70 |
|
| 71 |
EPS = EPS or tkz.epsilon |
| 72 |
|
| 73 |
-- cas dégénérés : une "droite" réduite à un point |
| 74 |
if (x1 == x2 and y1 == y2) or (x3 == x4 and y3 == y4) then |
| 75 |
return false |
| 76 |
end |
| 77 |
|
| 78 |
local DN = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4) |
| 79 |
|
| 80 |
-- droites parallèles ou quasi-parallèles |
| 81 |
if math.abs(DN) < EPS then |
| 82 |
return false |
| 83 |
end |
| 84 |
|
| 85 |
local NX = (x1 * y2 - y1 * x2) * (x3 - x4) - (x1 - x2) * (x3 * y4 - y3 * x4) |
| 86 |
local NY = (x1 * y2 - y1 * x2) * (y3 - y4) - (y1 - y2) * (x3 * y4 - y3 * x4) |
| 87 |
|
| 88 |
return point(NX / DN, NY / DN) |
| 89 |
end |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
function intersection_lc_(pa, pb, c, p, EPS) |
| 95 |
-- pa, pb : deux points définissant la droite |
| 96 |
-- c : centre du cercle |
| 97 |
-- p : point sur le cercle (pour le rayon) |
| 98 |
-- Retour : |
| 99 |
-- - false, false si pas d’intersection |
| 100 |
-- - P, P si tangence |
| 101 |
-- - P1, P2 si sécante (ordonnées par angle autour de c) |
| 102 |
EPS = EPS or tkz.epsilon |
| 103 |
|
| 104 |
|
| 105 |
-- Rayon du cercle |
| 106 |
local r = point.abs(c - p) |
| 107 |
|
| 108 |
-- Vecteur directeur de la droite (pa -> pb) |
| 109 |
local ab = pb - pa |
| 110 |
local abx, aby = ab.re, ab.im |
| 111 |
local lab2 = abx*abx + aby*aby |
| 112 |
|
| 113 |
-- Droite dégénérée : pa == pb |
| 114 |
if lab2 < EPS * EPS then |
| 115 |
return false, false |
| 116 |
end |
| 117 |
|
| 118 |
local lab = math.sqrt(lab2) |
| 119 |
local ex, ey = abx / lab, aby / lab -- vecteur unitaire sur la droite |
| 120 |
|
| 121 |
-- Projection du centre c sur la droite (pa,pb) |
| 122 |
local zh = projection_(pa, pb, c) |
| 123 |
local dh = point.abs(c - zh) -- distance centre -> droite |
| 124 |
|
| 125 |
-- Pas d'intersection : la droite est trop loin du cercle |
| 126 |
if dh > r + EPS then |
| 127 |
return false, false |
| 128 |
end |
| 129 |
|
| 130 |
-- Tangence : un seul point (renvoyé deux fois) |
| 131 |
if math.abs(dh - r) <= EPS then |
| 132 |
return zh, zh |
| 133 |
end |
| 134 |
|
| 135 |
-- Cas général : deux intersections. |
| 136 |
-- Longueur le long de la droite à partir du pied de la perpendiculaire. |
| 137 |
local h2 = r*r - dh*dh |
| 138 |
local R2 = math.max(r*r, lab2) |
| 139 |
|
| 140 |
if h2 < -EPS * R2 then |
| 141 |
-- numériquement négatif => pas de solution réelle |
| 142 |
return false, false |
| 143 |
end |
| 144 |
if h2 < 0 then |
| 145 |
h2 = 0 |
| 146 |
end |
| 147 |
|
| 148 |
local h = math.sqrt(h2) |
| 149 |
|
| 150 |
local xh, yh = zh.re, zh.im |
| 151 |
local x1 = xh + h * ex |
| 152 |
local y1 = yh + h * ey |
| 153 |
local x2 = xh - h * ex |
| 154 |
local y2 = yh - h * ey |
| 155 |
|
| 156 |
local z1 = point(x1, y1) |
| 157 |
local z2 = point(x2, y2) |
| 158 |
|
| 159 |
-- Tri par angle autour du centre c, comme pour intersection_cc_ |
| 160 |
local a1 = angle_normalize_(point.arg(z1 - c)) |
| 161 |
local a2 = angle_normalize_(point.arg(z2 - c)) |
| 162 |
|
| 163 |
if a1 <= a2 then |
| 164 |
return z1, z2 |
| 165 |
else |
| 166 |
return z2, z1 |
| 167 |
end |
| 168 |
end |
| 169 |
|
| 170 |
function intersection_cc_(ca, pa, cb, pb, EPS) |
| 171 |
-- ca, pa, cb, pb : points (type point/complex) |
| 172 |
-- Retour : |
| 173 |
-- - false, false si pas d'intersection |
| 174 |
-- - P, P si tangence |
| 175 |
-- - P1, P2 si sécante (ordre croissant en angle autour de ca) |
| 176 |
EPS = EPS or tkz.epsilon |
| 177 |
local ra = point.abs(ca - pa) |
| 178 |
local rb = point.abs(cb - pb) |
| 179 |
|
| 180 |
-- vecteur entre centres |
| 181 |
local dc = cb - ca |
| 182 |
local dx, dy = dc.re, dc.im |
| 183 |
local d2 = dx*dx + dy*dy |
| 184 |
local d = math.sqrt(d2) |
| 185 |
|
| 186 |
-- centres (quasi) confondus |
| 187 |
if d < EPS then |
| 188 |
-- mêmes rayons -> cercles confondus : géométriquement infinité de solutions |
| 189 |
-- pour l’instant on renvoie "pas d’intersection" (comportement ancien : false,false) |
| 190 |
if math.abs(ra - rb) < EPS then |
| 191 |
return false, false |
| 192 |
else |
| 193 |
return false, false |
| 194 |
end |
| 195 |
end |
| 196 |
|
| 197 |
-- positions relatives simples : trop loin ou l'un dans l'autre sans contact |
| 198 |
-- (ces tests ne sont pas obligatoires mais évitent des racines négatives parasites) |
| 199 |
if d > ra + rb + EPS then |
| 200 |
-- trop éloignés |
| 201 |
return false, false |
| 202 |
end |
| 203 |
if d < math.abs(ra - rb) - EPS then |
| 204 |
-- l’un est strictement à l’intérieur de l’autre, sans tangence |
| 205 |
return false, false |
| 206 |
end |
| 207 |
|
| 208 |
-- projection du point d’intersection "médian" sur la droite des centres |
| 209 |
local a = (ra*ra - rb*rb + d2) / (2*d) |
| 210 |
local h2 = ra*ra - a*a |
| 211 |
|
| 212 |
-- tolérance relative sur h2 |
| 213 |
local R2 = math.max(ra*ra, rb*rb, d2) |
| 214 |
if h2 < -EPS * R2 then |
| 215 |
-- numériquement négatif -> pas de solution réelle |
| 216 |
return false, false |
| 217 |
end |
| 218 |
|
| 219 |
if h2 < 0 then |
| 220 |
-- très léger négatif dû aux erreurs de flottants |
| 221 |
h2 = 0 |
| 222 |
end |
| 223 |
|
| 224 |
-- point "milieu" sur la droite (ca -> cb) |
| 225 |
local ux, uy = dx / d, dy / d -- vecteur unitaire ca->cb |
| 226 |
local mx = ca.re + a * ux |
| 227 |
local my = ca.im + a * uy |
| 228 |
|
| 229 |
local h = math.sqrt(h2) |
| 230 |
|
| 231 |
-- si h ~ 0 : tangence |
| 232 |
if h < EPS * d then |
| 233 |
local P = point(mx, my) |
| 234 |
return P, P |
| 235 |
end |
| 236 |
|
| 237 |
-- vecteur unitaire perpendiculaire |
| 238 |
local px, py = -uy, ux |
| 239 |
|
| 240 |
local x1 = mx + h * px |
| 241 |
local y1 = my + h * py |
| 242 |
local x2 = mx - h * px |
| 243 |
local y2 = my - h * py |
| 244 |
|
| 245 |
local z1 = point(x1, y1) |
| 246 |
local z2 = point(x2, y2) |
| 247 |
|
| 248 |
-- ordre croissant en angle autour de ca (comme ton ancienne version) |
| 249 |
local c1 = angle_normalize_(point.arg(z1 - ca)) |
| 250 |
local c2 = angle_normalize_(point.arg(z2 - ca)) |
| 251 |
|
| 252 |
if c1 <= c2 then |
| 253 |
return z1, z2 |
| 254 |
else |
| 255 |
return z2, z1 |
| 256 |
end |
| 257 |
end |
| 258 |
|
| 259 |
|
| 260 |
function intersection(X, Y, opts) |
| 261 |
-- Normalisation des arguments |
| 262 |
local EPS |
| 263 |
|
| 264 |
if type(opts) == "number" then |
| 265 |
-- Ancien style : intersection(X, Y, EPS) |
| 266 |
EPS = opts |
| 267 |
opts = nil |
| 268 |
elseif type(opts) == "table" then |
| 269 |
-- Nouveau style : opts.eps (ou opts.EPS) |
| 270 |
EPS = opts.EPS or tkz.epsilon |
| 271 |
elseif opts == nil then |
| 272 |
EPS = tkz.epsilon |
| 273 |
else |
| 274 |
tex.error("intersection: invalid third argument (expected table or number).") |
| 275 |
return |
| 276 |
end |
| 277 |
|
| 278 |
opts = opts or {} |
| 279 |
|
| 280 |
local t = {} -- Table pour stocker les points d'intersection |
| 281 |
|
| 282 |
-- Intersection cercle-cercle, ligne-cercle, ligne-ligne, conique-ligne... |
| 283 |
local function add(z1, z2) |
| 284 |
if z1 then |
| 285 |
table.insert(t, z1) |
| 286 |
end |
| 287 |
if z2 then |
| 288 |
table.insert(t, z2) |
| 289 |
end |
| 290 |
end |
| 291 |
|
| 292 |
-- Cas principaux |
| 293 |
if X.type == "circle" then |
| 294 |
if Y.type == "circle" then |
| 295 |
add(intersection_cc(X, Y, EPS)) |
| 296 |
else |
| 297 |
add(intersection_lc(Y, X, EPS)) |
| 298 |
end |
| 299 |
elseif X.type == "line" then |
| 300 |
if Y.type == "circle" then |
| 301 |
add(intersection_lc(X, Y, EPS)) |
| 302 |
elseif Y.type == "line" then |
| 303 |
add(intersection_ll(X, Y, EPS)) |
| 304 |
elseif Y.type == "conic" then |
| 305 |
if Y.subtype == "parabola" then |
| 306 |
add(Y:inter_Pa_line(X.pa, X.pb)) |
| 307 |
elseif Y.subtype == "hyperbola" then |
| 308 |
add(Y:inter_Hy_line(X.pa, X.pb)) |
| 309 |
elseif Y.subtype == "ellipse" then |
| 310 |
add(intersection_le(X, Y, EPS)) |
| 311 |
end |
| 312 |
end |
| 313 |
elseif X.type == "conic" then |
| 314 |
if X.subtype == "parabola" then |
| 315 |
add(X:inter_Pa_line(Y.pa, Y.pb)) |
| 316 |
elseif X.subtype == "hyperbola" then |
| 317 |
add(X:inter_Hy_line(Y.pa, Y.pb)) |
| 318 |
elseif X.subtype == "ellipse" then |
| 319 |
add(intersection_le(Y, X, EPS)) |
| 320 |
end |
| 321 |
end |
| 322 |
|
| 323 |
-- Réorganisation selon l'option |
| 324 |
if opts then |
| 325 |
if opts.known and #t == 2 then |
| 326 |
local z1, z2 = t[1], t[2] |
| 327 |
if z1 == opts.known then |
| 328 |
t = { z2, z1 } |
| 329 |
elseif z2 == opts.known then |
| 330 |
t = { z1, z2 } |
| 331 |
end |
| 332 |
elseif opts.near and #t == 2 then |
| 333 |
local z1, z2 = t[1], t[2] |
| 334 |
local d1 = (z1 - opts.near):abs() |
| 335 |
local d2 = (z2 - opts.near):abs() |
| 336 |
if d2 < d1 then |
| 337 |
t = { z2, z1 } |
| 338 |
end |
| 339 |
end |
| 340 |
end |
| 341 |
|
| 342 |
return table.unpack(t) |
| 343 |
end |