% File: tkz-elements.sty
% Copyright (c) 2026 Alain Matthes
% SPDX-License-Identifier: LPPL-1.3c
% Maintainer: Alain Matthes

\ProvidesPackage{tkz-elements}[2026/03/11 v5.04c Graphic Object Library]

\typeout{Package tkz-elements v5.04c (2026/03/11) loaded.}

\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()
      }%

% new version of the next macro proposed by Sanskar Singh
\makeatletter
\let\percentchar\@percentchar
\@ifpackageloaded{tkz-euclide}{%
  \@ifpackagelater{tkz-euclide}{2024/04/19}{%
   \def\tkzGetNodes{\directlua{%
   tkz.GetNodes()
   tkz.flush_coord_warnings()
   }}

\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}%

 \let\tkzDrawPath\tkzDrawCoordinates


\def\tkzPrintNumber{\pgfutil@ifnextchar[{\tkz@PrintNumber}{\tkz@PrintNumber[2]}}%
\def\tkz@PrintNumber[#1]#2{%
  \begingroup
    \pgfkeys{/pgf/number format/.cd,fixed,precision=#1}%
    \pgfmathprintnumber{#2}%
  \endgroup
}
\let\tkzPN\tkzPrintNumber

\def\tkzDrawSegmentsFromPaths{\pgfutil@ifnextchar[{\tkz@DrawSegmentsFromPaths}{\tkz@DrawSegmentsFromPaths[]}}
\def\tkz@DrawSegmentsFromPaths[#1](#2,#3){%
\begingroup
\directlua{%
  local P = #2
   for i = 1, P:count() do
    local x1, y1 = #2:get_number_path(i)
    local x2, y2 = #3:get_number_path(i)
    tex.print("\\tkzDefPoint("..x1..","..y1.."){start}")
    tex.print("\\tkzDefPoint("..x2..","..y2.."){end}")
    tex.print("\\tkzDrawSegment[" .. "\luaescapestring{#1}" .. "](start,end)")
   end
}%
\endgroup}%

\def\tkzUseLua#1{\directlua{tex.sprint(#1)}}

\def\tkzDrawPointsFromPath{\pgfutil@ifnextchar[{\tkz@DrawPointsFromPath}{\tkz@DrawPointsFromPath[]}}
\def\tkz@DrawPointsFromPath[#1](#2){%
  \begingroup
   \foreach[expand list] \r/\s in {\directlua{tex.sprint(#2:concat_rawpair(","))}} {%
    \tkzDrawPoint[#1](\r,\s)%
  }%
  \endgroup
}


\def\tkzDrawCirclesFromPaths{\pgfutil@ifnextchar[{\tkz@DrawCirclesFromPaths}{\tkz@DrawCirclesFromPaths[]}}
\def\tkz@DrawCirclesFromPaths[#1](#2,#3){%
  \begingroup
  \directlua{
    local P = #2
    for i = 1, P:count() do
      local cx, cy = #2:get_number_path(i)
      local tx, ty = #3:get_number_path(i)
      tex.print("\\node[" .. "\luaescapestring{#1}" .. ",circle through={(" .. tx .. "," .. ty .. ")}] at (" .. cx .. "," .. cy .. ") {};")
    end
  }
  \endgroup
}

\def\tkzGetPointsFromPath(#1,#2){%
\begingroup
   \foreach[expand list][count = \i] \r/\s in {%
    \directlua{tex.sprint(#1:concat_rawpair(","))}}%
{%
      \tkzDefPoint(\r,\s){#2_\i}%
    }%
  \endgroup
}


\def\tkzDrawFromPointToPath{%
  \pgfutil@ifnextchar[{\tkz@DrawFromPointToPath}{\tkz@DrawFromPointToPath[]}%
}
\def\tkz@DrawFromPointToPath[#1](#2,#3){%
  \begingroup
  \directlua{%
    local P = #3
    local A = "\luaescapestring{#2}"
    for i,x,y in P:iter() do
      tex.print("\\tkzDefPoint("..x..","..y.."){tmp}")
      tex.print("\\tkzDrawSegment["..
                "\luaescapestring{#1}".."]("..A..",tmp)")
    end
  }%
  \endgroup
}

\makeatother


% Returns the number of points in a path (usage: \tkzPathCount{PA.A}{N})
\def\tkzPathCount(#1)#2{%
  \directlua{
    local n = #1:count()
    tex.print("\\edef\\#2{"..n.."}")  % <-- \N = "10" (par ex.)
  }%
}

%\newcommand{\tkzGetPointFromPath}[3]{%
\def\tkzGetPointFromPath(#1,#2)#3{%
\begingroup
  \directlua{%
    local i = \number#2
    local cx,cy = #1:get_number_path(i)
    tex.print("\\tkzDefPoint("..cx..","..cy.."){#3}")
  }%
  \endgroup
}

\def\tkzEraseLuaObj#1{\directlua{#1 = nil}}

}{%
\PackageWarning{tkz-elements}{%
      tkz-euclide is loaded but too old.\MessageBreak
      Minimum version required : 5.10c 2024/04/19%
}}}{%
}%



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\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()
}}


\endinput
