如何使用 atan2 函数旋转节点

如何使用 atan2 函数旋转节点

在此 tikzpicture 中

\documentclass[margin=2mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning}
\usetikzlibrary{calc}
\usetikzlibrary{shapes.arrows}
\usetikzlibrary{shapes.geometric}
\usetikzlibrary{arrows.meta}
\usetikzlibrary{shadings}
\usetikzlibrary{angles,quotes}
\usetikzlibrary{decorations.markings}

\begin{document}
    
\begin{tikzpicture}[]
    
    \coordinate [circle, ] (center) at (0,0);
    \coordinate (basecenter) at (0,-2);
    \coordinate (baseleft) at (-3,-2);
    \coordinate (baseright) at (3,-2);
    
    \draw [thick, gray, line width=0.5mm] ($(baseleft) + (-1,0)$) -- (basecenter) -- ($(baseright) + (1,0)$);
    \draw [fill=black!20, draw=black!50] ($(baseleft)+(-0.1,0)$) rectangle ($(baseleft) + (0.1,4)$);
    
    \coordinate (TXbottom) at ($(baseleft) + (0.1, 3)$);
    \coordinate (TXcenter) at ($(TXbottom) +(0.075,0.3)$);
    %\draw [draw=red, fill=red!30!white,rotate=-20] (TXbottom) rectangle ($(TXbottom) + (0.15,0.6)$);
    \draw[ draw=red, fill=red!30!white ] let \p1=($(TXcenter)-(center)$),\n1={atan2(\y1,\x1) - 90} in [rotate=\n1] (TXbottom) rectangle ($(TXbottom) + (0.6,-0.15)$);
    \draw[thin,blue] let \p1=($(TXcenter)-(center)$),\n1={atan2(\y1,\x1) - 90} in (TXbottom) -- ++(\n1:1.2) coordinate (incliTX);
    
    \path ($(TXbottom) + (0.075,0)$) -- ($(TXbottom) + (0.075,0.6)$) let \p1=($(TXcenter)-(center)$),\n1={atan2(\y1,\x1)-90 } in  node[midway, rotate=\n1, ] (inicioTX) {};
    \fill[black!70!] (inicioTX) circle (0.05);
    
    \draw[thin,blue] (TXbottom) -- ++(90:1.2) coordinate (vertTX);
    \pic [draw, latex-, "$\theta$", angle eccentricity=1.6, angle radius = .75cm,pic text options={shift={(0pt,0.25pt)}}] {angle = incliTX--TXbottom--vertTX};
    
    \draw [line width=1.2mm, draw=yellow!70!brown] ($(center) + (-2.9,0)$) -- ($(center) + (2.9,0)$);   

    \fill[black!70!] (center) circle (0.05);
    \draw [ draw=red,dashed,thick, -> ] (inicioTX) -- (center) coordinate (pabajo);

\end{tikzpicture}
\end{document}

在此处输入图片描述

我想从 $\theta$ 旋转的红色矩形的几何中心开始绘制虚线红色箭头线。我曾尝试从红色矩形的底部和顶部旋转路径中的中间节点,但不起作用。该节点仍处于未旋转的坐标中(黑色圆圈)

此外,代码似乎效率不高,需要一些时间来编译。我想知道如何从旋转的红色矩形中心开始绘制虚线。

答案1

我其实很喜欢这个语法。你可以在路径构造后calc放置一个坐标来访问矩形的中心。pos=0.5rectangle

\documentclass[tikz,margin=2mm]{standalone}
\usetikzlibrary{positioning}
\usetikzlibrary{calc}
\usetikzlibrary{shapes.arrows}
\usetikzlibrary{shapes.geometric}
\usetikzlibrary{arrows.meta}
\usetikzlibrary{shadings}
\usetikzlibrary{angles,quotes}
\usetikzlibrary{decorations.markings}

\begin{document}
    
