summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/tkz/tkz-euclide/latex/tools-lua/tkz-tools-lua-math.tex
blob: 1a0491d5991896ecb9d1d0110449e28f584682c7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
% tkz-tools-lua-math.tex
% Copyright 2023  Alain Matthes
% This work may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either version 1.3
% of this license or (at your option) any later version.
% The latest version of this license is in
%   http://www.latex-project.org/lppl.txt
% and version 1.3 or later is part of all distributions of LaTeX
% version 2005/12/01 or later.
% This work has the LPPL maintenance status “maintained”.
% The Current Maintainer of this work is Alain Matthes.

\def\fileversion{5.00c}
\def\filedate{2023/01/23} 
\typeout{2023/01/23 5.00c tkz-tools-lua-math.tex}     
\makeatletter
%<-------------------------------------------------------------------------->
%<-------------------------------------------------------------------------->
%                           Lengths
%<-------------------------------------------------------------------------->
%<-------------------------------------------------------------------------->
\begin{luacode*}
complex = {} -- global complex numbers registry
M = {}         -- the module
local mt = {} --metatable for complex numbers
setmetatable(_ENV, {__index = complex})
   function new (r, i)
       local cp = {}
        cp = {r=r, i=i}
        return setmetatable(cp,mt)
      end
      M.new = new        -- add 'new' to the module
      -- create constant 'i'
      M.i = new(0, 1)

      function M.add (c1, c2)
        return new(c1.r + c2.r, c1.i + c2.i)
      end

      function M.sub (c1, c2)
        return new(c1.r - c2.r, c1.i - c2.i)
      end

      function M.mul (c1, c2)
        return new(c1.r*c2.r - c1.i*c2.i, c1.r*c2.i + c1.i*c2.r)
      end

    function M.inv (c)
        local n = c.r^2 + c.i^2
        return new(c.r/n, -c.i/n)
      end

      function M.div (c1, c2)
        return M.mul(c1, M.inv(c2))
      end

      function M.re (c)
        return new(c.r,0)
      end

      function M.im (c)
        return new(c.i,0)
      end

      function M.mod (c)
        local n = c.r^2 + c.i^2
        return new(n,0)
      end
	  
	  function M.prinarg(c)
        local arg
        if c.r > 0 then
        arg = math.atan(c.i/c.r)
        elseif c.r < 0 and c.i >= 0 then
        arg = math.atan(c.i/c.r) + math.pi
        elseif c.r < 0 and c.i < 0 then
        arg = math.atan(c.i/c.r) - math.pi
        elseif c.r == 0 and c.i > 0 then
        arg =  math.pi / 2
        elseif c.r == 0 and c.i <  0 then
        arg =  - math.pi / 2
        else 
        error("Principal argument not defined.")
        end
        return arg
	  end

    function M.op (...)
        return ...
      end

      function M.tostring (c)
        if c.i ==0 then
        return string.format("%g", c.r)
        elseif c.i> 0 and c.i==1 then
        return string.format("%g+i", c.r)
        elseif c.i> 0 and c.i~=1 then
        return string.format("%g+%gi", c.r, c.i)
        else
        return string.format("%g%gi", c.r, c.i) --to avoid +-
        end
      end
      
    --Setting Metatable operations.
    mt.__add = M.add
    mt.__mul = M.mul
    mt.__sub = M.sub
    mt.__tostring = M.tostring
\end{luacode*}



\begin{luacode*} 
  function normalize(angleA,angleB)       
      if angleA > 0 then
       if angleA > angleB then
          angleA = angleA - 360
          end
      else
     if angleA > angleB then
          angleB = angleB + 360
        end
      end
      return angleA, angleB
  end
  
  function math.angle(x1, y1, x2, y2)
      local a = math.deg(math.atan(y2 - y1, x2 - x1))
      if a < 0 then
          return a + 360
      else
          return a
      end
  end
  
  function tkzop(...)
  inf = math.huge
  return ...
end
 
