%!TEX pdflatex

\documentclass[a4paper,landscape]{article}
\usepackage[margin=1cm]{geometry}
\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage{simplekv}
\usepackage{listofitems}
\usepackage{codehigh}

\defKV[puzzlpytha]{Unite=\def\puzzlpythascale{#1},ListeCouleurs=\def\puzzlpythacolors{#1}}
\setKVdefault[puzzlpytha]{Unite=1,ListeCouleurs={yellow!50,teal!50,orange!50,red!50,violet!50},AffCouleurs=false,Solution=false,SolutionComplete=false}
\tikzset{puzzlpythathick/.style={line width=\fpeval{\puzzlpythascale*0.8}pt}}

\NewDocumentCommand\PuzzlePythagore{ O{} D<>{} m m }{%
	\restoreKV[puzzlpytha]%
	\setKV[puzzlpytha]{#1}
	\xdef\ValA{\fpeval{max(#3,#4)}}%
	\xdef\ValB{\fpeval{min(#3,#4)}}%
	\xdef\ValALPHA{\fpeval{(\ValA)*(\ValA)*(\ValB)/((\ValA)*(\ValA)+(\ValB)*(\ValB))}}%
	\xdef\ValBETA{\fpeval{-(\ValA)*(\ValB)*(\ValB)/((\ValA)*(\ValA)+(\ValB)*(\ValB))}}%
	\readlist*\puzzlpythalstcolors{\puzzlpythacolors}%
	\begin{tikzpicture}[line cap=rect,line join=bevel,scale=\puzzlpythascale,transform shape,#2]
		\coordinate (A) at (0,0) ;
		\coordinate (L) at (\ValA,0) ;
		\coordinate (U) at ({(\ValA)+(\ValB)},\ValA) ;
		\coordinate (S) at (\ValB,{(\ValA)+(\ValB)}) ;
		\coordinate (P) at (0,\ValB) ;
		\coordinate (O) at ({-(\ValB)},\ValB) ;
		\coordinate (B) at ({-(\ValB)},0) ;
		\coordinate (M) at (0,{-(\ValA)}) ;
		\coordinate (I) at (\ValA,{-(\ValA)}) ;
		\coordinate (OM) at (\ValALPHA,\ValBETA) ;
		\coordinate (D1) at (\ValA,{-(\ValB)}) ;
		\coordinate (D2) at (\ValB,0) ;
		%les carrés
		\draw[puzzlpythathick] (P)--(A)--(B)--(O)--cycle ; %carré gauche
		\draw[puzzlpythathick] (P)--(L)--(U)--(S)--cycle ; %carré du bas
		\draw[puzzlpythathick] (L)--(A)--(M)--(I)--cycle ; %carré du haut
		\ifboolKV[puzzlpytha]{Solution}%
			{%
				\ifboolKV[puzzlpytha]{AffCouleurs}%
					{%
						\draw[puzzlpythathick,fill={\puzzlpythalstcolors[1]}] (P)--(A)--(B)--(O)--cycle ;%carré gauche
						\draw[puzzlpythathick,fill={\puzzlpythalstcolors[2]}] (A)--(OM)--(M)--cycle ; %pièce moyen G
						\draw[puzzlpythathick,fill={\puzzlpythalstcolors[3]}] (A)--(OM)--(D2)--cycle ; %pièce moyen H
						\draw[puzzlpythathick,fill={\puzzlpythalstcolors[5]}] (L)--(D2)--(OM)--(D1)--cycle ; %pièce moyen B
						\draw[puzzlpythathick,fill={\puzzlpythalstcolors[4]}] (M)--(OM)--(D1)--(I)--cycle ; %pièce moyen D
						\draw[puzzlpythathick,fill={\puzzlpythalstcolors[1]}] (P)--(L)--(U)--(S)--cycle ; %carré gauche
						%pièces décalées
						\draw[puzzlpythathick,fill={\puzzlpythalstcolors[3]}] (P)--++({-(\ValBETA)},\ValALPHA)--++(0,{-(\ValB)})--cycle ; %pièce grand G
						\draw[puzzlpythathick,fill={\puzzlpythalstcolors[2]}] (L)--++({-(\ValBETA)},\ValALPHA)--++({-(\ValA)},0)--cycle ; %pièce grand B
						\draw[puzzlpythathick,fill={\puzzlpythalstcolors[4]}] (U)--++({-(\ValA)-(\ValBETA)},\ValALPHA)--++(0,{-(\ValA)})--++({(\ValA)-(\ValB)},0)--cycle ; %pièce grand G
						\draw[puzzlpythathick,fill={\puzzlpythalstcolors[5]}] (S)--++({-(\ValB)-(\ValBETA)},{-(\ValA)+(\ValALPHA)})--++(\ValB,0)--++(0,{(\ValA)-(\ValB)})--cycle ; %pièce grand H
					}%
					{%
						\draw[puzzlpythathick] (A)--(OM)--(M)--cycle ; %pièce moyen G
						\draw[puzzlpythathick] (A)--(OM)--(D2)--cycle ; %pièce moyen H
						\draw[puzzlpythathick] (L)--(D2)--(OM)--(D1)--cycle ; %pièce moyen B
						\draw[puzzlpythathick] (M)--(OM)--(D1)--(I)--cycle ; %pièce moyen D
						\draw[puzzlpythathick] (P)--++({-(\ValBETA)},\ValALPHA)--++(0,{-(\ValB)})--cycle ; %pièce grand G
						\draw[puzzlpythathick] (L)--++({-(\ValBETA)},\ValALPHA)--++({-(\ValA)},0)--cycle ; %pièce grand B
						\draw[puzzlpythathick] (U)--++({-(\ValA)-(\ValBETA)},\ValALPHA)--++(0,{-(\ValA)})--++({(\ValA)-(\ValB)},0)--cycle ; %pièce grand G
						\draw[puzzlpythathick] (S)--++({-(\ValB)-(\ValBETA)},{-(\ValA)+(\ValALPHA)})--++(\ValB,0)--++(0,{(\ValA)-(\ValB)})--cycle ; %pièce grand H
					}%
			}%
			{}%
	\end{tikzpicture}
}

\begin{document}

\begin{demohigh}[language=latex/latex2]
\PuzzlePythagore{1.5}{2}~
\PuzzlePythagore[Solution]{1.5}{2}~~
\PuzzlePythagore[AffCouleurs,Solution]{1.5}{2}~
\PuzzlePythagore[Solution,AffCouleurs,ListeCouleurs={orange,green,brown,cyan,red}]{1.5}{2}
\end{demohigh}

\begin{demohigh}[language=latex/latex2]
\PuzzlePythagore{4}{1.5}~
\PuzzlePythagore[AffCouleurs,Solution]{4}{1.5}
\end{demohigh}

\end{document}