| 1 |
-- File: tkz_elements_functions_circles.lua |
| 2 |
-- Copyright (c) 2026 Alain Matthes |
| 3 |
-- SPDX-License-Identifier: LPPL-1.3c |
| 4 |
-- Maintainer: Alain Matthes |
| 5 |
|
| 6 |
-- define a circle by the center and a radius |
| 7 |
function through(c, r, an) |
| 8 |
if an then |
| 9 |
return c, c + point(r * math.cos(an), r * math.sin(an)) |
| 10 |
else |
| 11 |
return c, c + point(r, 0) |
| 12 |
end |
| 13 |
end |
| 14 |
from_radius = through |
| 15 |
|
| 16 |
function diameter(a, b, option) |
| 17 |
local center = midpoint_(a, b) |
| 18 |
if type(option) == "string" and option == "swap" then |
| 19 |
return center, a |
| 20 |
elseif type(option) == "number" then |
| 21 |
local u = (b - a) / point.abs(b - a) |
| 22 |
local rotated = point.polar(point.abs((b - a)) / 2, option) |
| 23 |
return center, center + rotated * u |
| 24 |
else |
| 25 |
return center, b |
| 26 |
end |
| 27 |
end |
| 28 |
from_diameter = diameter |
| 29 |
----------------------- |
| 30 |
-- Result -> boolean |
| 31 |
----------------------- |
| 32 |
function on_circle_(o, t, p, EPS) |
| 33 |
EPS = EPS or tkz.epsilon |
| 34 |
local r = point.abs(t - o) |
| 35 |
local d = point.abs(p - o) |
| 36 |
return math.abs(d - r) <= EPS |
| 37 |
end |
| 38 |
|
| 39 |
function in_disk_(o, t, p, EPS) |
| 40 |
EPS = EPS or tkz.epsilon |
| 41 |
local r = point.abs(t - o) |
| 42 |
return point.abs(p - o) <= r + EPS |
| 43 |
end |
| 44 |
|
| 45 |
function in_disk_strict_(o, t, p, EPS) |
| 46 |
EPS = EPS or tkz.epsilon |
| 47 |
local r = point.abs(t - o) |
| 48 |
local rr = r - EPS |
| 49 |
if rr <= 0 then return false end |
| 50 |
return point.abs(p - o) < rr |
| 51 |
end |
| 52 |
|
| 53 |
function out_disk_strict_(o, t, p, EPS) |
| 54 |
EPS = EPS or tkz.epsilon |
| 55 |
local r = point.abs(t - o) |
| 56 |
return point.abs(p - o) > r + EPS |
| 57 |
end |
| 58 |
|
| 59 |
-- internal helper |
| 60 |
function point_circle_position_(o, t, p, EPS) |
| 61 |
EPS = EPS or tkz.epsilon |
| 62 |
local d = point.abs(p - o) |
| 63 |
local r = point.abs(t - o) |
| 64 |
if math.abs(d - r) <= EPS then |
| 65 |
return "ON" |
| 66 |
elseif d < r - EPS then |
| 67 |
return "IN" |
| 68 |
else |
| 69 |
return "OUT" |
| 70 |
end |
| 71 |
end |
| 72 |
|
| 73 |
function circle_point_position_(C, p, EPS) |
| 74 |
EPS = EPS or tkz.epsilon |
| 75 |
local d = point.abs(p - C.center) |
| 76 |
local r = C.radius |
| 77 |
|
| 78 |
if is_zero_(d - r, EPS) then |
| 79 |
return "ON" |
| 80 |
elseif d < r then |
| 81 |
return "IN" |
| 82 |
else |
| 83 |
return "OUT" |
| 84 |
end |
| 85 |
end |
| 86 |
|
| 87 |
|
| 88 |
function in_out_(o, t, p, EPS) |
| 89 |
EPS = EPS or tkz.epsilon |
| 90 |
local r = point.abs(t - o) |
| 91 |
local d = point.abs(p - o) |
| 92 |
return math.abs(d - r) <= EPS |
| 93 |
end |
| 94 |
|
| 95 |
function in_out_disk_(a, b, pt) |
| 96 |
return point.abs(pt - a) <= point.abs(b - a) |
| 97 |
end |
| 98 |
|
| 99 |
|
| 100 |
function is_tangent_(a, b, o, t, EPS) |
| 101 |
EPS = EPS or tkz.epsilon |
| 102 |
local r = length_(o, t) |
| 103 |
local d = distance_(a, b, o) |
| 104 |
return math.abs(d - r) <= EPS |
| 105 |
end |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
function act_(O1, T1, O2, T2, opts) |
| 111 |
opts = opts or {} |
| 112 |
local eps_abs = (opts.abs ~= nil) and opts.abs or 1e-4 |
| 113 |
local eps_rel = (opts.rel ~= nil) and opts.rel or 1e-6 |
| 114 |
|
| 115 |
local R1 = length_(O1, T1) |
| 116 |
local R2 = length_(O2, T2) |
| 117 |
local d = length_(O1, O2) |
| 118 |
|
| 119 |
local sum = R1 + R2 |
| 120 |
local dif = math.abs(R1 - R2) |
| 121 |
|
| 122 |
local eE_abs = math.abs(d - sum) |
| 123 |
local eI_abs = math.abs(d - dif) |
| 124 |
|
| 125 |
local eE_rel = eE_abs / math.max(1.0, sum) |
| 126 |
local eI_rel = eI_abs / math.max(1.0, dif, 1.0) |
| 127 |
|
| 128 |
return (eE_abs <= eps_abs) or (eI_abs <= eps_abs) |
| 129 |
or (eE_rel <= eps_rel) or (eI_rel <= eps_rel) |
| 130 |
end |
| 131 |
|
| 132 |
-- distance from point C to the infinite line (A,B) |
| 133 |
-- you already have distance_(A,B,C) apparently |
| 134 |
function line_circle_position_(L, C, EPS) |
| 135 |
EPS = EPS or tkz.epsilon |
| 136 |
local r = C.radius |
| 137 |
local d = distance_(L.pa, L.pb, C.center) |
| 138 |
|
| 139 |
if d > r + EPS then |
| 140 |
return "DISJOINT" |
| 141 |
elseif is_zero_(d - r, EPS) then |
| 142 |
return "TANGENT" |
| 143 |
else |
| 144 |
return "SECANT" |
| 145 |
end |
| 146 |
end |
| 147 |
----------------------- |
| 148 |
-- Result -> point |
| 149 |
----------------------- |
| 150 |
|
| 151 |
function midarc_(o, a, b) -- a -> b |
| 152 |
local phi = 0.5 * get_angle_(a, o, b) |
| 153 |
return rotation_(o, phi, b) |
| 154 |
end |
| 155 |
|
| 156 |
|
| 157 |
function tangent_point_two_circles_(c1, r1, c2, r2) |
| 158 |
local dc = c2 - c1 |
| 159 |
local d = point.abs(dc) |
| 160 |
if d == 0 or r1 == 0 then |
| 161 |
return false |
| 162 |
end |
| 163 |
local u = dc / d -- vecteur unitaire c1 -> c2 |
| 164 |
return c1 + r1 * u |
| 165 |
end |
| 166 |
|
| 167 |
function tangent_from_(c, p, pt) |
| 168 |
local o = midpoint_(c, pt) |
| 169 |
return intersection_cc_(o, c, c, p) |
| 170 |
end |
| 171 |
|
| 172 |
function tangent_at_(a, p) |
| 173 |
return rotation_(p, math.pi / 2, a), rotation_(p, -math.pi / 2, a) |
| 174 |
end |
| 175 |
|
| 176 |
function orthogonal_from_(a, b, p) |
| 177 |
return tangent_from_(a, b, p) |
| 178 |
end |
| 179 |
|
| 180 |
-- pb avec l'inversion |
| 181 |
-- 1) si l'un des points est sur le cercle |
| 182 |
-- 2) si l'un des points est l'inverse de l'autre par rapport au cercle |
| 183 |
function orthogonal_through_(a, b, x, y, EPS) |
| 184 |
EPS = EPS or tkz.epsilon |
| 185 |
if math.abs(point.mod(x - a) - point.mod(b - a)) < EPS then |
| 186 |
local ta, tb = tangent_at_(a, x) |
| 187 |
local mx, my = mediator_(x, y) |
| 188 |
local z = intersection_ll_(ta, tb, mx, my) |
| 189 |
return z |
| 190 |
else |
| 191 |
local z = inversion_(a, b, x) |
| 192 |
return circum_center_(x, y, z) |
| 193 |
end |
| 194 |
end |
| 195 |
|
| 196 |
function are_inverses_(a, b, x, y, EPS) |
| 197 |
EPS = EPS or tkz.epsilon |
| 198 |
local z = inversion_(a, b, x) |
| 199 |
return point.mod(z - y) < EPS |
| 200 |
end |
| 201 |
|
| 202 |
function orthogonal_circle_through_(a, b, x, y, EPS) |
| 203 |
EPS = EPS or tkz.epsilon |
| 204 |
|
| 205 |
-- rayon du cercle de référence C(a,b) |
| 206 |
local r0 = point.abs(b - a) |
| 207 |
|
| 208 |
-- garde-fous |
| 209 |
if point.abs(x - y) <= EPS then |
| 210 |
return nil, "orthogonal_circle_through_: x and y must be distinct" |
| 211 |
end |
| 212 |
|
| 213 |
-- tests "sur le cercle" |
| 214 |
local x_on = on_circle_(a, b, x, EPS) |
| 215 |
local y_on = on_circle_(a, b, y, EPS) |
| 216 |
|
| 217 |
---------------------------------------------------------------------- |
| 218 |
-- Cas 1 : x sur C(a,b) (ou y sur C(a,b)) |
| 219 |
-- Propriété: si deux cercles sont orthogonaux et partagent un point P, |
| 220 |
-- alors les centres sont alignés avec P et le centre du cercle de référence. |
| 221 |
-- Donc: centre C recherché est sur la droite (a,x) et aussi sur la médiatrice de [x,y]. |
| 222 |
---------------------------------------------------------------------- |
| 223 |
if x_on then |
| 224 |
local m1, m2 = mediator_(x, y) -- médiatrice de [x,y] |
| 225 |
local c = intersection_ll_(a, x, m1, m2) |
| 226 |
if not c then |
| 227 |
return nil, "orthogonal_circle_through_: degenerate case (x on circle)" |
| 228 |
end |
| 229 |
return c, x |
| 230 |
end |
| 231 |
|
| 232 |
if y_on then |
| 233 |
local m1, m2 = mediator_(x, y) |
| 234 |
local c = intersection_ll_(a, y, m1, m2) |
| 235 |
if not c then |
| 236 |
return nil, "orthogonal_circle_through_: degenerate case (y on circle)" |
| 237 |
end |
| 238 |
return c, y |
| 239 |
end |
| 240 |
|
| 241 |
---------------------------------------------------------------------- |
| 242 |
-- Cas 2 : x et y sont inverses par rapport à C(a,b) |
| 243 |
-- Dans ce cas, (x,y) forment une paire inverse : il existe des cercles |
| 244 |
-- orthogonaux passant par x,y ; on fixe une solution géométrique stable. |
| 245 |
---------------------------------------------------------------------- |
| 246 |
if are_inverses_(a, b, x, y, EPS) then |
| 247 |
local m1, m2 = mediator_(x, y) -- centre sur la médiatrice de [x,y] |
| 248 |
|
| 249 |
-- On choisit un point "le plus éloigné" du centre a pour une construction plus stable |
| 250 |
local p = (point.abs(x - a) > point.abs(y - a)) and x or y |
| 251 |
|
| 252 |
-- tangentes depuis p au cercle C(a,b) |
| 253 |
local t1, t2 = tangent_from_(a, b, p) |
| 254 |
if not t1 or not t2 then |
| 255 |
return nil, "orthogonal_circle_through_: tangents undefined (inverse case)" |
| 256 |
end |
| 257 |
|
| 258 |
-- centre aussi sur la médiatrice de [t1,p] (ou [t2,p], au choix) |
| 259 |
local n1, n2 = mediator_(t1, p) |
| 260 |
local c = intersection_ll_(m1, m2, n1, n2) |
| 261 |
if not c then |
| 262 |
return nil, "orthogonal_circle_through_: cannot determine center (inverse case)" |
| 263 |
end |
| 264 |
|
| 265 |
return c, x |
| 266 |
end |
| 267 |
|
| 268 |
---------------------------------------------------------------------- |
| 269 |
-- Cas 3 : cas général (x pas sur le cercle, y pas sur le cercle, non inverses) |
| 270 |
-- Construction standard : si z = inversion(x), alors le cercle (x,y,z) est orthogonal à C(a,b). |
| 271 |
---------------------------------------------------------------------- |
| 272 |
local z = inversion_(a, b, x, EPS) |
| 273 |
if not z then |
| 274 |
return nil, "orthogonal_circle_through_: inversion failed" |
| 275 |
end |
| 276 |
|
| 277 |
local c = circum_center_(x, y, z) |
| 278 |
if not c then |
| 279 |
return nil, "orthogonal_circle_through_: circumcenter undefined (collinear points?)" |
| 280 |
end |
| 281 |
|
| 282 |
return c, x |
| 283 |
end |
| 284 |
|
| 285 |
function inversion_(c, p, pt, EPS) |
| 286 |
EPS = EPS or tkz.epsilon |
| 287 |
local ry = point.abs(c - p) -- radius of reference circle (c,p) |
| 288 |
local d = point.abs(c - pt) -- distance from c to pt |
| 289 |
|
| 290 |
if d < EPS then |
| 291 |
-- Avoid division by zero: return point at infinity or raise error |
| 292 |
tex.error("Inversion undefined at center", { "inversion_(c, p, pt): pt == c" }) |
| 293 |
return nil |
| 294 |
end |
| 295 |
|
| 296 |
local r = (ry * ry) / d -- inversion radius |
| 297 |
return c + polar_(r, point.arg(pt - c)) |
| 298 |
end |
| 299 |
|
| 300 |
function line_position_(c, r, a, b, EPS) |
| 301 |
EPS = EPS or tkz.epsilon |
| 302 |
local d = distance_(a, b, c) |
| 303 |
|
| 304 |
if d > r + EPS then |
| 305 |
return "DISJOINT" |
| 306 |
|
| 307 |
elseif math.abs(d - r) <= EPS then |
| 308 |
return "TANGENT" |
| 309 |
|
| 310 |
else |
| 311 |
return "SECANT" |
| 312 |
end |
| 313 |
end |
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
-- Relation between two circles (O1,r1) and (O2,r2) |
| 318 |
-- returns: COINCIDENT, CONCENTRIC, DISJOINT_EXT, TANGENT_EXT, SECANT, |
| 319 |
-- TANGENT_INT, DISJOINT_INT |
| 320 |
function circles_position_(o1, r1, o2, r2, EPS) |
| 321 |
EPS = EPS or tkz.epsilon |
| 322 |
|
| 323 |
local d = point.abs(o2 - o1) |
| 324 |
local rp = r1 + r2 |
| 325 |
local rm = math.abs(r1 - r2) |
| 326 |
|
| 327 |
-- same center? |
| 328 |
if is_zero_(d, EPS) then |
| 329 |
if is_zero_(r1 - r2, EPS) then |
| 330 |
return "IDENTICAL" |
| 331 |
else |
| 332 |
return "CONCENTRIC" |
| 333 |
end |
| 334 |
end |
| 335 |
|
| 336 |
-- compare d with r1+r2 and |r1-r2| |
| 337 |
if d > rp + EPS then |
| 338 |
return "DISJOINT_EXT" |
| 339 |
elseif is_zero_(d - rp, EPS) then |
| 340 |
return "TANGENT_EXT" |
| 341 |
elseif d < rp - EPS and d > rm + EPS then |
| 342 |
return "SECANT" |
| 343 |
elseif is_zero_(d - rm, EPS) then |
| 344 |
return "TANGENT_INT" |
| 345 |
else |
| 346 |
-- d < |r1-r2| - EPS |
| 347 |
return "DISJOINT_INT" |
| 348 |
end |
| 349 |
end |
| 350 |
|
| 351 |
|
| 352 |
function radical_axis_(c1, p1, c2, p2) |
| 353 |
local r1 = point.abs(c1 - p1) |
| 354 |
local r2 = point.abs(c2 - p2) |
| 355 |
local d = point.abs(c1 - c2) |
| 356 |
local ck = radical_center_(c1, p1, c2, p2) |
| 357 |
local cj = rotation_(ck, -math.pi / 2, c1) |
| 358 |
local ci = symmetry_(ck, cj) |
| 359 |
return cj, ci |
| 360 |
end |
| 361 |
|
| 362 |
function radical_center_(c1, p1, c2, p2) |
| 363 |
local d, r1, r2, h |
| 364 |
r1 = point.abs(c1 - p1) |
| 365 |
r2 = point.abs(c2 - p2) |
| 366 |
d = point.abs(c1 - c2) |
| 367 |
h = (r1 * r1 - r2 * r2 + d * d) / (2 * d) |
| 368 |
return h * (c2 - c1) / d + c1 |
| 369 |
end |
| 370 |
|
| 371 |
function radical_center3(C1, C2, C3) |
| 372 |
local t1, t2, t3, t4 |
| 373 |
t1, t2 = radical_axis_(C1.center, C1.through, C2.center, C2.through) |
| 374 |
if C3 == nil then |
| 375 |
return intersection_ll_(t1, t2, C1.center, C2.center) |
| 376 |
else |
| 377 |
t3, t4 = radical_axis_(C3.center, C3.through, C2.center, C2.through) |
| 378 |
return intersection_ll_(t1, t2, t3, t4) |
| 379 |
end |
| 380 |
end |
| 381 |
|
| 382 |
function south_pole_(c, p) |
| 383 |
return c - point(0, point.abs(c - p)) |
| 384 |
end |
| 385 |
|
| 386 |
function north_pole_(c, p) |
| 387 |
return c + point(0, point.abs(c - p)) |
| 388 |
end |
| 389 |
|
| 390 |
function antipode_(c, pt) |
| 391 |
return 2 * c - pt |
| 392 |
end |
| 393 |
|
| 394 |
function internal_similitude_(c1, r1, c2, r2) |
| 395 |
return barycenter_({ c2, r1 }, { c1, r2 }) |
| 396 |
end |
| 397 |
|
| 398 |
function external_similitude_(c1, r1, c2, r2) |
| 399 |
return barycenter_({ c2, r1 }, { c1, -r2 }) |
| 400 |
end |
| 401 |
|
| 402 |
|
| 403 |
function circlepoint_(c, t, k) |
| 404 |
local phi = 2 * k * math.pi |
| 405 |
return rotation_(c, phi, t) |
| 406 |
end |
| 407 |
|
| 408 |
-- |
| 409 |
-- midcircle_cc = midcircle_ |
| 410 |
|
| 411 |
function midcircle_cc_(o1, t1, o2, t2, EPS) |
| 412 |
EPS = EPS or tkz.epsilon |
| 413 |
local state, r, s, tg1, TG1, p, a, b, c, d, Cx, Cy, i, j, r1, r2 |
| 414 |
r1 = length_(o1, t1) |
| 415 |
r2 = length_(o2, t2) |
| 416 |
state = circles_position_(o1, r1, o2, r2, EPS) |
| 417 |
|
| 418 |
i = barycenter_({ o2, r1 }, { o1, -r2 }) |
| 419 |
j = barycenter_({ o2, r1 }, { o1, r2 }) |
| 420 |
tg1, _ = tangent_from_(o1, t1, i) |
| 421 |
TG1, _ = tangent_from_(o2, t2, i) |
| 422 |
|
| 423 |
if (state == "DISJOINT_EXT") or (state == "TANGENT_EXT") then |
| 424 |
p = math.sqrt(point.mod(i - tg1) * point.mod(i - TG1)) |
| 425 |
return circle:radius(i, p) |
| 426 |
elseif state == "SECANT" then |
| 427 |
r, s = intersection_cc_(o1, t1, o2, t2, EPS) |
| 428 |
return circle:radius(i, point.mod(r - i)), circle:radius(j, point.mod(r - j)) |
| 429 |
elseif (state == "DISJOINT_INT") or (state == "TANGENT_INT") then |
| 430 |
a, b = intersection_lc_(o1, o2, o1, t1, EPS) |
| 431 |
c, d = intersection_lc_(o1, o2, o2, t2, EPS) |
| 432 |
|
| 433 |
-- Ensure the smaller radius circle is used first |
| 434 |
local u, v, rr, ss |
| 435 |
if r1 < r2 then |
| 436 |
u, v, rr, ss = a, b, c, d |
| 437 |
else |
| 438 |
u, v, rr, ss = c, d, a, b |
| 439 |
end |
| 440 |
|
| 441 |
if in_segment_(ss, v, u) then |
| 442 |
Cx = circle:diameter(rr, v) |
| 443 |
Cy = circle:diameter(u, ss) |
| 444 |
else |
| 445 |
Cx = circle:diameter(ss, v) |
| 446 |
Cy = circle:diameter(u, rr) |
| 447 |
end |
| 448 |
|
| 449 |
-- Return the circle with the smaller radius orthogonal from j |
| 450 |
if Cx.radius < Cy.radius then |
| 451 |
return Cy:orthogonal_from(j) |
| 452 |
else |
| 453 |
return Cx:orthogonal_from(j) |
| 454 |
end |
| 455 |
end |
| 456 |
end |
| 457 |
|
| 458 |
-- Midcircle(s) between a line L and a circle C. |
| 459 |
-- Conventions used: |
| 460 |
-- - circle:new(center, through_point) |
| 461 |
-- - intersection_lc_(X, Y, O, T): line (X,Y) with circle (O,T) |
| 462 |
-- - ortho_from_(P, X, Y): line through P perpendicular to line (X,Y) |
| 463 |
-- - distance_(a, b, P): distance from point P to line L =(a,b) |
| 464 |
-- - antipode_(O, S): point opposite to S on circle centered at O and through S (your helper) |
| 465 |
-- |
| 466 |
-- Returns: |
| 467 |
-- * secant case: two circles (centered at A and B) passing through S1 (hence tangent to L at S1) |
| 468 |
-- * tangent case: your original behavior kept (center at antipode of T, through T) |
| 469 |
-- * disjoint case: one (or two) circles of radius R = sqrt(2*r*d), with centers found |
| 470 |
-- geometrically as intersections of C with parallels to L at distance R. |
| 471 |
|
| 472 |
-- Midcircle between a circle C(O,r) and a line L |
| 473 |
-- Returns: |
| 474 |
-- - disjoint: two circles (centers A and B on OH) |
| 475 |
-- - tangent : one circle (center K = antipode of S on C) |
| 476 |
-- - secant : two circles (centers A and B through an intersection S) |
| 477 |
function midcircle_cl_(O, T, x, y, EPS) |
| 478 |
EPS = EPS or tkz.epsilon |
| 479 |
local r = length_(T, O) |
| 480 |
|
| 481 |
-- Foot H of the perpendicular from O to line (x,y) |
| 482 |
local H = projection_(x, y, O) |
| 483 |
|
| 484 |
-- A,B = intersections of line (O,H) with circle C |
| 485 |
local A, B = intersection_lc_(O, H, O, T, EPS) |
| 486 |
-- Stable ordering: make A the farther from H |
| 487 |
if length_(B, H) > length_(A, H) then A, B = B, A end |
| 488 |
|
| 489 |
-- Intersections of L with C (nil if disjoint; doubled if tangent) |
| 490 |
local S1, S2 = intersection_lc_(x, y, O, T, EPS) |
| 491 |
local S = S1 or S2 |
| 492 |
|
| 493 |
-- test "tangent" via d(O, L) ≈ r |
| 494 |
local OL = distance_(x, y, O) |
| 495 |
local is_tangent = math.abs(OL - r) <= EPS |
| 496 |
|
| 497 |
-- Disjoint case: TWO solutions (centers A and B) |
| 498 |
if not S then |
| 499 |
-- Power condition → R^2 = 2 r · d(center, L) |
| 500 |
local AL = distance_(x, y, A) |
| 501 |
local BL = distance_(x, y, B) |
| 502 |
local R1 = math.sqrt(2 * r * AL) |
| 503 |
local R2 = math.sqrt(2 * r * BL) |
| 504 |
return circle:radius(A, R1), circle:radius(B, R2) |
| 505 |
end |
| 506 |
|
| 507 |
-- Tangent case: ONE solution |
| 508 |
if is_tangent then |
| 509 |
local K = antipode_(O, S1) -- S1==S2 |
| 510 |
return circle:new(K, S1) |
| 511 |
end |
| 512 |
|
| 513 |
-- Secant case: TWO solutions (pick either S1 or S2, both give same radii) |
| 514 |
local Sref = S1 |
| 515 |
return circle:new(A, Sref), circle:new(B, Sref) |
| 516 |
end |
| 517 |
|
| 518 |
|
| 519 |
--- =========== Common tangents of two circles =======----- |
| 520 |
function common_tangent_(C1, C2, mode, EPS) |
| 521 |
EPS = EPS or tkz.epsilon |
| 522 |
mode = mode or "external" -- "external" | "internal" | "both" |
| 523 |
|
| 524 |
local A, rA = C1.center, C1.radius |
| 525 |
local B, rB = C2.center, C2.radius |
| 526 |
|
| 527 |
local pos = circles_position_(A, rA, B, rB, EPS) |
| 528 |
local S_ext = external_similitude_(A, rA, B, rB) |
| 529 |
local S_int = internal_similitude_(A, rA, B, rB) |
| 530 |
|
| 531 |
local function equal_radii() |
| 532 |
return math.abs(rA - rB) < EPS |
| 533 |
end |
| 534 |
|
| 535 |
-- --------- Helpers factorisés ---------- |
| 536 |
local function pair_with_inout(S, t1a, t1b, t2a, t2b) |
| 537 |
if line_in_out_(t1a, S, t2a) then |
| 538 |
return t1a, t2a, t1b, t2b |
| 539 |
else |
| 540 |
return t1a, t2b, t1b, t2a |
| 541 |
end |
| 542 |
end |
| 543 |
|
| 544 |
local function externals_via_S(S) |
| 545 |
local t1a, t1b = tangent_from_(A, C1.through, S) |
| 546 |
local t2a, t2b = tangent_from_(B, C2.through, S) |
| 547 |
local P1, Q1, P2, Q2 = pair_with_inout(S, t1a, t1b, t2a, t2b) |
| 548 |
return P1, Q1, P2, Q2 |
| 549 |
end |
| 550 |
|
| 551 |
local function externals_equal_radii() |
| 552 |
-- S_ext à l’infini → deux tangentes externes parallèles |
| 553 |
local v = (B - A) |
| 554 |
local P1 = v:orthogonal(rA):at(A) |
| 555 |
local Q1 = v:orthogonal(rA):at(B) |
| 556 |
local P2 = v:orthogonal(-rA):at(A) |
| 557 |
local Q2 = v:orthogonal(-rA):at(B) |
| 558 |
return P1, Q1, P2, Q2 |
| 559 |
end |
| 560 |
|
| 561 |
local function internals_via_S(S) |
| 562 |
local t1a, t1b = tangent_from_(A, C1.through, S) |
| 563 |
local t2a, t2b = tangent_from_(B, C2.through, S) |
| 564 |
local P1, Q1, P2, Q2 = pair_with_inout(S, t1a, t1b, t2a, t2b) |
| 565 |
return P1, Q1, P2, Q2 |
| 566 |
end |
| 567 |
|
| 568 |
local function internals_equal_radii() |
| 569 |
-- rA == rB : S_int = milieu(AB) → on passe par la voie générale |
| 570 |
return internals_via_S(S_int) |
| 571 |
end |
| 572 |
|
| 573 |
local function one_tangent_proc() |
| 574 |
-- Cas “une seule tangente commune” (cercles tangents) |
| 575 |
local T = intersection(C1, C2) -- (unique) point de contact |
| 576 |
local U = T:shift_orthogonal_to(A, rA) -- point distinct sur la tangente en T à C1 |
| 577 |
return T, U, T, U |
| 578 |
end |
| 579 |
|
| 580 |
-- --------- Procédures par position ---------- |
| 581 |
local function inside_proc() |
| 582 |
-- Aucune tangente commune |
| 583 |
return nil, nil |
| 584 |
end |
| 585 |
|
| 586 |
local function intersect_proc() |
| 587 |
-- Deux tangentes externes seulement |
| 588 |
if mode == "internal" then |
| 589 |
tex.error("internal tangents non-existent (intersect)") |
| 590 |
return nil, nil |
| 591 |
end |
| 592 |
if equal_radii() then |
| 593 |
return externals_equal_radii() |
| 594 |
else |
| 595 |
return externals_via_S(S_ext) |
| 596 |
end |
| 597 |
end |
| 598 |
|
| 599 |
local function outside_tangent_proc() |
| 600 |
-- Cercles tangents extérieurement. |
| 601 |
if mode == "internal" or mode == "both" then |
| 602 |
-- La famille "interne" dégénère : une seule tangente commune (au point de contact). |
| 603 |
return one_tangent_proc() |
| 604 |
elseif mode == "external" then |
| 605 |
-- Les deux tangentes externes existent toujours. |
| 606 |
if equal_radii() then |
| 607 |
return externals_equal_radii() -- deux parallèles si rA == rB |
| 608 |
else |
| 609 |
return externals_via_S(S_ext) -- deux tangentes via le centre de similitude externe |
| 610 |
end |
| 611 |
else |
| 612 |
-- Par défaut, on suit le comportement "external". |
| 613 |
if equal_radii() then |
| 614 |
return externals_equal_radii() |
| 615 |
else |
| 616 |
return externals_via_S(S_ext) |
| 617 |
end |
| 618 |
end |
| 619 |
end |
| 620 |
|
| 621 |
|
| 622 |
local function inside_tangent_proc() |
| 623 |
-- Cercles tangents intérieurement. |
| 624 |
if mode == "external" then |
| 625 |
tex.error("external tangents non-existent (inside tangent)") |
| 626 |
return nil, nil, nil, nil |
| 627 |
end |
| 628 |
-- La famille "interne" dégénère : une seule tangente au point de contact. |
| 629 |
return one_tangent_proc() |
| 630 |
end |
| 631 |
|
| 632 |
local function outside_proc() |
| 633 |
-- Les 4 existent (2 externes + 2 internes). On respecte 'mode'. |
| 634 |
if mode == "internal" then |
| 635 |
if equal_radii() then |
| 636 |
return internals_equal_radii() |
| 637 |
else |
| 638 |
return internals_via_S(S_int) |
| 639 |
end |
| 640 |
elseif mode == "external" then |
| 641 |
if equal_radii() then |
| 642 |
return externals_equal_radii() |
| 643 |
else |
| 644 |
return externals_via_S(S_ext) |
| 645 |
end |
| 646 |
end |
| 647 |
end |
| 648 |
|
| 649 |
|
| 650 |
-- --------- Dispatcher clair ---------- |
| 651 |
if pos == "DISJOINT_INT" then return inside_proc() |
| 652 |
elseif pos == "SECANT" then return intersect_proc() |
| 653 |
elseif pos == "TANGENT_INT" then return inside_tangent_proc() |
| 654 |
elseif pos == "TANGENT_EXT" then return outside_tangent_proc() |
| 655 |
elseif pos == "DISJOINT_EXT" then return outside_proc() |
| 656 |
end |
| 657 |
|
| 658 |
tex.error("common_tangent_: invalid configuration ('"..tostring(pos).."', mode='"..tostring(mode).."').") |
| 659 |
return nil, nil, nil, nil |
| 660 |
end |
| 661 |
|
| 662 |
-- nouvel ajout for CLL |
| 663 |
--====== CLL =-=====- |
| 664 |
|
| 665 |
-- ==== Helpers for CLL ==== --- |
| 666 |
|
| 667 |
function orient_ray_(i, a, b) |
| 668 |
return (a - i) ^ (b - i) |
| 669 |
end |
| 670 |
|
| 671 |
function left_of_ray_(i, a, p) |
| 672 |
return orient_ray_(i, a, p) > 0 |
| 673 |
end |
| 674 |
|
| 675 |
function point_in_open_sector_(i, a, b, p) |
| 676 |
return left_of_ray_(i, a, p) and not left_of_ray_(i, b, p) |
| 677 |
end |
| 678 |
|
| 679 |
function circle_single_sector_(i, u, up, v, vp, o, r) |
| 680 |
local S = { |
| 681 |
{u, v}, |
| 682 |
{v, up}, |
| 683 |
{up, vp}, |
| 684 |
{vp, u}, |
| 685 |
} |
| 686 |
for k = 1, 4 do |
| 687 |
local a, b = S[k][1], S[k][2] |
| 688 |
if point_in_open_sector_(i, a, b, o) then |
| 689 |
local dA = distance_(i, a, o) |
| 690 |
local dB = distance_(i, b, o) |
| 691 |
if dA >= r and dB >= r then |
| 692 |
return k |
| 693 |
end |
| 694 |
end |
| 695 |
end |
| 696 |
return nil |
| 697 |
end |
| 698 |
|
| 699 |
|
| 700 |
function circle_touched_sectors_(i, u, up, v, vp, o, r) |
| 701 |
|
| 702 |
if (o - i):mod() <= r + tkz.epsilon then |
| 703 |
return {1, 2, 3, 4} |
| 704 |
end |
| 705 |
|
| 706 |
local adjacent = { |
| 707 |
u = {4, 1}, |
| 708 |
v = {1, 2}, |
| 709 |
up = {2, 3}, |
| 710 |
vp = {3, 4}, |
| 711 |
} |
| 712 |
|
| 713 |
local hit = { false, false, false, false } |
| 714 |
|
| 715 |
local k0 = (function() |
| 716 |
if point_in_open_sector_(i, u, v, o) then return 1 |
| 717 |
elseif point_in_open_sector_(i, v, up, o) then return 2 |
| 718 |
elseif point_in_open_sector_(i, up, vp, o) then return 3 |
| 719 |
elseif point_in_open_sector_(i, vp, u, o) then return 4 |
| 720 |
else return nil end |
| 721 |
end)() |
| 722 |
if k0 then hit[k0] = true end |
| 723 |
|
| 724 |
function touch_border_(ray_label, a) |
| 725 |
local d = distance_(i, a, o) |
| 726 |
if d <= r + tkz.epsilon then |
| 727 |
local s1, s2 = adjacent[ray_label][1], adjacent[ray_label][2] |
| 728 |
hit[s1], hit[s2] = true, true |
| 729 |
end |
| 730 |
end |
| 731 |
|
| 732 |
touch_border_('u', u) |
| 733 |
touch_border_('v', v) |
| 734 |
touch_border_('up', up) |
| 735 |
touch_border_('vp', vp) |
| 736 |
|
| 737 |
local out = {} |
| 738 |
for k = 1, 4 do |
| 739 |
if hit[k] then out[#out+1] = k end |
| 740 |
end |
| 741 |
return out |
| 742 |
end |
| 743 |
|
| 744 |
function classify_by_sector_(i, u, up, v, vp, circles) |
| 745 |
local buckets = { [1]={}, [2]={}, [3]={}, [4]={} } |
| 746 |
for _, C in ipairs(circles) do |
| 747 |
local w = C.center |
| 748 |
local k = |
| 749 |
(point_in_open_sector_(i, u, v, w) and 1) or |
| 750 |
(point_in_open_sector_(i, v, up, w) and 2) or |
| 751 |
(point_in_open_sector_(i, up, vp, w) and 3) or |
| 752 |
(point_in_open_sector_(i, vp, u, w) and 4) or |
| 753 |
nil |
| 754 |
if k then table.insert(buckets[k], C) end |
| 755 |
-- (cas limites: si w est sur un bord, à traiter plus tard comme tu l’as noté) |
| 756 |
end |
| 757 |
return buckets |
| 758 |
end |
| 759 |
|
| 760 |
function circle_sector_signature_(i, u, up, v, vp, o, r) |
| 761 |
|
| 762 |
-- 0) cercle contenant le sommet i -> 4 secteurs |
| 763 |
if (o - i):mod() <= r + tkz.epsilon then |
| 764 |
return {1, 2, 3, 4} |
| 765 |
end |
| 766 |
|
| 767 |
-- helper orientation strict / non-strict |
| 768 |
local function in_open(i,a,b,p) |
| 769 |
return ((a - i) ^ (p - i)) > 0 and |
| 770 |
((b - i) ^ (p - i)) <= 0 |
| 771 |
end |
| 772 |
|
| 773 |
-- 1) secteur strict du centre (k0) |
| 774 |
local k0 = |
| 775 |
(in_open(i, u, v, o) and 1) or |
| 776 |
(in_open(i, v, up, o) and 2) or |
| 777 |
(in_open(i, up, vp, o) and 3) or |
| 778 |
(in_open(i, vp, u, o) and 4) or |
| 779 |
0 -- 0 = centre sur un bord (cas limite) |
| 780 |
|
| 781 |
-- 2) distances aux DROITES sous-jacentes (pas aux 4 demi-droites) |
| 782 |
-- L1 = (a,b) porte u/up ; L2 = (c,d) porte v/vp |
| 783 |
-- IMPORTANT : une seule fois par droite. |
| 784 |
local D1 = distance_(u, up, o) -- équiv. distance à la droite de L1 |
| 785 |
local D2 = distance_(v, vp, o) -- équiv. distance à la droite de L2 |
| 786 |
local T1 = D1 <= r + tkz.epsilon |
| 787 |
local T2 = D2 <= r + tkz.epsilon |
| 788 |
|
| 789 |
-- 3) si k0 connu : ne considérer que les deux bords qui bornent k0 |
| 790 |
if k0 == 1 then |
| 791 |
if T1 and not T2 then return {4, 1} -- touche u |
| 792 |
elseif T2 and not T1 then return {1, 2 } -- touche v |
| 793 |
elseif T1 and T2 then return {4, 1, 2} -- touche u et v (3 secteurs, cas réel) |
| 794 |
else return {1} |
| 795 |
end |
| 796 |
elseif k0 == 2 then |
| 797 |
if T2 and not T1 then return {1, 2} -- v |
| 798 |
elseif T1 and not T2 then return {2, 3} -- up |
| 799 |
elseif T1 and T2 then return {1, 2, 3} |
| 800 |
else return {2} |
| 801 |
end |
| 802 |
elseif k0 == 3 then |
| 803 |
if T1 and not T2 then return {2, 3} -- up |
| 804 |
elseif T2 and not T1 then return {3, 4} -- vp |
| 805 |
elseif T1 and T2 then return {2, 3, 4} |
| 806 |
else return {3} |
| 807 |
end |
| 808 |
elseif k0 == 4 then |
| 809 |
if T2 and not T1 then return {3, 4} -- vp |
| 810 |
elseif T1 and not T2 then return {4, 1} -- u |
| 811 |
elseif T1 and T2 then return {3, 4, 1} |
| 812 |
else return {4} |
| 813 |
end |
| 814 |
end |
| 815 |
|
| 816 |
-- 4) cas limite : centre sur un bord. |
| 817 |
-- Éviter le double-compte en regroupant par DROITE : |
| 818 |
local Tu = distance_(i, u, o) <= r + tkz.epsilon |
| 819 |
local Tv = distance_(i, v, o) <= r + tkz.epsilon |
| 820 |
local Tup = distance_(i, up, o) <= r + tkz.epsilon |
| 821 |
local Tvp = distance_(i, vp, o) <= r + tkz.epsilon |
| 822 |
local L1_touched = (Tu or Tup) -- une seule fois pour la droite L1 |
| 823 |
local L2_touched = (Tv or Tvp) -- idem pour L2 |
| 824 |
|
| 825 |
if L1_touched and not L2_touched then |
| 826 |
-- tangence à la seule L1 -> exactement 2 secteurs, |
| 827 |
-- choisir la paire adjacente qui correspond au côté où se trouve o. |
| 828 |
-- On discrimine avec le secteur "à droite" du bord considéré. |
| 829 |
-- Ici, renvoyer la paire la plus sûre : "4/1" (côté u) |
| 830 |
return {4, 1} |
| 831 |
elseif L2_touched and not L1_touched then |
| 832 |
return {1, 2} |
| 833 |
elseif L1_touched and L2_touched then |
| 834 |
-- tangence aux deux droites alors que le centre est sur un bord : |
| 835 |
-- raisonnablement 3 secteurs (géométrie réelle) |
| 836 |
return {4, 1, 2} |
| 837 |
else |
| 838 |
-- pas de tangence détectée → considérer 1 secteur voisin |
| 839 |
return {1} |
| 840 |
end |
| 841 |
end |
| 842 |
|
| 843 |
---- ==== end helpers for CLL ====----- |
| 844 |
|
| 845 |
function lines_secant_circle_(o, r, a, b, c, d) |
| 846 |
local i = intersection_ll_(a, b, c, d) |
| 847 |
if not i then |
| 848 |
-- fallback (en théorie jamais atteint si la détection est faite au-dessus) |
| 849 |
return lines_parallel_circle_(o, r, a, b, c, d) |
| 850 |
end |
| 851 |
local s = ((b - a) ^ (d - c)) >= 0 and 1 or -1 |
| 852 |
local u = report_(a, b, 1, i) |
| 853 |
local up = report_(a, b, -1, i) |
| 854 |
local v = report_(c, d, s, i) |
| 855 |
local vp = report_(c, d, -s, i) |
| 856 |
return circle_sector_signature_(i, u, up, v, vp, o, r) |
| 857 |
end |
| 858 |
-- L1=(a,b), L2=(c,d), cercle=(o,r) |
| 859 |
-- Secteurs : 1=entre L1/L2, 2=outside L1, 3=outside L2 |
| 860 |
function lines_parallel_circle_(o, r, a, b, c, d) |
| 861 |
local d1 = distance_(a, b, o) -- dist(o, L1) |
| 862 |
local d2 = distance_(c, d, o) -- dist(o, L2) |
| 863 |
local D = distance_(a, b, c) -- écart des parallèles |
| 864 |
|
| 865 |
local inside_strip = tkz.approx(d1 + d2, D) -- o entre L1 et L2 |
| 866 |
local outside_strip = tkz.approx(math.abs(d1 - d2), D) -- o du même côté |
| 867 |
|
| 868 |
local mn, mx = math.min(d1, d2), math.max(d1, d2) |
| 869 |
local nearer_is_L1 = (d1 <= d2) -- en cas d’égalité, côté L1 |
| 870 |
local OUT = nearer_is_L1 and 2 or 3 -- secteur extérieur du côté proche |
| 871 |
|
| 872 |
if inside_strip then |
| 873 |
if r < mn and not tkz.approx(r, mn) then return {1} end |
| 874 |
if tkz.approx(r, mn) then return {1, OUT} end |
| 875 |
if r > mn and r < mx then return {1, OUT} end |
| 876 |
-- r ≥ mx (tangence à la plus éloignée ou dépassement) |
| 877 |
return {1,2,3} |
| 878 |
end |
| 879 |
|
| 880 |
if outside_strip then |
| 881 |
if r < mn and not tkz.approx(r, mn) then return {OUT} end |
| 882 |
if tkz.approx(r, mn) then return {OUT,1} end |
| 883 |
if r > mn and r < mx then return {OUT,1} end |
| 884 |
-- r ≥ mx |
| 885 |
return {1,2,3} |
| 886 |
end |
| 887 |
|
| 888 |
-- zone grise numérique : choisir le scénario le plus proche |
| 889 |
if math.abs((d1 + d2) - D) <= math.abs(math.abs(d1 - d2) - D) then |
| 890 |
-- traiter comme inside_strip |
| 891 |
if r < mn and not tkz.approx(r, mn) then return {1} end |
| 892 |
if tkz.approx(r, mn) then return {1, OUT} end |
| 893 |
if r > mn and r < mx then return {1, OUT} end |
| 894 |
return {1,2,3} |
| 895 |
else |
| 896 |
-- traiter comme outside_strip |
| 897 |
if r < mn and not tkz.approx(r, mn) then return {OUT} end |
| 898 |
if tkz.approx(r, mn) then return {OUT,1} end |
| 899 |
if r > mn and r < mx then return {OUT,1} end |
| 900 |
return {1,2,3} |
| 901 |
end |
| 902 |
end |