Debian Codegraph @

texmf-dist/tex/latex/tkz-elements/tkz_elements_functions_maths.lua

raw
1 -- File: tkz_elements_functions_maths.lua
2 -- Copyright (c) 2026 Alain Matthes
3 -- SPDX-License-Identifier: LPPL-1.3c
4 -- Maintainer: Alain Matthes
5
6 ---------------------------------------------------------------------------
7 ---- numbers --------------------------------------------------------------
8 ---------------------------------------------------------------------------
9
10 function is_integer_(x)
11 return type(x) == "number"
12 and x == x -- exclude NaN
13 and x < math.huge -- exclude +inf
14 and -x < math.huge -- exclude -inf
15 and is_zero_(x - math.floor(x + 0.5))
16 end
17
18 function near_integer_(x)
19 return is_zero_(x - tkz_round_(x))
20 end
21
22
23 function residue_(x)
24 return x % 1
25 end
26
27 function is_zero_(x)
28 return math.abs(x) < tkz.epsilon
29 end
30
31 function set_zero_(x)
32 return is_zero_(x) and 0 or x
33 end
34
35 function checknumber_(x)
36 if type(x) == "number" then
37 return string.format("%.12f", x)
38 elseif type(x) == "string" and tonumber(x) then
39 return string.format("%.12f", tonumber(x))
40 else
41 return x -- table ou invalide
42 end
43 end
44
45 function tkz_round_(num, idp)
46 idp = idp or 0
47 local mult = 10 ^ idp
48 return math.floor(num * mult + 0.5) / mult
49 end
50
51 ---------------------------------------------------------------------------
52 ---------------------------------------------------------------------------
53 ---------------------------------------------------------------------------
54
55 function dot_product_(z1, z2, z3)
56 return (z2 - z1) .. (z3 - z1)
57 end
58
59 --[[
60 Computes the 2D cross product (signed area) of the triangle
61 formed by (x1,y1), (x2,y2), and (x3,y3).
62
63 Returns:
64 > 0 if counter-clockwise (left turn),
65 < 0 if clockwise (right turn),
66 = 0 if colinear.
67
68 Equivalent to the imaginary part of the complex wedge product:
69 (z2 - z1) ^ (z3 - z1)
70 where zi = xi + i*yi
71 --]]
72 function oriented_area2_(x1, y1, x2, y2, x3, y3)
73 return (x2 - x1) * (y3 - y1) - (x3 - x1) * (y2 - y1)
74 end
75
76
77 function det3pt_(z1, z2, z3)
78 return oriented_area2_(z1.re, z1.im, z2.re, z2.im, z3.re, z3.im)
79 end
80 --orient2d or det3pt determinant de 3 pts
81
82 function islinear_(z1, z2, z3)
83 return math.abs(det3pt_(z1, z2, z3)) < tkz.epsilon
84 end
85 is_linear_ = islinear_
86
87 function isortho_(z1, z2, z3)
88 return math.abs((z2 - z1) .. (z3 - z1)) < tkz.epsilon
89 end
90
91 function parabola_(xa, ya, xb, yb, xc, yc) -- added
92 local D = (xa - xb) * (xa - xc) * (xb - xc)
93 if math.abs(D) < tkz.epsilon then
94 return nil
95 end
96
97 local A = (xc * (yb - ya) + xb * (ya - yc) + xa * (yc - yb)) / D
98 local B = (xc * xc * (ya - yb) + xb * xb * (yc - ya) + xa * xa * (yb - yc)) / D
99 local C = (xb * xc * (xb - xc) * ya + xc * xa * (xc - xa) * yb + xa * xb * (xa - xb) * yc) / D
100 return A, B, C
101 end
102
103 ---------------------------------------------------------------------------
104 ---- angles -------------------------------------
105 ---------------------------------------------------------------------------
106
107 -- function get_angle_(a, b, c)
108 -- if (b == a) or (c == a) then
109 -- tex.error("Points confused in get_angle_")
110 -- end
111 -- return point.arg((c - a) / (b - a))
112 -- end
113
114 -- Angle orienté ∠BAC en A, dans (-π, π]
115 function get_angle_(a, b, c)
116 if point.abs(b - a) < tkz.epsilon or point.abs(c - a) < tkz.epsilon then tex.error("Points confused in get_angle_")
117 end
118 -- vecteur AB = b - a, AC = c - a
119 -- arg(AC / AB) = angle pour tourner AB vers AC
120 return point.arg((c - a) / (b - a))
121 end
122 angle_at = get_angle_
123
124
125 function inner_angle_(a, b, c)
126 return math.abs(get_angle_(a, b, c))
127 end
128
129
130 function angle_normalize_(a)
131 return a % tkz.tau -- dans [0, 2π)
132 end
133
134
135 function get_angle_normalize_(a, b, c)
136 return angle_normalize_(get_angle_(a, b, c))
137 end
138
139
140
141 function tkz_angle_between_vectors_(a, b, c, d)
142 -- Vector calculation
143 local zab = b - a
144 local zcd = d - c
145
146 -- Angle between vectors using ratio argument
147 local y = -zab.im * zcd.re + zab.re * zcd.im
148 local x = zab.re * zcd.re + zab.im * zcd.im
149 local theta = math.atan(y / x)
150 if x < 0 then theta = theta + math.pi end
151
152
153 return theta -- Angle in radians
154 end
155
156
157 ---------------------------------------------------------------------------
158 -------------------------- end angles --------------------------
159 ---------------------------------------------------------------------------
160
161 ---------------------------------------------------------------------------
162 ----------------- display ---------------------------------------
163 -----------------------------------------------------
164 -- Optionnel : si utils.sign n’est pas fiable, utilise ceci
165 local function sign(x)
166 if x > 0 then
167 return "+"
168 end
169 if x < 0 then
170 return "-"
171 end
172 return ""
173 end
174
175 function tkz_display_(z)
176 local function display_real(r)
177 if r == nil then
178 return ""
179 end
180 local fmt
181 if near_integer_(r) then
182 r = math.round(r)
183 fmt = "%.0f"
184 else
185 fmt = string.format("%%.%df", tkz.dc)
186 end
187 return string.format(fmt, r)
188 end
189
190 local function display_imag(r, force_sign)
191 local sgn = sign(r)
192 if not force_sign and sgn == "+" then
193 sgn = "" -- supprime le + quand il n’est pas nécessaire
194 end
195 r = math.abs(r)
196 local part
197 if math.abs(r - 1) < tkz.epsilon then
198 part = "" -- afficher juste "i" ou "-i"
199 elseif near_integer_(r) then
200 part = tostring(math.round(r))
201 else
202 part = display_real(r)
203 end
204 return sgn, part
205 end
206
207 if type(z) == "number" then
208 return display_real(z)
209 end
210
211 local re, im = z.re, z.im
212 local is_re_zero = is_zero_(re)
213 local is_im_zero = is_zero_(im)
214
215 if is_re_zero and is_im_zero then
216 return "0"
217 end
218
219 if is_im_zero then
220 return display_real(re)
221 end
222
223 if is_re_zero then
224 local sgn, part = display_imag(im, false)
225 return sgn .. part .. "i"
226 end
227
228 -- Cas général : partie réelle et partie imaginaire
229 local str_re = display_real(re)
230 local sgn_im, part_im = display_imag(im, true)
231 return str_re .. sgn_im .. part_im .. "i"
232 end
233
234
235