我想画一个图的物理模型,每个顶点都有一个球,每条边都有一根绳子。在这个模型中,如果你把一个球举得足够高,其他球也会随之被拉起,受到弦的压力和重力。如下图所示。
然而,使用tikz-qtree
,我只能画出一棵普通的树,无法将绳子或重力形象化(而且有点丑)。
因此,我想知道
如何绘制在线和重力作用下的带有球的实体树?
我使用的代码tikz-qtree
如下。
\documentclass{standalone}
\usepackage{tikz}
\usepackage{tikz-qtree}
\begin{document}
\begin{tikzpicture}
\node [draw, circle] (r) at (0,0) {$r$};
\node [draw, circle, blue, very thick] (s) at (2,0) {$s$};
\node [draw, circle] (t) at (4,0) {$t$};
\node [draw, circle] (u) at (6,0) {$u$};
\node [draw, circle] (v) at (0,-2) {$v$};
\node [draw, circle] (w) at (2,-2) {$w$};
\node [draw, circle] (x) at (4,-2) {$x$};
\node [draw, circle] (y) at (6,-2) {$y$};
\draw (r) to (v);
\draw (r) to (s);
\draw (s) to (w);
\draw (t) to (u);
\draw (t) to (w);
\draw (t) to (x);
\draw (u) to (x);
\draw (u) to (y);
\draw (x) to (y);
\draw (y) to (u);
\end{tikzpicture}
\begin{tikzpicture}[]
\tikzset{level distance = 36pt, sibling distance = 25pt}
\tikzset{every node/.style = {draw, circle}}
\Tree [.\node[blue, very thick](s){$s$};
[.$r$ $v$ ]
[.\node(w){$w$};
[.\node(t){$t$}; \node(u){$u$}; ]
[.\node(x){$x$}; \node(y){$y$}; ]
]
]
% cross edges
\draw [dashed, thick, red] (t) to [out = -45, in = 225] (x);
\draw [dashed, thick, red] (u) to [out = -45, in = 225] (y);
\draw [dotted, thick, purple] (x.-110) to (u);
\end{tikzpicture}
\end{document}
答案1
以下是最新 PGF 版本中图形绘制的一些示例。我不太确定定义新边的最佳方法,但至少我使用的方法似乎有效。它需要lualatex
:
\documentclass[border=5]{standalone}
\usepackage{tikz}
\usetikzlibrary{topaths,graphs,graphdrawing,decorations}
\usegdlibrary{layered,trees}
\pgfdeclaredecoration{squiggle}{draw}{
\state{draw}[width=\pgfdecoratedpathlength]{
\pgfpathmoveto{\pgfpointorigin}
\pgfpathcurveto{\pgfpoint{0.25*\pgfdecoratedpathlength}{0.25*\pgfdecoratedpathlength}}%
{\pgfpoint{0.75*\pgfdecoratedpathlength}{-0.25*\pgfdecoratedpathlength}}%
{\pgfpoint{\pgfdecoratedpathlength}{0pt}}
}}
\tikzgraphsset{
edge squiggle/.style={
new --/.code n args={4}{
\path [-, every new --/.try]
(##1\tikzgraphleftanchor)
edge[##3, decoration=squiggle, decorate] ##4
(##2\tikzgraphrightanchor);}
},
edge loop/.style={
new --/.code n args={4}{
\draw [-, every new --/.try]
(##1)
edge [out=260, in=280, looseness=3] ##4
(##2);}
}
}
\begin{document}
\tikz\graph [nodes={shape=circle, fill=gray!50}, edge squiggle,
chain shift=(0:2cm), group shift=(270:2cm)]{
E -- S -- A;
D -!- C -- B;
D -- E; D -- S;
C -- S; B -- A;
};
\hskip1cm
\tikz\graph [layered layout, nodes={shape=circle, fill=gray!50},
level distance=1.5cm] {
S -- {A -- B, C -- B, E, D};
{[edge loop] E -- D};
{[same layer] A, C, D, E};
};
\end{document}