# \pgftransformtriangle problem

Why the red square and circle are not set and oriented on the blue points ?

\documentclass[tikz]{standalone}

\usepackage{tikz}
\usetikzlibrary{calc}

\def\Azi{30}%30
\def\Alt{20}%20

\def\Length{5}

\newcommand{\SetRelAngle}[1]{%
\def\RelAngle{#1}
\def\Angle{\AngleMax+#1}

\pgfmathsetmacro\LCos{\Length*cos(\Angle)}
\pgfmathsetmacro\LSin{\Length*sin(\Angle)}

\pgfmathsetmacro\CosAlt{cos(\Alt)}
\pgfmathsetmacro\SinAlt{sin(\Alt)}
\pgfmathsetmacro\CosAzi{cos(\Azi)}
\pgfmathsetmacro\SinAzi{sin(\Azi)}

\pgfmathsetmacro\SinAziSinAlt{\SinAzi*\SinAlt}
\pgfmathsetmacro\CosAziSinAlt{\CosAzi*\SinAlt}
}

% calculate projection
\newcommand{\CoorXY}[4][MyNode]{%
\pgfmathsetmacro\PjX{%
#2*\CosAzi
-#3*\SinAzi}
\pgfmathsetmacro\PjY{%
#2*\SinAziSinAlt
+#3*\CosAziSinAlt
+#4*\CosAlt}
\coordinate (#1) at (\PjX,\PjY) ;
}

% The same with a dot for debugging
\newcommand{\CoorXYT}[4][MyNode]{%
\CoorXY[#1]{#2}{#3}{#4}
\node[small dot,label={[font=\scriptsize]#1}] at (#1) {}
}

\tikzset{%
small dot/.style={fill=blue,circle,scale=0.3},
}

% ##############
\begin{document}
% ##############

\SetRelAngle{1}
\begin{tikzpicture}[scale=2]

% Debugging
\CoorXYT[CB]{0}{0}{0} ;

\draw[blue] (BH)--(BB) (BD)--(BG) ;

\begin{scope}[red]
\pgftransformtriangle{%
\pgfpointnormalised{\pgfpointanchor{CB}{center}}}{%
\pgfpointnormalised{\pgfpointanchor{BG}{center}}}{%
\pgfpointnormalised{\pgfpointanchor{BH}{center}}} ;
\draw (-1,-1) rectangle (1,1) ;
\draw (0,-1) -- (0,1) (-1,0) -- (1,-0) ;
\draw (0,0) circle (1) ;
\end{scope}

\end{tikzpicture}
\end{document}

-

There are two problems, first of all the \pgfpointnormalised causes the angle to be off, although I'm not sure why.

Second, as per the pgfmanual, the pgftransformtriangle causes the points (0pt,0pt), (1pt,0pt), and (0pt,1pt) to be placed on the specified coordinates. Note the use of the pt as units.

\documentclass[tikz]{standalone}

\usepackage{tikz}
\usetikzlibrary{calc}

\def\Azi{30}%30
\def\Alt{20}%20

\def\Length{5}

\newcommand{\SetRelAngle}[1]{%
\def\RelAngle{#1}
\def\Angle{\AngleMax+#1}

\pgfmathsetmacro\LCos{\Length*cos(\Angle)}
\pgfmathsetmacro\LSin{\Length*sin(\Angle)}

\pgfmathsetmacro\CosAlt{cos(\Alt)}
\pgfmathsetmacro\SinAlt{sin(\Alt)}
\pgfmathsetmacro\CosAzi{cos(\Azi)}
\pgfmathsetmacro\SinAzi{sin(\Azi)}

\pgfmathsetmacro\SinAziSinAlt{\SinAzi*\SinAlt}
\pgfmathsetmacro\CosAziSinAlt{\CosAzi*\SinAlt}
}

% calculate projection
\newcommand{\CoorXY}[4][MyNode]{%
\pgfmathsetmacro\PjX{%
#2*\CosAzi
-#3*\SinAzi}
\pgfmathsetmacro\PjY{%
#2*\SinAziSinAlt
+#3*\CosAziSinAlt
+#4*\CosAlt}
\coordinate (#1) at (\PjX,\PjY) ;
}

% The same with a dot for debugging
\newcommand{\CoorXYT}[4][MyNode]{%
\CoorXY[#1]{#2}{#3}{#4}
\node[small dot,label={[font=\scriptsize]#1}] at (#1) {}
}

\tikzset{%
small dot/.style={fill=blue,circle,scale=0.3},
}

% ##############
\begin{document}
% ##############

\SetRelAngle{1}
\begin{tikzpicture}[
scale=2,
]

% Debugging
\CoorXYT[CB]{0}{0}{0} ;

\draw[blue] (BH)--(BB) (BD)--(BG) ;

\begin{scope}[red]
\pgftransformtriangle
{\pgfpointanchor{CB}{center}}
{\pgfpointanchor{BG}{center}}
{\pgfpointanchor{BH}{center}} ;
\draw (-1pt,-1pt) rectangle (1pt,1pt) ;
\draw (0pt,-1pt) -- (0pt,1pt) (-1pt,0pt) -- (1pt,-0pt) ;
\draw (0pt,0pt) circle (1pt) ;
\end{scope}

\end{tikzpicture}
\end{document}


-
I tried to use normalised (1 pt length node or vector) to avoid using pt unit, and it almost worked, but as you said normalised brings errors. Thank you. –  Tarass Mar 3 '14 at 21:03
I just realised you might be able to use \pgfpointnormalised and omit the pt unit later on, if you use for all the points, also outside the red' scope. That way the blue lines and the red rectangle would be drawn on the same scale etc. However, I could not get this to work right now, I'll see if I can fix this tomorrow. –  hugovdberg Mar 3 '14 at 22:47

A solution with PSTricks. The square with the circle can be rotated in any direction with Alpha and Beta.

\documentclass[pstricks]{standalone}
\usepackage{pst-3dplot}
\begin{document}

\begin{pspicture}(-6,-3)(2,2)
\psset{Alpha=10,Beta=20}
\pstThreeDCoor[xMin=-1,xMax=5,yMin=-1,yMax=5,zMin=-1,zMax=1]
\pstThreeDSquare[fillcolor=blue!40,fillstyle=solid,opacity=0.5](0,0,0)(4,0,0)(0,4,0)
\pstThreeDCircle[linecolor=red](2,2,0)(2,0,0)(0,2,0)
\pstThreeDLine[linecolor=red](2,0,0)(2,4,0)
\pstThreeDLine[linecolor=red](0,2,0)(4,2,0)
\pstThreeDDot(2,2,0)\pstThreeDDot(2,0,0)\pstThreeDDot(2,4,0)
\pstThreeDDot(0,2,0)\pstThreeDDot(4,2,0)
\scriptsize
\pstThreeDPut(2.2,2.2,0){CB}\pstThreeDPut(2,-0.3,0){ BH}\pstThreeDPut(2,4.2,0){BB}
\pstThreeDPut(-0.3,2,0){BG}\pstThreeDPut(4.2,2,0){BD}
\end{pspicture}

\end{document}
`

-