Debian Codegraph @

texmf-dist/tex/lualatex/luahyperbolic/luahyperbolic-tilings.lua

raw
1 -----------------------------------------------------------------------
2 -- @module luahyperbolic-tilings
3 -- Pure Lua hyperbolic geometry
4 --
5 -- License:
6 -- Public Domain / CC0 1.0 Universal
7 -- 2026 Damien Mégy
8 -- This software is released into the public domain.
9 -- You may use, modify, and distribute it freely, without restriction.
10 --
11 -- SPDX-License-Identifier: CC0-1.0
12 --
13 -----------------------------------------------------------------------
14
15 -- ============ BEGIN MODULE "LUAGYPERBOLIC-TILINGS" ============
16
17 local complex = require("complex")
18 local core = require("luahyperbolic-core")
19 local tikz = require("luahyperbolic-tikz")
20 local m = {}
21
22 m.module = "luahyperbolic-tilings"
23
24 -- for quantization (geodesic propagation)
25 m.QUANTIZATION_SCALE = 1e9
26
27 local PI = 3.1415926535898
28 local min = math.min
29 local max = math.max
30 local sin = math.sin
31 local cos = math.cos
32 local atan2 = math.atan2
33 local exp = math.exp
34 local log = math.log
35 local sqrt = math.sqrt
36 local abs = math.abs
37 local sinh = math.sinh
38 local cosh = math.cosh
39 local tanh = math.tanh
40
41 local atanh = function(x)
42 return 0.5 * log((1 + x) / (1 - x))
43 end
44
45 local acosh = function(x)
46 return log(x + sqrt(x*x - 1))
47 end
48
49 local asinh = function(x)
50 return log(x + sqrt(x*x + 1))
51 end
52
53
54
55
56 local function _quantize(x)
57 return math.floor(x * m.QUANTIZATION_SCALE + 0.5)
58 end
59
60
61 local function _quantize_normalize_pair(a, b)
62
63 local ar = _quantize(a.re)
64 local ai = _quantize(a.im)
65 local br = _quantize(b.re)
66 local bi = _quantize(b.im)
67
68 -- lexicographic sort on integers
69 if br < ar or (br == ar and bi < ai) then
70 ar, br = br, ar
71 ai, bi = bi, ai
72 end
73
74 return ar .. ":" .. ai .. ":" .. br .. ":" .. bi
75 end
76
77
78 -- ================== TILINGS ==============
79
80
81 -- TODO : use better algorithm... use the group and work with words
82 function m.propagateGeodesics(geodesics, depth, MAX_ENDPOINT_DISTANCE)
83 MAX_ENDPOINT_DISTANCE = MAX_ENDPOINT_DISTANCE or 0.01
84 core._assert(#geodesics > 1, "must have at least 2 geodesics")
85
86 local reflections = {}
87 for i, side in ipairs(geodesics) do
88 reflections[i] = core.reflection(side[1], side[2])
89 end
90
91 local seen = {}
92 local frontier = {}
93
94 for _, g in ipairs(geodesics) do
95 local key = _quantize_normalize_pair(g[1], g[2])
96 if not seen[key] then
97 seen[key] = true
98 frontier[#frontier + 1] = {
99 p1 = g[1],
100 p2 = g[2],
101 last = nil -- no reflection yet
102 }
103 end
104 end
105
106 for _ = 1, depth do
107 local new_frontier = {}
108 for _, g in ipairs(frontier) do
109 for i, refl in ipairs(reflections) do
110 -- avoid immediate inverse reflection
111 if g.last ~= i then
112 local p1_new = refl(g.p1)
113 local p2_new = refl(g.p2)
114 if complex.abs(p1_new - p2_new) > MAX_ENDPOINT_DISTANCE then
115 local key = _quantize_normalize_pair(p1_new, p2_new)
116 if not seen[key] then
117 seen[key] = true
118
119 new_frontier[#new_frontier + 1] = {
120 p1 = p1_new,
121 p2 = p2_new,
122 last = i
123 }
124 end
125 end
126 end
127 end
128 end
129
130 for _, g in ipairs(new_frontier) do
131 geodesics[#geodesics + 1] = { g.p1, g.p2 }
132 end
133 frontier = new_frontier
134 end
135 return geodesics
136 end
137
138
139 function m.fillTilingFromTriangle(A, B, C, depth, options)
140 -- fills with "even odd rule"
141 options = options or "black"
142 local geodesics = {
143 {core.endpoints(A,B)},
144 {core.endpoints(B,C)},
145 {core.endpoints(C,A)}
146 }
147 geodesics = m.propagateGeodesics(geodesics, depth)
148 local shapes = {}
149 for _,pair in ipairs(geodesics) do
150 table.insert(shapes,tikz.tikz_shape_closed_line(pair[1], pair[2]))
151 end
152 local shapes_string = table.concat(shapes, " ")
153 tikz.tikzPrintf(
154 "\\fill[even odd rule, %s] (0,0) circle (1) %s ;",
155 options,
156 shapes_string
157 )
158 end
159
160 function m.drawTilingFromTriangle(A, B, C, depth, options)
161 options = options or tikz.GEODESIC_STYLE
162 local geodesics = {
163 {core.endpoints(A,B)},
164 {core.endpoints(B,C)},
165 {core.endpoints(C,A)}
166 }
167 geodesics = m.propagateGeodesics(geodesics, depth)
168 local shapes = {}
169 for _,pair in ipairs(geodesics) do
170 table.insert(shapes,tikz.tikz_shape_segment(pair[1], pair[2]))
171 end
172 local shapes_string = table.concat(shapes, " ")
173 tikz.tikzPrintf(
174 "\\draw[%s] (0,0) circle (1) %s ;",
175 options,
176 shapes_string
177 )
178 end
179
180 function m.drawTilingFromAngles(alpha, beta, gamma, depth, options)
181 local A, B, C = core.triangleWithAngles(alpha, beta, gamma)
182 m.drawTilingFromTriangle(A, B, C, depth, options)
183 end
184
185 function m.fillTilingFromAngles(alpha, beta, gamma, depth, options)
186 local A, B, C = core.triangleWithAngles(alpha, beta, gamma)
187 m.fillTilingFromTriangle(A, B, C, depth, options)
188 end
189
190
191 -- ============ END MODULE "TILINGS" ============
192
193 return m