function tkzround(nb, ND)
  local p = 10^(ND or 0)
   return math.floor(nb * p + 0.5) / p
end   
\end{luacode*}

\newcommand\cpxNew[2]{%
\directlua{complex[\luastringN{#1}] = M.new(#2)}}
\newcommand\cpxPrint[1]{%
\directlua{tex.sprint(tostring(complex[\luastringN{#1}]))}}
\newcommand\cpxAdd[3]{%
\directlua{complex[\luastringN{#1}]%
  =M.add(complex[\luastringN{#2}],complex[\luastringN{#3}])}}


\def\tkz@Dec#1{%
     \directlua{tex.print(string.format('\@percentchar.6f',#1))}
     }
\def\tkz@Op#1{\directlua{tex.sprint(tostring(tkzop(#1)))}}
\def\tkz@Log#1{\directlua{tex.sprint(math.log(#1))}}
\def\tkz@Exp#1{\directlua{tex.sprint(math.exp(#1))}}
\def\tkz@Sqrt#1{\directlua{tex.sprint(math.sqrt(#1))}}
\def\tkz@Abs#1{\directlua{tex.sprint(math.abs(#1))}}
\def\tkz@Pi{\directlua{tex.sprint(math.pi)}}
\def\tkz@Cos#1{\directlua{tex.sprint(math.cos(#1))}}
\def\tkz@Sin#1{\directlua{tex.sprint(tostring(math.sin(#1)))}}
\def\tkz@Tan#1{\directlua{tex.sprint(math.tan(#1))}}
\def\tkz@Rad#1{\directlua{tex.sprint(math.rad(#1))}}
\def\tkz@Acos#1{\directlua{tex.sprint(math.acos(#1))}}
\def\tkz@Asin#1{\directlua{tex.sprint(math.asin(#1))}}
\def\tkz@Atan#1{\directlua{tex.sprint(math.atan(#1))}}
\def\tkz@Round#1#2{\directlua{tex.sprint(tostring(tkzround(#1,#2)))}}
\def\tkz@Angle#1#2#3#4{\directlua{tex.sprint(math.angle(#1,#2,#3,#4))}}
\def\tkz@Ceil#1{\directlua{tex.sprint(math.ceil(#1))}}
\def\tkz@Floor#1{\directlua{tex.sprint(math.floor(#1))}}
\def\tkz@Huge{\directlua{tex.sprint(math.huge)}}
\def\tkz@Max#1{\directlua{tex.sprint(math.max(#1))}}
\def\tkz@Min#1{\directlua{tex.sprint(math.min(#1))}}
\def\tkz@Random#1{\directlua{tex.sprint(math.random(#1))}}
\def\tkz@veclen#1#2{%
  \directlua{%
    tex.print(string.format('\@percentchar.6f',math.sqrt((#1)^2+(#2)^2)))%
  }%
}
\let\tkzSqrt\tkz@Sqrt
\let\tkzPi\tkz@Pi
\let\tkzExp\tkz@Exp
\let\tkzLog\tkz@Log
\let\tkzSin\tkz@Sin
\let\tkzCos\tkz@Cos
% \tkzpointnormalised#
% tkzCalcLength
% \tkzGetLength
% \tkzpttocm
% \tkzcmtopt
% \tkzFindSlope
% option xfp

%  \tkzpointnormalised    normalise un point A-->A' tq ||v(OA')=1||
% équivalent de \pgfpointnormalised avec fp
% example
% \tkzpointnormalised{%
% \pgfpointdiff{\pgfpointanchor{A}{center}}
%              {\pgfpointanchor{B}{center}}}

% or
% \pgf@x=1 cm
% \pgf@y=12 cm 
% \tkzpointnormalised{}
%<--------------------------------------------------------------------------
\def\tkzpointnormalised#1{%
\pgf@process{#1}%
\pgf@xa=\pgf@x%
\pgf@ya=\pgf@y%
\edef\tkz@temp@xa{\strip@pt\pgf@xa}%
\edef\tkz@temp@ya{\strip@pt\pgf@ya}%
\edef\tkz@den{\tkz@veclen{\tkz@temp@xa}{\tkz@temp@ya}}
\edef\tkz@coordx{\tkz@Op{\tkz@temp@xa/\tkz@den}}
\edef\tkz@coordx{\tkz@Dec{\tkz@Round{\tkz@coordx}{5}}}
\edef\tkz@coordy{\tkz@Op{\tkz@temp@ya/\tkz@den}}
\edef\tkz@coordy{\tkz@Dec{\tkz@Round{\tkz@coordy}{5}}}
\pgf@x = \tkz@coordx pt
\pgf@y = \tkz@coordy pt
}
%\def\tkz@Dec#1{\directlua{tex.print(string.format('\@percentchar.12f',#1))}}
%<-------------------------------------------------------------------------->
% restaure and save length
\def\tkz@save@length{\global\let\tkz@temp@length\tkzLengthResult}%
\def\tkz@restore@length{\global\let\tkzLengthResult\tkz@temp@length }% 
%<-------------------------------------------------------------------------->
%    \tkzCalcLength      Distance entre deux points en pt ou en cm  avec xfp 
% \veclen mais avec fp 
%  option cm le résultat est en cm sinon en pt with cm=false
%<-------------------------------------------------------------------------->
\pgfkeys{tkzcalclen/.cd,
       cm/.is if         = tkzLengthIncm,
       cm/.default       = true,
       cm                = true}   

\def\tkzCalcLength{\pgfutil@ifnextchar[{\tkz@CalcLength}{\tkz@CalcLength[]}}  
\def\tkz@CalcLength[#1](#2,#3){%
\pgfqkeys{/tkzcalclen}{#1}%   
\begingroup
\tkz@@CalcLength(#2,#3){tkzLengthResult}
\iftkzLengthIncm 
   \edef\tkz@xfpMathLen{\tkz@Dec{\tkz@Round{\tkzLengthResult/28.45274}{6}}}
   \global\let\tkzLengthResult\tkz@xfpMathLen  
\fi 
\endgroup
}%

\def\tkz@@CalcLength(#1,#2)#3{%
\pgfpointdiff{\pgfpointanchor{#1}{center}}%
             {\pgfpointanchor{#2}{center}}%
\edef\tkz@xa{\strip@pt\pgf@x}%
\edef\tkz@ya{\strip@pt\pgf@y}%
\edef\tkz@xfpMathLen{\tkz@veclen{\tkz@xa}{\tkz@ya}}
\global\expandafter\edef\csname #3\endcsname{\tkz@xfpMathLen}
}

\def\tkz@@CalcLengthcm(#1,#2)#3{%
\pgfpointdiff{\pgfpointanchor{#1}{center}}%
             {\pgfpointanchor{#2}{center}}%
\edef\tkz@xa{\strip@pt\pgf@x}%
\edef\tkz@ya{\strip@pt\pgf@y}%
\edef\tkz@xfpMathLen{\tkz@veclen{\tkz@xa}{\tkz@ya}}
\edef\tkz@xfpMathLen{\tkz@Dec{\tkz@Round{\tkz@xfpMathLen/28.45274}{6}}}
\global\expandafter\edef\csname #3\endcsname{\tkz@xfpMathLen}
}
\def\tkz@@CalcLengthb(#1,#2)#3{%
\pgfpointdiff{\pgfpointanchor{#1}{center}}%
             {\pgfpointanchor{#2}{center}}%
\edef\tkz@xfpMathLen{\fpeval{sqrt((\pgf@x)^2+(\pgf@y)^2)}}
\edef\tkz@xfpMathLen{\fpeval{round(\tkz@xfpMathLen,6)}}
\global\expandafter\edef\csname #3\endcsname{\tkz@xfpMathLen}
}
%<-------------------------------------------------------------------------->
\def\tkzGetLength#1{%
\global\expandafter\edef\csname #1\endcsname{\tkzLengthResult}}  
%<-------------------------------------------------------------------------->
%     \tkzpttocm  passage de pt   cm div par 28.45274
%<-------------------------------------------------------------------------->
\def\tkzpttocm(#1)#2{%
\begingroup  
  \edef\tkz@mathresult{\tkz@Round{#1/28.45274}{6}}
 \global\expandafter\edef\csname #2\endcsname{\tkz@mathresult}%
\endgroup
}%
%<-------------------------------------------------------------------------->
%     \tkzcmtopt  passage de cm   pt mul par 28.45274
%<--------------------------------------------------------------------------
\def\tkzcmtopt(#1)#2{%
\begingroup 
  \edef\tkz@mathresult{\tkz@Round{#1*28.45274}{6}}
  \global\expandafter\edef\csname #2\endcsname{\tkz@mathresult}% 
\endgroup  
}% 
%<---------------------------------------------------------–>
 \def\tkzGetResult#1{%
   \global\expandafter\edef\csname #1\endcsname{\tkzMathResult}}
%<---------------------------------------------------------–>
%  Schrodinger's cat idea 03/01/20
\tikzset{veclen/.code={%
\pgfmathdeclarefunction*{veclen}{2}{%
\begingroup%
    \pgfmath@x##1pt\relax%
    \pgfmath@y##2pt\relax%
    \pgf@xa=\pgf@x%
    \pgf@ya=\pgf@y%
    \edef\tkz@temp@xa{\strip@pt\pgf@xa}% 
    \edef\tkz@temp@ya{\strip@pt\pgf@ya}%
    \edef\tkz@xfpMathLen{\tkz@veclen{\tkz@temp@xa}{\tkz@temp@ya}}%
    \pgfmath@returnone\tkz@xfpMathLen pt%
\endgroup%
}}}%
%<---------------------------------------------------------–>
\def\tkzSwapPoints(#1,#2){
   \pgfnodealias{tkzPointTmp}{#2}
   \pgfnodealias{#2}{#1}
   \pgfnodealias{#1}{tkzPointTmp}}
%<---------------------------------------------------------–>
\def\tkzPermute(#1,#2,#3){
   \tkzURotateWithNodes(#1,#3,#2)(#3)  \tkzGetPoint{tkzpt}
   \tkzURotateWithNodes(#1,#2,#3)(#2)  \tkzGetPoint{#2}
   \tkzSwapPoints(tkzpt,#3)
}
%<---------------------------------------------------------–>
\def\tkzDotProduct(#1,#2,#3){%
\begingroup
\pgfextractx{\pgf@x}{\pgfpointanchor{#1}{center}}%
\pgfextracty{\pgf@y}{\pgfpointanchor{#1}{center}}% 
\edef\tkzax{\strip@pt\pgf@x}%
\edef\tkzay{\strip@pt\pgf@y}%
\pgfextractx{\pgf@x}{\pgfpointanchor{#2}{center}}%
\pgfextracty{\pgf@y}{\pgfpointanchor{#2}{center}}% 
\edef\tkzbx{\strip@pt\pgf@x}%
\edef\tkzby{\strip@pt\pgf@y}%
\pgfextractx{\pgf@x}{\pgfpointanchor{#3}{center}}%
\pgfextracty{\pgf@y}{\pgfpointanchor{#3}{center}}% 
\edef\tkzcx{\strip@pt\pgf@x}%
\edef\tkzcy{\strip@pt\pgf@y}%
\edef\tkz@tmp{\tkz@Dec{\tkz@Round{((\tkzbx-(\tkzax))*(\tkzcx-(\tkzax))+(\tkzby-(\tkzay))*(\tkzcy-(\tkzay)))/809.55841}{6}}}
\global\let\tkzMathResult\tkz@tmp
\endgroup
}

% #1,#2 and #3 aligned
\def\tkzIsLinear(#1,#2,#3){%
\begingroup
\tkz@@CalcLengthcm(#1,#2){tkz@la}
\tkz@@CalcLengthcm(#1,#3){tkz@lb}
\tkzDotProduct(#1,#2,#3)
\edef\tkzResult{\tkz@Dec{\tkz@Abs{\tkzMathResult}-(\tkz@la)*(\tkz@lb)}}
\ifdim \tkzResult pt < 0.01 pt\relax%
\global\tkzLineartrue
\else
\global\tkzLinearfalse
\fi
\endgroup
}
%<---------------------------------------------------------–>
% syntax : vec(#2,#1) ortho vec(#3,#1)
\def\tkzIsOrtho(#1,#2,#3){%
\begingroup
\tkzDotProduct(#1,#2,#3)
\edef\tkzResult{\tkz@Dec{\tkz@Abs{\tkzMathResult}}}
\ifdim \tkzResult pt < 1 pt\relax%
\global\tkzOrthotrue
\else
\global\tkzOrthofalse
\fi
\endgroup
}
%<---------------------------------------------------------–>
% \tkzPowerCircle(M)(O,A) --> OM^2-OA^2
\def\tkzPowerCircle(#1)(#2,#3){%     
\begingroup 
\tkz@@CalcLengthcm(#2,#3){tkz@ra}
\tkz@@CalcLengthcm(#1,#2){tkz@om}
\gdef\tkzMathResult{\tkz@Dec{(\tkz@om)^2-(\tkz@ra)^2}}
\endgroup
}
%<---------------------------------------------------------–>
\def\tkzDefRadicalAxis(#1,#2)(#3,#4){%
\begingroup
\tkz@@CalcLengthcm(#1,#3){tkz@d}
\tkz@@CalcLengthcm(#1,#2){tkz@ra}
\tkz@@CalcLengthcm(#3,#4){tkz@rb}
\edef\tkzMathResult{\tkz@Dec{\tkz@d-(\tkz@ra+\tkz@rb)}}
\edef\tkzMathResultb{\tkz@Dec{\tkz@Abs{(\tkz@d-(\tkz@ra+\tkz@rb))}}}
\edef\tkzMathResultc{\tkz@Dec{\tkz@Abs{\tkz@d-\tkz@Abs{(\tkz@ra-(\tkz@rb))}}}}
\ifdim \tkzMathResultc pt < 0.1 pt\relax%
 \tkzURotateAngle(#2,90)(#3) \tkzGetPoint{tkzFirstPointResult}
  \tkzURotateAngle(#2,-90)(#3) \tkzGetPoint{tkzSecondPointResult}
\else
\ifdim \tkzMathResultb pt < 0.1 pt\relax%
 \tkzURotateAngle(#2,90)(#3) \tkzGetPoint{tkzFirstPointResult}
  \tkzURotateAngle(#2,-90)(#3) \tkzGetPoint{tkzSecondPointResult}
  \else
\ifdim \tkzMathResult pt > 1 pt\relax%
  \tkzURotateAngle(#1,60)(#3)    \tkzGetPoint{tkz@aux}
  \tkzInterCC(#1,#2)(tkz@aux,#1) \tkzGetPoints{tkz@pta}{tkz@ptb}
  \tkzInterCC(#3,#4)(tkz@aux,#1) \tkzGetPoints{tkz@ptc}{tkz@ptd}
  \tkzInterLL(tkz@pta,tkz@ptb)(tkz@ptc,tkz@ptd) \tkzGetPoint{tkz@pta}
  \tkzUProjection(#1,#3)(tkz@pta)  \tkzGetPoint{tkz@ptb}
  \pgfnodealias{tkzSecondPointResult}{tkz@ptb}
  \pgfnodealias{tkzFirstPointResult}{tkz@pta}
\else
\tkzInterCCR(#1,\tkz@ra)(#3,\tkz@rb){tkzFirstPointResult}{tkzSecondPointResult}
\fi
\fi
\fi
\endgroup
} 

\makeatother 
\endinput