有没有办法对一组点进行编码,其中“一组点”是指包含一个或多个点的实线(或虚线)?例如,一个有三个相同实线点(内部)的圆圈就是“一组三个点”。此外,如果可以有嵌套组以及其他形状(如正方形、三角形等),那将会很有帮助。
答案1
更新
改进的版本允许随机放置组内元素;我还decorations.shapes
按照建议添加了一个使用该库的示例克劳迪奥·菲安德里诺:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{decorations.shapes,shapes.geometric,fit}
\newcommand\Group[4][circle,draw=red]{%
\pgfresetboundingbox
\begin{scope}[scale=0.3,transform shape]
\foreach \i in {1,...,#2}
\pgfmathsetmacro\x{rnd*#2}
\pgfmathsetmacro\y{rnd*#2}
\node[#3] (\i) at (\x,\y) {};
\end{scope}
\node[circle,#1,fit=(current bounding box.north west) (current bounding box.south east)] (#4) {};
}
\tikzset{
paint/.style={draw=#1!50!black, fill=#1!50},
decorate with/.style={
decorate,decoration={shape backgrounds,shape=#1,shape size=2mm}
}
}
\begin{document}
\begin{tikzpicture}
\Group{3}{draw=red,fill=red!20,circle,inner sep=2pt}{a}
\begin{scope}[xshift=2cm]
\Group[draw=cyan]{7}{draw=cyan,fill=cyan!60,diamond}{b}
\end{scope}
\begin{scope}[xshift=6cm]
\Group[ellipse,draw=magenta]{6}{draw=none,fill=magenta!60,isosceles triangle}{c}
\end{scope}
\begin{scope}[xshift=-4cm]
\Group{3}{draw=red,fill=red!20,circle}{d}
\begin{scope}[yshift=1.5cm]
\Group[draw=orange,ellipse]{5}{draw=orange,fill=orange!60,cylinder}{e}
\end{scope}
\node[draw=olive,circle,fit=(d) (e)] {};
\end{scope}
\end{tikzpicture}
\vspace*{1.5cm}
\begin{tikzpicture}
\pgfresetboundingbox
\useasboundingbox (-0.5,-1) rectangle (2.5,1);
\draw[decorate with=circle,paint=green]
(0,0) .. controls (0.5,-3) and (1.5,3).. (2,0);
\node[draw=green!80!black,circle,fit=(current bounding box.north west) (current bounding box.south east)] (f) {};
\end{tikzpicture}
\end{document}
一种选择是使用TikZ 中的shapes.geometric
和fit
库:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric,fit}
\newcommand\Group[4][circle,draw=red]{%
\foreach \i in {1,...,#2}
\node[#3] (\i) at (\i,0) {};
\node[circle,#1,fit=(1) (#2)] (#4) {};
}
\begin{document}
\begin{tikzpicture}
\Group{3}{draw=red,fill=red!20,circle}{a}
\begin{scope}[xshift=5cm]
\Group[draw=cyan,diamond]{2}{draw=cyan,fill=cyan!60,diamond}{b}
\end{scope}
\begin{scope}[yshift=-4cm]
\Group[rectangle,draw=magenta]{6}{draw=none,fill=magenta!60,diamond}{c}
\end{scope}
\end{tikzpicture}
\vspace{1cm}
\begin{tikzpicture}
\Group{3}{draw=red,fill=red!20,circle}{d}
\begin{scope}[xshift=5cm]
\Group[draw=cyan,diamond]{2}{draw=cyan,fill=cyan!60,diamond}{e}
\end{scope}
\node[draw=olive,circle,fit=(d) (e)] {};
\end{tikzpicture}
\end{document}
\Group
有一个可选参数和三个强制参数:
\Group[<options for fitting node>]{<number of inner shapes>}{<options for inner shapes>}{<name>}
答案2
您的问题让我想起了 TikZpetri
库(除了一些样式之外,它只是树的自定义生长函数)。
这里有一些想法(第一个 TikZ 图片取自手册),最后一个例子展示了使用库place
中的自定义增长函数(没有但有)fit
。当然,任何形状都可以用于“标记”或“位置”。
代码
\documentclass[tikz,convert=false]{standalone}
\usetikzlibrary{petri,fit,backgrounds}
\begin{document}
\begin{tikzpicture}[every place/.style={minimum size=9mm}]
\foreach \x/\y/\tokennumber in {0/2/1,1/2/2,2/2/3,
0/1/4,1/1/5,2/1/6,
0/0/7,1/0/8,2/0/9}
\node [place,tokens=\tokennumber] at (\x,\y) {};
\end{tikzpicture}
\begin{tikzpicture}[every place/.style={shape=rectangle},
every token/.style={fill=green!\the\tikznumberofcurrentchild 0!red},
token distance={min(3mm,120/\the\tikznumberofchildren mm)}
]
\foreach \x/\y/\tokennumber in {0/2/1,1/2/2,2/2/3,
0/1/4,1/1/5,2/1/6,
0/0/7,1/0/8,2/0/9}
\node [place,tokens=\tokennumber,fill=blue!\tokennumber 0!green] at (\x,\y) {};
\end{tikzpicture}
\begin{tikzpicture}[every token/.style={shape=rectangle,name=t-\the\tikznumberofchildren-\the\tikznumberofcurrentchild}]
\foreach \x/\y/\tokennumber in {2/2/3,
2/1/6}
\node [structured tokens={1,...,\tokennumber}] at (\x,\y) {};
\begin{scope}[on background layer]
\node [fit=(t-3-1)(t-3-2)(t-3-3),draw,fill=blue,circle] {};
\node [fit=(t-6-1)(t-6-6),inner sep=2pt,draw=green,fill=red,thick,circle] {};
\end{scope}
\end{tikzpicture}
\end{document}