我正在尝试在网格上绘制一些人。这些人使用 来定义\def
,它表示一个点和一个角度。但我喜欢传递两个点而不是一个角度:形状应放置的位置和箭头应指向的点。人应相应地旋转,以便人朝箭头的方向看。
这是 MWE。请注意,我将角度设置为任意数字。
\documentclass{standalone}
\usepackage[utf8]{inputenc}
\usepackage{xcolor}
\usepackage{tikz}
\begin{document}
\def\person#1#2{
\begin{scope}[shift={#1},rotate around={{#2}:(0.5cm,0.5cm)}]
\draw (0.2cm,0.5cm) [thick, fill=green!50] circle (0.15cm);
\draw (0.8cm,0.5cm) [thick, fill=green!50] circle (0.15cm);
\draw (0.15cm,0.3cm) [thick, rounded corners=3.5, fill=green!50] rectangle (0.85cm,0.7cm);
\draw (0.5cm,0.47cm) [thick, fill=black!50] circle (0.22cm);
\end{scope}
}
\begin{tikzpicture}
\draw[help lines] (0,0) grid (6,4);
%Person 1
\person{(1,1)}{32};
\draw (1.5,1.5) [->, ultra thick] -- (2.5,3.5); %remove this line and put it to \person
%Person 2
\person{(3,2)}{124};
\draw (3.5,2.5) [->, ultra thick] -- (5.5,0.5);
\end{tikzpicture}
\end{document}
答案1
这并没有使用你要求的确切语法,但也许这样也可以。一个人的定义是使用
\pic at (x,y) {person={xrel}{yrel}};
这(x,y)
是人(中心)所在坐标,和分别xrel
是yrel
箭头的 x 和 y 分量。因此,
\pic at (1,1) {person={2}{3}};
将会把一个人置于 (1,1) 的中心,箭头终点为 (1,1)+(2,3) = (3,4)。
\documentclass[border=5mm]{standalone}
\usepackage{tikz}
\tikzset{
pics/person/.style 2 args={
code={
\pgfmathsetmacro{\personangle}{atan2(#2,#1)}
\pgfmathsetlengthmacro{\personwidth}{0.4cm}
\draw [rotate=\personangle] (0,\personwidth) [thick, fill=green!50] circle[radius=0.15cm];
\draw [rotate=\personangle] (0,-\personwidth) [thick, fill=green!50] circle[radius=0.15cm];
\draw [rotate=\personangle] (-0.2cm,-\personwidth) [thick, rounded corners=3.5, fill=green!50] rectangle (0.2cm,\personwidth);
\draw [rotate=\personangle] (0.05cm,0) [thick, fill=black!50] circle (0.22cm);
\draw [->, ultra thick] (0,0) -- (#1,#2);
}}}
\begin{document}
\begin{tikzpicture}
\draw[help lines] (0,0) grid (6,4);
%Person 1
\pic at (1,1) {person={1}{2}};
%Person 2
\pic at (3,2) {person={2}{1}};
\end{tikzpicture}
\end{document}
答案2
该解决方案采用了您的语法,但做了一些更改:
- 该
xcolor
包已由 加载tikz
,因此无需明确加载它。 - 该命令是通过
\newcommand
而不是 来定义的\def
。并且它被放置在序言中(不是必须的,但我认为这样更有条理)。 该命令的结构如下:
\person{<position>}{<end arrow>}
输出
代码
\documentclass[margin=10pt]{standalone}
\usepackage{tikz}
\tikzset{
bodypart/.style={draw, at start, sloped, rotate=-90}
}
\newcommand{\person}[2]{%
\draw[->, thick] (#1)-- node[bodypart,rounded corners=1.2mm,
text width=4.5mm, fill=green!50] {}
node[bodypart,rounded corners=.8mm, fill=green!50,
text width=3mm] {}
node[bodypart,circle, fill=black!50,rotate=-90] {} (#2);
}
\begin{document}
\begin{tikzpicture}
\draw[help lines] (0,0) grid (6,4);
\person{1,1}{1,3}
\person{3,2}{2,4}
\person{3,0}{4,2}
\end{tikzpicture}
\end{document}