% encoding : utf8 
% tkz-elements.sty v4.00c
% Copyright 2025  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.

\ProvidesPackage{tkz-elements}[2025/05/25 version 4.00c Graphic Object Library]
\RequirePackage{iftex}
\ifluatex\else
  \PackageError{tkz-elements}{%
    This package requires LuaLaTeX.%
  }{%
    Please compile with LuaLaTeX.\MessageBreak
    Aborted compilation.%
  }
\fi
 \directlua{
      require "tkz_elements_main"
      init_elements()}%
      
\newenvironment{tkzelements}
  {  \directlua{ z = {}
                 C = {}
                 L = {}
                 M = {}
                 O = {}
                 P = {}
                 PA = {}
                 Q = {}
                 R = {}
                 RP= {}
                 S = {}
                 T = {}
                 V = {}
                 CO={}}
      \luacode}
  {\endluacode}%

% new version of the next macro proposed by Sanskar Singh
\makeatletter

\@ifpackageloaded{tkz-euclide}{%
  \@ifpackagelater{tkz-euclide}{2024/04/19}{%
   \def\tkzGetNodes{\directlua{%
     for K,V in pairs(z) do
        local n,sd,ft
        n = string.len(K)
        if n > 1 then
        _,_,ft, sd = string.find(K, "(.+)(.)" )  
       if sd == "p" then   K=ft.."'" end
       _,_,xft, xsd = string.find(ft, "(.+)(.)" )
       if xsd == "p" then  K=xft.."'".."'" end
         end    
    tex.sprint("\\coordinate ("..K..") at ("..V.re..","..V.im..") ; \string\r")
   end
   }}
   \def\tkzDrawLuaEllipse{\pgfutil@ifnextchar[{\tkz@DrawLuaEllipse}{\tkz@DrawLuaEllipse[]}}
   \def\tkz@DrawLuaEllipse[#1](#2,#3,#4){% 
   \begingroup 
   \draw[#1](#2) ellipse [x radius=\tkzUseLua{length(z.#3,z.#2)}, y radius =   
    \tkzUseLua{length(z.#4,z.#2)},rotate=\tkzUseLua{math.deg(slope_ (z.#3,z.#2))}]; 
   \endgroup}%
   \def\tkzDrawCoordinates{\pgfutil@ifnextchar[{\tkz@DrawCoordinates}{\tkz@DrawCoordinates[]}}
   \def\tkz@DrawCoordinates[#1](#2){% 
   \begingroup 
   \draw[#1] plot coordinates {\directlua{tex.print(#2)}}; 
   \endgroup}%
   \def\tkzDrawPointOnCurve{\pgfutil@ifnextchar[{\tkz@DrawPointOnCurve}{\tkz@DrawPointOnCurve[]}}
   \def\tkz@DrawPointOnCurve[#1](#2){% 
   \begingroup 
    \draw[#1] plot coordinates {(#2)};
   \endgroup}%
   \def\tkzDN{\pgfutil@ifnextchar[{\tkz@DN}{\tkz@DN[2]}}%
   \def\tkz@DN[#1]#2{%
   \begingroup
   \pgfkeys{/pgf/number format/.cd,std,precision=#1}%
   \pgfmathprintnumber{#2}
   \endgroup}}{%
    \PackageWarning{tkz-elements}{%
      tkz-euclide is loaded but too old.\MessageBreak
      Minimum version required : 5.10c 2024/04/19%
    }}}{%
}%
\def\tkzUseLua#1{\directlua{tex.print(#1)}} 

\let\percentchar\@percentchar

\def\tkzGetNodesMP#1{\directlua{
  local out = assert(io.open("#1.mp", "w"))
  local names = {}
  for K, _ in pairs(z) do
    table.insert(names, tostring(K))
  end
  table.sort(names)
  out:write("pair ", table.concat(names, ", "), ";\string\n")
  for _, name in ipairs(names) do
    local V = z[name]
    if V then
      out:write(name, " := (", V.re, "cm,", V.im, "cm);\string\n")
    end
  end
  out:close()
}}
\makeatother
\def\tkzEraseLuaObj#1{\directlua{#1 = nil}}
\endinput