\begin{tikzpicture}[]
    
    \coordinate [circle, ] (center) at (0,0);
    \coordinate (basecenter) at (0,-2);
    \coordinate (baseleft) at (-3,-2);
    \coordinate (baseright) at (3,-2);
    
    \draw [thick, gray, line width=0.5mm] ($(baseleft) + (-1,0)$) -- (basecenter) -- ($(baseright) + (1,0)$);
    \draw [fill=black!20, draw=black!50] ($(baseleft)+(-0.1,0)$) rectangle ($(baseleft) + (0.1,4)$);
    
    \coordinate (TXbottom) at ($(baseleft) + (0.1, 3)$);
    \coordinate (TXcenter) at ($(TXbottom) +(0.075,0.3)$);
    %\draw [draw=red, fill=red!30!white,rotate=-20] (TXbottom) rectangle ($(TXbottom) + (0.15,0.6)$);
    \draw[ draw=red, fill=red!30!white ] let
    \p1=($(TXcenter)-(center)$),\n1={atan2(\y1,\x1) - 90} 
    in [rotate=\n1] (TXbottom)  rectangle ($(TXbottom) + (0.6,-0.15)$)
    coordinate[pos=0.5] (c)
    (TXbottom)  ++(\n1:1.2) coordinate (incliTX)
    ($(TXbottom) + (0.075,0.6)$);
    \draw[thin,blue] let \p1=($(TXcenter)-(center)$),\n1={atan2(\y1,\x1) - 90}
    in (TXbottom) -- ++(\n1:1.2) coordinate (incliTX);
    
    
    \draw[thin,blue] (TXbottom) -- ++(90:1.2) coordinate (vertTX);
    \pic [draw, latex-, "$\theta$", angle eccentricity=1.6, 
    angle radius =.75cm,pic text options={shift={(0pt,0.25pt)}}] {angle =incliTX--TXbottom--vertTX};
    
    \draw [line width=1.2mm, draw=yellow!70!brown] ($(center) + (-2.9,0)$) -- ($(center) + (2.9,0)$);   

    \fill[black!70!] (center) circle (0.05);
    \draw [ draw=red,dashed,thick, -> ] (c) -- (center) coordinate (pabajo);
    \fill[black!70!] (c) circle (0.05);

\end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

我不太喜欢整个\p1, \x1,\y1方法,因为它们都不是合法的宏名,而且我也不知道\n1应该是什么。此外,这种方式只需要计算atan2一次。

\documentclass[margin=2mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning}
\usetikzlibrary{calc}
\usetikzlibrary{shapes.arrows}
\usetikzlibrary{shapes.geometric}
\usetikzlibrary{arrows.meta}
\usetikzlibrary{shadings}
\usetikzlibrary{angles,quotes}
\usetikzlibrary{decorations.markings}

\begin{document}
    
\begin{tikzpicture}[]
    
    \coordinate (center) at (0,0);
    \coordinate (basecenter) at (0,-2);
    \coordinate (baseleft) at (-3,-2);
    \coordinate (baseright) at (3,-2);
    
    \draw [thick, gray, line width=0.5mm] ($(baseleft) + (-1,0)$) -- (basecenter) -- ($(baseright) + (1,0)$);
    \draw [fill=black!20, draw=black!50] ($(baseleft)+(-0.1,0)$) rectangle ($(baseleft) + (0.1,4)$);
    
    \coordinate (TXbottom) at ($(baseleft) + (0.1, 3)$);
    \coordinate (TXcenter) at ($(TXbottom) +(0.075,0.3)$);
    %\draw [draw=red, fill=red!30!white,rotate=-20] (TXbottom) rectangle ($(TXbottom) + (0.15,0.6)$);
    \path[overlay] ($(TXcenter)-(center)$);
    \pgfgetlastxy{\xp}{\yp}
    \pgfmathsetmacro{\angle}{atan2(\yp,\xp) - 90}
    
    \begin{scope}[shift=(TXbottom), local bounding box=redbox]
      \path[ draw=red, fill=red!30!white , rotate=\angle]  (0,0) rectangle (0.6,-0.15);
    \end{scope}
    
    \draw[thin,blue] (TXbottom) -- ++(\angle:1.2) coordinate (incliTX);
    
    \path ($(TXbottom) + (0.075,0)$) -- ($(TXbottom) + (0.075,0.6)$) node[midway, rotate=\angle, ] (inicioTX) {};
    %\fill[black!70!] (inicioTX) circle (0.05);
    
    \draw[thin,blue] (TXbottom) -- ++(90:1.2) coordinate (vertTX);
    \pic [draw, latex-, "$\theta$", angle eccentricity=1.6, angle radius = .75cm,pic text options={shift={(0pt,0.25pt)}}] {angle = incliTX--TXbottom--vertTX};
    
    \draw [line width=1.2mm, draw=yellow!70!brown] ($(center) + (-2.9,0)$) -- ($(center) + (2.9,0)$);   

    \fill[black!70!] (redbox.center) circle (0.05);
    \draw [ draw=red,dashed,thick, -> ] (redbox.center) -- (center) coordinate (pabajo);% set to (center)

\end{tikzpicture}
\end{document}

相关内容