我正在绘制一个节点格子,我想用不同的颜色突出显示某些路径。当路径重叠时,我希望颜色彼此相邻。我在这个网站上看到了当节点仅垂直或仅水平地相互位移时的解决方案。我的情况是节点被某个非零斜率偏移,但我还没有找到答案。
我可以有点儿达到了我想要的效果。但是,我对我的解决方案并不完全满意。它需要我做太多的修改。
为了重复我所做的事情,我想变换画布与某条路径正交。(或者也许还有另一种我不知道的方法。)
以下是我的尝试,
这是我的代码:
\documentclass[border=3pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\def\aev{\dimexpr12pt+0.65cm}
\def\aeh{\dimexpr12pt+1.00cm}
\newcommand\aes[2]{\scalebox{\ifcase#1\or1.00\or0.90\or0.80\or0.70\fi}{#2}}
\begin{document}
\begin{tikzpicture}[
aec/.style={circle,draw,inner sep=1pt,minimum size=0.80cm},
ae transform top/.style={transform canvas={yshift= 1.3pt,xshift= 0.3pt}},
ae transform bot/.style={transform canvas={yshift=-1.3pt,xshift=-0.3pt}},
]
%% ------------------------------------------------------------
%% a bit of a work around for several issues
%% the primary one here is how "transformed" path connect
%% at the nodes
%% ------------------------------------------------------------
\coordinate (cA) at (0,0);
\coordinate (cB) at ($(cA.south)+(0,-\aev)$);
\coordinate (cC) at ($(cB.south)+(0,-\aev)$);
\coordinate (cD) at ($(cC.south)+(0,-\aev)$);
\coordinate (cAB) at ($($(cA.south)!0.5!(cB.north)$)+(\aeh,0pt)$);
\coordinate (cBC) at ($($(cB.south)!0.5!(cC.north)$)+(\aeh,0pt)$);
\coordinate (cCD) at ($($(cC.south)!0.5!(cD.north)$)+(\aeh,0pt)$);
\coordinate (cABC) at ($($(cAB.south)!0.5!(cBC.north)$)+(\aeh,0pt)$);
\coordinate (cBCD) at ($($(cBC.south)!0.5!(cCD.north)$)+(\aeh,0pt)$);
\coordinate (cABCD) at ($($(cABC.south)!0.5!(cBCD.north)$)+(\aeh,0pt)$);
\node[aec] (A) at (cA) {A};
\node[aec] (B) at (cB) {B};
\node[aec] (C) at (cC) {C};
\node[aec] (D) at (cD) {D};
\node[aec] (AB) at (cAB) {\aes{2}{AB}};
\node[aec] (BC) at (cBC) {\aes{2}{BC}};
\node[aec] (CD) at (cCD) {\aes{2}{CD}};
\node[aec] (BCD) at (cBCD) {\aes{3}{BCD}};
\draw (AB) -- (A);
\draw (AB) -- (B);
\draw (BC) -- (B);
\draw (BC) -- (C);
\draw (CD) -- (C);
\draw (CD) -- (D);
%\draw (ABC) -- (AB);
%\draw (ABC) -- (BC);
\draw (BCD) -- (BC);
\draw (BCD) -- (CD);
%\draw (ABCD) -- (ABC);
%\draw (ABCD) -- (BCD);
%% ------------------------------------------------------------
%% Using my "coordinate" construct work around to make the
%% lines connecting nodes look more "beautiful
%% ------------------------------------------------------------
\draw[ red,line width=3pt] (cABC) -- (BC);
\draw[green,line width=3pt] (cABC) -- (AB) -- (B);
\draw[green,line width=3pt,ae transform top] (cABCD) -- (cABC);
\draw[ red,line width=3pt,ae transform bot] (cABCD) -- (cABC);
%% ------------------------------------------------------------
%% Using a more pure "node" approach to this, but the
%% transformed colored paths don't connect to the nodes very
%% beautifully and I'm not happy with that.
%% ------------------------------------------------------------
\node[aec,fill=white] (ABC) at (cABC) {\aes{3}{ABC}};
\node[aec,fill=white] (ABCD) at (cABCD) {\aes{4}{ABCD}};
\draw[ blue,line width=3pt] (ABCD) -- (BCD) -- (BC);
\draw[ red,line width=3pt,ae transform top] (BC) -- (B);
\draw[blue,line width=3pt,ae transform bot] (BC) -- (B);
\end{tikzpicture}
\end{document}
因此,有两个问题我想得到解决。
其中最重要的是如何更轻松地变换画布或者使用其他技术来着色基本的路径并行(同一路径最多可使用三种颜色)。
其次,我感兴趣的是(因为我已经有了一些解决方法):一旦我得到了我想要的路径颜色(第 1 点),我怎样才能使与节点的连接更加美丽的。
关于第二点,你可以看到我创造的外观解决方法节点ABCD
和之间ABC
。
您可以看到丑陋的通过不使用我的连接创建解决方法节点BC
和之间B
。
答案1
您可以使用此答案中的方法:在 TikZ 中绘制额外的平行路径
-或者因为它只是线条,所以你可以用calc
符号计算偏移。当线宽远大于节点边界宽度时,线与圆节点的连接永远不会很好。我通过在背景中绘制线条来避免这种情况。我没有理解你的代码,而是从头开始,只是复制代码,而不是制作相关的样式。
\documentclass[tikz, border=1 cm]{standalone}
\usetikzlibrary{positioning,calc}
\tikzset{aec/.style={circle, draw, inner sep=1pt, minimum size=0.80cm, on grid, fill=white}}
\begin{document}
\pgfdeclarelayer{background}
\pgfsetlayers{background, main}
\begin{tikzpicture}[node distance=0.5 cm and 1.5cm]
\node[aec] (A) {A};
\node[aec, below right=of A] (AB) {\scalebox{0.9}{AB}};
\node[aec, below left=of AB] (B) {B};
\node[aec, below right=of B] (BC) {\scalebox{0.9}{BC}};
\node[aec, below left=of BC] (C) {C};
\node[aec, below right=of C] (CD) {\scalebox{0.9}{CD}};
\node[aec, below left=of CD] (D) {D};
\node[aec, below right=of AB] (ABC) {\scalebox{0.8}{ABC}};
\node[aec, below right=of BC] (BCD) {\scalebox{0.8}{BCD}};
\node[aec, below right=of ABC] (ABCD) {\scalebox{0.7}{ABCD}};
\begin{pgfonlayer}{background}
\draw (A) -- (AB);
\draw (C) -- (BC);
\draw (C) -- (CD);
\draw (D) -- (CD);
\draw[green, line width=3pt] (B.center) -- (AB.center);
\draw[green, line width=3pt] (AB.center) -- (ABC.center);
\draw[red, line width=3pt] (BC.center) -- (ABC.center);
\draw[blue, line width=3pt] (BC.center) -- (BCD.center);
\draw[blue, line width=3pt] (BCD.center) -- (ABCD.center);
\draw[red, line width=3pt] ($ (B.center)!1.5pt!90:(BC.center) $) -- ($ (BC.center)!1.5pt!-90:(B.center) $);
\draw[blue, line width=3pt] ($ (B.center)!-1.5pt!90:(BC.center) $) -- ($ (BC.center)!-1.5pt!-90:(B.center) $);
\draw[green, line width=3pt] ($ (ABC.center)!1.5pt!90:(ABCD.center) $) -- ($ (ABCD.center)!1.5pt!-90:(ABC.center) $);
\draw[red, line width=3pt] ($ (ABC.center)!-1.5pt!90:(ABCD.center) $) -- ($ (ABCD.center)!-1.5pt!-90:(ABC.center) $);
\end{pgfonlayer}
\end{tikzpicture}
\end{document}
为了完全避免这种情况calc
,您还可以使用符号进行计算。我会进行坐标移位,而不是画布移位。shift={(\angle:1.5pt)}
\angle
atan2
let
编辑:
节点放置不positioning
:
\documentclass[tikz, border=1 cm]{standalone}
\usetikzlibrary{calc}
\def\aev{\dimexpr12pt+0.65cm}
\def\aeh{\dimexpr12pt+1.00cm}
\begin{document}
\begin{tikzpicture}[
aec/.style={circle,draw,inner sep=1pt,minimum size=0.80cm},
]
\coordinate (A) at (0,0);
\coordinate (B) at ([yshift=-\aev] A);
\coordinate (C) at ([yshift=-\aev] B);
\coordinate (D) at ([yshift=-\aev] C);
\coordinate (AB) at ($(A)!0.5!(B)+(\aeh,0)$);
\coordinate (BC) at ($(B)!0.5!(C)+(\aeh,0)$);
\coordinate (CD) at ($(C)!0.5!(D)+(\aeh,0)$);
\coordinate (ABC) at ($(AB)!0.5!(BC)+(\aeh,0)$);
\coordinate (BCD) at ($(BC)!0.5!(CD)+(\aeh,0)$);
\coordinate (ABCD) at ($(ABC)!0.5!(BCD)+(\aeh,0)$);
\foreach \text/\scale in {A/1, B/1, C/1, D/1, AB/2, BC/2, CD/2, ABC/3, BCD/3, ABCD/4}
\node[aec] at (\text) {\scalebox{\ifcase\scale\or1.00\or0.90\or0.80\or0.70\fi}{\text}};
\end{tikzpicture}
\end{document}
positioning
有和没有的节点放置calc
:
\documentclass[tikz, border=1 cm]{standalone}
\def\aev{\dimexpr12pt+0.65cm}
\def\aeh{\dimexpr12pt+1.00cm}
\begin{document}
\begin{tikzpicture}[
aec/.style={circle,draw,inner sep=1pt,minimum size=0.80cm},
]
\coordinate (A) at (0,0);
\coordinate (B) at ([yshift=-\aev] A);
\coordinate (C) at ([yshift=-\aev] B);
\coordinate (D) at ([yshift=-\aev] C);
\path (A) --coordinate[right=\aeh](AB) (B);
\path (B) --coordinate[right=\aeh](BC) (C);
\path (C) --coordinate[right=\aeh](CD) (D);
\path (AB) --coordinate[right=\aeh](ABC) (BC);
\path (BC) --coordinate[right=\aeh](BCD) (CD);
\path (ABC) --coordinate[right=\aeh](ABCD) (BCD);
\foreach \text/\scale in {A/1, B/1, C/1, D/1, AB/2, BC/2, CD/2, ABC/3, BCD/3, ABCD/4}
\node[aec] at (\text) {\scalebox{\ifcase\scale\or1.00\or0.90\or0.80\or0.70\fi}{\text}};
\end{tikzpicture}
\end{document}
如果节点需要命名,则可以使用例如:
\node[aec] (\text_node) at (\text) {\scalebox{\ifcase\scale\or1.00\or0.90\or0.80\or0.70\fi}{\text}};
对于节点名称(A_node), ...
。