我正在尝试在特定路径上绘制一个矩形,也就是说,我有一条从 A 到 B 的倾斜线,我需要使用该角度绘制一个矩形。
这是我想要复制的图表:
我在绘制手臂的矩形(以及随后与从 C 到 B 的路径对齐的矩形)时遇到了一些麻烦。
这是我目前拥有的部分代码:
\documentclass[crop=true]{standalone}
\usepackage{tikz,pgf,import,pgfplots}
\usetikzlibrary{intersections}
\usetikzlibrary{arrows}
\usetikzlibrary{positioning}
\usetikzlibrary{fit}
\usetikzlibrary{calc}
\pgfplotsset{compat=1.3}
\begin{document}
\centering
\begin{tikzpicture}
\draw[->] (-40mm,0) -- (15mm,0) node[right] {$x$} coordinate(x axis);
\draw[->] (0,-5mm) -- (0,15mm) node[above] {$y$} coordinate(y axis);
\draw[help lines, step=0.5cm] (-55mm,-15mm) grid (20mm,60mm);
\filldraw[fill=green!20!white, draw=green!50!black] (8mm,0mm)
arc [start angle=0, end angle=-180, radius=8mm] -- (-8mm,3mm)--(8mm,3mm)--cycle;
\node [anchor=south,rectangle,draw=black!50,fill=black!20,inner sep=0pt,minimum width=3mm, minimum height=2mm] at (0mm,3mm) {};
\coordinate (P1) at (-27mm,8mm);
\coordinate (Float) at (0mm,3mm);
\draw (P1) rectangle +(3mm,52mm);
\draw let \p1 = ($ (P1) + (-1.5mm,8mm) $),
\p2 = ($ (\p1) + (0mm,27.33mm) $)
in
(\p1) coordinate[label=below left:\textcolor{blue}{$A$}] (A)
(\p2) coordinate[label=below right:\textcolor{blue}{$B$}](B);
\path[name path=arm line] (Float) -- ($ (Float) !1.5! (A) $);
\path[name path=C] (A) circle[radius=13.33mm];
\path [name intersections={of= arm line and C, by={x1,x2}}];
\coordinate [label=above left:\textcolor{blue}{$C$}] (C) at (x1);
\draw (Float) -- (C) -- (B);
\foreach \point in {A,B,C}
{
\filldraw[fill=white,draw=black] (\point) circle (1mm);
\filldraw[fill=black,draw=black] (\point) circle (0.3mm);
}
\node (arm) [rotate=150, draw=black,thick,rounded corners, inner sep=2mm, fit=(C)(Float)] {};
\end{tikzpicture}
\end{document}
我也尝试过使用:
\draw[rotate=150] (Float) rectangle (C);
但它不起作用。
随时欢迎任何指导。
干杯!胡安
答案1
这可能不是一个优雅的解决方案,但你确实可以\draw
结合使用层来实现这一点:
\documentclass{standalone}
\usepackage{tikz,pgfplots}
\usetikzlibrary{intersections,arrows.meta,positioning,fit,calc}
\pgfplotsset{compat=1.18}
\pgfdeclarelayer{background}
\pgfdeclarelayer{foreground}
\pgfsetlayers{background,main,foreground}
\begin{document}
\centering
\begin{tikzpicture}[>=Stealth,x=1mm,y=1mm]
\filldraw[fill=green!20!white, draw=green!50!black] (8,0)
arc [start angle=0, end angle=-180, radius=8mm] -- (-8,3)-- (8,3) -- cycle;
\node[anchor=south,rectangle,draw=black!50,fill=black!20,inner sep=0pt,minimum width=3mm, minimum height=2mm] at (0,3) {};
%Coordinate system
\draw[->] (-40mm,0) -- (15mm,0) node[right] {$x$} coordinate(x axis);
\draw[->] (0,-5mm) -- (0,15mm) node[above] {$z$} coordinate(y axis);
\coordinate (P1) at (-27,8);
\coordinate (Float) at (0,3);
\coordinate[label=below left:\textcolor{blue}{$A$}] (A) at ($ (P1) + (-1.5,8) $);
\coordinate[label=below right:\textcolor{blue}{$B$}] (B) at ($ (A) + (0,27.33) $);
%Calculate position of C
\path[name path=arm line] (Float) -- ($ (Float) !1.5! (A) $);
\path[name path=C] (A) circle[radius=13.33mm];
\path [name intersections={of= arm line and C, by={x1,x2}}];
\coordinate[label=above left:\textcolor{blue}{$C$}] (C) at (x1);
\foreach \point in {A,B,C}
{
\draw[fill=white] (\point) circle (1);
\filldraw (\point) circle (0.3);
}
\begin{pgfonlayer}{background}
\draw (P1) rectangle +(3,52);
\draw[line width=7.5pt] ($(Float)!1.055!(C)$) -- ($(C)!1.035!(Float)$);
\draw[gray!20,line width=6pt] ($(Float)!1.05!(C)$) -- ($(C)!1.03!(Float)$);
\end{pgfonlayer}
\end{tikzpicture}
\end{document}
tikz
肯定有更好的方法来实现这一点,例如使用可以为您计算的三个点之间的角度。
答案2
我将使用以下calc
符号通过四条线自己构建矩形:
\documentclass[tikz, border=1 cm]{standalone}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\coordinate (A) at (1,5);
\coordinate (B) at (8,2);
\fill (A) circle[radius=0.1] node[below]{A};
\fill (B) circle[radius=0.1] node[below]{B};
%calc notation
\draw ($ (A)!.1!135:(B) $) -- ($ (A)!.1!-135:(B) $) -- ($ (B)!.1!135:(A) $) -- ($ (B)!.1!-135:(A) $) -- cycle;
%alternative with a node
\draw[red] let \p1=($(B)-(A)$), \n1={atan2(\y1,\x1)} in node[draw, minimum width=10 cm, minimum height=2 cm, xshift=4cm, rotate around={\n1:(A)}] at (A){};
\end{tikzpicture}
\end{document}
($ (A)!.1!135:(B) $)
表示相对于 线沿 deg方向移动(A)
的点。0.1
135
(A)
(B)
答案3
你的角度不是150°你必须事先确定
我添加了从两个点的坐标计算角度的功能(请参阅文档)
我借此机会设置了一个范围,让您可以绘制倾斜的基准。
我们注意到节点没有在范围内转动(即使具有变换形状)
\documentclass[crop=true]{standalone}
\usepackage{tikz,pgf,import,pgfplots}
\usetikzlibrary{intersections}
\usetikzlibrary{arrows}
\usetikzlibrary{positioning}
\usetikzlibrary{fit}
\usetikzlibrary{calc}
\pgfplotsset{compat=1.3}
\begin{document}
\centering
\begin{tikzpicture}
\draw[->] (-40mm,0) -- (15mm,0) node[right] {$x$} coordinate(x axis);
\draw[->] (0,-5mm) -- (0,15mm) node[above] {$y$} coordinate(y axis);
\draw[help lines, step=0.5cm] (-55mm,-15mm) grid (20mm,60mm);
\filldraw[fill=green!20!white, draw=green!50!black] (8mm,0mm)
arc [start angle=0, end angle=-180, radius=8mm] -- (-8mm,3mm)--(8mm,3mm)--cycle;
\node [anchor=south,rectangle,draw=black!50,fill=black!20,inner sep=0pt,minimum width=3mm, minimum height=2mm] at (0mm,3mm) {};
\coordinate (P1) at (-27mm,8mm);
\coordinate (Float) at (0mm,3mm);
\draw (P1) rectangle +(3mm,52mm);
\draw let \p1 = ($ (P1) + (-1.5mm,8mm) $),
\p2 = ($ (\p1) + (0mm,27.33mm) $)
in
(\p1) coordinate[label=below left:\textcolor{blue}{$A$}] (A)
(\p2) coordinate[label=below right:\textcolor{blue}{$B$}](B);
\path[name path=arm line] (Float) -- ($ (Float) !1.5! (A) $);
\path[name path=C] (A) circle[radius=13.33mm];
\path [name intersections={of= arm line and C, by={x1,x2}}];
\coordinate [label=above left:\textcolor{blue}{$C$}] (C) at (x1);
\draw (Float) -- (C) -- (B);
\foreach \point in {A,B,C}
{
\filldraw[fill=white,draw=black] (\point) circle (1mm);
\filldraw[fill=black,draw=black] (\point) circle (0.3mm);
}
\newcommand{\pgfextractangle}[3]{%
\pgfmathanglebetweenpoints{\pgfpointanchor{#2}{center}}
{\pgfpointanchor{#3}{center}}
\global\let#1\pgfmathresult
}
\pgfextractangle{\angle}{C}{A}
\begin{scope}[rotate=\angle,transform canvas]
\draw (C) -- ++(5,0);
\node (arm) [ rotate=\angle,draw=black,thick,rounded corners, inner sep=2mm, fit=(C)(Float)] {};
\end{scope}
\end{tikzpicture}
\end{document}