\pgftransformtriangle 问题

\pgftransformtriangle 问题

为什么红色正方形和圆形没有设置并定位在蓝点上?

在此处输入图片描述

\documentclass[tikz]{standalone}

\usepackage{tikz}
\usetikzlibrary{calc}

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

\def\Radius{1.5}
\def\Length{5}
\def\AngleMax{atan(2*\Radius/\Length)}

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

    \pgfmathsetmacro\RadCos{\Radius*cos(\Angle)}
    \pgfmathsetmacro\RadSin{\Radius*sin(\Angle)}
    \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} ; 
\CoorXYT[BD]{0}{\Radius}{0} ;
\CoorXYT[BG]{0}{-\Radius}{0} ;
\CoorXYT[BH]{-\RadSin}{0}{\RadCos} ;
\CoorXYT[BB]{\RadSin}{0}{-\RadCos} ;

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

答案1

有两个问题,首先\pgfpointnormalised导致角度偏离,虽然我不知道为什么。

第二,根据手册pgftransformtriangle使得点(0pt,0pt)(1pt,0pt)(0pt,1pt)被放置在指定的坐标上。注意使用作为单位。

\documentclass[tikz]{standalone}

\usepackage{tikz}
\usetikzlibrary{calc}

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

\def\Radius{1.5}
\def\Length{5}
\def\AngleMax{atan(2*\Radius/\Length)}

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

    \pgfmathsetmacro\RadCos{\Radius*cos(\Angle)}
    \pgfmathsetmacro\RadSin{\Radius*sin(\Angle)}
    \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} ; 
\CoorXYT[BD]{0}{\Radius}{0} ;
\CoorXYT[BG]{0}{-\Radius}{0} ;
\CoorXYT[BH]{-\RadSin}{0}{\RadCos} ;
\CoorXYT[BB]{\RadSin}{0}{-\RadCos} ;

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

\pgf变换三角形

答案2

使用 PSTricks 的解决方案。带有圆圈的正方形可以使用 Alpha 和 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}

在此处输入图片描述

相关内容