我想用 tikz 表示三维匹配。我想用类似 Wiki 中的内容来表示它这里。
特别是 - 这是来自 Wiki 的图片:
我曾尝试使用 fit 命令,但我认为除此之外它还需要某种节点合并。
任何帮助都将非常感激!
答案1
您可以使用double
样式以及和来line cap=round
实现这一点line join=round
。
\documentclass{article}
\usepackage{tikz}
\tikzset{
mycircle/.style={draw, ultra thick, circle, fill=#1, inner sep=0pt, minimum size=3mm},
matching/.style={thick, double=gray!20, double distance=#1, line cap=round, line join=round}
}
\begin{document}
\begin{tikzpicture}
\draw[matching=7mm](0,0)node[mycircle=red]{}--(3,3)node[mycircle=blue]{}--(6,1)node[mycircle=green]{};
\draw[matching=5mm](0,0)node[mycircle=red]{}--(3,1)node[mycircle=blue]{}--(6,3)node[mycircle=green]{};
\end{tikzpicture}
\end{document}
答案2
这可以给你一个开始:
\documentclass[tikz,border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning,shapes.geometric}
\begin{document}
\begin{tikzpicture}[every node/.style={draw,circle,minimum size=1cm}]
% Define vertices
\node (A1) at (0,0) {A1};
\node (A2) at (0,-2) {A2};
\node (A3) at (0,-4) {A3};
\node (B1) at (4,0) {B1};
\node (B2) at (4,-2) {B2};
\node (B3) at (4,-4) {B3};
\node (C1) at (8,0) {C1};
\node (C2) at (8,-2) {C2};
\node (C3) at (8,-4) {C3};
% Connect vertices to show matching
\draw (A1) -- (B2) -- (C1);
\draw (A2) -- (B2) -- (C2);
\draw (A3) -- (B1) -- (C1);
\end{tikzpicture}
\end{document}
然后,您可以根据自己的心意进行标记和装饰。
\documentclass[tikz,border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc,positioning,shapes.geometric}
\tikzstyle{A} = [fill=red!30]
\tikzstyle{B} = [fill=blue!30]
\tikzstyle{C} = [fill=green!30]
\tikzset{
connect/.style args={#1 with #2}{
to path={
($(\tikztostart)!#1!90:(\tikztotarget)$) --
($(\tikztotarget)!#1!-90:(\tikztostart)$) --
($(\tikztotarget)!#1!90:(\tikztostart)$) --
($(\tikztostart)!#1!-90:(\tikztotarget)$) -- cycle
},
fill=#2,draw,rounded corners
}
}
\begin{document}
\begin{tikzpicture}[every node/.style={draw,circle,minimum size=0.5cm}]
% Define vertices
\node[A] (A1) at (0,0) {A1};
\node[A] (A2) at (0,-2) {A2};
\node[A] (A3) at (0,-4) {A3};
\node[B] (B1) at (4,0) {B1};
\node[B] (B2) at (4,-2) {B2};
\node[B] (B3) at (4,-4) {B3};
\node[C] (C1) at (8,0) {C1};
\node[C] (C2) at (8,-1.5) {C2};
\node[C] (C3) at (8,-3) {C3};
\node[C] (C4) at (8,-4.5) {C4};
% Custom connections using the corrected 'connect' style
\draw (A1) to[connect=3mm with red!50] (B1);
\draw (B1) to[connect=3mm with blue!50] (C1);
\draw (A2) to[connect=3mm with green!50] (B2);
\draw (B2) to[connect=3mm with orange!50] (C2);
\draw (A3) to[connect=3mm with purple!50] (B3);
\draw (B3) to[connect=3mm with yellow!50] (C3);
\end{tikzpicture}
\end{document}