| 1 |
-- File: tkz_elements_functions_points.lua |
| 2 |
-- Copyright (c) 2026 Alain Matthes |
| 3 |
-- SPDX-License-Identifier: LPPL-1.3c |
| 4 |
-- Maintainer: Alain Matthes |
| 5 |
|
| 6 |
|
| 7 |
function polar_(radius, phi) |
| 8 |
return point(radius * math.cos(phi), radius * math.sin(phi)) |
| 9 |
end |
| 10 |
|
| 11 |
function polar(radius, phi) |
| 12 |
return polar_(radius, phi) |
| 13 |
end |
| 14 |
|
| 15 |
function polar_deg(radius, phi) |
| 16 |
return polar_(radius, phi * math.pi / 180) |
| 17 |
end |
| 18 |
|
| 19 |
function barycenter_(...) |
| 20 |
local cp = table.pack(...) |
| 21 |
local sum = 0 |
| 22 |
local weight = 0 |
| 23 |
for i = 1, cp.n do |
| 24 |
sum = sum + cp[i][1] * cp[i][2] |
| 25 |
weight = weight + cp[i][2] |
| 26 |
end |
| 27 |
if weight == 0 then |
| 28 |
tex.error("barycenter_: sum of weights is zero") |
| 29 |
end |
| 30 |
return sum / weight |
| 31 |
end |
| 32 |
|
| 33 |
function rotation_(c, a, pt) |
| 34 |
local r = point(math.cos(a), math.sin(a)) |
| 35 |
return r * (pt - c) + c |
| 36 |
end |
| 37 |
|
| 38 |
-- Define the set_rotation_ function |
| 39 |
function set_rotation_(c, angle, ...) |
| 40 |
local tp = table.pack(...) |
| 41 |
local t = {} |
| 42 |
for i = 1, tp.n do |
| 43 |
table.insert(t, rotation_(c, angle, tp[i])) |
| 44 |
end |
| 45 |
return table.unpack(t) |
| 46 |
end |
| 47 |
|
| 48 |
function symmetry_(c, pt) |
| 49 |
return 2 * c - pt |
| 50 |
end |
| 51 |
|
| 52 |
function set_symmetry_(c, ...) |
| 53 |
local tp = table.pack(...) |
| 54 |
local t = {} |
| 55 |
for i = 1, tp.n do |
| 56 |
table.insert(t, symmetry_(c, tp[i])) |
| 57 |
end |
| 58 |
return table.unpack(t) |
| 59 |
end |
| 60 |
|
| 61 |
function homothety_(c, t, p) |
| 62 |
return c + t * (p - c) |
| 63 |
end |
| 64 |
|
| 65 |
function set_homothety_(c, coeff, ...) |
| 66 |
local tp = table.pack(...) |
| 67 |
local t = {} |
| 68 |
for i = 1, tp.n do |
| 69 |
table.insert(t, homothety_(c, coeff, tp[i])) |
| 70 |
end |
| 71 |
return table.unpack(t) |
| 72 |
end |
| 73 |
|
| 74 |
function translation_(u, p) |
| 75 |
return u + p |
| 76 |
end |
| 77 |
|
| 78 |
function set_translation_(u, ...) |
| 79 |
local tp = table.pack(...) |
| 80 |
local t = {} |
| 81 |
for i = 1, tp.n do |
| 82 |
table.insert(t, (u + tp[i])) |
| 83 |
end |
| 84 |
return table.unpack(t) |
| 85 |
end |
| 86 |
|
| 87 |
function random_point_(lower, upper) |
| 88 |
if not tkz._seeded then |
| 89 |
math.randomseed(os.time()) |
| 90 |
tkz._seeded = true |
| 91 |
end |
| 92 |
local x = math.random(lower, upper) |
| 93 |
local y = math.random(lower, upper) |
| 94 |
return point(x, y) |
| 95 |
end |
| 96 |
|
| 97 |
-- midpoints_chain_(...) |
| 98 |
-- midpoints_cycle_(...) |
| 99 |
function midpoints_(...) |
| 100 |
local arg = table.pack(...) |
| 101 |
local n = arg.n |
| 102 |
local t = {} |
| 103 |
for i = 1, n - 1 do |
| 104 |
table.insert(t, (arg[i] + arg[i + 1]) / 2) |
| 105 |
end |
| 106 |
table.insert(t, (arg[n] + arg[1]) / 2) |
| 107 |
return table.unpack(t) |
| 108 |
end |
| 109 |
|
| 110 |
function midpoint_(a, b) |
| 111 |
return (a + b) / 2 |
| 112 |
end |
| 113 |
|
| 114 |
function midpoints_chain_(...) |
| 115 |
local arg = table.pack(...) |
| 116 |
local n = arg.n |
| 117 |
local t = {} |
| 118 |
|
| 119 |
if n < 2 then |
| 120 |
return nil |
| 121 |
end |
| 122 |
|
| 123 |
for i = 1, n - 1 do |
| 124 |
t[#t + 1] = (arg[i] + arg[i + 1]) / 2 |
| 125 |
end |
| 126 |
|
| 127 |
return table.unpack(t) |
| 128 |
end |
| 129 |
|
| 130 |
function midpoints_cycle_(...) |
| 131 |
local arg = table.pack(...) |
| 132 |
local n = arg.n |
| 133 |
local t = {} |
| 134 |
|
| 135 |
if n < 2 then |
| 136 |
return nil |
| 137 |
end |
| 138 |
|
| 139 |
for i = 1, n - 1 do |
| 140 |
t[#t + 1] = (arg[i] + arg[i + 1]) / 2 |
| 141 |
end |
| 142 |
|
| 143 |
-- fermeture du cycle |
| 144 |
t[#t + 1] = (arg[n] + arg[1]) / 2 |
| 145 |
|
| 146 |
return table.unpack(t) |
| 147 |
end |
| 148 |
|
| 149 |
|
| 150 |
-- Deprecated remove ellipse |
| 151 |
function get_points(obj) |
| 152 |
-- Map of object types to their respective point keys |
| 153 |
local point_map = { |
| 154 |
line = { "pa", "pb" }, -- Line has two points |
| 155 |
triangle = { "pa", "pb", "pc" }, -- Triangle has three points |
| 156 |
circle = { "center", "through" }, -- Circle has center and a point through its circumference |
| 157 |
square = { "pa", "pb", "pc", "pd" }, -- Square has four vertices |
| 158 |
rectangle = { "pa", "pb", "pc", "pd" }, -- Rectangle has four vertices |
| 159 |
quadrilateral = { "pa", "pb", "pc", "pd" }, -- Quadrilateral has four vertices |
| 160 |
parallelogram = { "pa", "pb", "pc", "pd" }, -- Parallelogram has four vertices |
| 161 |
} |
| 162 |
|
| 163 |
-- Check if the object's type is recognized |
| 164 |
if point_map[obj.type] then |
| 165 |
local points = {} |
| 166 |
-- Iterate over the keys for the given type and extract the corresponding values |
| 167 |
for _, key in ipairs(point_map[obj.type]) do |
| 168 |
table.insert(points, obj[key]) |
| 169 |
end |
| 170 |
-- Return all points as multiple return values |
| 171 |
return table.unpack(points) |
| 172 |
end |
| 173 |
end |
| 174 |
|
| 175 |
|