-- luadraw_hyperbolicg.lua -- date 2026/12/06 -- version 3.8 -- Copyright 2026 Patrick Fradin -- This work may be distributed and/or modified under the -- conditions of the LaTeX Project Public License. -- The latest version of this license is in -- https://www.ctan.org/license/lppl require 'luadraw_pdfliteral' local ld = luadraw local graph = ld.graph local cpx = ld.cpx local Z = cpx.Z local i, exp, abs, sqrt, th = cpx.I, cpx.exp, math.abs, math.sqrt, math.tanh local pi, sin, cos, tan = math.pi, math.sin, math.cos, math.tan local tblInsert = table.insert local tblAppend = table.append ld.hg_mode = "plane" -- or "disc" local function argth(x) if x >= 1 then return math.huge end if x <= -1 then return -math.huge end return 0.5 * math.log((1 + x) / (1 - x)) end function ld.hg_plane2disc(L) local function f(z) if z == "inf" then return Z(1, 0) end z = cpx.toComplex(z) return (z - i) / (z + i) end if L == "inf" or cpx.isComplex(L) or type(L) == "number" then return f(L) end local res = {} for k, v in ipairs(L) do res[k] = ld.hg_plane2disc(v) end return res end function ld.hg_disc2plane(L) local function f(z) z = cpx.toComplex(z) if cpx.abs(z - 1) < 1e-12 then return "inf" end return -i * (z + 1) / (z - 1) end if cpx.isComplex(L) or type(L) == "number" then return f(L) end local res = {} for k, v in ipairs(L) do res[k] = ld.hg_disc2plane(v) end return res end function ld.hg_dist(A,B) -- returns the hyperbolic distance from A to B A = cpx.toComplex(A) B = cpx.toComplex(B) if ld.hg_mode == "disc" then A = ld.hg_disc2plane(A) B = ld.hg_disc2plane(B) end return 2*argth( cpx.abs( (B-A)/(B-cpx.bar(A)) ) ) end local function disc_ortho_center(A, B) -- Returns the Euclidean center of the circle orthogonal to the unit circle passing through A and B if abs(cpx.abs2(A) - 1) > 1e-8 then return ld.interD(ld.med(A, B), ld.med(A, 1 / cpx.bar(A))) elseif abs(cpx.abs2(B) - 1) > 1e-8 then return ld.interD(ld.med(A, B), ld.med(B, 1 / cpx.bar(B))) else -- Both A and B are ideal points on the unit circle (not diametrically opposite) return 2 * A * B / (A + B) end end function ld.hg_polyline(L, close) -- L is a complex number or a list of complex numbers or a list of lists of complex numbers -- Supports ideal vertices on the boundary (including "inf" in "plane" mode) close = close or false if L == nil or type(L) ~= "table" or #L == 0 then return end if cpx.isComplex(L[1]) or L[1] == "inf" then L = {L} end local function to_vertex(z) if z == "inf" then return "inf" end return cpx.toComplex(z) end local y_top = 1000 local jump_cmd = close and "l" or "m" local p = {} local first = true for _, raw_cp in ipairs(L) do local cp = {} for idx_v, v in ipairs(raw_cp) do cp[idx_v] = to_vertex(v) end if #cp >= 2 then local B, A = cp[1] if close then table.insert(cp, B) end local start_pt if B == "inf" then for _, v in ipairs(cp) do if v ~= "inf" then start_pt = Z(v.re, y_top) break end end else start_pt = B end if start_pt ~= nil then if first then p = {start_pt} first = false else tblAppend(p, {start_pt, "m"}) end if ld.hg_mode == "disc" then for k = 2, #cp do A = B; B = cp[k] if abs(cpx.det(A, B)) < 1e-8 then tblAppend(p, {B, "l"}) else local C = disc_ortho_center(A, B) local r = cpx.abs(A - C) local sens = ((cpx.det(A - C, B - C) > 0) and 1) or -1 tblAppend(p, {C, B, r, sens, "ca"}) end end else -- plane for k = 2, #cp do A = B; B = cp[k] if A == "inf" then if B ~= "inf" then tblAppend(p, {Z(B.re, y_top), jump_cmd, B, "l"}) end elseif B == "inf" then tblAppend(p, {Z(A.re, y_top), "l"}) elseif abs(A.re - B.re) < 1e-8 then tblAppend(p, {B, "l"}) else local C = ld.interD({0, 1}, ld.med(A, B)) local r = cpx.abs(A - C) local sens = ((A.re > B.re) and 1) or -1 tblAppend(p, {C, B, r, sens, "ca"}) end end end if close then tblInsert(p, "cl") end end end end return p end function graph:Dhg_polyline(L, close, draw_options) -- draw an hyperbolic polyline if type(close) == "string" then draw_options = close; close = false end local p = ld.hg_polyline(L, close) if p ~= nil then self:Dpath(p,draw_options) end end function ld.hg_line(A, B) -- or hg_line({A, B}) if type(A) == "table" and not cpx.isComplex(A) and B == nil then A, B = table.unpack(A) end if A == nil or B == nil then return end local p if ld.hg_mode == "disc" then A = cpx.toComplex(A); B = cpx.toComplex(B) if abs(cpx.det(A, B)) < 1e-8 then local ref = (cpx.abs(A) > 1e-8) and A or B p = {-cpx.normalize(ref), cpx.normalize(ref), "l"} else local C = disc_ortho_center(A, B) local R = cpx.abs(A - C) local absC2 = cpx.abs2(C) local U = C * (1 + i * R) / absC2 local V = C * (1 - i * R) / absC2 local sens = ((cpx.det(U - C, V - C) > 0) and 1) or -1 p = {U, C, V, R, sens, "ca"} end else -- plane if A == "inf" then B = cpx.toComplex(B) p = {B.re, Z(B.re, 100), "l"} elseif B == "inf" then A = cpx.toComplex(A) p = {A.re, Z(A.re, 100), "l"} else A = cpx.toComplex(A); B = cpx.toComplex(B) if abs(A.re - B.re) < 1e-8 then p = {A.re, Z(A.re, 100), "l"} else local O = ld.interD({0, 1}, ld.med(A, B)) local R = cpx.abs(B - O) p = {O + R, O, O - R, R, 1, "ca"} end end end return p end function graph:Dhg_line(A, B, draw_options) -- or Dhg_line({A, B}, draw_options) if type(A) == "table" and not cpx.isComplex(A) and (B == nil or type(B) == "table" or (type(B) == "string" and B ~= "inf")) then draw_options = B A, B = table.unpack(A) end local p = ld.hg_line(A, B) if p ~= nil then self:Dpath(p, draw_options) end end function ld.hg_iline(A, B) -- or hg_iline({A,B}) -- A and B are ideal points on the boundary (|A|=|B|=1 in disc mode, or Im=0 / "inf" in plane mode) if type(A) == "table" and not cpx.isComplex(A) and B == nil then A, B = table.unpack(A) end local p if ld.hg_mode == "disc" then A = cpx.toComplex(A); B = cpx.toComplex(B) if abs(cpx.det(A, B)) < 1e-8 then -- points diamétralement opposés p = {A, B, "l"} else local C = 2 * A * B / (A + B) local R = cpx.abs(A - C) local sens = ((cpx.det(A - C, B - C) > 0) and 1) or -1 p = {A, C, B, R, sens, "ca"} end else -- plane (A et B sur l'axe réel, ou l'un des deux égal à "inf" / nil) if A == nil or A == "inf" then B = cpx.toComplex(B) p = {B.re, Z(B.re, 100), "l"} elseif B == nil or B == "inf" then A = cpx.toComplex(A) p = {Z(A.re, 100), A.re, "l"} else A = cpx.toComplex(A); B = cpx.toComplex(B) local O = (A.re + B.re) / 2 local R = abs(B.re - A.re) / 2 local sens = (A.re > B.re) and 1 or -1 p = {A.re, O, B.re, R, sens, "ca"} end end return p end function graph:Dhg_iline(A, B, draw_options) if type(A) == "table" and not cpx.isComplex(A) and (B == nil or type(B) == "table" or (type(B) == "string" and B ~= "inf")) then draw_options = B A, B = table.unpack(A) end local p = ld.hg_iline(A, B) if p ~= nil then self:Dpath(p, draw_options) end end function ld.hg_circle(z0, r) -- returns the hyperbolic circle with center z0 and radius R (path) local A if ld.hg_mode == "plane" then A = cpx.toComplex(z0) else A = ld.hg_disc2plane(z0) end local k = th(r/2) local C = (A + k*cpx.bar(A)) / (k+1) local D = (A - k*cpx.bar(A)) / (1-k) local O = (C+D)/2 local R = cpx.abs(C-O) local p if ld.hg_mode == "plane" then p = {O+R,O,"c"} else -- disc local t = R / cpx.abs(i+O) local C1 = ld.hg_plane2disc(O - t*(i+O)) local D1 = ld.hg_plane2disc(O + t*(i+O)) local O1 = (C1+D1)/2 R = cpx.abs(C1 - O1) p = {O1+R,O1,"c"} end return p end function graph:Dhg_circle(A,r, draw_options) -- draws the hyperbolic circle with center A and radius r local p = ld.hg_circle(A,r) if p ~= nil then self:Dpath( p, draw_options) end end function graph:Dhg_horocycle(O, A, draw_options) A = cpx.toComplex(A) if ld.hg_mode == "disc" then -- O est sur le cercle unité (|O| = 1), A est à l'intérieur (|A| < 1) local d2 = cpx.abs2(O-A) --(O.re - A.re)^2 + (O.im - A.im)^2 local dot = cpx.dot(A,O) local r = d2 / (2 * (1 - dot)) local C = (1 - r) * O self:Dcircle(C, r, draw_options) elseif ld.hg_mode == "plane" then -- Cas 1 : O est le point à l'infini (ex: O == nil ou O == "inf") if O == nil or O == "inf" then -- Droite horizontale passant par A (y = A.im) self:Dline({A, 1}, draw_options) else -- Cas 2 : O est sur l'axe réel (O.im == 0), A est dans le demi-plan supérieur (A.im > 0) O = cpx.toComplex(O) local d2 = cpx.abs2(A-O) --(A.re - O.re)^2 + (A.im - O.im)^2 local r = d2 / (2 * A.im) local C = Z(O.re, r) -- centre à la verticale de O à hauteur r self:Dcircle(C, r, draw_options) end end end function graph:Dhg_angle(B, A, C, r, draw_options) -- draws a right angle using the g:Dangle method local u = ld.hg_tangentV({A,B},A) local v = ld.hg_tangentV({A,C},A) r = r or 0.25 / self:Abs(1) self:Dangle(A+u,A,A+v,r,draw_options) end function graph:Dhg_markseg(A, B, n, len, dist, draw_options) -- marks segments like the g:Dmarkseg() method A = cpx.toComplex(A); B =cpx.toComplex(B) len = len or 0.25 len = len / self:Abs(1) dist = dist or 0.0625 dist = dist / self:Abs(1) local p = ld.hg_polyline({A,B}) local cmd = table.remove(p) if cmd == "l" then -- segment self:Dmarkseg(p[1], p[2], n, len, dist, draw_options) else -- arc local sens = table.remove(p) if sens == 1 then self:Dmarkarc(p[1],p[2], p[3], p[4], n, len, dist, draw_options) else self:Dmarkarc(p[3],p[2], p[1], p[4], n, len, dist, draw_options) end end end ------------ transformations ---------------------- function ld.hg_sym(L, axis) -- Returns the image of L under the hyperbolic reflection across axis = {A, B} local A0, B0 = axis[1], axis[2] if ld.hg_mode == "disc" then L = ld.ftransform(L, cpx.toComplex) local A = cpx.toComplex(A0) local B = cpx.toComplex(B0) if abs(cpx.det(A, B)) < 1e-8 then local dir = (cpx.abs(B - A) > 1e-8) and (B - A) or ((cpx.abs(A) > 1e-8) and A or B) local base = (cpx.abs(A) > 1e-8) and A or B return ld.sym(L, {base, dir}) else local C = disc_ortho_center(A, B) local R2 = cpx.abs2(C - A) local function f(z) return C + R2 / cpx.bar(z - C) end return ld.ftransform(L, f) end else -- plane local function sym_plane(z) if A0 == "inf" or B0 == "inf" then if z == "inf" then return "inf" end z = cpx.toComplex(z) local a = (A0 == "inf") and cpx.toComplex(B0).re or cpx.toComplex(A0).re return 2 * a - cpx.bar(z) end local A = cpx.toComplex(A0) local B = cpx.toComplex(B0) if abs(A.re - B.re) < 1e-8 then if z == "inf" then return "inf" end z = cpx.toComplex(z) local a = (A.re + B.re) / 2 return 2 * a - cpx.bar(z) else local O = ld.interD({0, 1}, ld.med(A, B)) local R = cpx.abs(B - O) if z == "inf" then return Z(O.re, 0) end z = cpx.toComplex(z) local z1 = cpx.bar(z - O) if cpx.abs(z1) < 1e-8 then return "inf" else return O + R * R / z1 end end end if L == "inf" or cpx.isComplex(L) or type(L) == "number" then return sym_plane(L) end local res = {} for k, v in ipairs(L) do res[k] = ld.hg_sym(v, axis) end return res end end function ld.hg_symC(L, C) -- Returns the image of L under the hyperbolic point reflection centered at C C = cpx.toComplex(C) if ld.hg_mode == "disc" then L = ld.ftransform(L, cpx.toComplex) local k = 1 + cpx.abs2(C) return ld.ftransform(L, function(z) return (k * z - 2 * C) / (2 * z * cpx.bar(C) - k) end) else -- plane local cbar = cpx.bar(C) local function symC_plane(z) if z == "inf" then return Z(C.re, 0) -- Z = 1 -> (C - cbar)/0? No: (C - z)/(z - cbar) -> -1, so (C + cbar)/2 = C.re end z = cpx.toComplex(z) local W = (C - z) / (z - cbar) if cpx.abs(1 - W) < 1e-8 then return "inf" end return (C - cbar * W) / (1 - W) end if L == "inf" or cpx.isComplex(L) or type(L) == "number" then return symC_plane(L) end local res = {} for k, v in ipairs(L) do res[k] = ld.hg_symC(v, C) end return res end end function ld.hg_rotate(L, alpha, C) -- Returns the image of L under the hyperbolic rotation with center C and angle alpha (in degrees) C = cpx.toComplex(C) local angle = alpha / 2 * ld.deg if ld.hg_mode == "disc" then L = ld.ftransform(L, cpx.toComplex) local A = exp(i * angle) - cpx.abs2(C) * exp(-i * angle) local B = -2 * i * C * sin(angle) return ld.ftransform(L, function(z) return (A * z + B) / (cpx.bar(B) * z + cpx.bar(A)) end) else -- plane local cbar = cpx.bar(C) local rot = exp(i * angle) local function rot_plane(z) local W = (z == "inf") and rot or ((z - C) / (z - cbar) * rot) if cpx.abs(1 - W) < 1e-8 then return "inf" end return (C - cbar * W) / (1 - W) end if L == "inf" or cpx.isComplex(L) or type(L) == "number" then return rot_plane(L) end local res = {} for k, v in ipairs(L) do res[k] = ld.hg_rotate(v, alpha, C) end return res end end function ld.hg_shift(L, A, B) --L is a complex number or a list of complex number or a list of lists of complex numbers, all in Klein's disc (|z| < 1) if ld.hg_mode="disc", or all in Poincaré's half-plan (z.im > 0) if ld.hg_mode = "plane" -- returns the image of L under the hyperbolic translation that maps A to B. A = cpx.toComplex(A) B = cpx.toComplex(B) local d1 = ld.hg_perp({A, B}, A) local d2 = ld.hg_perp_bisector(A, B) local function f(P) return ld.hg_sym( ld.hg_sym(P, d1), d2) end return ld.ftransform(L, f) end ---------------------- constructions ------------------------ function ld.hg_mid(a,b) -- or hg_mid({a,b}) -- returns mid of [a;b] if b == nil then a, b = table.unpack(a) end a = cpx.toComplex(a); b = cpx.toComplex(b) if ld.hg_mode == "disc" then local B = (b-a)/(1-cpx.bar(a)*b) local ab = cpx.abs2(B) local u = 0 if ab >= 1e-8 then u = (1-sqrt(1-ab))*B/ab end return (u+a)/(1+cpx.bar(a)*u) else -- plane if abs(a.re-b.re) < 1e-8 then return a.re + i * sqrt(a.im * b.im) else local O = ld.interD({0,5}, ld.med(a,b)) local R = cpx.abs(a-O) local Z2 = O-R local Z3 = O+R local omega = -((a-Z3)/(a-Z2)).im local Y1 = i*sqrt( cpx.abs( omega*( (b-Z2)/(b-Z3) ) ) ) return (Z3*Y1-omega*Z2) / (Y1-omega) end end end function ld.hg_tangentV(d,C) -- d = {A,B} -- returns a unit vector tangent to the (hyperbolic) line (AB) at point C—assumed to lie on this line—in the direction from A to B} local A, B = table.unpack( ld.map(cpx.toComplex, d) ) C = cpx.toComplex(C) if ld.hg_mode == "disc" then if abs(cpx.det(A,B)) < 1e-8 then -- 0 A B aligned return cpx.normalize(B-A) else local D = ld.interD( ld.med(A,B), ld.med(A,1/cpx.bar(A)) ) if cpx.det(A-D,B-D) < 0 then return i*cpx.normalize(D-C) else return i*cpx.normalize(C-D) end end else -- plane if abs(A.re - B.re) < 1e-8 then return cpx.normalize(B-A) else local O = ld.interD( {0,5}, ld.med(A,B) ) local u = C-O local v = i*cpx.normalize(u) if cpx.dot(v,B-A) >= 0 then return v else return -v end end end end function ld.hg_perp(d,c) -- d = {A,B} --returns 2 points on the hyperbolic perpendicular to (AB) passing through C, local a, b = table.unpack( ld.map(cpx.toComplex, d) ) c = cpx.toComplex(c) if ld.hg_mode == "disc" then -- on se ramène au cas ou c=0 local a1 = (a-c) / (-cpx.bar(c)*a + 1) local b1 = (b-c) / (-cpx.bar(c)*b + 1) local c1 if abs( cpx.det(a1,b1) ) < 1e-8 then if cpx.abs(a1) < 1e-8 then c1 = i*b1 else c1 = i*a1 end --0,a1,b1 alignés else local c2 = ld.interD( ld.med(a1,b1), ld.med(a1,1/cpx.bar(a1) ) ) c1 = cpx.normalize(c2)/2 end return {c, (c1 + c) / (cpx.bar(c)*c1 + 1) } else -- plane if abs(a.re - b.re) < 1e-8 then local x = a.re local r = cpx.abs(c-x) return {x+exp(i*pi/12)*r, x-r*exp(-i*pi/12)} else local O = ld.interD( {0,5}, ld.med(a,b)) local R = cpx.abs(a-O) local c1 = O + R^2 / cpx.bar(c-O) if cpx.abs(c1-c) < 1e-8 then local o1 = ld.interD( {0,5}, {c,i*(c-O)}) return {c, (2*o1-c).re + i*c1.im} else return {c,c1} end end end end function ld.hg_perp_bisector(A,B) --or hg_med({A,B}) -- returns 2 points on the perpendicular bisector of the hyperbolic segment [AB] if B == nil then A, B = table.unpack(A) end return ld.hg_perp({A,B}, ld.hg_mid(A,B) ) end ld.hg_med = ld.hg_perp_bisector function ld.hg_bisector(B,A,C,external) --returns two points on the internal or external bisector of angle BAC external = external or false if ld.hg_mode == "disc" then A = ld.hg_disc2plane(A); B = ld.hg_disc2plane(B); C = ld.hg_disc2plane(C) end A = cpx.toComplex(A) B = cpx.toComplex(B) C = cpx.toComplex(C) local oldmode = ld.hg_mode ld.hg_mode = "plane" local u1 = ld.hg_tangentV({A,B},A) local u2 = ld.hg_tangentV({A,C},A) ld.hg_mode = oldmode local v if external then v = u1 - u2 else v = u1 + u2 end if abs(v.re) < 1e-8 then v = i*v.im end local M1, M2 if v.re == 0 then M1 = A; if v.im > 0 then M2 = A+v else M2 = A-v end else local O = ld.interD( ld.perp({A,v},A), {0,5} ) local R = cpx.abs(A-O) M1 = ld.rotate(O-R,-5,O); M2 = ld.rotate(O+R,5,O) end if ld.hg_mode == "plane" then return {M1,M2} else return {ld.hg_plane2disc(M1), ld.hg_plane2disc(M2) } end end function ld.hg_polyreg(arg1, arg2, arg3) -- Usage 1: ld.hg_polyreg(n, p) -> regular (n,p) polygon centered at 0 (disc) or i (plane) -- Usage 2: ld.hg_polyreg(center, vertex, nb) -> regular nb-gon centered at 'center' with first vertex 'vertex' if arg3 ~= nil then -- Case 2: (center, vertex, nb) local center = cpx.toComplex(arg1) local vertex = cpx.toComplex(arg2) local nb = arg3 local P = {vertex} local step = 360 / nb for k = 1, nb - 1 do P[k + 1] = ld.hg_rotate(vertex, k * step, center) end return P else -- Case 1: (n, p) local n, p = arg1, arg2 local u = sin(pi/n + pi/p) + cos(pi/n + pi/p)/tan(pi/n) local r = 1/sqrt(u^2 - 1) local s1 = r*cos(pi/n + pi/p)/sin(pi/n)*exp(i*pi/n) local P = {s1} for k = 1, n - 1 do P[k + 1] = exp(i*2*k*pi/n)*s1 end if ld.hg_mode == "disc" then return P else return ld.hg_disc2plane(P) end end end function ld.hg_triangle_angles(alpha, beta, gamma) -- alpha, beta, gamma in radians or "inf" (0 angle, ideal vertex on the boundary) -- with alpha + beta + gamma < pi local function parse_angle(a) if a == "inf" or a == nil then return 0 end return a end local a = parse_angle(alpha) local b = parse_angle(beta) local c = parse_angle(gamma) if a < 0 or b < 0 or c < 0 or a + b + c >= pi - 1e-8 then return nil -- not an hyperbolic triangle end local T if a == 0 and b == 0 and c == 0 then -- Ideal equilateral triangle on the unit circle T = {Z(1, 0), exp(i * 2 * pi / 3), exp(i * 4 * pi / 3)} elseif a == 0 and b > 0 then -- Cyclic shift so that the first angle is strictly positive local T_shift = ld.hg_triangle_angles(b, c, a) return {T_shift[3], T_shift[1], T_shift[2]} elseif a == 0 and c > 0 then local T_shift = ld.hg_triangle_angles(c, a, b) return {T_shift[2], T_shift[3], T_shift[1]} else -- Here a > 0 local rc if b == 0 then rc = 1 else local ch_c = (cos(c) + cos(a) * cos(b)) / (sin(a) * sin(b)) rc = sqrt((ch_c - 1) / (ch_c + 1)) end local rb if c == 0 then rb = 1 else local ch_b = (cos(b) + cos(a) * cos(c)) / (sin(a) * sin(c)) rb = sqrt((ch_b - 1) / (ch_b + 1)) end local A = Z(0, 0) local B = Z(rc, 0) local C = rb * exp(i * a) T = {A, B, C} end if ld.hg_mode == "plane" then -- Note: in disc2plane, z = 1 maps to infinity (complex with huge/inf components or handled safely) local res = {} for k = 1, 3 do if cpx.abs(T[k] - 1) < 1e-12 then res[k] = "inf" else res[k] = ld.hg_disc2plane(T[k]) end end return res end return T end function ld.hg_interD(d1, d2) -- returns the intersection of two hyperbolic lines (or nil if they don't intersect) local A, B = table.unpack( ld.map(cpx.toComplex, d1) ) local C, D = table.unpack( ld.map(cpx.toComplex, d2) ) if ld.hg_mode == "disc" then A, B, C, D = table.unpack( ld.hg_disc2plane({A,B,C,D}) ) end local rep if abs(A.re - B.re) < 1e-8 then if abs(C.re - D.re) >= 1e-8 then local O = ld.interD( {0,5}, ld.med(C,D)) local R = cpx.abs(C-O) local delta = R^2 - (A.re - O.re)^2 if delta > 1e-12 then rep = A.re + i*sqrt(delta) end end else local O = ld.interD( {0,5}, ld.med(A,B)) local R = cpx.abs(A-O) if abs(C.re - D.re) < 1e-8 then local delta = R^2 - (C.re - O.re)^2 if delta > 1e-12 then rep = C.re + i*sqrt(delta) end else local O1 = ld.interD( {0,5}, ld.med(C,D)) local R1 = cpx.abs(C-O1) local u = (O - O1).re if abs(u) >= 1e-8 then -- évite la division par zéro (cercles concentriques ou confondus) local X = (R1^2 - R^2 - u^2)/(2*u) local delta = R^2 - X^2 if delta > 1e-12 then rep = X + O + i*sqrt(delta) end end end end if rep == nil then return nil end if ld.hg_mode == "plane" then return rep else return ld.hg_plane2disc(rep) end end function ld.hg_interL(L1, L2) -- return intersection of two hyperbolic polygonal lines local l1 = ld.hg_polyline(L1) local l2 = ld.hg_polyline(L2) return ld.interL( ld.path(l1), ld.path(l2) ) end ------------------ tiling -------------------------------------------- function ld.graph:Dhg_tiling(P0, max_level, options) -- Usage 1: g:Dhg_tiling(P0, max_level, options) -> stops at depth max_level -- Usage 2: g:Dhg_tiling(P0, options) -> stops according to min_size (in cm on the graph, default: 0.05 cm) -- options = { min_size = 0.05, max_tiles = 35000, draw = "none", fill = "blue", fill2 = ..., width = ..., mode = 0 } -- mode: 0 = even levels (default), 1 = odd levels, 2 = both levels (using fill and fill2) local P_clean = {} for idx_v, v in ipairs(P0) do P_clean[idx_v] = (v == "inf") and "inf" or cpx.toComplex(v) end P0 = P_clean if type(max_level) == "table" then options = max_level max_level = nil end options = options or {} local mode = options.mode if mode == nil then mode = options.inverse and 1 or 0 end local draw = options.draw or "none" local fill = options.fill or "blue" local fill2 = options.fill2 or fill local width = options.width or self.param.linewidth local opt_even = {draw = draw, fill = fill, width = width} local opt_odd = {draw = draw, fill = fill2, width = width} local wireframe = (fill == "none" and (options.fill2 == nil or fill2 == "none") and draw ~= "none") local max_tiles = options.max_tiles or 35000 local min_size = options.min_size or (max_level == nil and 0.05 or 0) local min_size2 = min_size * min_size max_level = max_level or math.huge -- Compute a strictly interior witness point c0 via the disc model local c0 if ld.hg_mode == "disc" then c0 = cpx.isobar(P0) else local P_disc = {} for idx_v, v in ipairs(P0) do P_disc[idx_v] = (v == "inf") and Z(1, 0) or ld.hg_plane2disc(v) end c0 = ld.hg_disc2plane(cpx.isobar(P_disc)) end local x1, x2, y1, y2 = table.unpack( self.param.coordsystem ) local xmin, xmax, ymin, ymax if ld.hg_mode == "plane" then local dx = (x2 - x1) * 0.25 local dy = (y2 - y1) * 0.25 xmin = x1 - dx xmax = x2 + dx ymin = math.max(0, y1 - dy) ymax = y2 + dy end local tiling, idx = {}, 0 local visited = {} local queue = {{P0, c0, 0}} local head = 1 local n = #P0 local function key(c) if c == nil or c == "inf" then return "inf" end c = cpx.toComplex(c) return math.floor(c.re * 1e7 + 0.5) .. ":" .. math.floor(c.im * 1e7 + 0.5) end local function can_expand(P, c) if c == nil or c == "inf" then return false end if ld.hg_mode == "plane" then local out = (c.re < xmin or c.re > xmax or c.im < ymin or c.im > ymax) if out then for k = 1, n do local z = P[k] if z ~= "inf" and z.re >= xmin and z.re <= xmax and z.im >= ymin and z.im <= ymax then out = false break end end if out then return false end end end if min_size2 <= 0 then return true end for k = 1, n do local A, B = P[k], P[k % n + 1] if A == "inf" or B == "inf" then return true end if self:Abs2(B - A) > min_size2 then return true end end return false end visited[key(c0)] = true while head <= #queue do local P, c, level = table.unpack(queue[head]) head = head + 1 local parity = level % 2 if wireframe or mode == 2 or parity == mode then idx = idx + 1; tiling[idx] = ld.hg_polyline(P, true) idx = idx + 1; tiling[idx] = (parity == 0) and opt_even or opt_odd end if level < max_level and #queue < max_tiles and can_expand(P, c) then for k = 1, n do local mirror = {P[k], P[k % n + 1]} local c1 = ld.hg_sym(c, mirror) local k1 = key(c1) if not visited[k1] then visited[k1] = true local P1 = ld.hg_sym(P, mirror) table.insert(queue, {P1, c1, level + 1}) end end end end self:Dliteralpath(table.unpack(tiling)) end