| 1 |
-- File: tkz_elements_path.lua |
| 2 |
-- Copyright (c) 2026 Alain Matthes |
| 3 |
-- SPDX-License-Identifier: LPPL-1.3c |
| 4 |
-- Maintainer: Alain Matthes |
| 5 |
|
| 6 |
path = {} |
| 7 |
path.__index = path |
| 8 |
|
| 9 |
function path:new(data) |
| 10 |
if data ~= nil and type(data) ~= "table" then |
| 11 |
error("Expected table or nil in path constructor, got " .. type(data), 2) |
| 12 |
end |
| 13 |
return setmetatable(data or {}, self) |
| 14 |
end |
| 15 |
|
| 16 |
setmetatable(path, { |
| 17 |
__call = function(cls, ...) |
| 18 |
return cls:new(...) |
| 19 |
end, |
| 20 |
}) |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
function path.__add(p1, p2) |
| 25 |
local result = {} |
| 26 |
for _, pt in ipairs(p1) do |
| 27 |
result[#result + 1] = pt |
| 28 |
end |
| 29 |
|
| 30 |
if #p1 > 0 and #p2 > 0 and p1[#p1] == p2[1] then |
| 31 |
for i = 2, #p2 do |
| 32 |
result[#result + 1] = p2[i] |
| 33 |
end |
| 34 |
else |
| 35 |
for _, pt in ipairs(p2) do |
| 36 |
result[#result + 1] = pt |
| 37 |
end |
| 38 |
end |
| 39 |
|
| 40 |
return path:new(result) |
| 41 |
end |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
function path.__unm(p) |
| 46 |
local result = {} |
| 47 |
for i = #p, 1, -1 do |
| 48 |
result[#result + 1] = p[i] |
| 49 |
end |
| 50 |
return path:new(result) |
| 51 |
end |
| 52 |
|
| 53 |
function path.__sub(p1, p2) |
| 54 |
return p1 + -p2 |
| 55 |
end |
| 56 |
|
| 57 |
function path:__tostring() |
| 58 |
local t = {} |
| 59 |
for i, v in ipairs(self) do |
| 60 |
t[i] = tostring(v) |
| 61 |
end |
| 62 |
return "path: { " .. table.concat(t, " , ") .. " }" |
| 63 |
end |
| 64 |
|
| 65 |
|
| 66 |
-- function path:add_point(z, decimals) |
| 67 |
-- table.insert(self, utils.format_point(z, decimals)) |
| 68 |
-- end |
| 69 |
|
| 70 |
function path:add_point(z, decimals) |
| 71 |
if type(z) == "string" then |
| 72 |
local x,y = utils.parse_point(z) |
| 73 |
z = point(x,y) |
| 74 |
end |
| 75 |
table.insert(self, utils.format_point(z, decimals)) |
| 76 |
end |
| 77 |
|
| 78 |
function path:concat(sep) |
| 79 |
sep = sep or " " |
| 80 |
return table.concat(self, sep) |
| 81 |
end |
| 82 |
|
| 83 |
function path:add_pair_to_path(z1, z2, decimals) |
| 84 |
decimals = decimals or 5 |
| 85 |
local x1 = utils.format_coord(z1.re, decimals) |
| 86 |
local y1 = utils.format_coord(z1.im, decimals) |
| 87 |
local x2 = utils.format_coord(z2.re, decimals) |
| 88 |
local y2 = utils.format_coord(z2.im, decimals) |
| 89 |
local pt = string.format("%s/%s/%s/%s", x1, y1, x2, y2) |
| 90 |
table.insert(self, pt) |
| 91 |
end |
| 92 |
path.add_pair = path.add_pair_to_path |
| 93 |
|
| 94 |
function path:show() |
| 95 |
for _, pt in ipairs(self) do |
| 96 |
tex.print(pt) |
| 97 |
end |
| 98 |
end |
| 99 |
|
| 100 |
function path:copy() |
| 101 |
local c = {} |
| 102 |
for i, pt in ipairs(self) do |
| 103 |
c[i] = pt |
| 104 |
end |
| 105 |
return path:new(c) |
| 106 |
end |
| 107 |
|
| 108 |
-- Translation |
| 109 |
function path:translate(dx, dy) |
| 110 |
local moved = {} |
| 111 |
for _, pt in ipairs(self) do |
| 112 |
local x, y = utils.parse_point(pt) |
| 113 |
local newx = x + dx |
| 114 |
local newy = y + dy |
| 115 |
table.insert(moved, string.format("(%s,%s)", checknumber_(newx), checknumber_(newy))) |
| 116 |
end |
| 117 |
return path:new(moved) |
| 118 |
end |
| 119 |
|
| 120 |
-- Homothétie de centre (cx, cy), rapport k |
| 121 |
function path:homothety(center, k) |
| 122 |
local scaled = {} |
| 123 |
for _, pt in ipairs(self) do |
| 124 |
local x, y = utils.parse_point(pt) |
| 125 |
local newx = center.re + k * (x - center.re) |
| 126 |
local newy = center.im + k * (y - center.im) |
| 127 |
table.insert(scaled, string.format("(%s,%s)", checknumber_(newx), checknumber_(newy))) |
| 128 |
end |
| 129 |
return path:new(scaled) |
| 130 |
end |
| 131 |
|
| 132 |
-- Mise à l'échelle anisotrope |
| 133 |
function path:scale(kx, ky) |
| 134 |
local scaled = {} |
| 135 |
for _, pt in ipairs(self) do |
| 136 |
local x, y = utils.parse_point(pt) |
| 137 |
local newx = x * kx |
| 138 |
local newy = y * ky |
| 139 |
table.insert(scaled, |
| 140 |
string.format("(%s,%s)", |
| 141 |
checknumber_(newx), |
| 142 |
checknumber_(newy) |
| 143 |
) |
| 144 |
) |
| 145 |
end |
| 146 |
return path:new(scaled) |
| 147 |
end |
| 148 |
|
| 149 |
-- Rotation autour de (cx, cy) d'un angle theta en radians |
| 150 |
function path:rotate(center, theta) |
| 151 |
local rotated = {} |
| 152 |
local cos_t = math.cos(theta) |
| 153 |
local sin_t = math.sin(theta) |
| 154 |
for _, pt in ipairs(self) do |
| 155 |
local x, y = utils.parse_point(pt) |
| 156 |
local dx, dy = x - center.re, y - center.im |
| 157 |
local newx = center.re + dx * cos_t - dy * sin_t |
| 158 |
local newy = center.im + dx * sin_t + dy * cos_t |
| 159 |
table.insert(rotated, string.format("(%s,%s)", checknumber_(newx), checknumber_(newy))) |
| 160 |
end |
| 161 |
return path:new(rotated) |
| 162 |
end |
| 163 |
|
| 164 |
-- Fermeture |
| 165 |
function path:close() |
| 166 |
if #self == 0 then |
| 167 |
return self |
| 168 |
end |
| 169 |
|
| 170 |
local x1, y1 = utils.parse_point(self[1]) |
| 171 |
local x2, y2 = utils.parse_point(self[#self]) |
| 172 |
|
| 173 |
local eps = tkz.epsilon or 1e-9 |
| 174 |
if math.abs(x1 - x2) > eps or math.abs(y1 - y2) > eps then |
| 175 |
local closed = {} |
| 176 |
for i, pt in ipairs(self) do |
| 177 |
closed[i] = pt |
| 178 |
end |
| 179 |
closed[#closed + 1] = self[1] |
| 180 |
return path:new(closed) |
| 181 |
else |
| 182 |
return self |
| 183 |
end |
| 184 |
end |
| 185 |
|
| 186 |
|
| 187 |
-- Sous-chemin |
| 188 |
function path:sub(i1, i2) |
| 189 |
local subp = {} |
| 190 |
for i = i1 or 1, i2 or #self do |
| 191 |
subp[#subp + 1] = self[i] |
| 192 |
end |
| 193 |
return path:new(subp) |
| 194 |
end |
| 195 |
|
| 196 |
function path:concat_rawpair(sep) |
| 197 |
sep = sep or "," |
| 198 |
local list = {} |
| 199 |
for i = 1, #self do |
| 200 |
local x, y = self[i]:match("%(([^,]+),([^%)]+)%)") |
| 201 |
list[#list + 1] = x .. "/" .. y |
| 202 |
end |
| 203 |
return table.concat(list, sep) |
| 204 |
end |
| 205 |
|
| 206 |
function path:get_rawpair_list() |
| 207 |
local result = {} |
| 208 |
for _, pt in ipairs(self) do |
| 209 |
local x, y = utils.parse_point(pt) |
| 210 |
table.insert(result, string.format("%.5f/%.5f", x, y)) |
| 211 |
end |
| 212 |
return result |
| 213 |
end |
| 214 |
|
| 215 |
function path:count() |
| 216 |
return #self |
| 217 |
end |
| 218 |
|
| 219 |
-- format interne: "(x,y)" uniquement |
| 220 |
local function parse_xy(entry) |
| 221 |
local s = tostring(entry) |
| 222 |
local x, y = s:match("^%(%s*([%+%-]?[%d%.eE]+)%s*,%s*([%+%-]?[%d%.eE]+)%s*%)$") |
| 223 |
return tonumber(x), tonumber(y) |
| 224 |
end |
| 225 |
|
| 226 |
|
| 227 |
-- coordinates of the i-th point |
| 228 |
function path:get_number_path(i) |
| 229 |
local item = self[i] |
| 230 |
if not item then |
| 231 |
error(("path:get_number_path: index %s out of bounds (1..%d)"):format(tostring(i), #self)) |
| 232 |
end |
| 233 |
local x, y = parse_xy(item) |
| 234 |
if not x or not y then |
| 235 |
error(("path:get_number_path: Unable to analyze the input #%d"):format(i)) |
| 236 |
end |
| 237 |
return x, y |
| 238 |
end |
| 239 |
|
| 240 |
|
| 241 |
function path:get_point(i) |
| 242 |
local x, y = self:get_number_path(i) |
| 243 |
return point(x, y) |
| 244 |
end |
| 245 |
path.get = path.get_point |
| 246 |
|
| 247 |
-- Practical iterator |
| 248 |
-- for i, x, y in PA.A:iter() do |
| 249 |
-- -- do something with (i, x, y) |
| 250 |
-- end |
| 251 |
function path:iter() |
| 252 |
local i, n = 0, #self |
| 253 |
return function() |
| 254 |
i = i + 1 |
| 255 |
if i <= n then |
| 256 |
local x, y = self:get_number_path(i) |
| 257 |
return i, x, y |
| 258 |
end |
| 259 |
end |
| 260 |
end |
| 261 |
|
| 262 |
|
| 263 |
function path:map(fx, fy) |
| 264 |
fy = fy or fx |
| 265 |
|
| 266 |
local mapped = {} |
| 267 |
|
| 268 |
for _, pt in ipairs(self) do |
| 269 |
local x, y = utils.parse_point(pt) |
| 270 |
|
| 271 |
local newx = fx(x) |
| 272 |
local newy = fy(y) |
| 273 |
|
| 274 |
mapped[#mapped+1] = |
| 275 |
string.format("(%s,%s)", |
| 276 |
checknumber_(newx), |
| 277 |
checknumber_(newy) |
| 278 |
) |
| 279 |
end |
| 280 |
|
| 281 |
return path:new(mapped) |
| 282 |
end |
| 283 |
|
| 284 |
|
| 285 |
return path |