| 1 |
-- File: tkz_elements_circle.lua |
| 2 |
-- Copyright (c) 2026 Alain Matthes |
| 3 |
-- SPDX-License-Identifier: LPPL-1.3c |
| 4 |
-- Maintainer: Alain Matthes |
| 5 |
--------------------------------------------------------------------------- |
| 6 |
-- circles |
| 7 |
--------------------------------------------------------------------------- |
| 8 |
circle = {} |
| 9 |
circle.__index = circle |
| 10 |
function circle:new(c, t) -- c --> center t --> through |
| 11 |
local type = "circle" |
| 12 |
local ct = line:new(c, t) |
| 13 |
local opp = antipode_(c, t) |
| 14 |
local radius = point.abs(c - t) |
| 15 |
local south = c - point(0, radius) |
| 16 |
local east = c + point(radius, 0) |
| 17 |
local north = c + point(0, radius) |
| 18 |
local west = c - point(radius, 0) |
| 19 |
local perimeter = tkz.tau * radius |
| 20 |
local area = 0.5 * tkz.tau * radius * radius |
| 21 |
local cir = { |
| 22 |
center = c, |
| 23 |
through = t, |
| 24 |
ct = ct, |
| 25 |
opp = opp, |
| 26 |
radius = radius, |
| 27 |
south = south, |
| 28 |
east = east, |
| 29 |
north = north, |
| 30 |
west = west, |
| 31 |
type = type, |
| 32 |
perimeter = perimeter, |
| 33 |
area = area, |
| 34 |
} |
| 35 |
setmetatable(cir, self) |
| 36 |
return cir |
| 37 |
end |
| 38 |
|
| 39 |
setmetatable(circle, { |
| 40 |
__call = function(cls, ...) |
| 41 |
return cls:new(...) |
| 42 |
end, |
| 43 |
}) |
| 44 |
|
| 45 |
function circle:get(i) |
| 46 |
if i == nil then |
| 47 |
return self.center, self.through |
| 48 |
elseif i == 1 then |
| 49 |
return self.center |
| 50 |
elseif i == 2 then |
| 51 |
return self.through |
| 52 |
else |
| 53 |
return nil |
| 54 |
end |
| 55 |
end |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
-- other definition |
| 60 |
function circle:radius(center, radius) |
| 61 |
return circle:new(center, center + point(radius, 0)) |
| 62 |
end |
| 63 |
|
| 64 |
function circle:diameter(za, zb) |
| 65 |
return circle:new(midpoint_(za, zb), zb) |
| 66 |
end |
| 67 |
----------------------- |
| 68 |
-- Result -> boolean |
| 69 |
----------------------- |
| 70 |
function circle:on_circle(p, EPS) |
| 71 |
return on_circle_(self.center, self.through, p, EPS) |
| 72 |
end |
| 73 |
|
| 74 |
function circle:in_disk(p, EPS) |
| 75 |
return in_disk_(self.center, self.through, p, EPS) |
| 76 |
end |
| 77 |
|
| 78 |
function circle:in_disk_strict(p, EPS) |
| 79 |
return in_disk_strict_(self.center, self.through, p, EPS) |
| 80 |
end |
| 81 |
|
| 82 |
function circle:out_disk_strict(p, EPS) |
| 83 |
return out_disk_strict_(self.center, self.through, p, EPS) |
| 84 |
end |
| 85 |
|
| 86 |
circle.in_out = circle.on_circle |
| 87 |
circle.in_out_disk = circle.in_disk |
| 88 |
circle.in_out_disk_strict = circle.in_disk_strict |
| 89 |
|
| 90 |
|
| 91 |
function circle:position(obj, EPS) |
| 92 |
EPS = EPS or tkz.epsilon |
| 93 |
local mt = getmetatable(obj) |
| 94 |
|
| 95 |
if mt == point then |
| 96 |
return circle_point_position_(self, obj, EPS) |
| 97 |
|
| 98 |
elseif mt == line then |
| 99 |
return line_circle_position_(obj, self, EPS) |
| 100 |
|
| 101 |
elseif mt == circle then |
| 102 |
return circles_position_(self.center, self.radius, |
| 103 |
obj.center, obj.radius, EPS) |
| 104 |
end |
| 105 |
|
| 106 |
error("Unsupported object in circle:position()") |
| 107 |
end |
| 108 |
|
| 109 |
-- legacy alias (keep for backward compatibility) |
| 110 |
circle.point_position = circle.position |
| 111 |
|
| 112 |
-- Disk (closed): IN / OUT (boundary counted as IN) |
| 113 |
function circle:position_disk(p, EPS) |
| 114 |
EPS = EPS or tkz.epsilon |
| 115 |
local d = point.abs(p - self.center) |
| 116 |
local r = point.abs(self.through - self.center) |
| 117 |
if d <= r + EPS then |
| 118 |
return "IN" |
| 119 |
else |
| 120 |
return "OUT" |
| 121 |
end |
| 122 |
end |
| 123 |
|
| 124 |
-- Boolean shortcuts (same default tolerance) |
| 125 |
function circle:is_disjoint(L) |
| 126 |
return self:line_position(L) == "DISJOINT" |
| 127 |
end |
| 128 |
|
| 129 |
function circle:is_tangent(L, EPS) |
| 130 |
return self:line_position(L, EPS) == "TANGENT" |
| 131 |
end |
| 132 |
|
| 133 |
function circle:is_secant(L, EPS) |
| 134 |
return self:line_position(L, EPS) == "SECANT" |
| 135 |
end |
| 136 |
|
| 137 |
function circle:line_position(L, EPS) |
| 138 |
EPS = EPS or tkz.epsilon |
| 139 |
return line_position_(self.center, self.radius, L.pa, L.pb, EPS) |
| 140 |
end |
| 141 |
|
| 142 |
-- Relative position between two lines and a circle- |
| 143 |
|
| 144 |
function circle:lines_position(L1, L2, mode) |
| 145 |
local a, b = L1:get() |
| 146 |
local c, d = L2:get() |
| 147 |
local o = self.center |
| 148 |
local r = self.radius |
| 149 |
|
| 150 |
if mode == "parallel" or is_parallel_(a, b, c, d) then |
| 151 |
return lines_parallel_circle_(o, r, a, b, c, d) |
| 152 |
else |
| 153 |
return lines_secant_circle_(o, r, a, b, c, d) |
| 154 |
end |
| 155 |
end |
| 156 |
|
| 157 |
function circle:are_circles_tangents(C, EPS) |
| 158 |
return act_(self.center, self.through, C.center, C.through, EPS) |
| 159 |
end |
| 160 |
|
| 161 |
------------------------ |
| 162 |
-- string -------------- |
| 163 |
------------------------ |
| 164 |
function circle:position(obj, EPS) |
| 165 |
EPS = EPS or tkz.epsilon |
| 166 |
local mt = getmetatable(obj) |
| 167 |
|
| 168 |
if mt == point then |
| 169 |
return circle_point_position_(self, obj, EPS) |
| 170 |
|
| 171 |
elseif mt == line then |
| 172 |
return line_circle_position_(obj, self, EPS) |
| 173 |
|
| 174 |
elseif mt == circle then |
| 175 |
return circles_position_(self.center, self.radius, |
| 176 |
obj.center, obj.radius, EPS) |
| 177 |
end |
| 178 |
|
| 179 |
error("Unsupported object in circle:position()") |
| 180 |
end |
| 181 |
|
| 182 |
|
| 183 |
function circle:circles_position(C, EPS) |
| 184 |
return circles_position_(self.center, self.radius, C.center, C.radius, EPS) |
| 185 |
end |
| 186 |
|
| 187 |
----------------------- |
| 188 |
-- Result -> real |
| 189 |
----------------------- |
| 190 |
function circle:power(pt) |
| 191 |
local d = point.abs(self.center - pt) |
| 192 |
return d * d - self.radius * self.radius |
| 193 |
end |
| 194 |
----------------------- |
| 195 |
-- Result -> point |
| 196 |
----------------------- |
| 197 |
function circle:antipode(pt) |
| 198 |
return 2 * self.center - pt |
| 199 |
end |
| 200 |
|
| 201 |
function circle:midarc(z1, z2) |
| 202 |
local phi = 0.5 * get_angle_normalize_(self.center, z1, z2) |
| 203 |
return rotation_(self.center, phi, z1) |
| 204 |
end |
| 205 |
|
| 206 |
-- function circle:point(t) |
| 207 |
-- local phi = 2 * t * math.pi |
| 208 |
-- return rotation_(self.center, phi, self.through) |
| 209 |
-- end |
| 210 |
|
| 211 |
function circle:point(t, P, EPS) |
| 212 |
local phi = 2 * t * math.pi |
| 213 |
local A = P or self.through |
| 214 |
EPS = EPS or tkz.epsilon |
| 215 |
|
| 216 |
-- Vérification avec le modèle "position" |
| 217 |
if self:position(A, EPS) ~= "ON" then |
| 218 |
tex.error("Point not on circle in circle:point") |
| 219 |
return nil |
| 220 |
end |
| 221 |
|
| 222 |
return rotation_(self.center, phi, A) |
| 223 |
end |
| 224 |
|
| 225 |
|
| 226 |
function circle:random(inside) |
| 227 |
inside = (inside == "inside") |
| 228 |
if inside then |
| 229 |
local x = self:point(math.random()) |
| 230 |
local L = line:new(self.center, x) |
| 231 |
return L:random() |
| 232 |
else |
| 233 |
return self:point(math.random()) |
| 234 |
end |
| 235 |
end |
| 236 |
|
| 237 |
|
| 238 |
function circle:internal_similitude(C) |
| 239 |
return internal_similitude_(self.center, self.radius, C.center, C.radius) |
| 240 |
end |
| 241 |
|
| 242 |
function circle:external_similitude(C) |
| 243 |
return external_similitude_(self.center, self.radius, C.center, C.radius) |
| 244 |
end |
| 245 |
|
| 246 |
function circle:similitude(C, mode) |
| 247 |
mode = mode or "internal" |
| 248 |
local s = (mode == "external") and -1 or 1 |
| 249 |
|
| 250 |
local c1, r1 = self.center, self.radius |
| 251 |
local c2, r2 = C.center, C.radius |
| 252 |
|
| 253 |
return barycenter_({ c2, r1 }, { c1, s * r2 }) |
| 254 |
end |
| 255 |
|
| 256 |
|
| 257 |
function circle:radical_center(C1, C2) |
| 258 |
if C2 == nil then |
| 259 |
if self.radius > C1.radius then |
| 260 |
return radical_center_(self.center, self.through, C1.center, C1.through) |
| 261 |
else |
| 262 |
return radical_center_(C1.center, C1.through, self.center, self.through) |
| 263 |
end |
| 264 |
else |
| 265 |
return radical_center3(self, C1, C2) |
| 266 |
end |
| 267 |
end |
| 268 |
|
| 269 |
-- Pôle d’une droite L par rapport au cercle self |
| 270 |
-- Retour : le point P (ou nil,"INFINITE" si L passe par O) |
| 271 |
function circle:pole(L, EPS) |
| 272 |
EPS = EPS or tkz.epsilon |
| 273 |
local O = self.center |
| 274 |
local R = self.radius |
| 275 |
|
| 276 |
-- 1) H = projection de O sur L |
| 277 |
local H = L:projection(O) |
| 278 |
if not H then |
| 279 |
return nil, "NO_PROJECTION" -- cas pathologique |
| 280 |
end |
| 281 |
|
| 282 |
-- 2) Si L passe par O -> pôle à l’infini (direction ⟂ L) |
| 283 |
if length_(O, H) < EPS then |
| 284 |
return nil, "INFINITE" -- le pôle est au point à l’infini |
| 285 |
end |
| 286 |
|
| 287 |
-- 3) P = inversion de H dans le cercle (centre O, rayon R) |
| 288 |
-- OP * OH = R^2, P sur la demi-droite OH |
| 289 |
local P = self:inversion(H) |
| 290 |
return P |
| 291 |
end |
| 292 |
|
| 293 |
|
| 294 |
----------------------- |
| 295 |
-- Result -> line |
| 296 |
----------------------- |
| 297 |
function circle:tangent_at(pt) |
| 298 |
return line:new(rotation_(pt, math.pi / 2, self.center), rotation_(pt, -math.pi / 2, self.center)) |
| 299 |
end |
| 300 |
|
| 301 |
function circle:tangent_from(pt) |
| 302 |
local t1, t2 = tangent_from_(self.center, self.through, pt) |
| 303 |
return line:new(pt, t1), line:new(pt, t2) |
| 304 |
end |
| 305 |
|
| 306 |
function circle:tangent_parallel(L) |
| 307 |
local LP = L:orthogonal_from(self.center) |
| 308 |
local X, Y = intersection(LP, self) |
| 309 |
return self:tangent_at(X), self:tangent_at(Y) |
| 310 |
end |
| 311 |
|
| 312 |
function circle:radical_axis(C) |
| 313 |
local t1, t2 |
| 314 |
if self.radius > C.radius then |
| 315 |
t1, t2 = radical_axis_(self.center, self.through, C.center, C.through) |
| 316 |
else |
| 317 |
t1, t2 = radical_axis_(C.center, C.through, self.center, self.through) |
| 318 |
end |
| 319 |
return line:new(t1, t2) |
| 320 |
end |
| 321 |
|
| 322 |
function circle:polar(p) |
| 323 |
local q = self:inversion(p) |
| 324 |
local qa = (p - q):orthogonal(1):at(q) |
| 325 |
local qb = (q - p):orthogonal(1):at(q) |
| 326 |
return line:new(qa, qb) |
| 327 |
end |
| 328 |
|
| 329 |
function circle:external_tangent(C) |
| 330 |
local i, t1, t2, k, T1, T2 |
| 331 |
-- Find the barycenter of the two circles |
| 332 |
i = barycenter_({ C.center, self.radius }, { self.center, -C.radius }) |
| 333 |
|
| 334 |
-- Calculate the tangents from the circle to the point of intersection |
| 335 |
t1, t2 = tangent_from_(self.center, self.through, i) |
| 336 |
|
| 337 |
-- Calculate the scaling factor for the homothety |
| 338 |
k = point.mod((C.center - i) / (self.center - i)) |
| 339 |
|
| 340 |
-- Apply homothety to the tangents |
| 341 |
T1 = homothety_(i, k, t1) |
| 342 |
T2 = homothety_(i, k, t2) |
| 343 |
|
| 344 |
-- Return the two tangent lines |
| 345 |
return line:new(t1, T1), line:new(t2, T2) |
| 346 |
end |
| 347 |
|
| 348 |
function circle:internal_tangent(C) |
| 349 |
local i, t1, t2, k, T1, T2 |
| 350 |
-- Find the barycenter of the two circles with opposite signs for radii |
| 351 |
i = barycenter_({ C.center, self.radius }, { self.center, C.radius }) |
| 352 |
|
| 353 |
-- Calculate the tangents from the circle to the point of intersection |
| 354 |
t1, t2 = tangent_from_(self.center, self.through, i) |
| 355 |
|
| 356 |
-- Calculate the scaling factor for the homothety |
| 357 |
k = -point.mod((C.center - i) / (self.center - i)) |
| 358 |
|
| 359 |
-- Apply homothety to the tangents |
| 360 |
T1 = homothety_(i, k, t1) |
| 361 |
T2 = homothety_(i, k, t2) |
| 362 |
|
| 363 |
-- Return the two tangent lines |
| 364 |
return line:new(t1, T1), line:new(t2, T2) |
| 365 |
end |
| 366 |
|
| 367 |
function circle:common_tangent(C, mode, EPS) |
| 368 |
local x, y, z, t = common_tangent_(self, C, mode, EPS) |
| 369 |
return line:new(x, y), line:new(z, t) |
| 370 |
end |
| 371 |
|
| 372 |
----------------------- |
| 373 |
-- Result -> circle |
| 374 |
----------------------- |
| 375 |
--[[ |
| 376 |
Constructs a circle orthogonal to the current circle (`self`) and passing through a given external point `pt`. |
| 377 |
|
| 378 |
The resulting circle: |
| 379 |
- has its center at the given point `pt`, |
| 380 |
- is orthogonal to `self`, |
| 381 |
- and uses the distance from `pt` to a point of tangency as its radius. |
| 382 |
|
| 383 |
Geometric condition: |
| 384 |
- The given point `pt` must lie **outside** the current circle. |
| 385 |
- This is verified using `in_out_disk`. |
| 386 |
|
| 387 |
If the condition is not met, the function raises a TeX error. |
| 388 |
|
| 389 |
Returns: |
| 390 |
A new circle (center = `pt`, radius = distance to the tangent point). |
| 391 |
--]] |
| 392 |
|
| 393 |
function circle:orthogonal_from(pt) |
| 394 |
-- test if pt is outside the circle |
| 395 |
if self:in_out_disk(pt) then |
| 396 |
tex.error("circle:orthogonal_from", "The point must lie outside the circle to construct an orthogonal circle.") |
| 397 |
else |
| 398 |
-- Calculate tangents from point ‘pt’. |
| 399 |
local t1, t2 = tangent_from_(self.center, self.through, pt) |
| 400 |
-- Return a circle with the center in 'pt' and one of the tangents as the radius |
| 401 |
return circle:new(pt, t1) |
| 402 |
end |
| 403 |
end |
| 404 |
|
| 405 |
-- function circle:orthogonal_through(pta, ptb, EPS) |
| 406 |
-- EPS = EPS or tkz.epsilon |
| 407 |
-- -- Case where the three points are collinear |
| 408 |
-- if is_linear_(self.center, pta, ptb) then |
| 409 |
-- -- and the two points are inverses of each other |
| 410 |
-- if are_inverses_(self.center, self.through, pta, ptb, EPS) then |
| 411 |
-- local c = midpoint_(pta, ptb) |
| 412 |
-- -- Write a note to the log instead of throwing an error |
| 413 |
-- texio.write_nl( |
| 414 |
-- "term and log", |
| 415 |
-- "[tkz-elements] circle:orthogonal_through: the two points are inverses; " |
| 416 |
-- .. "the midpoint is used as the centre, but any point on the perpendicular bisector could serve as centre." |
| 417 |
-- ) |
| 418 |
-- |
| 419 |
-- tex.print( |
| 420 |
-- "\\PackageWarningNoLine{tkz-elements}{The two points are inverses; the midpoint is used as the centre, but any point on the perpendicular bisector could serve as centre.}" |
| 421 |
-- ) |
| 422 |
-- |
| 423 |
-- return circle:new(c, pta) |
| 424 |
-- else |
| 425 |
-- -- Error case: collinear but not inverses |
| 426 |
-- tex.error("circle:orthogonal_through", "The points are aligned but not inverses.") |
| 427 |
-- return nil |
| 428 |
-- end |
| 429 |
-- |
| 430 |
-- -- General (non‑collinear) case |
| 431 |
-- else |
| 432 |
-- local o = orthogonal_through_(self.center, self.through, pta, ptb, EPS) |
| 433 |
-- return circle:new(o, pta) |
| 434 |
-- end |
| 435 |
-- end |
| 436 |
|
| 437 |
function circle:orthogonal_through(pta, ptb, EPS) |
| 438 |
EPS = EPS or tkz.epsilon |
| 439 |
|
| 440 |
-- Case where the three points are collinear |
| 441 |
if is_linear_(self.center, pta, ptb) then |
| 442 |
|
| 443 |
-- the two points are inverses of each other |
| 444 |
if are_inverses_(self.center, self.through, pta, ptb, EPS) then |
| 445 |
-- There is a FAMILY of solutions; picking midpoint is generally NOT orthogonal. |
| 446 |
-- Return a clear diagnostic and let caller choose a convention. |
| 447 |
return nil, "circle:orthogonal_through: collinear inverse points -> infinite family; choose a convention" |
| 448 |
|
| 449 |
else |
| 450 |
tex.error("circle:orthogonal_through", {"The points are aligned but not inverses."}) |
| 451 |
return nil |
| 452 |
end |
| 453 |
|
| 454 |
else |
| 455 |
local o = orthogonal_through_(self.center, self.through, pta, ptb, EPS) |
| 456 |
if not o then |
| 457 |
return nil, "circle:orthogonal_through: orthogonal_through_ failed" |
| 458 |
end |
| 459 |
return circle:new(o, pta) |
| 460 |
end |
| 461 |
end |
| 462 |
|
| 463 |
circle.orthogonal_circle_through = circle.orthogonal_through |
| 464 |
|
| 465 |
|
| 466 |
-- alias retained |
| 467 |
circle.orthogonal_circle_through = circle.orthogonal_through |
| 468 |
|
| 469 |
function circle:radical_circle(C1, C2) |
| 470 |
local rc = self:radical_center(C1, C2) |
| 471 |
if C2 == nil then |
| 472 |
return self:orthogonal_from(rc) |
| 473 |
else |
| 474 |
return C1:orthogonal_from(rc) |
| 475 |
end |
| 476 |
end |
| 477 |
|
| 478 |
|
| 479 |
function circle:midcircle(obj, EPS) |
| 480 |
if type(obj) == "table" and obj.type == "circle" then |
| 481 |
-- Retourne le cercle médian entre 'self' et 'C' |
| 482 |
return midcircle_cc_(self.center, self.through, obj.center, obj.through, EPS) |
| 483 |
elseif type(obj) == "table" and obj.type == "line" then |
| 484 |
return midcircle_cl_(self.center, self.through, obj.pa, obj.pb, EPS) |
| 485 |
else |
| 486 |
tex.error("midcircle: unsupported types (expect circle/circle or circle/line)") |
| 487 |
end |
| 488 |
end |
| 489 |
|
| 490 |
|
| 491 |
----------------------------------------------------------- |
| 492 |
-- Result -> drawing |
| 493 |
----------------------------------------------------------- |
| 494 |
function circle:draw() |
| 495 |
-- Récupère les coordonnées du centre et le rayon du cercle |
| 496 |
local x, y = self.center:get() |
| 497 |
local r = self.radius |
| 498 |
|
| 499 |
-- Format de la commande LaTeX pour dessiner le cercle |
| 500 |
local frmt = "\\draw (%0.3f,%0.3f) circle [radius=%0.3f];" |
| 501 |
|
| 502 |
-- Affiche la commande LaTeX en formatant les valeurs de x, y et r |
| 503 |
tex.sprint(string.format(frmt, x, y, r)) |
| 504 |
end |
| 505 |
|
| 506 |
function circle:set_inversion(...) |
| 507 |
local tp = table.pack(...) |
| 508 |
local i |
| 509 |
local t = {} |
| 510 |
for i = 1, tp.n do |
| 511 |
table.insert(t, inversion_(self.center, self.through, tp[i])) |
| 512 |
end |
| 513 |
return table.unpack(t) |
| 514 |
end |
| 515 |
|
| 516 |
function circle:inversion_L(L) |
| 517 |
-- Vérifie si le centre du cercle est à l'intérieur ou à l'extérieur de la ligne L |
| 518 |
if L:in_out(self.center) then |
| 519 |
return L -- Retourne la ligne L inchangée si le centre est du bon côté |
| 520 |
else |
| 521 |
-- Calcul de la projection du centre sur la ligne L |
| 522 |
local p = L:projection(self.center) |
| 523 |
|
| 524 |
-- Inversion du point projeté par rapport au cercle défini par 'self.center' et 'self.through' |
| 525 |
local q = inversion_(self.center, self.through, p) |
| 526 |
|
| 527 |
-- Retourne un cercle avec le centre au milieu de 'self.center' et 'q' et comme rayon 'q' |
| 528 |
return circle:new(midpoint_(self.center, q), q) |
| 529 |
end |
| 530 |
end |
| 531 |
|
| 532 |
function circle:inversion_C(C) |
| 533 |
local p, q, x, y, X, Y |
| 534 |
if C:in_out(self.center) then |
| 535 |
p = C:antipode(self.center) |
| 536 |
q = inversion_(self.center, self.through, p) |
| 537 |
x = ortho_from_(q, self.center, p) |
| 538 |
y = ortho_from_(q, p, self.center) |
| 539 |
return line:new(x, y) |
| 540 |
else |
| 541 |
x, y = intersection_lc_(self.center, C.center, C.center, C.through) |
| 542 |
X = inversion_(self.center, self.through, x) |
| 543 |
Y = inversion_(self.center, self.through, y) |
| 544 |
return circle:new(midpoint_(X, Y), X) |
| 545 |
end |
| 546 |
end |
| 547 |
|
| 548 |
function circle:inversion(...) |
| 549 |
local tp = table.pack(...) |
| 550 |
local obj = tp[1] |
| 551 |
local nb = tp.n |
| 552 |
if nb == 1 then |
| 553 |
if obj.type == "point" then |
| 554 |
return inversion_(self.center, self.through, obj) |
| 555 |
elseif obj.type == "line" then |
| 556 |
return self:inversion_L(obj) |
| 557 |
else |
| 558 |
return self:inversion_C(obj) |
| 559 |
end |
| 560 |
else |
| 561 |
local t = {} |
| 562 |
for i = 1, nb do |
| 563 |
table.insert(t, inversion_(self.center, self.through, tp[i])) |
| 564 |
end |
| 565 |
return table.unpack(t) |
| 566 |
end |
| 567 |
end |
| 568 |
|
| 569 |
-- Alias simple : inversion de rapport < 0 |
| 570 |
function circle:inversion_neg(obj) |
| 571 |
return self.center:symmetry(self:inversion(obj)) |
| 572 |
end |
| 573 |
|
| 574 |
function circle:path(za, zb, nb) |
| 575 |
local P = path() |
| 576 |
for i = 0, nb do |
| 577 |
local t = i / nb |
| 578 |
local pt = self:point_arc(za, zb, t) |
| 579 |
P:add_point(pt) |
| 580 |
end |
| 581 |
return P |
| 582 |
end |
| 583 |
|
| 584 |
function circle:point_arc(za, zb, t) |
| 585 |
local anglezazb = get_angle_normalize_(self.center, za, zb) |
| 586 |
local phi = t * anglezazb |
| 587 |
return rotation_(self.center, phi, za) |
| 588 |
end |
| 589 |
|
| 590 |
---=== PB CONTACT APOLLONIUS ===--- |
| 591 |
|
| 592 |
|
| 593 |
-- ----------------------------------------------------------- |
| 594 |
-- CPP CCP CLP CLL |
| 595 |
-- CCC(?) CCL(?) |
| 596 |
-- ================== CPP ========================== |
| 597 |
function circle:CPP(a, b) |
| 598 |
|
| 599 |
local o = self.center |
| 600 |
local th = self.through |
| 601 |
local u, v = mediator_(a, b) |
| 602 |
|
| 603 |
local pa_center = path:new() |
| 604 |
local pa_through = path:new() |
| 605 |
local n = 0 |
| 606 |
|
| 607 |
local function push(o, t) |
| 608 |
if o and t then |
| 609 |
pa_center:add_point(o) |
| 610 |
pa_through:add_point(t) |
| 611 |
n = n + 1 |
| 612 |
end |
| 613 |
end |
| 614 |
|
| 615 |
local ain = in_disk_(o, th, a) |
| 616 |
local bin = in_disk_(o, th, b) |
| 617 |
|
| 618 |
------------------------------------------------------------ |
| 619 |
-- 0) Un point dedans et l’autre dehors → aucune solution |
| 620 |
------------------------------------------------------------ |
| 621 |
if (ain and not bin) or (bin and not ain) then |
| 622 |
return pa_center, pa_through, 0 |
| 623 |
|
| 624 |
------------------------------------------------------------ |
| 625 |
-- 1) Droite (AB) tangent au cercle (self) |
| 626 |
------------------------------------------------------------ |
| 627 |
elseif is_tangent_(a, b, o, th) and (not is_equidistant_(a, b, o)) then |
| 628 |
local w = circum_center_(a, b, o) |
| 629 |
local c, d = intersection_cc_(o, th, w, a) |
| 630 |
local i = intersection_ll_(c, d, a, b) |
| 631 |
local t, tp = tangent_from_(o, th, i) |
| 632 |
|
| 633 |
if is_parallel_(o, t, u, v) then |
| 634 |
local w1 = intersection_ll_(o, tp, u, v) |
| 635 |
push(w1, tp) |
| 636 |
elseif is_parallel_(o, tp, u, v) then |
| 637 |
local w1 = intersection_ll_(o, t, u, v) |
| 638 |
push(w1, t) |
| 639 |
else |
| 640 |
local w1 = intersection_ll_(u, v, o, t) |
| 641 |
local w2 = intersection_ll_(u, v, o, tp) |
| 642 |
push(w1, t) |
| 643 |
push(w2, tp) |
| 644 |
end |
| 645 |
return pa_center, pa_through, n |
| 646 |
|
| 647 |
------------------------------------------------------------ |
| 648 |
-- 2) Droite (AB) parallèle à une tangente au cercle |
| 649 |
------------------------------------------------------------ |
| 650 |
elseif is_equidistant_(a, b, o) then |
| 651 |
local t1, t2 = intersection_lc_(u, v, o, th) |
| 652 |
local w1 = circum_center_(a, b, t1) |
| 653 |
local w2 = circum_center_(a, b, t2) |
| 654 |
push(w1, t1) |
| 655 |
push(w2, t2) |
| 656 |
return pa_center, pa_through, n |
| 657 |
|
| 658 |
------------------------------------------------------------ |
| 659 |
-- 3) Deux points EXTERIEURS : cas classique (ton cas général) |
| 660 |
------------------------------------------------------------ |
| 661 |
elseif (not ain) and (not bin) then |
| 662 |
local w = circum_center_(a, b, o) |
| 663 |
local c, d = intersection_cc_(o, th, w, a) |
| 664 |
local i = intersection_ll_(a, b, c, d) |
| 665 |
local t, tp = tangent_from_(o, th, i) |
| 666 |
local w1 = intersection_ll_(u, v, o, t) |
| 667 |
local w2 = intersection_ll_(u, v, o, tp) |
| 668 |
push(w1, t) |
| 669 |
push(w2, tp) |
| 670 |
return pa_center, pa_through, n |
| 671 |
|
| 672 |
------------------------------------------------------------ |
| 673 |
-- 4) Deux points INTERIEURS → inversion |
| 674 |
------------------------------------------------------------ |
| 675 |
elseif ain and bin then |
| 676 |
local Cinv = circle:new(a, b) |
| 677 |
local Csol = Cinv:inversion(self) |
| 678 |
if not Csol then return pa_center, pa_through, 0 end |
| 679 |
|
| 680 |
local LTA, LTB = Csol:tangent_from(b) |
| 681 |
if LTA then |
| 682 |
local C1 = Cinv:inversion(LTA) |
| 683 |
if C1 then push(C1.center, a) end |
| 684 |
end |
| 685 |
if LTB then |
| 686 |
local C2 = Cinv:inversion(LTB) |
| 687 |
if C2 then push(C2.center, a) end |
| 688 |
end |
| 689 |
return pa_center, pa_through, n |
| 690 |
end |
| 691 |
|
| 692 |
end |
| 693 |
|
| 694 |
circle.c_c_pp = circle.CPP |
| 695 |
|
| 696 |
|
| 697 |
--====================== START of CCP ========--- |
| 698 |
------------------------------------- |
| 699 |
|
| 700 |
-------------------------------------------------------- |
| 701 |
local function sp_c_cc_p(p, o1, t1, o2, t2, r1, r2, ex) |
| 702 |
local n = 0 |
| 703 |
local pa_center = path:new() |
| 704 |
local pa_through = path:new() |
| 705 |
|
| 706 |
local function push(o, t) |
| 707 |
if o and t then |
| 708 |
pa_center:add_point(o) |
| 709 |
pa_through:add_point(t) |
| 710 |
n = n + 1 |
| 711 |
end |
| 712 |
end |
| 713 |
|
| 714 |
-- NB: on suppose que report_ accepte (A,B,scale[,origin]) comme ailleurs. |
| 715 |
local x = report_(o1, o2, r1) -- point sur la direction o1->o2, distance ~r1 |
| 716 |
local y = report_(o2, o1, r2) -- point sur la direction o2->o1, distance ~r2 |
| 717 |
local o = circum_center_(p, x, y) -- centre du cercle (p,x,y) |
| 718 |
|
| 719 |
-- intersections de cercles : (o,p) et (o2,t2) |
| 720 |
local u, v = intersection_cc_(o, p, o2, t2) |
| 721 |
if not u or not v then |
| 722 |
return pa_center, pa_through, 0 |
| 723 |
end |
| 724 |
|
| 725 |
-- choisir u tel que u != y (échange sinon) |
| 726 |
if u:identity(y) then u, v = v, u end |
| 727 |
|
| 728 |
-- ligne (y,u) croise (ex,p) en i |
| 729 |
local i = intersection_ll_(y, u, ex, p) |
| 730 |
if not i then |
| 731 |
return pa_center, pa_through, 0 |
| 732 |
end |
| 733 |
|
| 734 |
-- tangentes depuis i au cercle (o2,t2) |
| 735 |
local tau1, tau2 = tangent_from_(o2, t2, i) -- renommé pour ne pas masquer paramètres |
| 736 |
if not tau1 or not tau2 then |
| 737 |
return pa_center, pa_through, 0 |
| 738 |
end |
| 739 |
|
| 740 |
-- centres = intersections des droites (o2,tau_k) et (p,o) |
| 741 |
local w1 = intersection_ll_(o2, tau1, p, o) |
| 742 |
local w2 = intersection_ll_(o2, tau2, p, o) |
| 743 |
if not w1 or not w2 then |
| 744 |
-- on pousse ce qui existe |
| 745 |
if w1 then push(w1, tau1) end |
| 746 |
if w2 then push(w2, tau2) end |
| 747 |
return pa_center, pa_through, n |
| 748 |
end |
| 749 |
|
| 750 |
-- ordre : w1 le plus proche de p |
| 751 |
if length_(w1, p) > length_(w2, p) then w1, w2, tau1, tau2 = w2, w1, tau2, tau1 end |
| 752 |
push(w1, tau1) |
| 753 |
push(w2, tau2) |
| 754 |
return pa_center, pa_through, n |
| 755 |
end |
| 756 |
|
| 757 |
|
| 758 |
-- =================================================== |
| 759 |
-- solve_intersect(Cself, C, p, mode) |
| 760 |
-- → Cherche les cercles tangents à Cself et C passant par p |
| 761 |
-- → version simple, sans inversion, sans dépendances externes |
| 762 |
-- =================================================== |
| 763 |
|
| 764 |
-- Cercle tangent à self et C, passant par p (cas général) |
| 765 |
local function solve_intersect(Cself, C, p, mode) |
| 766 |
if Cself:on_circle(p) then |
| 767 |
local tA, tB = tangent_at_(Cself.center, p) |
| 768 |
local T = line:new(tA, tB) |
| 769 |
return C:CLP(T, p) |
| 770 |
elseif C:on_circle(p) then |
| 771 |
local tA, tB = tangent_at_(C.center, p) |
| 772 |
local T = line:new(tA, tB) |
| 773 |
return Cself:CLP(T, p) |
| 774 |
else |
| 775 |
local pa_center = path:new() |
| 776 |
local pa_through = path:new() |
| 777 |
local n = 0 |
| 778 |
local function push(o, t) |
| 779 |
if o and t then |
| 780 |
pa_center:add_point(o) |
| 781 |
pa_through:add_point(t) |
| 782 |
n = n + 1 |
| 783 |
end |
| 784 |
end |
| 785 |
local I = midpoint_(Cself.center, C.center) |
| 786 |
local K = circle:new(p, I) |
| 787 |
local A = K:inversion(Cself) |
| 788 |
local B = K:inversion(C) |
| 789 |
local T1, T2 = A:common_tangent(B, "external") |
| 790 |
local C1 = K:inversion(T1) |
| 791 |
local C2 = K:inversion(T2) |
| 792 |
local O1, T1 = C1:get() |
| 793 |
local O2, T2 = C2:get() |
| 794 |
push(O1, T1) |
| 795 |
push(O2, T2) |
| 796 |
return pa_center, pa_through, n |
| 797 |
end |
| 798 |
end |
| 799 |
|
| 800 |
-------------------------------------------------------- |
| 801 |
-- CCP : deux cercles tangents intérieurement, cercle solution passant par p |
| 802 |
-- (Uniformisation push/paths, logique intacte) |
| 803 |
-------------------------------------------------------- |
| 804 |
local function solve_inside_tangent(Cself, C, p, mode) |
| 805 |
if Cself:on_circle(p) then |
| 806 |
local tA, tB = tangent_at_(Cself.center, p) |
| 807 |
local T = line:new(tA, tB) |
| 808 |
return C:CLP(T, p) |
| 809 |
elseif C:on_circle(p) then |
| 810 |
local tA, tB = tangent_at_(C.center, p) |
| 811 |
local T = line:new(tA, tB) |
| 812 |
return Cself:CLP(T, p) |
| 813 |
else |
| 814 |
|
| 815 |
local T = intersection(Cself, C) -- point de tangence commun |
| 816 |
if p:identity(T) then |
| 817 |
return path:new(), path:new(), 0 -- P = T -> aucune solution |
| 818 |
end |
| 819 |
|
| 820 |
-- Inversion en p |
| 821 |
local K = circle:new(p, Cself.through or (p + point(1,0))) |
| 822 |
local A = K:inversion(Cself) -- typiquement une droite (p ∈ Cself ?) |
| 823 |
local B = K:inversion(C) |
| 824 |
local L1, L2 = A:common_tangent(B, "all") |
| 825 |
local function valid(L) return L and (not L:on_line(p)) end |
| 826 |
local pa_center = path:new() |
| 827 |
local pa_through = path:new() |
| 828 |
local n = 0 |
| 829 |
local function push(o, t) |
| 830 |
if o and t then |
| 831 |
pa_center:add_point(o) |
| 832 |
pa_through:add_point(t) |
| 833 |
n = n + 1 |
| 834 |
end |
| 835 |
end |
| 836 |
|
| 837 |
if valid(L1) then |
| 838 |
local S1 = K:inversion(L1) |
| 839 |
if S1 and (S1.radius or 0) > tkz.epsilon then push(S1.center, S1.through) end |
| 840 |
end |
| 841 |
if valid(L2) then |
| 842 |
local S2 = K:inversion(L2) |
| 843 |
if S2 and (S2.radius or 0) > tkz.epsilon then push(S2.center, S2.through) end |
| 844 |
end |
| 845 |
|
| 846 |
return pa_center, pa_through, n |
| 847 |
end |
| 848 |
end |
| 849 |
|
| 850 |
-------------------------------------------------------- |
| 851 |
-- CCP : cas "inside" |
| 852 |
-------------------------------------------------------- |
| 853 |
|
| 854 |
local function solve_inside(Cself, C, p, mode) |
| 855 |
local a = Cself:point_position(p) |
| 856 |
local b = C:point_position(p) |
| 857 |
|
| 858 |
-- Cas spéciaux (0 solution) |
| 859 |
if (a == "OUT" and b == "ON") or (a == "IN" and b == "IN") then |
| 860 |
return path:new(), path:new(), 0 |
| 861 |
else |
| 862 |
-- Création du cercle d'inversion K (exactement comme tu as proposé) |
| 863 |
local K = circle:new(C.through, Cself.through) |
| 864 |
local A = K:inversion(Cself) |
| 865 |
local B = K:inversion(C) |
| 866 |
|
| 867 |
-- Image inversée du point p |
| 868 |
local ip = K:inversion(p) |
| 869 |
|
| 870 |
-- On crée les chemins de sortie |
| 871 |
local pa_center = path:new() |
| 872 |
local pa_through = path:new() |
| 873 |
local n = 0 |
| 874 |
|
| 875 |
local function push(o, t) |
| 876 |
if o and t then |
| 877 |
pa_center:add_point(o) |
| 878 |
pa_through:add_point(t) |
| 879 |
n = n + 1 |
| 880 |
end |
| 881 |
end |
| 882 |
local pc_inv, pt_inv, m = A:CLP(B, ip) |
| 883 |
|
| 884 |
if pc_inv and pt_inv and m and m > 0 then |
| 885 |
for i = 1, m do |
| 886 |
local o_inv = pc_inv:get(i) |
| 887 |
local t_inv = pt_inv:get(i) |
| 888 |
if o_inv and t_inv then |
| 889 |
-- Reconstitue le cercle dans le plan inversé |
| 890 |
local Cinv = circle:new(o_inv, t_inv) |
| 891 |
-- Retour dans le plan original |
| 892 |
local S = K:inversion(Cinv) |
| 893 |
push(S.center, S.through) |
| 894 |
-- if S and S.center and S.through then |
| 895 |
-- local r = length_(S.center, S.through) |
| 896 |
-- if r and r > tkz.epsilon then |
| 897 |
-- |
| 898 |
-- end |
| 899 |
-- end |
| 900 |
end |
| 901 |
end |
| 902 |
end |
| 903 |
|
| 904 |
return pa_center, pa_through, n |
| 905 |
end |
| 906 |
end |
| 907 |
|
| 908 |
-------------------------------------------------------- |
| 909 |
-- CCP : cas "outside" |
| 910 |
-- (Uniformisation + garde-fous, même structure) |
| 911 |
-------------------------------------------------------- |
| 912 |
|
| 913 |
-- ==================== CAS SPÉCIAL r1 == r2 ==================== |
| 914 |
local function solve_outside_equal_radii(Cself, C, p, mode) |
| 915 |
mode = mode or "external" |
| 916 |
local pa_center = path:new() |
| 917 |
local pa_through = path:new() |
| 918 |
local n = 0 |
| 919 |
local function push(o, t) |
| 920 |
if o and t then |
| 921 |
pa_center:add_point(o) |
| 922 |
pa_through:add_point(t) |
| 923 |
n = n + 1 |
| 924 |
end |
| 925 |
end |
| 926 |
|
| 927 |
local M |
| 928 |
if C:on_circle(Cself.through) then |
| 929 |
M = antipode_(Cself.center, Cself.through) |
| 930 |
else |
| 931 |
M = Cself.through |
| 932 |
end |
| 933 |
local K = circle:new(M, C.center) |
| 934 |
local A = K:inversion(Cself) |
| 935 |
local B = K:inversion(C) |
| 936 |
local ip = K:inversion(p) |
| 937 |
local pc_inv, pt_inv, m = B:CLP(A, ip) |
| 938 |
if pc_inv and pt_inv and m then |
| 939 |
for i = 1, m do |
| 940 |
local oi = pc_inv:get(i) |
| 941 |
local ti = pt_inv:get(i) |
| 942 |
if oi and ti then |
| 943 |
local S = K:inversion(circle:new(oi, ti)) |
| 944 |
if S and S.center and S.through then |
| 945 |
push(S.center, S.through) |
| 946 |
end |
| 947 |
end |
| 948 |
end |
| 949 |
end |
| 950 |
|
| 951 |
return pa_center, pa_through, n |
| 952 |
end |
| 953 |
|
| 954 |
-- ================== FIN CAS r1 == r2 ================== |
| 955 |
|
| 956 |
|
| 957 |
local function solve_outside(Cself, C, p, mode) |
| 958 |
local o1, t1, r1 = Cself.center, Cself.through, Cself.radius |
| 959 |
local o2, t2, r2 = C.center, C.through, C.radius |
| 960 |
local pos1 = Cself:point_position(p) -- "IN", "ON", "OUT" |
| 961 |
local pos2 = C:point_position(p) |
| 962 |
|
| 963 |
if pos1 == "IN" or pos2 == "IN" then |
| 964 |
return path:new(), path:new(), 0 |
| 965 |
|
| 966 |
elseif pos1 == "ON" then |
| 967 |
local tA, tB = tangent_at_(o1, p) -- deux points sur la tangente à self en p |
| 968 |
local T = line:new(tA, tB) |
| 969 |
return C:CLP(T, p) |
| 970 |
|
| 971 |
elseif pos2 == "ON" then |
| 972 |
local tA, tB = tangent_at_(o2, p) -- deux points sur la tangente à self en p |
| 973 |
local T = line:new(tA, tB) |
| 974 |
return Cself:CLP(T, p) |
| 975 |
|
| 976 |
else |
| 977 |
if mode == "all" then |
| 978 |
local PCe, PTe, ne = Cself:CCP(C, p, "external") |
| 979 |
local PCi, PTi, ni = Cself:CCP(C, p, "internal") |
| 980 |
return (PCe + PCi), (PTe + PTi), (ne + ni) |
| 981 |
end |
| 982 |
|
| 983 |
-- ======= DÉLÉGATION SI r1 == r2 ======= |
| 984 |
if tkz.approx(r1, r2) then |
| 985 |
return solve_outside_equal_radii(Cself, C, p, mode) |
| 986 |
end |
| 987 |
|
| 988 |
if mode == "internal" then |
| 989 |
local i = Cself:internal_similitude(C) |
| 990 |
local x = report_(o1, o2, -r1, o1) |
| 991 |
local y = report_(o2, o1, r2, o2) |
| 992 |
local o = circum_center_(x, y, p) |
| 993 |
local r, s = intersection_lc_(i, p, o, p) -- r ≠ p |
| 994 |
if not r or not s then |
| 995 |
return path:new(), path:new(), 0 |
| 996 |
end |
| 997 |
if r:identity(p) then r, s = s, r end |
| 998 |
return Cself:CPP(p, r) |
| 999 |
else |
| 1000 |
-- external |
| 1001 |
local Cmid = Cself:midcircle(C) |
| 1002 |
local ex = Cmid.center |
| 1003 |
local q = Cmid:inversion(p) |
| 1004 |
if p:identity(q) then |
| 1005 |
return sp_c_cc_p(p, o1, t1, o2, t2, r1, r2, ex) |
| 1006 |
else |
| 1007 |
return Cself:CPP(p, q) |
| 1008 |
end |
| 1009 |
end |
| 1010 |
end |
| 1011 |
end |
| 1012 |
|
| 1013 |
|
| 1014 |
|
| 1015 |
-------------------------------------------------------- |
| 1016 |
-- Routeur CCP (inchangé structurellement) |
| 1017 |
-------------------------------------------------------- |
| 1018 |
function circle:CCP(C, p, mode) |
| 1019 |
mode = mode or "all" |
| 1020 |
local pos = self:circles_position(C) |
| 1021 |
|
| 1022 |
if pos == "DISJOINT_EXT" then |
| 1023 |
return solve_outside(self, C, p, mode) |
| 1024 |
|
| 1025 |
elseif pos == "SECANT" then |
| 1026 |
return solve_intersect(self, C, p, mode) |
| 1027 |
|
| 1028 |
elseif pos == "TANGENT_EXT" then |
| 1029 |
return solve_outside(self, C, p, mode) |
| 1030 |
|
| 1031 |
elseif pos == "TANGENT_INT" then |
| 1032 |
return solve_inside_tangent(self, C, p, mode) |
| 1033 |
|
| 1034 |
elseif pos == "DISJOINT_INT" then |
| 1035 |
return solve_inside(self, C, p, mode) |
| 1036 |
|
| 1037 |
elseif pos == "CONCENTRIC" or pos == "IDENTICAL" then |
| 1038 |
return false -- ou gestion spécifique |
| 1039 |
|
| 1040 |
else |
| 1041 |
error("Unknown circle position in CCP: "..tostring(pos)) |
| 1042 |
end |
| 1043 |
end |
| 1044 |
|
| 1045 |
|
| 1046 |
----==== END of CCP ====== |
| 1047 |
|
| 1048 |
--===================== |
| 1049 |
-- ======= START of CLP |
| 1050 |
--===================== |
| 1051 |
-- Circle tangent to one circle, on line and passing through a point |
| 1052 |
-- Returns: PA.center, PA.through, n (paths of centers / through-points, and count) |
| 1053 |
|
| 1054 |
--===================== |
| 1055 |
-- CLP : circle tangent to a line and passing through a point |
| 1056 |
-- Retour : pa_center, pa_through, n |
| 1057 |
--===================== |
| 1058 |
function circle:CLP(l, p, which) |
| 1059 |
which = which or "all" |
| 1060 |
|
| 1061 |
local pos_line = self:line_position(l) |
| 1062 |
local os, ts, rs = self.center, self.through, self.radius |
| 1063 |
local ip = projection_(l.pa, l.pb, p) -- ou l:projection(p) |
| 1064 |
local i = projection_(l.pa, l.pb, os) -- ou l:projection(os) |
| 1065 |
local no = report_(os, i, -rs) |
| 1066 |
local so = report_(os, i, rs) |
| 1067 |
local pos = intersection_ll_(l.pa, l.pb, os, p) |
| 1068 |
local sO, sP = l:side_line(os), l:side_line(p) |
| 1069 |
|
| 1070 |
-- chemins + compteur |
| 1071 |
local pa_center, pa_through = path:new(), path:new() |
| 1072 |
local n = 0 |
| 1073 |
local function push(o, t) |
| 1074 |
if o and t then |
| 1075 |
pa_center:add_point(o) |
| 1076 |
pa_through:add_point(t) |
| 1077 |
n = n + 1 |
| 1078 |
end |
| 1079 |
end |
| 1080 |
|
| 1081 |
if pos_line == "DISJOINT" then |
| 1082 |
-- (1) P = i |
| 1083 |
if p:identity(i) then |
| 1084 |
local o1 = midpoint_(p, no) |
| 1085 |
local o2 = midpoint_(p, so) |
| 1086 |
push(o1, p) |
| 1087 |
push(o2, p) |
| 1088 |
return pa_center, pa_through, n |
| 1089 |
|
| 1090 |
-- (2) P sur l (P ≠ i) |
| 1091 |
elseif l:on_line(p) then |
| 1092 |
if not l.length or l.length == 0 then return pa_center, pa_through, n end |
| 1093 |
local k = rs / l.length |
| 1094 |
local u = orthogonal_at_(l.pa, l.pb, p, k) |
| 1095 |
local v = orthogonal_at_(l.pa, l.pb, p, -k) |
| 1096 |
local ux, uy = mediator_(os, u) |
| 1097 |
local vx, vy = mediator_(os, v) |
| 1098 |
local o1 = intersection_ll_(u, v, ux, uy) |
| 1099 |
local o2 = intersection_ll_(u, v, vx, vy) |
| 1100 |
push(o1, p) |
| 1101 |
push(o2, p) |
| 1102 |
return pa_center, pa_through, n |
| 1103 |
|
| 1104 |
-- (3) P du mauvais côté |
| 1105 |
elseif sO ~= sP then |
| 1106 |
return pa_center, pa_through, n |
| 1107 |
|
| 1108 |
-- (4) P strictement dans le disque |
| 1109 |
elseif self:in_disk_strict(p) then |
| 1110 |
return pa_center, pa_through, n |
| 1111 |
|
| 1112 |
-- (5) P sur C |
| 1113 |
elseif self:on_circle(p) then |
| 1114 |
if p:identity(no) then |
| 1115 |
push(midpoint_(no, i), p) |
| 1116 |
return pa_center, pa_through, n |
| 1117 |
elseif p:identity(so) then |
| 1118 |
push(midpoint_(so, i), p) |
| 1119 |
return pa_center, pa_through, n |
| 1120 |
else |
| 1121 |
local i1 = intersection_ll_(l.pa, l.pb, no, p) |
| 1122 |
local i2 = intersection_ll_(l.pa, l.pb, so, p) |
| 1123 |
if i1 then |
| 1124 |
local j1 = orthogonal_at_(l.pa, l.pb, i1) |
| 1125 |
local o1 = intersection_ll_(i1, j1, os, p) |
| 1126 |
push(o1, p) |
| 1127 |
end |
| 1128 |
if i2 then |
| 1129 |
local j2 = orthogonal_at_(l.pa, l.pb, i2) |
| 1130 |
local o2 = intersection_ll_(i2, j2, os, p) |
| 1131 |
push(o2, p) |
| 1132 |
end |
| 1133 |
return pa_center, pa_through, n |
| 1134 |
end |
| 1135 |
|
| 1136 |
-- (6) Cas général : inversion en P |
| 1137 |
else |
| 1138 |
local Cinv = circle:new(p, ip) |
| 1139 |
local C1 = Cinv:inversion(self) |
| 1140 |
local C2 = Cinv:inversion(l) |
| 1141 |
|
| 1142 |
if which == "external" or which == "all" then |
| 1143 |
local L1, L2 = C1:common_tangent(C2, "external") |
| 1144 |
if L1 then |
| 1145 |
local S = Cinv:inversion(L1) |
| 1146 |
if S and S.center and S.through then push(S.center, S.through) end |
| 1147 |
end |
| 1148 |
if L2 then |
| 1149 |
local S = Cinv:inversion(L2) |
| 1150 |
if S and S.center and S.through then push(S.center, S.through) end |
| 1151 |
end |
| 1152 |
end |
| 1153 |
if which == "internal" or which == "all" then |
| 1154 |
local L1, L2 = C1:common_tangent(C2, "internal") |
| 1155 |
if L1 then |
| 1156 |
local S = Cinv:inversion(L1) |
| 1157 |
if S and S.center and S.through then push(S.center, S.through) end |
| 1158 |
end |
| 1159 |
if L2 then |
| 1160 |
local S = Cinv:inversion(L2) |
| 1161 |
if S and S.center and S.through then push(S.center, S.through) end |
| 1162 |
end |
| 1163 |
end |
| 1164 |
return pa_center, pa_through, n |
| 1165 |
end |
| 1166 |
|
| 1167 |
elseif pos_line == "TANGENT" then |
| 1168 |
-- (1) P = i |
| 1169 |
if p:identity(i) then |
| 1170 |
push(os, i) |
| 1171 |
return pa_center, pa_through, n |
| 1172 |
|
| 1173 |
-- (2) P sur l (config tangente) |
| 1174 |
elseif l:on_line(p) then |
| 1175 |
local Cinv = circle:new(i, p) |
| 1176 |
local Linv = Cinv:inversion(self) -- image de C : une droite |
| 1177 |
local pac, pat, m = l:LLP(Linv, p) |
| 1178 |
for k = 1, (m or 0) do |
| 1179 |
local o = pac:get(k) |
| 1180 |
local t = pat:get(k) |
| 1181 |
if o and t then |
| 1182 |
local S1 = Cinv:inversion(circle:new(o, t)) |
| 1183 |
if S1 then |
| 1184 |
local oc, tc = S1:get() |
| 1185 |
push(oc, tc) |
| 1186 |
end |
| 1187 |
end |
| 1188 |
end |
| 1189 |
return pa_center, pa_through, n |
| 1190 |
|
| 1191 |
-- (3) P du mauvais côté |
| 1192 |
elseif sO ~= sP then |
| 1193 |
return pa_center, pa_through, n |
| 1194 |
|
| 1195 |
-- (4) P strictement dans le disque |
| 1196 |
elseif self:in_disk_strict(p) then |
| 1197 |
return pa_center, pa_through, n |
| 1198 |
|
| 1199 |
-- (5) P sur C, au point singulier (no/so) |
| 1200 |
elseif self:on_circle(p) and (p:identity(no) or p:identity(so)) then |
| 1201 |
push(os, ts) |
| 1202 |
return pa_center, pa_through, n |
| 1203 |
|
| 1204 |
-- (6) P sur C, cas général |
| 1205 |
elseif self:on_circle(p) then |
| 1206 |
local Cinv = circle:new(i, p) |
| 1207 |
local Lself = Cinv:inversion(self) |
| 1208 |
local mpt = midpoint_(p, ip) |
| 1209 |
local S1 = Cinv:inversion(circle:new(mpt, p)) |
| 1210 |
if S1 then |
| 1211 |
local oc, tc = S1:get() |
| 1212 |
push(oc, tc) |
| 1213 |
end |
| 1214 |
return pa_center, pa_through, n |
| 1215 |
|
| 1216 |
-- (7) Cas général tangent |
| 1217 |
else |
| 1218 |
local Cinv = circle:new(i, p) |
| 1219 |
local Lself = Cinv:inversion(self) |
| 1220 |
local pac, pat, m = l:LLP(Lself, p) |
| 1221 |
for k = 1, (m or 0) do |
| 1222 |
local o = pac:get(k) |
| 1223 |
local t = pat:get(k) |
| 1224 |
if o and t then |
| 1225 |
local S1 = Cinv:inversion(circle:new(o, t)) |
| 1226 |
if S1 then |
| 1227 |
local oc, tc = S1:get() |
| 1228 |
push(oc, tc) |
| 1229 |
end |
| 1230 |
end |
| 1231 |
end |
| 1232 |
return pa_center, pa_through, n |
| 1233 |
end |
| 1234 |
|
| 1235 |
else -- "secant" |
| 1236 |
if l:on_line(p) and self:on_circle(p) then |
| 1237 |
return pa_center, pa_through, 0 |
| 1238 |
|
| 1239 |
elseif self:on_circle(p) then |
| 1240 |
|
| 1241 |
local Cinv = circle:new(p, ip) |
| 1242 |
local Lself = Cinv:inversion(self) -- droite |
| 1243 |
local C2 = Cinv:inversion(l) -- cercle |
| 1244 |
|
| 1245 |
if not (Lself and C2 and C2.center and C2.radius) then |
| 1246 |
return pa_center, pa_through, n |
| 1247 |
end |
| 1248 |
|
| 1249 |
local d = (Lself.pb - Lself.pa) |
| 1250 |
local dn = point.abs(d) |
| 1251 |
if dn <= tkz.epsilon then |
| 1252 |
return pa_center, pa_through, n |
| 1253 |
end |
| 1254 |
-- normale unitaire : n = perp(d)/|d| |
| 1255 |
local nux = (d.im / dn) |
| 1256 |
local nuy = -(d.re / dn) |
| 1257 |
local nvec = point(nux, nuy) |
| 1258 |
|
| 1259 |
-- Deux points à distance r2 du centre de C2, le long de la normale à Lself |
| 1260 |
local O2 = C2.center |
| 1261 |
local r2 = C2.radius |
| 1262 |
local Q1 = O2 + r2 * nvec |
| 1263 |
local Q2 = O2 - r2 * nvec |
| 1264 |
|
| 1265 |
-- Deux droites parallèles à Lself (donc "tangentes" à Lself au sens requis) et tangentes à C2 |
| 1266 |
local L1 = line:new(Q1, Q1 + d) |
| 1267 |
local L2 = line:new(Q2, Q2 + d) |
| 1268 |
|
| 1269 |
-- Ré-inversion → deux cercles solutions (passant par P, tangents à C et à l) |
| 1270 |
if L1 then local S = Cinv:inversion(L1); if S and S.center and S.through then push(S.center, S.through) end end |
| 1271 |
if L2 then local S = Cinv:inversion(L2); if S and S.center and S.through then push(S.center, S.through) end end |
| 1272 |
|
| 1273 |
return pa_center, pa_through, n |
| 1274 |
|
| 1275 |
elseif l:on_line(p) then |
| 1276 |
|
| 1277 |
local _, tp = through(p, 1) |
| 1278 |
local Cinv = circle:new(p, tp) |
| 1279 |
local C1 = Cinv:inversion(self) |
| 1280 |
if not C1 or not C1.center or not C1.radius then |
| 1281 |
return pa_center, pa_through, n |
| 1282 |
end |
| 1283 |
local o1, r1 = C1.center, C1.radius |
| 1284 |
|
| 1285 |
if not l.length or l.length == 0 then return pa_center, pa_through, n end |
| 1286 |
local k = r1 / l.length |
| 1287 |
local q1 = orthogonal_at_(l.pa, l.pb, o1, k) |
| 1288 |
local q2 = orthogonal_at_(l.pa, l.pb, o1, -k) |
| 1289 |
|
| 1290 |
local dir = (l.pb - l.pa) |
| 1291 |
local L1 = line:new(q1, q1 + dir) |
| 1292 |
local L2 = line:new(q2, q2 + dir) |
| 1293 |
|
| 1294 |
if L1 then |
| 1295 |
local S = Cinv:inversion(L1) |
| 1296 |
if S and S.center and S.through then push(S.center, S.through) end |
| 1297 |
end |
| 1298 |
if L2 then |
| 1299 |
local S = Cinv:inversion(L2) |
| 1300 |
if S and S.center and S.through then push(S.center, S.through) end |
| 1301 |
end |
| 1302 |
return pa_center, pa_through, n |
| 1303 |
|
| 1304 |
else |
| 1305 |
|
| 1306 |
local Cinv = circle:new(p, ip) |
| 1307 |
local C1 = Cinv:inversion(self) -- cercle |
| 1308 |
local C2 = Cinv:inversion(l) -- cercle (car p ∉ l) |
| 1309 |
|
| 1310 |
local Le1, Le2 = C1:common_tangent(C2, "external") |
| 1311 |
if Le1 then |
| 1312 |
local S = Cinv:inversion(Le1) |
| 1313 |
if S and S.center and S.through then push(S.center, S.through) end |
| 1314 |
end |
| 1315 |
if Le2 then |
| 1316 |
local S = Cinv:inversion(Le2) |
| 1317 |
if S and S.center and S.through then push(S.center, S.through) end |
| 1318 |
end |
| 1319 |
return pa_center, pa_through, n |
| 1320 |
end |
| 1321 |
end |
| 1322 |
|
| 1323 |
end |
| 1324 |
-- Aliases |
| 1325 |
circle.c_lc_p = circle.CLP |
| 1326 |
circle.c_cl_p = circle.CLP |
| 1327 |
--===================== |
| 1328 |
-- ======= END of CLP |
| 1329 |
--===================== |
| 1330 |
|
| 1331 |
|
| 1332 |
|
| 1333 |
--======================================== |
| 1334 |
-- CLL : cercle tangent à 2 droites L1,L2 |
| 1335 |
--======================================== |
| 1336 |
|
| 1337 |
--============================== |
| 1338 |
-- Helper : cercle dégénéré ? |
| 1339 |
--============================== |
| 1340 |
local function is_degenerate_circle(C, I) |
| 1341 |
if not C then return true end |
| 1342 |
if math.abs(C.radius or 0) < tkz.epsilon then return true end |
| 1343 |
if C.through and C.through:identity(I) then return true end |
| 1344 |
return false |
| 1345 |
end |
| 1346 |
|
| 1347 |
local function circle_outside_strip_(o, r, a, b, c, d) |
| 1348 |
local d1 = distance_(a, b, o) -- distance à L1 |
| 1349 |
local d2 = distance_(c, d, o) -- distance à L2 |
| 1350 |
local D = distance_(a, b, c) -- distance entre L1 et L2 |
| 1351 |
local EPS = 0.00001 |
| 1352 |
if math.abs((d1 + d2) - D) <= EPS then |
| 1353 |
return false |
| 1354 |
end |
| 1355 |
|
| 1356 |
if d1 > r + EPS and d2 > r + EPS then |
| 1357 |
return true |
| 1358 |
else |
| 1359 |
return false |
| 1360 |
end |
| 1361 |
end |
| 1362 |
|
| 1363 |
function circle:CLL_parallel(L1, L2) |
| 1364 |
local pa_center = path:new() |
| 1365 |
local pa_through = path:new() |
| 1366 |
local n = 0 |
| 1367 |
|
| 1368 |
local function push(o, t) |
| 1369 |
if o and t then |
| 1370 |
pa_center:add_point(o) |
| 1371 |
pa_through:add_point(t) |
| 1372 |
n = n + 1 |
| 1373 |
end |
| 1374 |
end |
| 1375 |
local o = self.center |
| 1376 |
local t = self.through |
| 1377 |
local r = self.radius |
| 1378 |
local a, b = L1:get() |
| 1379 |
local c, d = L2:get() |
| 1380 |
-- manque cercle tgt ext de la bande |
| 1381 |
if circle_outside_strip_(o, r, a, b, c, d) then |
| 1382 |
return path:new(), path:new(), 0 |
| 1383 |
else |
| 1384 |
local u = projection_(c, d, a) |
| 1385 |
local v = projection_(c, d, b) |
| 1386 |
local x = midpoint_(a, u) |
| 1387 |
local y = midpoint_(b, v) |
| 1388 |
local d = length_(a, u) / 2 |
| 1389 |
local ta = report_(o, t, self.radius + d, o) |
| 1390 |
local w1, w2 = intersection_lc_(x, y, o, ta) |
| 1391 |
local t1 = projection_(a, b, w1) |
| 1392 |
local t2 = projection_(a, b, w2) |
| 1393 |
push(w1, t1) |
| 1394 |
push(w2, t2) |
| 1395 |
return pa_center, pa_through, n |
| 1396 |
end |
| 1397 |
end |
| 1398 |
|
| 1399 |
-- first sector u,i,v ; then v,i,up ; up,i,vp ; vp,i,u |
| 1400 |
function circle:CLL(L1, L2, choice, inside) |
| 1401 |
choice = choice or "all" |
| 1402 |
local a, b = L1:get() |
| 1403 |
local c, d = L2:get() |
| 1404 |
if is_parallel_(a, b, c, d) then |
| 1405 |
return self:CLL_parallel(L1, L2) |
| 1406 |
else |
| 1407 |
if choice == "all" then |
| 1408 |
return self:CLL_all(L1, L2) |
| 1409 |
else |
| 1410 |
local a, b = L1:get() |
| 1411 |
local c, d = L2:get() |
| 1412 |
local o = self.center |
| 1413 |
local r = self.radius |
| 1414 |
local i = intersection_ll_(a, b, c, d) |
| 1415 |
|
| 1416 |
-- Orientation relative des droites |
| 1417 |
local s = (((b - a) ^ (d - c)) >= 0) and 1 or -1 |
| 1418 |
local u = report_(a, b, 1, i) |
| 1419 |
local up = report_(a, b, -1, i) |
| 1420 |
local v = report_(c, d, s, i) |
| 1421 |
local vp = report_(c, d, -s, i) |
| 1422 |
|
| 1423 |
-- Choix du secteur (1..4) |
| 1424 |
local x, y |
| 1425 |
if choice == 1 then x, y = u, v |
| 1426 |
elseif choice == 2 then x, y = v, up |
| 1427 |
elseif choice == 3 then x, y = up, vp |
| 1428 |
elseif choice == 4 then x, y = vp, u |
| 1429 |
else |
| 1430 |
tex.error("Bad choice ! Integer must be between 1 and 4") |
| 1431 |
end |
| 1432 |
|
| 1433 |
-- Gestion inside : rayon changé de signe |
| 1434 |
inside = (inside == "inside") |
| 1435 |
if inside then r = -r end |
| 1436 |
|
| 1437 |
-- Deux droites à distance ±r depuis I vers x/y |
| 1438 |
local LL1 = line:new(collinear_at_distance_(i, x, -r)) |
| 1439 |
local LL2 = line:new(collinear_at_distance_(i, y, r)) |
| 1440 |
|
| 1441 |
-- LLP (version paths) : centres sur LL2, tangence sur LL1, passant par o |
| 1442 |
local pc, pt, m = LL2:c_ll_p(LL1, o) |
| 1443 |
|
| 1444 |
-- Reconstitution des cercles solution (projeter sur la droite (i,x)) |
| 1445 |
local S1, S2 = nil, nil |
| 1446 |
if (m or 0) >= 1 then |
| 1447 |
local w1 = pc:get(1) |
| 1448 |
local t1 = line(i, x):projection(w1) |
| 1449 |
S1 = circle:new(w1, t1) |
| 1450 |
end |
| 1451 |
if (m or 0) >= 2 then |
| 1452 |
local w2 = pc:get(2) |
| 1453 |
local t2 = line(i, x):projection(w2) |
| 1454 |
S2 = circle:new(w2, t2) |
| 1455 |
end |
| 1456 |
|
| 1457 |
-- Filtrage dégénérés |
| 1458 |
local d1 = S1 and is_degenerate_circle(S1, i) |
| 1459 |
local d2 = S2 and is_degenerate_circle(S2, i) |
| 1460 |
if d1 and (not d2) and S2 then |
| 1461 |
S1, S2 = S2, S1 |
| 1462 |
d1, d2 = d2, d1 |
| 1463 |
elseif d1 and d2 then |
| 1464 |
S1, S2 = nil, nil |
| 1465 |
end |
| 1466 |
|
| 1467 |
-- Emballage en paths avec push "à la CLP" |
| 1468 |
local pa_center = path:new() |
| 1469 |
local pa_through = path:new() |
| 1470 |
local n = 0 |
| 1471 |
|
| 1472 |
local function push(o, t) |
| 1473 |
if o and t then |
| 1474 |
pa_center:add_point(o) |
| 1475 |
pa_through:add_point(t) |
| 1476 |
n = n + 1 |
| 1477 |
end |
| 1478 |
end |
| 1479 |
|
| 1480 |
if S1 then push(S1.center, S1.through) end |
| 1481 |
if S2 then push(S2.center, S2.through) end |
| 1482 |
|
| 1483 |
return pa_center, pa_through, n |
| 1484 |
end |
| 1485 |
end |
| 1486 |
end |
| 1487 |
circle.c_cll = circle.CLL |
| 1488 |
|
| 1489 |
--===================================================== |
| 1490 |
-- CLL_all : toutes les solutions selon les “sectors” |
| 1491 |
--===================================================== |
| 1492 |
function circle:CLL_all(L1, L2) |
| 1493 |
local sectors = self:lines_position(L1, L2) |
| 1494 |
|
| 1495 |
local pa_center = path:new() |
| 1496 |
local pa_through = path:new() |
| 1497 |
local n = 0 |
| 1498 |
|
| 1499 |
-- push "à la CLP" (centralisé ici) |
| 1500 |
local function push(o_, t_) |
| 1501 |
if o_ and t_ then |
| 1502 |
pa_center:add_point(o_) |
| 1503 |
pa_through:add_point(t_) |
| 1504 |
n = n + 1 |
| 1505 |
end |
| 1506 |
end |
| 1507 |
|
| 1508 |
-- Ingestion (pc, pt, m) → paths locaux via push |
| 1509 |
local function push_from_paths(pc, pt, m) |
| 1510 |
if not pc or not pt or not m then return end |
| 1511 |
for i = 1, m do |
| 1512 |
push(pc:get(i), pt:get(i)) |
| 1513 |
end |
| 1514 |
end |
| 1515 |
|
| 1516 |
if not sectors or #sectors == 0 then |
| 1517 |
return pa_center, pa_through, 0 |
| 1518 |
end |
| 1519 |
|
| 1520 |
local function add_choice(choice, inside) |
| 1521 |
local pc, pt, m = self:CLL(L1, L2, choice, inside) |
| 1522 |
push_from_paths(pc, pt, m) |
| 1523 |
end |
| 1524 |
|
| 1525 |
if #sectors == 1 then |
| 1526 |
local m = sectors[1] |
| 1527 |
add_choice(m, nil) -- 2 |
| 1528 |
add_choice(m, "inside") -- 2 -> total 4 |
| 1529 |
elseif #sectors == 2 then |
| 1530 |
local m, n = sectors[1], sectors[2] -- adjacents |
| 1531 |
add_choice(m, nil) -- 2 |
| 1532 |
add_choice(n, nil) -- 2 -> total 4 |
| 1533 |
elseif #sectors == 3 then |
| 1534 |
local l, m, n = sectors[1], sectors[2], sectors[3] |
| 1535 |
add_choice(l, nil) -- 2 |
| 1536 |
add_choice(n, nil) -- 2 |
| 1537 |
add_choice(m, nil) -- 2 |
| 1538 |
add_choice(m, "inside") -- 2 -> total 8 |
| 1539 |
else |
| 1540 |
-- {1,2,3,4} -> 8 solutions (2 par secteur) |
| 1541 |
add_choice(1, nil) |
| 1542 |
add_choice(2, nil) |
| 1543 |
add_choice(3, nil) |
| 1544 |
add_choice(4, nil) |
| 1545 |
end |
| 1546 |
|
| 1547 |
return pa_center, pa_through, n |
| 1548 |
end |
| 1549 |
|
| 1550 |
|
| 1551 |
-- ========================================================= |
| 1552 |
-- ========================================================= |
| 1553 |
|
| 1554 |
|
| 1555 |
function circle:CCL_DDD(C2, D) |
| 1556 |
local pa_center, pa_through = path:new(), path:new() |
| 1557 |
local n = 0 |
| 1558 |
local function push(o, t) |
| 1559 |
if o and t then |
| 1560 |
pa_center:add_point(o) |
| 1561 |
pa_through:add_point(t) |
| 1562 |
n = n + 1 |
| 1563 |
end |
| 1564 |
end |
| 1565 |
|
| 1566 |
local function circle_with_new_radius_keep_center(C, newR) |
| 1567 |
local Tp = report_(C.center, C.through, newR) |
| 1568 |
return circle:new(C.center, Tp) |
| 1569 |
end |
| 1570 |
|
| 1571 |
|
| 1572 |
local C1, O1, R1 = self, self.center, self.radius |
| 1573 |
local O2, R2 = C2.center, C2.radius |
| 1574 |
local EPS = 0.0001 |
| 1575 |
|
| 1576 |
local function signed_side_(L, P) |
| 1577 |
local a, b = L:get() |
| 1578 |
local z = (b - a) ^ (P - a) |
| 1579 |
if math.abs(z) <= EPS then return 0 end |
| 1580 |
return (z > 0) and 1 or -1 |
| 1581 |
end |
| 1582 |
|
| 1583 |
local function line_separates_disks_(L, O1, R1, O2, R2) |
| 1584 |
local s1 = signed_side_(L, O1) |
| 1585 |
local s2 = signed_side_(L, O2) |
| 1586 |
if s1 == 0 or s2 == 0 or s1 * s2 >= 0 then |
| 1587 |
return false -- centres du même côté ou sur la droite -> pas “entre” |
| 1588 |
end |
| 1589 |
local d1 = D:distance(O1) |
| 1590 |
local d2 = D:distance(O2) |
| 1591 |
return (d1 > R1 + EPS) and (d2 > R2 + EPS) -- strictement en dehors des deux disques |
| 1592 |
end |
| 1593 |
|
| 1594 |
|
| 1595 |
|
| 1596 |
if line_separates_disks_(D, O1, R1, O2, R2) then |
| 1597 |
return pa_center, pa_through, 0 |
| 1598 |
end |
| 1599 |
---------------------------------------------------------- |
| 1600 |
-- ====== FAMILLE R1 - R2 : droite translatée -R2 ====== |
| 1601 |
---------------------------------------------------------- |
| 1602 |
do |
| 1603 |
local newR = R1 - R2 |
| 1604 |
local pc, pt, m |
| 1605 |
local Lplus = D:collinear_at_distance(-R2) |
| 1606 |
if math.abs(newR) < EPS then |
| 1607 |
pc, pt, m = Lplus:LPP(O1, O2) |
| 1608 |
else |
| 1609 |
local Cminus = circle_with_new_radius_keep_center(C1, newR) |
| 1610 |
pc, pt, m = Cminus:CLP(Lplus, O2) |
| 1611 |
end |
| 1612 |
|
| 1613 |
for i = 1, m do |
| 1614 |
local wi = pc:get(i) |
| 1615 |
local ti = pt:get(i) |
| 1616 |
|
| 1617 |
local r0 = length_(wi, ti) |
| 1618 |
local r = r0 - R2 |
| 1619 |
local Ti_new = report_(wi, ti, r) |
| 1620 |
|
| 1621 |
local di = D:distance(wi) |
| 1622 |
local li = length_(wi, Ti_new) |
| 1623 |
|
| 1624 |
if act_(O1, C1.through, wi, Ti_new) |
| 1625 |
and act_(O2, C2.through, wi, Ti_new) |
| 1626 |
and (math.abs(li - di) < EPS) |
| 1627 |
then |
| 1628 |
push(wi, Ti_new) |
| 1629 |
end |
| 1630 |
end |
| 1631 |
end |
| 1632 |
|
| 1633 |
---------------------------------------------------------- |
| 1634 |
-- ====== FAMILLE R1 - R2 : droite translatée +R2 ====== |
| 1635 |
---------------------------------------------------------- |
| 1636 |
do |
| 1637 |
local newR = R1 - R2 |
| 1638 |
local pc, pt, m |
| 1639 |
local Lplus = D:collinear_at_distance(R2) |
| 1640 |
if math.abs(newR) < EPS then |
| 1641 |
pc, pt, m = Lplus:LPP(O1, O2) |
| 1642 |
else |
| 1643 |
local Cminus = circle_with_new_radius_keep_center(C1, newR) |
| 1644 |
pc, pt, m = Cminus:CLP(Lplus, O2) |
| 1645 |
end |
| 1646 |
|
| 1647 |
|
| 1648 |
for i = 1, m do |
| 1649 |
local wi = pc:get(i) |
| 1650 |
local ti = pt:get(i) |
| 1651 |
|
| 1652 |
local r0 = length_(wi, ti) |
| 1653 |
local r = r0 + R2 |
| 1654 |
local Ti_new = report_(wi, ti, r) |
| 1655 |
|
| 1656 |
local di = D:distance(wi) |
| 1657 |
local li = length_(wi, Ti_new) |
| 1658 |
|
| 1659 |
if act_(O1, C1.through, wi, Ti_new) |
| 1660 |
and act_(O2, C2.through, wi, Ti_new) |
| 1661 |
and (math.abs(li - di) < EPS) |
| 1662 |
then |
| 1663 |
push(wi, Ti_new) |
| 1664 |
end |
| 1665 |
end |
| 1666 |
end |
| 1667 |
|
| 1668 |
---------------------------------------------------------- |
| 1669 |
-- ====== FAMILLE R1 + R2 : droite translatée -R2 ====== |
| 1670 |
---------------------------------------------------------- |
| 1671 |
do |
| 1672 |
local newR = R1 + R2 |
| 1673 |
local Cplus = circle_with_new_radius_keep_center(C1, newR) |
| 1674 |
local Lplus = D:collinear_at_distance(-R2) |
| 1675 |
local pc, pt, m = Cplus:CLP(Lplus, O2) |
| 1676 |
|
| 1677 |
for i = 1, m do |
| 1678 |
local wi = pc:get(i) |
| 1679 |
local ti = pt:get(i) |
| 1680 |
|
| 1681 |
local r0 = length_(wi, ti) |
| 1682 |
local r = r0 - R2 -- même principe que R1-R2 / -R2 |
| 1683 |
local Ti_new = report_(wi, ti, r) |
| 1684 |
|
| 1685 |
local di = D:distance(wi) |
| 1686 |
local li = length_(wi, Ti_new) |
| 1687 |
|
| 1688 |
if act_(O1, C1.through, wi, Ti_new) |
| 1689 |
and act_(O2, C2.through, wi, Ti_new) |
| 1690 |
and (math.abs(li - di) < EPS) |
| 1691 |
then |
| 1692 |
push(wi, Ti_new) |
| 1693 |
end |
| 1694 |
end |
| 1695 |
end |
| 1696 |
|
| 1697 |
---------------------------------------------------------- |
| 1698 |
-- ====== FAMILLE R1 + R2 : droite translatée +R2 ====== |
| 1699 |
---------------------------------------------------------- |
| 1700 |
do |
| 1701 |
local newR = R1 + R2 |
| 1702 |
local Cplus = circle_with_new_radius_keep_center(C1, newR) |
| 1703 |
local Lminus = D:collinear_at_distance( R2) |
| 1704 |
local pc, pt, m = Cplus:CLP(Lminus, O2) |
| 1705 |
|
| 1706 |
for i = 1, m do |
| 1707 |
local wi = pc:get(i) |
| 1708 |
local ti = pt:get(i) |
| 1709 |
|
| 1710 |
local r0 = length_(wi, ti) |
| 1711 |
local r = r0 + R2 -- même principe que R1-R2 / +R2 |
| 1712 |
local Ti_new = report_(wi, ti, r) |
| 1713 |
|
| 1714 |
local di = D:distance(wi) |
| 1715 |
local li = length_(wi, Ti_new) |
| 1716 |
|
| 1717 |
if act_(O1, C1.through, wi, Ti_new) |
| 1718 |
and act_(O2, C2.through, wi, Ti_new) |
| 1719 |
and (math.abs(li - di) < EPS) |
| 1720 |
then |
| 1721 |
push(wi, Ti_new) |
| 1722 |
end |
| 1723 |
end |
| 1724 |
end |
| 1725 |
|
| 1726 |
return pa_center, pa_through, n |
| 1727 |
end |
| 1728 |
--============================================ |
| 1729 |
---======== CCL circles tangent inside |
| 1730 |
--============================================ |
| 1731 |
|
| 1732 |
function circle:CCL_ITT(C2, D) |
| 1733 |
local pa_center = path:new() |
| 1734 |
local pa_through = path:new() |
| 1735 |
local count = 0 |
| 1736 |
|
| 1737 |
local function push(o, t) |
| 1738 |
if o and t then |
| 1739 |
pa_center:add_point(o) |
| 1740 |
pa_through:add_point(t) |
| 1741 |
count = count + 1 |
| 1742 |
end |
| 1743 |
end |
| 1744 |
|
| 1745 |
local C1 = self |
| 1746 |
local c1, t1 = C1:get() |
| 1747 |
local c2, t2 = C2:get() |
| 1748 |
local r1 = point.abs(c1 - t1) |
| 1749 |
local r2 = point.abs(c2 - t2) |
| 1750 |
-- cercle d'inversion : centre O (intersection des deux cercles), rayon OC2 |
| 1751 |
local O = tangent_point_two_circles_(c1, r1, c2, r2) |
| 1752 |
|
| 1753 |
|
| 1754 |
local Cinv = circle:new(O, c2) |
| 1755 |
|
| 1756 |
-- inversion des deux cercles et de la droite D |
| 1757 |
local L1 = Cinv:inversion(C1) -- ligne image de C1 |
| 1758 |
local L2 = Cinv:inversion(C2) -- ligne image de C2 |
| 1759 |
local CD = Cinv:inversion(D) -- cercle image de la droite D |
| 1760 |
|
| 1761 |
-- cercles tangents à CD et aux deux lignes L1, L2 |
| 1762 |
local pc, pt, nsol = CD:CLL(L1, L2) |
| 1763 |
|
| 1764 |
-- pour chaque solution de CLL, on ré-inverse le cercle trouvé |
| 1765 |
for i = 1, nsol do |
| 1766 |
local W = pc:get(i) |
| 1767 |
local T = pt:get(i) |
| 1768 |
if W and T then |
| 1769 |
local CW = circle:new(W, T) -- cercle solution dans le monde inversé |
| 1770 |
local w, t = Cinv:inversion(CW):get() -- retour dans la géométrie initiale |
| 1771 |
push(w, t) |
| 1772 |
end |
| 1773 |
end |
| 1774 |
return pa_center, pa_through, count |
| 1775 |
end |
| 1776 |
|
| 1777 |
|
| 1778 |
function circle:CCL_ISD(C2, D) |
| 1779 |
local C1 = self |
| 1780 |
local pa_center, pa_through = path:new(), path:new() |
| 1781 |
local n = 0 |
| 1782 |
local function push(o, t) |
| 1783 |
if o and t then |
| 1784 |
pa_center:add_point(o) |
| 1785 |
pa_through:add_point(t) |
| 1786 |
n = n + 1 |
| 1787 |
end |
| 1788 |
end |
| 1789 |
|
| 1790 |
local c1, t1 = C1:get() |
| 1791 |
local a, b = D:get() |
| 1792 |
local I1, I2 = intersection_lc_(a, b, c1, t1) |
| 1793 |
local I = I1 or I2 |
| 1794 |
local Cinv = circle:new(I, c1) |
| 1795 |
local L1 = Cinv:inversion(C1) |
| 1796 |
local C2i = Cinv:inversion(C2) -- cercle C2' |
| 1797 |
local pc, pt, m = C2i:CLL(L1, D) |
| 1798 |
|
| 1799 |
for i = 1, m do |
| 1800 |
local W = pc:get(i) |
| 1801 |
local T = pt:get(i) |
| 1802 |
|
| 1803 |
local Ctmp = circle:new(W, T) |
| 1804 |
local S = Cinv:inversion(Ctmp) |
| 1805 |
local O, Q = S:get() |
| 1806 |
|
| 1807 |
push(O, Q) |
| 1808 |
end |
| 1809 |
return pa_center, pa_through, n |
| 1810 |
end |
| 1811 |
|
| 1812 |
function circle:CCL(C2, D, EPS) |
| 1813 |
EPS = EPS or tkz.epsilon |
| 1814 |
local pa_center, pa_through = path:new(), path:new() |
| 1815 |
local n = 0 |
| 1816 |
local function push(o, t) |
| 1817 |
if o and t then |
| 1818 |
pa_center:add_point(o) |
| 1819 |
pa_through:add_point(t) |
| 1820 |
n = n + 1 |
| 1821 |
end |
| 1822 |
end |
| 1823 |
|
| 1824 |
local C1 = self |
| 1825 |
local c1 = C1.center |
| 1826 |
local r1 = C1.radius |
| 1827 |
local c2 = C2.center |
| 1828 |
local r2 = C2.radius |
| 1829 |
local posCC = circles_position_(c1, r1, c2, r2, EPS) |
| 1830 |
local posC1L = line_position_(c1, r1, D.pa, D.pb, EPS) |
| 1831 |
local posC2L = line_position_(c2, r2, D.pa, D.pb, EPS) |
| 1832 |
|
| 1833 |
-------------------------------------------------- |
| 1834 |
|
| 1835 |
if (posCC == "DISJOINT_INT" or posCC == "TANGENT_INT") |
| 1836 |
and posC1L == "DISJOINT" |
| 1837 |
then |
| 1838 |
return path:new(), path:new(), 0 |
| 1839 |
|
| 1840 |
elseif (posCC == "DISJOINT_INT") and (posC1L == "SECANT") |
| 1841 |
and ((posC2L == "DISJOINT") or (posC2L == "SECANT")) then |
| 1842 |
return self:CCL_ISD(C2, D) |
| 1843 |
|
| 1844 |
elseif posCC == "TANGENT_INT" and posC2L == "TANGENT" then |
| 1845 |
return self:CCL_ITT(C2, D) |
| 1846 |
|
| 1847 |
else |
| 1848 |
return self:CCL_DDD(C2, D) |
| 1849 |
end |
| 1850 |
|
| 1851 |
end |
| 1852 |
---===== START CCC by Viète =====------- |
| 1853 |
-- === Test de tangence robuste (absolu ET relatif) ============================ |
| 1854 |
|
| 1855 |
|
| 1856 |
-- === CCC : toutes les solutions (0..8) ====================================== |
| 1857 |
function circle:CCC_viete_core(C2, C3, opts) |
| 1858 |
opts = opts or {} |
| 1859 |
local C1, R3, W3 = self, C3.radius, C3.center |
| 1860 |
|
| 1861 |
local pc, pt, n = path:new(), path:new(), 0 |
| 1862 |
local function push(o,t) pc:add_point(o); pt:add_point(t); n = n + 1 end |
| 1863 |
|
| 1864 |
-- dédup : compare centre et rayon avec tolérance |
| 1865 |
local function exists_(o,t) |
| 1866 |
local r = length_(o,t) |
| 1867 |
for i=1,n do |
| 1868 |
local oi, ti = pc:get(i), pt:get(i) |
| 1869 |
if oi and ti then |
| 1870 |
local same_center = length_(o, oi) <= (opts.abs or 1e-4) |
| 1871 |
local same_radius = math.abs(r - length_(oi,ti)) <= (opts.abs or 1e-4) |
| 1872 |
if same_center and same_radius then return true end |
| 1873 |
end |
| 1874 |
end |
| 1875 |
return false |
| 1876 |
end |
| 1877 |
local function push_unique(o,t) if o and t and not exists_(o,t) then push(o,t) end end |
| 1878 |
|
| 1879 |
-- ta variante “garder centre, ajouter ΔR au rayon” |
| 1880 |
local function grow_keep_center(C, dR) |
| 1881 |
return circle:new(C.center, report_(C.center, C.through, dR, C.through)) |
| 1882 |
end |
| 1883 |
|
| 1884 |
-- test "rayon ~ 0" robuste (absolu OU relatif) |
| 1885 |
local function radius_is_zero_(r, ref) |
| 1886 |
local eps_abs = (opts.abs ~= nil) and opts.abs or 1e-4 |
| 1887 |
local eps_rel = (opts.rel ~= nil) and opts.rel or 1e-6 |
| 1888 |
return (math.abs(r) <= eps_abs) or (math.abs(r)/math.max(ref,1.0) <= eps_rel) |
| 1889 |
end |
| 1890 |
|
| 1891 |
-- petit helper : à partir d'une liste de centres (path pcv, nv), |
| 1892 |
-- produire les deux relèvements +/- R3 (comme dans ton code initial) |
| 1893 |
local function lift_from_centers_(pcv, nv) |
| 1894 |
for i=1,nv do |
| 1895 |
local O = pcv:get(i) |
| 1896 |
if O then |
| 1897 |
local Tplus = report_(O, W3, R3, W3) -- r = |OW3| + R3 (tangence interne à C3) |
| 1898 |
local Tminus = report_(O, W3, -R3, W3) -- r = |OW3| - R3 (tangence externe à C3) |
| 1899 |
|
| 1900 |
if Tplus then |
| 1901 |
local S = circle:new(O, Tplus) |
| 1902 |
if act_(S.center, S.through, C1.center, C1.through, opts) |
| 1903 |
and act_(S.center, S.through, C2.center, C2.through, opts) |
| 1904 |
and act_(S.center, S.through, C3.center, C3.through, opts) then |
| 1905 |
push_unique(O, Tplus) |
| 1906 |
end |
| 1907 |
end |
| 1908 |
|
| 1909 |
if Tminus then |
| 1910 |
local S = circle:new(O, Tminus) |
| 1911 |
if act_(S.center, S.through, C1.center, C1.through, opts) |
| 1912 |
and act_(S.center, S.through, C2.center, C2.through, opts) |
| 1913 |
and act_(S.center, S.through, C3.center, C3.through, opts) then |
| 1914 |
push_unique(O, Tminus) |
| 1915 |
end |
| 1916 |
end |
| 1917 |
end |
| 1918 |
end |
| 1919 |
end |
| 1920 |
|
| 1921 |
-- Les 4 branches de Viète (±R3 sur C1 et C2) |
| 1922 |
local mods = { {-R3,-R3}, {-R3, R3}, { R3,-R3}, { R3, R3} } |
| 1923 |
|
| 1924 |
for _, m in ipairs(mods) do |
| 1925 |
local C1T = grow_keep_center(C1, m[1]) -- (O1, R1 + s1*R3) |
| 1926 |
local C2T = grow_keep_center(C2, m[2]) -- (O2, R2 + s2*R3) |
| 1927 |
|
| 1928 |
local r1t = C1T.radius |
| 1929 |
local r2t = C2T.radius |
| 1930 |
|
| 1931 |
local ref = math.max(C1.radius, C2.radius, R3, 1.0) |
| 1932 |
local z1 = radius_is_zero_(r1t, ref) |
| 1933 |
local z2 = radius_is_zero_(r2t, ref) |
| 1934 |
|
| 1935 |
if z1 and z2 then |
| 1936 |
-- === PPP : les deux transformés sont des points => cercle passant par O1,O2,O3 |
| 1937 |
local pcv, ptv, nv = C1.center:PPP(C2.center, W3) |
| 1938 |
lift_from_centers_(pcv, nv) |
| 1939 |
|
| 1940 |
elseif z1 then |
| 1941 |
-- === CPP : un seul transformé est un point => tangent à C2T et passant par O1 et O3 |
| 1942 |
local pcv, ptv, nv = C2T:CPP(C1.center, W3) |
| 1943 |
-- on n’utilise que les centres pour garder la même logique de relèvement |
| 1944 |
lift_from_centers_(pcv, nv) |
| 1945 |
|
| 1946 |
elseif z2 then |
| 1947 |
-- === CPP : un seul transformé est un point => tangent à C1T et passant par O2 et O3 |
| 1948 |
local pcv, ptv, nv = C1T:CPP(C2.center, W3) |
| 1949 |
lift_from_centers_(pcv, nv) |
| 1950 |
|
| 1951 |
else |
| 1952 |
-- === CCP classique : cercle (préimage) passant par W3 et tangent à C1T et C2T |
| 1953 |
local pcv, _, nv = C1T:CCP(C2T, W3, "all") |
| 1954 |
lift_from_centers_(pcv, nv) |
| 1955 |
end |
| 1956 |
end |
| 1957 |
|
| 1958 |
return pc, pt, n |
| 1959 |
|
| 1960 |
end |
| 1961 |
|
| 1962 |
------------------------------------------------------------ |
| 1963 |
-- Dispatcher CCC : classification 3 cercles → résolution |
| 1964 |
-- - route "non-sécants" vers ta version qui marche (CCC_viete_ok) |
| 1965 |
-- - isole tous les cas "sécants" pour un traitement dédié (CCC_secant) |
| 1966 |
------------------------------------------------------------ |
| 1967 |
|
| 1968 |
-- === Tolérances (abs + rel) ==================================== |
| 1969 |
local function approx_(a, b, opts) |
| 1970 |
opts = opts or {} |
| 1971 |
local eps_abs = (opts.abs ~= nil) and opts.abs or 1e-4 |
| 1972 |
local eps_rel = (opts.rel ~= nil) and opts.rel or 1e-6 |
| 1973 |
local d = math.abs(a - b) |
| 1974 |
return (d <= eps_abs) or (d <= eps_rel * math.max(math.abs(a), math.abs(b), 1.0)) |
| 1975 |
end |
| 1976 |
|
| 1977 |
|
| 1978 |
-- === Signature des trois paires ================================= |
| 1979 |
-- Relation entre deux cercles Ci, Cj à partir de circles_position_ |
| 1980 |
|
| 1981 |
local function circle_relation_from_positions(Ci, Cj, EPS) |
| 1982 |
EPS = EPS or tkz.epsilon |
| 1983 |
|
| 1984 |
local c1, r1 = Ci.center, Ci.radius |
| 1985 |
local c2, r2 = Cj.center, Cj.radius |
| 1986 |
|
| 1987 |
-- d'abord : test "centres confondus" pour distinguer concentriques / égaux |
| 1988 |
local d = point.mod(c1 - c2) |
| 1989 |
local same_center = tkz.approx(d, 0, EPS) |
| 1990 |
if same_center then |
| 1991 |
if tkz.approx(r1, r2, EPS) then |
| 1992 |
return "equal" |
| 1993 |
else |
| 1994 |
return "concentric" |
| 1995 |
end |
| 1996 |
end |
| 1997 |
-- sinon, on délègue à circles_position_ |
| 1998 |
return circles_position_(c1, r1, c2, r2, EPS) |
| 1999 |
end |
| 2000 |
|
| 2001 |
-- === Signature des trois paires ================================= |
| 2002 |
local function three_circles_signature(C1, C2, C3, opts) |
| 2003 |
opts = opts or {} |
| 2004 |
local EPS = opts.EPS or opts.eps or tkz.epsilon |
| 2005 |
|
| 2006 |
-- relations pour chaque paire |
| 2007 |
local r12 = circle_relation_from_positions(C1, C2, EPS) |
| 2008 |
local r23 = circle_relation_from_positions(C2, C3, EPS) |
| 2009 |
local r31 = circle_relation_from_positions(C3, C1, EPS) |
| 2010 |
|
| 2011 |
-- comptage global comme avant |
| 2012 |
local counts = { |
| 2013 |
secant = 0, |
| 2014 |
tangent = 0, |
| 2015 |
disjoint = 0, |
| 2016 |
inside = 0, |
| 2017 |
concentric = 0, |
| 2018 |
equal = 0, |
| 2019 |
} |
| 2020 |
|
| 2021 |
local function acc(tag) |
| 2022 |
if tag == "SECANT" then |
| 2023 |
counts.secant = counts.secant + 1 |
| 2024 |
|
| 2025 |
elseif tag == "TANGENT_EXT" or tag == "TANGENT_INT" then |
| 2026 |
counts.tangent = counts.tangent + 1 |
| 2027 |
|
| 2028 |
elseif tag == "DISJOINT_EXT" then |
| 2029 |
counts.disjoint = counts.disjoint + 1 |
| 2030 |
|
| 2031 |
elseif tag == "DISJOINT_INT" then |
| 2032 |
counts.inside = counts.inside + 1 |
| 2033 |
|
| 2034 |
elseif tag == "CONCENTRIC" then |
| 2035 |
counts.concentric = counts.concentric + 1 |
| 2036 |
|
| 2037 |
elseif tag == "IDENTICAL" then |
| 2038 |
counts.equal = counts.equal + 1 |
| 2039 |
|
| 2040 |
else |
| 2041 |
-- optionnel mais utile pour éviter les faux silencieux |
| 2042 |
-- error("three_circles_signature: unexpected tag "..tostring(tag)) |
| 2043 |
end |
| 2044 |
end |
| 2045 |
|
| 2046 |
acc(r12) |
| 2047 |
acc(r23) |
| 2048 |
acc(r31) |
| 2049 |
|
| 2050 |
return { |
| 2051 |
r12 = r12, |
| 2052 |
r23 = r23, |
| 2053 |
r31 = r31, |
| 2054 |
counts = counts |
| 2055 |
} |
| 2056 |
end |
| 2057 |
|
| 2058 |
|
| 2059 |
|
| 2060 |
local function CCC_viete_ok(C1, C2, C3, opts) |
| 2061 |
return C1:CCC_viete_core(C2, C3, opts) -- <-- ajuste le nom si différent |
| 2062 |
end |
| 2063 |
|
| 2064 |
-- === 2) Stub pour les cas sécants (à implémenter ensuite) ========= |
| 2065 |
-- Cas CCC : les trois cercles sont deux à deux sécants |
| 2066 |
-- Stratégie : inversion en X = C1∩C2 → C1,C2 ↦ lignes |
| 2067 |
-- résoudre (S3:CLL(L1,L2)) ou (L1:LLL(L2,L3)) → ré-inversion |
| 2068 |
local function CCC_secant(C1, C2, C3, opts) |
| 2069 |
opts = opts or {} |
| 2070 |
|
| 2071 |
-- --- sorties (toujours renvoyées) |
| 2072 |
local pa_center = path:new() |
| 2073 |
local pa_through = path:new() |
| 2074 |
local n = 0 |
| 2075 |
|
| 2076 |
-- --- push unique (style unifié) |
| 2077 |
local function push(o, t) |
| 2078 |
if o and t then |
| 2079 |
pa_center:add_point(o) |
| 2080 |
pa_through:add_point(t) |
| 2081 |
n = n + 1 |
| 2082 |
end |
| 2083 |
end |
| 2084 |
|
| 2085 |
-- --- 1) intersections de C1 et C2 (doivent exister dans ce cas) |
| 2086 |
local X, Y = intersection(C1, C2) |
| 2087 |
if (not X) or (not Y) then |
| 2088 |
-- garde-fou : si pas sécants, on ne traite pas ici |
| 2089 |
return pa_center, pa_through, 0 |
| 2090 |
end |
| 2091 |
|
| 2092 |
-- --- 2) cercle d'inversion : centre X, rayon arbitraire (on prend XY) |
| 2093 |
local Cinv = circle:new(X, Y) |
| 2094 |
|
| 2095 |
-- --- 3) objets inversés |
| 2096 |
local L1 = Cinv:inversion(C1) -- attendu : ligne |
| 2097 |
local L2 = Cinv:inversion(C2) -- attendu : ligne |
| 2098 |
local S3 = Cinv:inversion(C3) -- cercle OU ligne (si C3 passe par X) |
| 2099 |
|
| 2100 |
-- --- helper : ré-inverser une solution donnée par (o_i, t_i) dans l'espace inversé |
| 2101 |
local function push_back_from_inverted(o_i, t_i) |
| 2102 |
if (not o_i) or (not t_i) then return end |
| 2103 |
local S_i = circle:new(o_i, t_i) -- cercle solution dans l'espace inversé |
| 2104 |
local S_o = Cinv:inversion(S_i) -- ré-inversion → cercle solution original |
| 2105 |
local t_out = Cinv:inversion(t_i) -- ré-inversion du point de tangence/through |
| 2106 |
if S_o and S_o.center and t_out then |
| 2107 |
push(S_o.center, t_out) |
| 2108 |
end |
| 2109 |
end |
| 2110 |
|
| 2111 |
-- --- 4) Résolution dans l'espace inversé puis ré-inversion |
| 2112 |
if S3 and S3.center then |
| 2113 |
-- 4.a) S3 est un cercle : problème CLL (cercle tangent à 2 lignes L1,L2 et à S3) |
| 2114 |
-- On demande toutes les solutions (secteurs) ; adaptez via opts.which / opts.inside si besoin. |
| 2115 |
local pc, pt, m = S3:CLL(L1, L2, "all") |
| 2116 |
if m and m > 0 then |
| 2117 |
for i = 1, m do |
| 2118 |
push_back_from_inverted(pc:get(i), pt:get(i)) -- pt[i] est le point de tangence sur S3 |
| 2119 |
end |
| 2120 |
end |
| 2121 |
|
| 2122 |
elseif S3 then |
| 2123 |
-- 4.b) S3 est une ligne (C3 passait par X) : problème LLL (trois lignes) |
| 2124 |
local pc, pt, m = L1:LLL(L2, S3, "all") |
| 2125 |
if m and m > 0 then |
| 2126 |
for i = 1, m do |
| 2127 |
push_back_from_inverted(pc:get(i), pt:get(i)) -- pt[i] : pied sur L1 (OK) |
| 2128 |
end |
| 2129 |
end |
| 2130 |
else |
| 2131 |
-- inversion de C3 échouée (hautement improbable) → pas de solution |
| 2132 |
return pa_center, pa_through, 0 |
| 2133 |
end |
| 2134 |
|
| 2135 |
return pa_center, pa_through, n |
| 2136 |
end |
| 2137 |
|
| 2138 |
-- === 3) Cas spécial : "deux tangents + un disjoint" ============== |
| 2139 |
local function CCC_two_tangent_one_disjoint(C1, C2, C3, sig, opts) |
| 2140 |
-- Ex : r12 tangent, r23 tangent, r31 disjoint (ou toute permutation) |
| 2141 |
return CCC_viete_ok(C1, C2, C3, opts) |
| 2142 |
end |
| 2143 |
|
| 2144 |
local function three_distinct_radii(C1, C2, C3, opts) |
| 2145 |
opts = opts or {} |
| 2146 |
local eps = opts.EPS or opts.eps or tkz.epsilon |
| 2147 |
local R1, R2, R3 = C1.radius, C2.radius, C3.radius |
| 2148 |
return (not tkz.approx(R1, R2, eps)) |
| 2149 |
and (not tkz.approx(R2, R3, eps)) |
| 2150 |
and (not tkz.approx(R3, R1, eps)) |
| 2151 |
end |
| 2152 |
|
| 2153 |
-- === 4) Dispatcher principal ===================================== |
| 2154 |
function circle:CCC(C2, C3, opts) |
| 2155 |
local C1 = self |
| 2156 |
local sig = three_circles_signature(C1, C2, C3, opts) |
| 2157 |
local k = sig.counts |
| 2158 |
-- (A) Si AU MOINS une paire est sécante → on isole et on traite à part |
| 2159 |
if k.secant > 0 then |
| 2160 |
return CCC_secant(C1, C2, C3, opts) |
| 2161 |
|
| 2162 |
-- (B) Cas spécial "deux tangents + un disjoint" |
| 2163 |
elseif (k.tangent == 2) and (k.disjoint == 1) and (k.inside == 0) and (k.concentric == 0) then |
| 2164 |
return CCC_two_tangent_one_disjoint(C1, C2, C3, sig, opts) |
| 2165 |
|
| 2166 |
elseif (k.tangent == 3) |
| 2167 |
and ( |
| 2168 |
(sig.r12 == "inside tangent" |
| 2169 |
and sig.r23 == "outside tangent" |
| 2170 |
and sig.r31 == "outside tangent") |
| 2171 |
or (sig.r23 == "inside tangent" |
| 2172 |
and sig.r31 == "outside tangent" |
| 2173 |
and sig.r12 == "outside tangent") |
| 2174 |
or (sig.r31 == "inside tangent" |
| 2175 |
and sig.r12 == "outside tangent" |
| 2176 |
and sig.r23 == "outside tangent") |
| 2177 |
) |
| 2178 |
then |
| 2179 |
return C1:CCC_gergonne(C2, C3, opts) |
| 2180 |
|
| 2181 |
else |
| 2182 |
return CCC_viete_ok(C1, C2, C3, opts) |
| 2183 |
end |
| 2184 |
end |
| 2185 |
|
| 2186 |
|
| 2187 |
----================================= |
| 2188 |
----=========== <END CCC viète =========== |
| 2189 |
----================================= |
| 2190 |
|
| 2191 |
---========================================================= |
| 2192 |
|
| 2193 |
-- CCC_gergonne : cercles tangents aux trois cercles (self, C2, C3) |
| 2194 |
-- Implémente la construction de Gergonne : |
| 2195 |
-- - lignes des centres de similitude (JJJ et JII), |
| 2196 |
-- - pôles Xi de ces lignes par rapport aux trois cercles, |
| 2197 |
-- - points de contact = (wX) ∩ (chaque cercle), |
| 2198 |
-- - centres solutions = cercles circonscrits à 3 points de contact |
| 2199 |
-- Retour : (PA.center, PA.through, n) — chemins + compteur |
| 2200 |
|
| 2201 |
function circle:CCC_gergonne(C2, C3, opts) |
| 2202 |
opts = opts or {} |
| 2203 |
local EPS = opts.eps or (tkz and tkz.epsilon) or 1e-6 |
| 2204 |
|
| 2205 |
local C1 = self |
| 2206 |
local O1, T1 = C1.center, C1.through |
| 2207 |
local O2, T2 = C2.center, C2.through |
| 2208 |
local O3, T3 = C3.center, C3.through |
| 2209 |
|
| 2210 |
-- chemins résultat |
| 2211 |
local PA_center = path:new() |
| 2212 |
local PA_through = path:new() |
| 2213 |
local n = 0 |
| 2214 |
|
| 2215 |
local function push(o_, t_) |
| 2216 |
if o_ and t_ then |
| 2217 |
PA_center:add_point(o_) |
| 2218 |
PA_through:add_point(t_) |
| 2219 |
n = n + 1 |
| 2220 |
end |
| 2221 |
end |
| 2222 |
|
| 2223 |
-- --- utilitaires locaux ----------------------------------------------- |
| 2224 |
local function d_(a,b) return length_(a,b) end |
| 2225 |
|
| 2226 |
-- test : un cercle (O,R) est tangent au cercle Ci |
| 2227 |
local function tangent_to(O, R, Ci) |
| 2228 |
local d = d_(O, Ci.center) |
| 2229 |
local r = Ci.radius |
| 2230 |
return tkz.approx(math.abs(d - (R + r)), 0, EPS) |
| 2231 |
or tkz.approx(math.abs(d - math.abs(R - r)), 0, EPS) |
| 2232 |
end |
| 2233 |
|
| 2234 |
-- dédoublonnage (même centre) |
| 2235 |
local function already_kept(O) |
| 2236 |
for i = 1, n do |
| 2237 |
if d_(O, PA_center:get(i)) <= 1e-4 then return true end |
| 2238 |
end |
| 2239 |
return false |
| 2240 |
end |
| 2241 |
|
| 2242 |
-- points de similitude externes/internes |
| 2243 |
local J12 = C1:external_similitude(C2) |
| 2244 |
local J13 = C1:external_similitude(C3) |
| 2245 |
local J23 = C2:external_similitude(C3) |
| 2246 |
local I12 = C1:internal_similitude(C2) |
| 2247 |
local I13 = C1:internal_similitude(C3) |
| 2248 |
local I23 = C2:internal_similitude(C3) |
| 2249 |
|
| 2250 |
-- centre radical (intersection de deux axes radicaux) |
| 2251 |
local w = C1:radical_center(C2, C3) |
| 2252 |
|
| 2253 |
-- Les 4 droites de Gergonne : 1× JJJ et 3× JII |
| 2254 |
local lines = { |
| 2255 |
line(J12, J23), -- JJJ (passe aussi par J13) |
| 2256 |
line(J12, I13), -- JII |
| 2257 |
line(J13, I12), -- JII |
| 2258 |
line(J23, I12), -- JII (peu importe laquelle des deux "I" on prend) |
| 2259 |
} |
| 2260 |
|
| 2261 |
-- Pour chaque droite : pôles, points de contact, combinaisons candidates |
| 2262 |
for _, L in ipairs(lines) do |
| 2263 |
-- Pôles par rapport aux trois cercles |
| 2264 |
local X1 = C1:pole(L) |
| 2265 |
local X2 = C2:pole(L) |
| 2266 |
local X3 = C3:pole(L) |
| 2267 |
|
| 2268 |
-- Intersections (wXi) avec chaque cercle = 2 points de contact par cercle |
| 2269 |
local a1, a2 = intersection_lc_(w, X1, O1, T1) |
| 2270 |
local b1, b2 = intersection_lc_(w, X2, O2, T2) |
| 2271 |
local c1, c2 = intersection_lc_(w, X3, O3, T3) |
| 2272 |
|
| 2273 |
-- candidates = 4 trios "à parité impaire" sur {1,2}^3 |
| 2274 |
-- (112), (121), (211) + leurs complémentaires (221), (212), (122) |
| 2275 |
-- On les calcule toutes puis on filtre par le test de tangence. |
| 2276 |
local triples = { |
| 2277 |
{a1,b1,c2}, {a1,b2,c1}, {a2,b1,c1}, {a2,b2,c1}, |
| 2278 |
{a2,b2,c1}, {a2,b1,c2}, {a1,b2,c2}, {a1,b1,c2}, |
| 2279 |
} |
| 2280 |
|
| 2281 |
for _, tri in ipairs(triples) do |
| 2282 |
local A, B, C = tri[1], tri[2], tri[3] |
| 2283 |
if A and B and C then |
| 2284 |
local O = circum_center_(A, B, C) |
| 2285 |
if O then |
| 2286 |
local R = d_(O, A) -- rayon = distance au point de contact choisi |
| 2287 |
-- garde seulement si tangent aux trois cercles |
| 2288 |
if tangent_to(O, R, C1) and tangent_to(O, R, C2) and tangent_to(O, R, C3) then |
| 2289 |
if not already_kept(O) then |
| 2290 |
push(O, A) -- "through" = un des points de contact |
| 2291 |
end |
| 2292 |
end |
| 2293 |
end |
| 2294 |
end |
| 2295 |
end |
| 2296 |
end |
| 2297 |
|
| 2298 |
return PA_center, PA_through, n |
| 2299 |
end |
| 2300 |
|
| 2301 |
|
| 2302 |
return circle |