| 1 |
----------------------------------------------------------------------- |
| 2 |
-- @module luahyperbolic-tikz |
| 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 "LUAHYPERBOLIC-TIKZ" ============ |
| 16 |
|
| 17 |
local complex = require("complex") |
| 18 |
local core = require("luahyperbolic-core") |
| 19 |
local m = {} |
| 20 |
|
| 21 |
m.module = "luahyperbolic-tikz" |
| 22 |
|
| 23 |
m.TIKZ_CLIP_DISK = true -- can be modified by user. |
| 24 |
m.TIKZ_BEGIN_DISK = [[ |
| 25 |
\begin{scope} |
| 26 |
\clip (0,0) circle (1); |
| 27 |
]] |
| 28 |
|
| 29 |
m.GEODESIC_STYLE = "black" |
| 30 |
m.CIRCLE_STYLE = "black" |
| 31 |
m.HOROCYCLE_STYLE = "black" |
| 32 |
m.HYPERCYCLE_STYLE = "black" |
| 33 |
m.ANGLE_STYLE = "black" |
| 34 |
m.MARKING_STYLE = "black" |
| 35 |
m.LABEL_STYLE = "above left" |
| 36 |
m.DRAW_POINT_RADIUS = 0.02 |
| 37 |
m.DRAW_POINT_STYLE = "white, draw=black" |
| 38 |
m.DRAW_ANGLE_DIST = 1/5 |
| 39 |
m.MARKING_SIZE = "footnotesize" |
| 40 |
m.BOUNDARY_CIRCLE_STYLE = "very thick, black" |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
-- ========= REDEFINE ERROR (TeX error) |
| 45 |
|
| 46 |
function m._error(msg) |
| 47 |
tex.error("Package " .. m.module .. " Error ", { msg }) |
| 48 |
end |
| 49 |
|
| 50 |
function m._warning(msg) |
| 51 |
texio.write_nl("[WARNING] " .. msg) |
| 52 |
end |
| 53 |
|
| 54 |
-- ========= HELPERS (EUCLIDEAN GEOM AND OTHER) |
| 55 |
|
| 56 |
local PI = 3.1415926535898 |
| 57 |
local deg = math.deg |
| 58 |
local min = math.min |
| 59 |
local max = math.max |
| 60 |
local sin = math.sin |
| 61 |
local cos = math.cos |
| 62 |
local atan2 = math.atan2 |
| 63 |
local exp = math.exp |
| 64 |
local log = math.log |
| 65 |
local sqrt = math.sqrt |
| 66 |
local abs = math.abs |
| 67 |
local sinh = math.sinh |
| 68 |
local cosh = math.cosh |
| 69 |
local tanh = math.tanh |
| 70 |
|
| 71 |
|
| 72 |
local function euclidean_circumcenter(a, b, c) |
| 73 |
a, b, c = complex.coerce(a, b, c) |
| 74 |
core._assert(abs(complex.det(b-a,c-a)) > core.EPS, "points must not be aligned") |
| 75 |
local ma2 = complex.abs2(a) |
| 76 |
local mb2 = complex.abs2(b) |
| 77 |
local mc2 = complex.abs2(c) |
| 78 |
local num = a*(mb2 - mc2) + b*(mc2 - ma2) + c*(ma2 - mb2) |
| 79 |
local den = a*complex.conj(b - c) + b*complex.conj(c - a) + c*complex.conj(a - b) |
| 80 |
|
| 81 |
return num / den |
| 82 |
end |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
local function parse_points_with_options(...) |
| 88 |
-- errors if no point provided |
| 89 |
local args = { ... } |
| 90 |
local n = #args |
| 91 |
local options = nil |
| 92 |
|
| 93 |
if n >= 1 and type(args[n]) == "string" then |
| 94 |
options = args[n] |
| 95 |
n = n - 1 |
| 96 |
end |
| 97 |
|
| 98 |
local points = {} |
| 99 |
for i = 1, n do |
| 100 |
points[i] = args[i] |
| 101 |
end |
| 102 |
|
| 103 |
core._assert(#points > 0, "parse_points_with_options : no points provided") |
| 104 |
|
| 105 |
return points, options |
| 106 |
end |
| 107 |
|
| 108 |
|
| 109 |
-- ========== TikZ API |
| 110 |
|
| 111 |
m.tikzpictureOptions = "" |
| 112 |
m.tikzBuffer = {} |
| 113 |
m.tikzNbPicturesExported = 0 |
| 114 |
|
| 115 |
|
| 116 |
function m.tikzGetFirstLines() |
| 117 |
local firstLines = string.format( |
| 118 |
"\\begin{tikzpicture}[%s]\n", |
| 119 |
m.tikzpictureOptions |
| 120 |
) |
| 121 |
if m.TIKZ_CLIP_DISK then |
| 122 |
firstLines = firstLines .. m.TIKZ_BEGIN_DISK |
| 123 |
end |
| 124 |
return firstLines |
| 125 |
end |
| 126 |
|
| 127 |
function m.tikzBegin(options) |
| 128 |
-- without drawing circle and clipping disk |
| 129 |
m.tikzpictureOptions = options or "scale=3" |
| 130 |
tex.print(m.tikzGetFirstLines()) |
| 131 |
m.tikzClearBuffer() |
| 132 |
|
| 133 |
end |
| 134 |
|
| 135 |
|
| 136 |
function m.tikzClearBuffer() |
| 137 |
m.tikzBuffer = {} |
| 138 |
end |
| 139 |
|
| 140 |
|
| 141 |
function m.tikzExport(filename) |
| 142 |
-- works even without filename, for automated exports |
| 143 |
m.tikzNbPicturesExported = m.tikzNbPicturesExported+1 |
| 144 |
filename = filename or "hyper_picture_" ..m.tikzNbPicturesExported .. ".tikz" |
| 145 |
local f = io.open(filename, "w") |
| 146 |
f:write(m.tikzGetFirstLines()) |
| 147 |
for _, line in ipairs(m.tikzBuffer) do |
| 148 |
f:write(line, "\n") |
| 149 |
end |
| 150 |
f:write("\\end{scope}\n") |
| 151 |
f:write("\\draw[".. m.BOUNDARY_CIRCLE_STYLE .."] (0,0) circle (1);\n") |
| 152 |
f:write("\\end{tikzpicture}\n") |
| 153 |
f:close() |
| 154 |
-- doesn't clear buffer, do it manually if wanted |
| 155 |
-- can be used to export different steps of the same picture |
| 156 |
end |
| 157 |
|
| 158 |
|
| 159 |
function m.tikzEnd(filename) |
| 160 |
tex.print("\\end{scope}") |
| 161 |
tex.print("\\draw[".. m.BOUNDARY_CIRCLE_STYLE .."] (0,0) circle (1);") |
| 162 |
tex.print("\\end{tikzpicture}") |
| 163 |
if filename ~= nil then |
| 164 |
m.tikzExport(filename) |
| 165 |
end |
| 166 |
m.tikzClearBuffer() |
| 167 |
end |
| 168 |
|
| 169 |
|
| 170 |
function m.tikzPrintf(fmt, ...) |
| 171 |
local line = string.format(fmt, ...) |
| 172 |
tex.print(line) |
| 173 |
table.insert(m.tikzBuffer, line) |
| 174 |
end |
| 175 |
|
| 176 |
function m.tikzDefineNodes(table) |
| 177 |
-- table of complex numbers |
| 178 |
for name, z in pairs(table) do |
| 179 |
core._assert(z ~= nil, "nil point for " .. name) |
| 180 |
core._assert(z.re ~= nil and z.im ~= nil, "not a complex for " .. name) |
| 181 |
m.tikzPrintf("\\coordinate (%s) at (%f,%f);", name, z.re, z.im) |
| 182 |
end |
| 183 |
end |
| 184 |
|
| 185 |
-- ========== DRAW POINT(S) ========== |
| 186 |
|
| 187 |
|
| 188 |
function m.drawPoint(z, options) |
| 189 |
options = options or m.DRAW_POINT_STYLE |
| 190 |
-- accept nil point (circumcenter can be nil) |
| 191 |
if z == nil then |
| 192 |
m._warning("drawPoint : point is nil, aborting") |
| 193 |
return |
| 194 |
end |
| 195 |
z = core._coerce_assert_in_closed_disk(z) |
| 196 |
m.tikzPrintf("\\fill[%s] (%f,%f) circle (%f);", options, z.re, z.im, m.DRAW_POINT_RADIUS) |
| 197 |
end |
| 198 |
|
| 199 |
|
| 200 |
function m.drawPoints(...) |
| 201 |
local points, options = parse_points_with_options(...) |
| 202 |
options = options or m.DRAW_POINT_STYLE |
| 203 |
|
| 204 |
for i = 1, #points do |
| 205 |
m.drawPoint(points[i], options) |
| 206 |
end |
| 207 |
end |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
function m.drawPointOrbit(point, func, n, options) |
| 212 |
-- draws n points. Doesn't draw original point |
| 213 |
options = options or "black" |
| 214 |
local points = core.pointOrbit(point, func, n) |
| 215 |
|
| 216 |
for i, z in ipairs(points) do |
| 217 |
local alpha = i / #points |
| 218 |
m.drawPoint(z, options .. ", fill opacity=" .. alpha) |
| 219 |
end |
| 220 |
end |
| 221 |
|
| 222 |
-- ========== DRAW LINES, SEGMENTS ETC ========== |
| 223 |
|
| 224 |
function m.drawSegment(z, w, options) |
| 225 |
options = options or m.GEODESIC_STYLE |
| 226 |
z,w = complex.coerce(z,w) |
| 227 |
core._assert(z:isNot(w), "points must be distinct") |
| 228 |
local shape = m.tikz_shape_segment(z,w) |
| 229 |
m.tikzPrintf("\\draw[%s] %s;",options, shape) |
| 230 |
end |
| 231 |
|
| 232 |
function m.markSegment(z, w, markString) |
| 233 |
size = m.MARKING_SIZE |
| 234 |
z,w = complex.coerce(z,w) |
| 235 |
core._assert(z:isNot(w), "points must be distinct") |
| 236 |
local shape = m.tikz_shape_segment(z,w) |
| 237 |
m.tikzPrintf("\\path %s node[sloped,midway,font=\\%s] {%s} ;", |
| 238 |
-- m.tikzPrintf("\\path[postaction={decorate,decoration={markings, mark=at position %f with {\\node[transform shape,sloped,font=\\%s] {%s};}}}] %s;", |
| 239 |
shape, |
| 240 |
size, |
| 241 |
markString |
| 242 |
) |
| 243 |
end |
| 244 |
|
| 245 |
function m.tikz_shape_segment(z, w) |
| 246 |
core._assert(z:isNot(w), "points must be distinct") |
| 247 |
local g = core._geodesic_data(z, w) |
| 248 |
|
| 249 |
-- If the geodesic is (almost) a diameter, draw straight segment |
| 250 |
if g.radius == math.huge or g.radius > 100 then |
| 251 |
return string.format("(%f,%f)--(%f,%f)", z.re, z.im, w.re, w.im) |
| 252 |
else |
| 253 |
local a1 = complex.arg(z - g.center) |
| 254 |
local a2 = complex.arg(w - g.center) |
| 255 |
local delta = atan2(sin(a2 - a1), cos(a2 - a1)) |
| 256 |
local a_end = a1 + delta |
| 257 |
local degPerRad = 180 / PI |
| 258 |
return string.format( |
| 259 |
"(%f,%f) ++(%f:%f) arc (%f:%f:%f)", |
| 260 |
g.center.re, |
| 261 |
g.center.im, |
| 262 |
a1 * degPerRad, |
| 263 |
g.radius, |
| 264 |
a1 * degPerRad, |
| 265 |
a_end * degPerRad, |
| 266 |
g.radius |
| 267 |
) |
| 268 |
end |
| 269 |
end |
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
function m.tikz_shape_closed_line(a,b) |
| 274 |
-- todo : add "close" flag to decide if we close diameters ? |
| 275 |
core._assert(a:isNot(b), "points must be distinct") |
| 276 |
if not a:isUnit() or not b:isUnit() then |
| 277 |
a, b = core.endpoints(a,b) |
| 278 |
end |
| 279 |
if a:isNear(-b) then |
| 280 |
-- WARNING : HACK : close diameter as rectangle ! |
| 281 |
-- (for filling with even-odd rule) |
| 282 |
local factor = 1.1 |
| 283 |
a, b = factor*a, factor*b |
| 284 |
local c, d = b*complex(1,-1), a * complex(1,1) |
| 285 |
return string.format("(%f,%f) -- (%f,%f) -- (%f,%f) -- (%f,%f) -- cycle ", |
| 286 |
a.re, a.im, b.re, b.im, c.re, c.im, d.re, d.im) |
| 287 |
else |
| 288 |
local c = 2*a*b/(a+b) |
| 289 |
local r = complex.abs(c-a) |
| 290 |
-- rest of the circle will be clipped |
| 291 |
return string.format("(%f,%f) circle (%f)", c.re, c.im, r) |
| 292 |
|
| 293 |
end |
| 294 |
end |
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
function m.drawLine(a, b, options) |
| 299 |
options = options or m.GEODESIC_STYLE |
| 300 |
|
| 301 |
a, b = core._coerce_assert_in_closed_disk(a,b) |
| 302 |
core._assert(not a:isNear(b), "drawLine : points must be distinct") |
| 303 |
|
| 304 |
local end_a, end_b = core.endpoints(a,b) |
| 305 |
local shape = m.tikz_shape_segment(end_a,end_b) |
| 306 |
m.tikzPrintf("\\draw[%s] %s;", options, shape) |
| 307 |
end |
| 308 |
|
| 309 |
|
| 310 |
function m.drawLinesFromTable(pairs, options) |
| 311 |
options = options or m.GEODESIC_STYLE |
| 312 |
for _, pair in ipairs(pairs) do |
| 313 |
m.drawLine(pair[1], pair[2], options) |
| 314 |
end |
| 315 |
end |
| 316 |
|
| 317 |
function m.drawPerpendicularThrough(P,A,B,options) |
| 318 |
-- perpendicular through P to geodesic (A,B) |
| 319 |
options = options or m.GEODESIC_STYLE |
| 320 |
P, A, B = core._coerce_assert_in_closed_disk(P, A, B) |
| 321 |
core._assert(A:isNot(B), "A and B must be distinct") |
| 322 |
local H = core.projection(A,B)(P) |
| 323 |
core._assert(P:isNot(H), "point must not be on line") |
| 324 |
-- todo : fix this : should be ok. |
| 325 |
m.drawLine(P,H,options) |
| 326 |
end |
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
function m.drawPerpendicularBisector(A, B, options) |
| 331 |
options = options or m.GEODESIC_STYLE |
| 332 |
A, B = core._coerce_assert_in_closed_disk(A, B) |
| 333 |
|
| 334 |
core._assert(A:isNot(B), "drawPerpendicularBisector: A and B must be distinct") |
| 335 |
|
| 336 |
local e1, e2 = core.endpointsPerpendicularBisector(A, B) |
| 337 |
m.drawLine(e1, e2, options) |
| 338 |
end |
| 339 |
|
| 340 |
function m.drawAngleBisector(A, O, B, options) |
| 341 |
options = options or m.GEODESIC_STYLE |
| 342 |
A, O, B = core._coerce_assert_in_closed_disk(A, O, B) |
| 343 |
|
| 344 |
core._assert(complex.distinct(O,A) and complex.distinct(O,B), |
| 345 |
"angle_bisector: O must be distinct from A and B") |
| 346 |
|
| 347 |
local e1, e2 = core.endpointsAngleBisector(A, O, B) |
| 348 |
m.drawLine(e1, e2, options) |
| 349 |
end |
| 350 |
|
| 351 |
--- Draw a hyperbolic ray from two points: start at p1, through p2 |
| 352 |
function m.drawRayFromPoints(p1, p2, options) |
| 353 |
options = options or m.GEODESIC_STYLE |
| 354 |
local _, e2 = core.endpoints(p1, p2) -- e2 is the "ahead" endpoint |
| 355 |
m.drawSegment(p1, e2, options) |
| 356 |
end |
| 357 |
|
| 358 |
m.drawRay = m.drawRayFromPoints |
| 359 |
|
| 360 |
--- Draw a hyperbolic ray from a start point p along a tangent vector v |
| 361 |
function m.drawRayFromVector(p, v, options) |
| 362 |
options = options or m.GEODESIC_STYLE |
| 363 |
p = core._coerce_assert_in_disk(p) |
| 364 |
-- TODO : allow point at infinity (check vector direction) FIX/TEST |
| 365 |
local q = core.expMap(p, v) -- move along v in hyperbolic space |
| 366 |
local _, e2 = core.endpoints(p, q) |
| 367 |
m.drawSegment(p, e2, options) |
| 368 |
end |
| 369 |
|
| 370 |
function m.drawLineFromVector(p, v, options) |
| 371 |
options = options or m.GEODESIC_STYLE |
| 372 |
-- TODO : allow point at infinity |
| 373 |
local q = core.expMap(p, v) -- move along v in hyperbolic space |
| 374 |
m.drawLine(p, q, options) |
| 375 |
end |
| 376 |
|
| 377 |
function m.drawTangentAt(center, point, options) |
| 378 |
-- draw tangent line of circle of center 'center' passing through 'point' |
| 379 |
options = options or m.GEODESIC_STYLE |
| 380 |
center, point = core._coerce_assert_in_disk(center, point) |
| 381 |
local Q = core.rotation(point, PI / 2)(center) |
| 382 |
m.drawLine(point, Q, options) |
| 383 |
end |
| 384 |
|
| 385 |
-- function m.drawTangentFrom(center, radius, point) |
| 386 |
-- TODO ! |
| 387 |
-- return |
| 388 |
-- end |
| 389 |
|
| 390 |
-- ========== VECTORS ============= |
| 391 |
|
| 392 |
|
| 393 |
function m.tikz_shape_euclidean_segment(a,b) |
| 394 |
return string.format( |
| 395 |
"(%f,%f) -- (%f,%f)",a.re, a.im, b.re, b.im) |
| 396 |
end |
| 397 |
|
| 398 |
function m.drawVector(p, v, options) |
| 399 |
options = options or "" |
| 400 |
local norm_v = complex.abs(v) |
| 401 |
core._assert(norm_v > core.EPS, "drawVector : vector must not be zero") |
| 402 |
local u = v / norm_v |
| 403 |
local factor = (1 - complex.abs2(p)) |
| 404 |
local euclid_vec = tanh(factor * norm_v / 2) * u |
| 405 |
local shape = m.tikz_shape_euclidean_segment(p, p+euclid_vec) |
| 406 |
m.tikzPrintf("\\draw[->,%s] %s;",options,shape) |
| 407 |
end |
| 408 |
|
| 409 |
-- ========== FOR CONVENIENCE (draw multiple objets/segments etc |
| 410 |
|
| 411 |
function m.drawLines(...) |
| 412 |
local points, options = parse_points_with_options(...) |
| 413 |
core._assert(#points % 2 == 0, "drawLines expects an even number of points, got " .. #points) |
| 414 |
|
| 415 |
for i = 1, #points, 2 do |
| 416 |
m.drawLine(points[i], points[i + 1], options) |
| 417 |
end |
| 418 |
end |
| 419 |
|
| 420 |
function m.drawSegments(...) |
| 421 |
local points, options = parse_points_with_options(...) |
| 422 |
|
| 423 |
core._assert(#points % 2 == 0, "drawSegments expects an even number of points, got " .. #points) |
| 424 |
|
| 425 |
for i = 1, #points, 2 do |
| 426 |
m.drawSegment(points[i], points[i + 1], options) |
| 427 |
end |
| 428 |
end |
| 429 |
|
| 430 |
function m.markSegments(...) |
| 431 |
-- parameters : points and optional options string |
| 432 |
local points, options = parse_points_with_options(...) |
| 433 |
for i = 1, #points, 2 do |
| 434 |
m.markSegment(points[i], points[i + 1], options) |
| 435 |
end |
| 436 |
end |
| 437 |
|
| 438 |
function m.drawTriangle(...) |
| 439 |
local points, options = parse_points_with_options(...) |
| 440 |
|
| 441 |
core._assert(#points == 3, "drawTriangle expects exactly 3 points, got " .. #points) |
| 442 |
|
| 443 |
local a, b, c = points[1], points[2], points[3] |
| 444 |
m.drawSegment(a, b, options) |
| 445 |
m.drawSegment(b, c, options) |
| 446 |
m.drawSegment(c, a, options) |
| 447 |
end |
| 448 |
|
| 449 |
-- Draw a polyline from a table of points (open chain) |
| 450 |
function m.drawPolylineFromTable(points, options) |
| 451 |
options = options or m.GEODESIC_STYLE |
| 452 |
core._assert(#points >= 2, "drawPolylineFromTable expects at least 2 points, got " .. #points) |
| 453 |
|
| 454 |
for i = 1, #points - 1 do |
| 455 |
m.drawSegment(points[i], points[i + 1], options) |
| 456 |
end |
| 457 |
end |
| 458 |
|
| 459 |
function m.drawPolyline(...) |
| 460 |
local points, options = parse_points_with_options(...) |
| 461 |
core._assert(#points >= 2, "drawPolyline expects at least 2 points, got " .. #points) |
| 462 |
m.drawPolylineFromTable(points, options) |
| 463 |
end |
| 464 |
|
| 465 |
|
| 466 |
function m.drawPolygonFromTable(points, options) |
| 467 |
options = options or m.GEODESIC_STYLE |
| 468 |
core._assert(#points >= 2, "drawPolygonFromTable expects at least 2 points, got " .. #points) |
| 469 |
|
| 470 |
for i = 1, #points do |
| 471 |
local z = points[i] |
| 472 |
local w = points[i % #points + 1] -- wrap around to first point |
| 473 |
m.drawSegment(z, w, options) |
| 474 |
end |
| 475 |
end |
| 476 |
|
| 477 |
function m.drawPolygon(...) |
| 478 |
local points, options = parse_points_with_options(...) |
| 479 |
|
| 480 |
-- a 2-gon is a polygon |
| 481 |
core._assert(#points >= 2, "drawPolygon expects at least 2 points, got " .. #points) |
| 482 |
m.drawPolygonFromTable(points, options) |
| 483 |
end |
| 484 |
|
| 485 |
function m.drawRegularPolygon(center, point, nbSides, options) |
| 486 |
options = options or m.GEODESIC_STYLE |
| 487 |
core._assert(nbSides>1, "drawRegularPolygon : expects >=2 sides, got " .. nbSides) |
| 488 |
core._assert_in_disk(center) |
| 489 |
core._assert_in_disk(point) |
| 490 |
core._assert(complex.distinct(center, point), "drawRegularPolygon : center and point must be distinct") |
| 491 |
local rot = core.rotation(center, 2*PI/nbSides) |
| 492 |
local vertices = {} |
| 493 |
for k=1,nbSides do |
| 494 |
point = rot(point) |
| 495 |
table.insert(vertices, point) |
| 496 |
end |
| 497 |
m.drawPolygonFromTable(vertices, options) |
| 498 |
end |
| 499 |
|
| 500 |
-- ========== DRAW CIRCLES, SEMICIRCLES, ARCS ========== |
| 501 |
|
| 502 |
function m.drawCircleRadius(z0, r, options) |
| 503 |
options = options or m.CIRCLE_STYLE |
| 504 |
z0 = core._coerce_assert_in_disk(z0) |
| 505 |
local c, R = core._circle_to_euclidean(z0, r) |
| 506 |
|
| 507 |
m.tikzPrintf("\\draw[%s] (%f,%f) circle (%f);", options, c.re, c.im, R) |
| 508 |
end |
| 509 |
|
| 510 |
m.drawCircle = m.drawCircleRadius |
| 511 |
|
| 512 |
function m.drawCircleThrough(center, point, options) |
| 513 |
options = options or m.CIRCLE_STYLE |
| 514 |
center, point = core._coerce_assert_in_disk(center, point) |
| 515 |
local r = core.distance(center, point) |
| 516 |
m.drawCircle(center, r, options) |
| 517 |
end |
| 518 |
|
| 519 |
function m.drawIncircle(A, B, C, options) |
| 520 |
options = options or m.CIRCLE_STYLE |
| 521 |
A, B, C = core._coerce_assert_in_disk(A, B, C) |
| 522 |
local I = core.triangleIncenter(A, B, C) |
| 523 |
local a = core.projection(B, C)(I) |
| 524 |
m.drawCircleThrough(I, a, options) |
| 525 |
end |
| 526 |
|
| 527 |
function m.drawCircumcircle(A, B, C, options) |
| 528 |
options = options or m.CIRCLE_STYLE |
| 529 |
A, B, C = core._coerce_assert_in_disk(A, B, C) |
| 530 |
local O = core.triangleCircumcenter(A, B, C) |
| 531 |
if O ~= nil then |
| 532 |
m.drawCircleThrough(O,A, options) |
| 533 |
else |
| 534 |
m._warning("drawCircumcircle : circumcenter does not exist") |
| 535 |
end |
| 536 |
end |
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
function m.drawArc(O, A, B, options) |
| 541 |
options = options or m.CIRCLE_STYLE |
| 542 |
O, A, B = core._coerce_assert_in_disk(O, A, B) |
| 543 |
|
| 544 |
-- Check points are on same hyperbolic circle |
| 545 |
local rA = core.distance(O, A) |
| 546 |
local rB = core.distance(O, B) |
| 547 |
core._assert(abs(rA - rB) < core.EPS, "drawArc: points A and B are not on the same hyperbolic circle") |
| 548 |
|
| 549 |
local c, R = core._circle_to_euclidean(O, rA) |
| 550 |
|
| 551 |
-- Compute angles of A and B on the Euclidean circle |
| 552 |
local function angleOnCircle(p) |
| 553 |
return deg(atan2(p.im - c.im, p.re - c.re)) % 360 |
| 554 |
end |
| 555 |
local a1 = angleOnCircle(A) |
| 556 |
local a2 = angleOnCircle(B) |
| 557 |
|
| 558 |
-- Keep increasing angles: TikZ arc goes from a1 to a2 |
| 559 |
-- If a2 < a1, TikZ automatically interprets end > start as crossing 0° |
| 560 |
if a2 < a1 then |
| 561 |
a2 = a2 + 360 |
| 562 |
end |
| 563 |
|
| 564 |
m.tikzPrintf("\\draw[%s] (%f,%f) ++(%f:%f) arc (%f:%f:%f);", options, c.re, c.im, a1, R, a1, a2, R) |
| 565 |
end |
| 566 |
|
| 567 |
function m.drawSemicircle(center, startpoint, options) |
| 568 |
options = options or m.CIRCLE_STYLE |
| 569 |
local endpoint = (core.symmetry(center))(startpoint) |
| 570 |
m.drawArc(center, startpoint, endpoint, options) |
| 571 |
end |
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
-- ========== HOROCYCLES AND HYPERCYCLES |
| 578 |
|
| 579 |
function m.drawHorocycle(idealPoint, point, options) |
| 580 |
options = options or m.HOROCYCLE_STYLE |
| 581 |
|
| 582 |
core._assert(complex.isClose(complex.abs(idealPoint), 1), "drawHorocycle: ideal point must be on unit circle") |
| 583 |
core._assert(core._in_disk(point), "drawHorocycle: second point must be in disk") |
| 584 |
-- rotate : |
| 585 |
local w = point / idealPoint |
| 586 |
local x, y = w.re, w.im |
| 587 |
-- compute center and radius |
| 588 |
local a = (x ^ 2 + y ^ 2 - 1) / (2 * (x - 1)) |
| 589 |
local r = abs(a - 1) |
| 590 |
local center = complex.new(a, 0) |
| 591 |
-- rotate back |
| 592 |
center = center * idealPoint |
| 593 |
|
| 594 |
m.tikzPrintf("\\draw[%s] (%f,%f) circle (%f);", options, center.re, center.im, r) |
| 595 |
end |
| 596 |
|
| 597 |
|
| 598 |
function m.drawHypercycleThrough(P, A, B, options) |
| 599 |
options = options or m.HYPERCYCLE_STYLE |
| 600 |
P, A, B = complex.coerce(P, A, B) |
| 601 |
if not A:isUnit() or not B:isUnit() then |
| 602 |
A, B = core.endpoints(A, B) |
| 603 |
end |
| 604 |
if abs(complex.det(P-A, P-B)) < core.EPS then |
| 605 |
m.tikzPrintf("\\draw[%s] (%f,%f) -- (%f,%f);", options, A.re, A.im, B.re, B.im) |
| 606 |
return |
| 607 |
end |
| 608 |
local O = euclidean_circumcenter(P, A, B) |
| 609 |
local radius = complex.abs(O-A) |
| 610 |
m.tikzPrintf("\\draw[%s] (%f,%f) circle (%f);", options, O.re, O.im, radius) |
| 611 |
end |
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
-- ========== DRAW ANGLES, RIGHT ANGLES |
| 616 |
|
| 617 |
function m.drawAngle(A, O, B, options, distFactor) |
| 618 |
distFactor = distFactor or m.DRAW_ANGLE_DIST |
| 619 |
options = options or m.ANGLE_STYLE |
| 620 |
core._assert_in_disk(A) |
| 621 |
core._assert_in_disk(O) |
| 622 |
core._assert_in_disk(B) |
| 623 |
|
| 624 |
local dOA = core.distance(O,A) |
| 625 |
local dOB = core.distance(O,B) |
| 626 |
local minDist = min(dOA,dOB) |
| 627 |
local radius= minDist*distFactor |
| 628 |
local AA = core.interpolate(O,A,radius / dOA) |
| 629 |
local BB = core.interpolate(O,B,radius/ dOB) |
| 630 |
m.drawArc(O,AA,BB, options) |
| 631 |
end |
| 632 |
|
| 633 |
function m.drawRightAngle(A, O, B, options, distFactor) |
| 634 |
-- assumes angle(AOB) = +90 deg ! |
| 635 |
distFactor = distFactor or m.DRAW_ANGLE_DIST |
| 636 |
options = options or m.ANGLE_STYLE |
| 637 |
core._assert_in_disk(A) |
| 638 |
core._assert_in_disk(O) |
| 639 |
core._assert_in_disk(B) |
| 640 |
local dOA = core.distance(O,A) |
| 641 |
local dOB = core.distance(O,B) |
| 642 |
local minDist = min(dOA, dOB) |
| 643 |
local radius = minDist*distFactor |
| 644 |
local AA = core.interpolate(O,A,radius / dOA) |
| 645 |
local BB = core.interpolate(O,B,radius / dOB) |
| 646 |
|
| 647 |
local v = core.tangentVector(AA,A)*complex.I |
| 648 |
local w = core.tangentVector(BB,B)*(-complex.I) |
| 649 |
local VV = core.expMap(AA,v) |
| 650 |
local WW = core.expMap(BB,w) |
| 651 |
local P = core.interLL(AA,VV, BB, WW) |
| 652 |
-- fast&lazy : euclidean polyline instead of geodesic: |
| 653 |
m.tikzPrintf("\\draw[%s] (%f,%f) -- (%f,%f) -- (%f,%f);", |
| 654 |
options, |
| 655 |
AA.re, AA.im, |
| 656 |
P.re, P.im, |
| 657 |
BB.re, BB.im |
| 658 |
) |
| 659 |
end |
| 660 |
|
| 661 |
-- ========== LABEL OBJETS ================== |
| 662 |
|
| 663 |
|
| 664 |
function m.labelPoint(z, label, options) |
| 665 |
options = options or m.LABEL_STYLE |
| 666 |
-- accept nil point (circumcenter can be nil) |
| 667 |
if z == nil then |
| 668 |
m._warning("labelPoint : point is nil, aborting") |
| 669 |
return |
| 670 |
end |
| 671 |
m.tikzPrintf("\\node[%s] at (%f,%f) {%s};", options, z.re, z.im, label) |
| 672 |
end |
| 673 |
|
| 674 |
function m.labelPoints(...) |
| 675 |
-- always above left ! |
| 676 |
local args = { ... } |
| 677 |
local n = #args |
| 678 |
local options = m.LABEL_STYLE -- default : "above left" |
| 679 |
|
| 680 |
if n >= 3 and type(args[n]) == "string" and (n % 2 == 1) then |
| 681 |
options = args[n] |
| 682 |
n = n - 1 |
| 683 |
end |
| 684 |
|
| 685 |
core._assert(n % 2 == 0, "labelPoints expects pairs: (point, label)") |
| 686 |
|
| 687 |
for i = 1, n, 2 do |
| 688 |
m.labelPoint(args[i], args[i + 1], options) |
| 689 |
end |
| 690 |
end |
| 691 |
|
| 692 |
function m.labelSegment(A, B, label, options) |
| 693 |
options = options or m.LABEL_STYLE |
| 694 |
local midpoint = core.midpoint(A, B) |
| 695 |
m.labelPoint(midpoint, label,options) |
| 696 |
end |
| 697 |
|
| 698 |
|
| 699 |
-- ====================== MODULE END |
| 700 |
|
| 701 |
return m |