如何在 tikz 中跟踪旋转坐标

如何在 tikz 中跟踪旋转坐标

我正在尝试绘制一个由三角形、圆形和矩形叠加而成的大炮。我需要旋转它,所以我意识到当我绘制旋转的形状时,它不会读取旧的坐标系,而是读取新的旋转坐标系。

我认为简单的数学运算可以跟踪所需的坐标。所以我定义了\rotx\roty函数来帮助进行转换。当我这样做时\rot{0},一切都很好。但是当我这样做\rot{30}或任何其他角度时,形状会呈现不理想的形状。

我的问题是:如何处理 Tikz 中的旋转坐标?

我的代码为例:

\begin{tikzpicture}
%-> USEFUL DEFINITIONS
\def\fs{0.1} %... fence width and height
\def\th{2} %... tower height
\def\wh{1} %... wall height
\def\cp{4.5} %... canon position
\def\cr{0.2} %... canon radius
%-> x-rotation (#1=x, #2=y, $3=angle)$
\def\rotx#1#2#3{#1*cos(#3)+#2*sin(#3)}
\def\roty#1#2#3{-#1*sin(#3)+#2*cos(#3)}
\def\rot{30}

%----------> END DEFINITIONS <----------%


%-> DRAW THE FLOOR
\fill[gray!50]
    (-5,-\fs) rectangle(2,0)
    ;

%-> DRAW THE CANNON
\fill[gray!50]
    (-\cp-\cr,0)--
        (-\cp+\cr,0)--
        (-\cp,\cr)--cycle
    (-\cp-\cr/2,3*\cr/2) circle(\cr)
    ;
\fill[gray!50,rotate=\rot]
    ({\rotx{-4.5}{0.2}{\rot}},{\roty{-4.5}{0.2}{\rot}})
        rectangle({\rotx{-4}{0.6}{\rot}},{\roty{-4}{0.6}{\rot}})
%    ({\rotx{-\cp}{3*\cr/2}{0}},
%        {\roty{-\cp}{3*\cr/2}{0}})
%            rectangle({\rotx{-\cp+3*\cr}{5*\cr/2}{0}},
%                {\roty{-\cp+3*\cr}{5*\cr/2}{0}})
    ;

%-> DRAW THE CASTLE
\fill[gray!50]
%... tower
    (0,0) rectangle(5*\fs,\th)
    ;
\foreach \x in {0,0.2,...,0.4}
    \fill[gray!50]
        (\x,\th) rectangle(\x+\fs,\th+\fs)
        ;
%... wall
\fill[gray!50]
    (5*\fs,0) rectangle(20*\fs,\wh)
    ;
\foreach \x in {0.6,0.8,...,1.8}
    \fill[gray!50]
        (\x,\wh) rectangle(\x+\fs,\wh+\fs)
        ;
\end{tikzpicture}

答案1

我可能忽略了一些基本的东西,但是如何从大炮圆圈的中心沿给定方向画一条一定宽度的线呢?我会用一张图片来做这件事,这样就不能把水平位置添加到所有坐标上。

\documentclass[tikz,border=3.14mm]{standalone}
\begin{document}
\begin{tikzpicture}
%-> USEFUL DEFINITIONS
\def\fs{0.1} %... fence width and height
\def\th{2} %... tower height
\def\wh{1} %... wall height
\def\cp{4.5} %... canon position
\def\cr{0.2} %... canon radius
%-> x-rotation (#1=x, #2=y, $3=angle)$
\tikzset{pics/cannon/.style={code={
\fill[gray!50]
    (-\cr,0)--
        (+\cr,0)--
        (0,\cr)--cycle
    (-\cr/2,3*\cr/2) circle(\cr)
    ;
\draw[gray!50,line width=1mm](-\cr/2,3*\cr/2) -- ++(#1:1);  
}}}


%----------> END DEFINITIONS <----------%


%-> DRAW THE FLOOR
\fill[gray!50]
    (-5,-\fs) rectangle(2,0)
    ;

\pic at (-\cp,0) {cannon=30};

%-> DRAW THE CASTLE
\fill[gray!50]
%... tower
    (0,0) rectangle(5*\fs,\th)
    ;
\foreach \x in {0,0.2,...,0.4}
    \fill[gray!50]
        (\x,\th) rectangle(\x+\fs,\th+\fs)
        ;
%... wall
\fill[gray!50]
    (5*\fs,0) rectangle(20*\fs,\wh)
    ;
\foreach \x in {0.6,0.8,...,1.8}
    \fill[gray!50]
        (\x,\wh) rectangle(\x+\fs,\wh+\fs)
        ;
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

我在这里介绍一种解决这个问题的方法。由于绘图是目的,因此可以将其视为一种解决方案。我没有尝试获取矩形的旋转坐标,而是简单地使用闭合路径生成所需的正典矩形。

我甚至使用了同样的想法来旋转星爆和速度矢量。

\begin{tikzpicture}
%-> USEFUL DEFINITIONS
\def\fs{0.1} %... fence width and height
\def\th{2} %... tower height
\def\wh{1} %... wall height
\def\cp{4.5} %... canon position
\def\cr{0.1} %... canon radius
\def\cl{0.3} %... canon length
\def\rot{45}

%----------> END DEFINITIONS <----------%


%-> DRAW THE FLOOR
\fill[gray!50]
    (-5,-\fs) rectangle(2,0)
    ;

%-> DRAW THE CANNON
\fill[gray!50]
    (-\cp-\cr,0)--
        (-\cp+\cr,0)--
        (-\cp,\cr)--cycle
    (-\cp-\cr/2,3*\cr/2) circle(\cr)
    ;
\fill[gray!50]
    (-\cp-\cr/2,3*\cr/2)--++
        ({\rot-90}:\cr)--++
        (\rot:\cl)--++
        ({\rot+90}:2*\cr)--++
        ({\rot+180}:\cl)--cycle
    ;

\begin{scope}
\clip
    ({-\cp-\cr/2+\cl*cos(\rot)},{3*\cr/2+\cl*sin(\rot)})--++
        ({\rot-90}:5*\cr)--++
        (\rot:5*\cr)--++
        ({\rot+90}:11*\cr)--++
        ({\rot+180}:5*\cr)--cycle
    ;
\node[starburst, draw, minimum width=\cr mm, minimum height=\cr mm, white, fill=gray!50] at ({-\cp-\cr/2+\cl*cos(\rot)},{3*\cr/2+\cl*sin(\rot)}) {};
\end{scope}

%... the vectors
\draw[->, very thick, gray!50]
    ({-\cp-\cr/2+\cl*cos(\rot)},{3*\cr/2+\cl*sin(\rot)})--
        ({-\cp-\cr/2+3*\cl*cos(\rot)},{3*\cr/2+3*\cl*sin(\rot)})
        node[above left]{$v_0$}
    ;
\draw[<->, gray!50]
    (-0.2,0)--
        (-0.2,\th) node[midway, fill=white]{$h$}
    ;
\draw[<->, gray!50]
    (-\cp,-0.4)--
        (0,-0.4) node[midway, fill=white]{$d$}
    ;


%-> DRAW THE CASTLE
\fill[gray!50]
%... tower
    (0,0) rectangle(5*\fs,\th)
    ;
\foreach \x in {0,0.2,...,0.4}
    \fill[gray!50]
        (\x,\th) rectangle(\x+\fs,\th+\fs)
        ;
%... wall
\fill[gray!50]
    (5*\fs,0) rectangle(20*\fs,\wh)
    ;
\foreach \x in {0.6,0.8,...,1.8}
    \fill[gray!50]
        (\x,\wh) rectangle(\x+\fs,\wh+\fs)
        ;

%-> DRAW THE BRICKS
\foreach \y in {0,0.2,...,\th}
    \foreach \x in {0,0.2,...,2}
        \draw[white]
            (\x,\y) rectangle(\x+2*\fs,\y+\fs)
            ;
\foreach \y in {0.1,0.3,...,\th}
    \foreach \x in {0.1,0.3,...,2}
        \draw[white]
            (\x,\y) rectangle(\x+2*\fs,\y+\fs)
            ;
\end{tikzpicture}

相关内容