答案1
这做了类似的事情。它使用这个答案在随机位置填充符号,使它们不重叠。例如,
\path (-3,-3) pic{random symbols={xmax=3,ymax=6,symbols={{5*s},{2*o},{1*+}}}};
你指示 Ti钾Z 将这些符号放在左下角坐标为(-3,-3)
、宽 3cm 、高 6cm 的矩形中。符号为 5 个星 ( 5*s
)、2 个圆圈 ( 2*o
) 和一个十字 ( 1*+
)。这些符号在单独的图片中定义。底层图表是用 完成的pgf-pie
。
\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{shapes.geometric}
\usepackage{pgf-pie}
\newcounter{nsymbols}
\tikzset{pics/random symbols/.style={code={\setcounter{nsymbols}{1}
\tikzset{random symbols/.cd,#1}
\def\pv##1{\pgfkeysvalueof{/tikz/random symbols/##1}}%
\def\mysplit##1*##2;{\edef\myn{##1}\edef\myitem{##2}}
\edef\temp{\pv{symbols}}
\edef\tmplist{"x"}
\foreach \X in \temp
{\expandafter\mysplit\X;
\foreach \Y in {1,...,\myn}
{\stepcounter{nsymbols}
\xdef\tmplist{\tmplist,"\myitem"}}}
\edef\nmax{\number\value{nsymbols}}
\setcounter{nsymbols}{1}
\pgfmathsetmacro\xlist{\pv{r}+rnd*(\pv{xmax}-2*\pv{r})}
\pgfmathsetmacro\ylist{\pv{r}+rnd*(\pv{ymax}-2*\pv{r})}
\pgfmathsetmacro{\myitem}{{\tmplist}[1]}
\path (\xlist,\ylist)pic{\myitem};
\foreach \XX in {1,...,\pv{n}}
{\pgfmathsetmacro\x{\pv{r}+rnd*(\pv{xmax}-2*\pv{r})}
\pgfmathsetmacro\y{\pv{r}+rnd*(\pv{ymax}-2*\pv{r})}
\xdef\collision{0}
% check if the mark is too close to the percentage
\pgfmathsetmacro\checkdistance{4*sqrt(pow(\x-\pv{xmax}/2,2)+pow(\y-\pv{ymax}/2,2))}
\ifdim\checkdistance pt<\pv{R} pt
\xdef\collision{1}
\fi
\foreach \element [count=\YY starting from 0] in \xlist{
\pgfmathsetmacro\checkdistance{sqrt(({\xlist}[\YY]-(\x))^2+({\ylist}[\YY]-(\y))^2)}
\ifdim\checkdistance pt<\pv{R} pt
\xdef\collision{1}
\breakforeach
\fi
}
\ifnum\collision=0
\xdef\xlist{\xlist,\x}
\xdef\ylist{\ylist,\y}
\stepcounter{nsymbols}
\ifnum\value{nsymbols}<\nmax
\pgfmathsetmacro{\myitem}{{\tmplist}[\value{nsymbols}]}
\path (\x,\y) pic{\myitem};
\fi
\fi
\unless\ifnum\value{nsymbols}<\nmax
\breakforeach
\fi
}
}
},
random symbols/.cd,n/.initial=50,% tries
xmax/.initial=3,% width
ymax/.initial=3,% height
r/.initial=0.15,%radius
R/.initial=0.4,% grace distance, should be greater than 2*r
symbols/.initial={{2*s}},
/tikz/.cd,
pics/s/.style={code={\node[star,draw,fill=orange,minimum size=3mm,inner sep=0pt]{};}},
pics/+/.style={code={\draw (-1.5mm,0mm) -- (1.5mm,0mm) (0,-1.5mm) -- (0,1.5mm);}},
pics/o/.style={code={\draw (0,0) circle[radius=1.5mm];}},
}
\begin{document}
\begin{tikzpicture}[font=\sffamily]
\pie[square,
color={red,blue,yellow},
text=inside,
text=legend,
]{50/A,30/B,20/C}
\pgfmathsetseed{42}
\path (-3,-3) pic{random symbols={xmax=3,ymax=6,symbols={{5*s},{2*o},{1*+}}}};
\path (0,-3) pic{random symbols={xmax=3,ymax=3.6,symbols={{1*s},{1*o}}}};
\path (0,0.6) pic{random symbols={xmax=3,ymax=2.4,symbols={{1*o},{5*+}}}};
\end{tikzpicture}
\end{document}