| 1 |
-- File: tkz_elements_list_point.lua |
| 2 |
-- Copyright (c) 2026 Alain Matthes |
| 3 |
-- SPDX-License-Identifier: LPPL-1.3c |
| 4 |
-- Maintainer: Alain Matthes |
| 5 |
-- ------------------------------------------------------------------------- |
| 6 |
-- List of points |
| 7 |
-- ------------------------------------------------------------------------ |
| 8 |
|
| 9 |
list_point = {} |
| 10 |
list_point.__index = list_point |
| 11 |
|
| 12 |
-- ------------------------------------------------------------------------- |
| 13 |
-- Constructor |
| 14 |
-- ------------------------------------------------------------------------- |
| 15 |
function list_point:new(...) |
| 16 |
return setmetatable({ ... }, self) |
| 17 |
end |
| 18 |
|
| 19 |
setmetatable(list_point, { |
| 20 |
__call = function(cls, ...) |
| 21 |
return cls:new(...) |
| 22 |
end, |
| 23 |
}) |
| 24 |
|
| 25 |
-- ------------------------------------------------------------------------- |
| 26 |
-- Basic accessors |
| 27 |
-- ------------------------------------------------------------------------- |
| 28 |
|
| 29 |
-- number of stored points |
| 30 |
function list_point:len() |
| 31 |
return #self |
| 32 |
end |
| 33 |
|
| 34 |
-- get i-th point |
| 35 |
function list_point:get(i) |
| 36 |
return self[i] |
| 37 |
end |
| 38 |
|
| 39 |
-- unpack points (useful for Lua / TikZ bridges) |
| 40 |
function list_point:unpack() |
| 41 |
return table.unpack(self) |
| 42 |
end |
| 43 |
|
| 44 |
-- ------------------------------------------------------------------------- |
| 45 |
-- Mutators |
| 46 |
-- ------------------------------------------------------------------------- |
| 47 |
|
| 48 |
-- add one point |
| 49 |
function list_point:add(p) |
| 50 |
self[#self + 1] = p |
| 51 |
return self |
| 52 |
end |
| 53 |
|
| 54 |
-- append another points list |
| 55 |
function list_point:extend(pl) |
| 56 |
for _, p in ipairs(pl) do |
| 57 |
self[#self + 1] = p |
| 58 |
end |
| 59 |
return self |
| 60 |
end |
| 61 |
|
| 62 |
-- remove all points |
| 63 |
function list_point:clear() |
| 64 |
for i = #self, 1, -1 do |
| 65 |
self[i] = nil |
| 66 |
end |
| 67 |
return self |
| 68 |
end |
| 69 |
|
| 70 |
-- ------------------------------------------------------------------------- |
| 71 |
-- Iteration helpers |
| 72 |
-- ------------------------------------------------------------------------- |
| 73 |
|
| 74 |
-- apply a function to each point |
| 75 |
function list_point:foreach(f) |
| 76 |
for i, p in ipairs(self) do |
| 77 |
f(p, i) |
| 78 |
end |
| 79 |
return self |
| 80 |
end |
| 81 |
|
| 82 |
-- map → returns a new points object |
| 83 |
function list_point:map(f) |
| 84 |
local res = list_point() |
| 85 |
for i, p in ipairs(self) do |
| 86 |
res:add(f(p, i)) |
| 87 |
end |
| 88 |
return res |
| 89 |
end |
| 90 |
|
| 91 |
-- ------------------------------------------------------------------------- |
| 92 |
-- Geometric helpers (lightweight) |
| 93 |
-- ------------------------------------------------------------------------- |
| 94 |
|
| 95 |
-- barycenter of the list |
| 96 |
function list_point:barycenter() |
| 97 |
local n = #self |
| 98 |
if n == 0 then return nil end |
| 99 |
local z = self[1] |
| 100 |
for i = 2, n do |
| 101 |
z = z + self[i] |
| 102 |
end |
| 103 |
return z / n |
| 104 |
end |
| 105 |
|
| 106 |
-- bounding box (min/max) |
| 107 |
function list_point:bbox() |
| 108 |
local n = #self |
| 109 |
if n == 0 then return nil end |
| 110 |
local xmin, xmax = self[1].re, self[1].re |
| 111 |
local ymin, ymax = self[1].im, self[1].im |
| 112 |
for i = 2, n do |
| 113 |
local p = self[i] |
| 114 |
xmin = math.min(xmin, p.re) |
| 115 |
xmax = math.max(xmax, p.re) |
| 116 |
ymin = math.min(ymin, p.im) |
| 117 |
ymax = math.max(ymax, p.im) |
| 118 |
end |
| 119 |
return xmin, ymin, xmax, ymax |
| 120 |
end |
| 121 |
|
| 122 |
-- ------------------------------------------------------------------------- |
| 123 |
-- Output helpers (for tkz / TikZ) |
| 124 |
-- ------------------------------------------------------------------------- |
| 125 |
|
| 126 |
-- export as path (x1,y1) -- (x2,y2) ... |
| 127 |
function point:to_tikz() |
| 128 |
return "(" .. self.re .. "," .. self.im .. ")" |
| 129 |
end |
| 130 |
|
| 131 |
function list_point:to_tikz_path() |
| 132 |
local t = {} |
| 133 |
for i = 1, self:len() do |
| 134 |
t[#t + 1] = self:get(i):to_tikz() |
| 135 |
end |
| 136 |
return table.concat(t, " -- ") |
| 137 |
end |
| 138 |
|
| 139 |
function list_point:as_path() |
| 140 |
local P = path() |
| 141 |
for i = 1, self:len() do |
| 142 |
P:add_point(self:get(i)) |
| 143 |
end |
| 144 |
return P |
| 145 |
end |
| 146 |
|
| 147 |
return list_point |