我画了三个节点并将它们连接到一个三角形:
\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\node [ circle, draw ] (A) at ( 1, 0) {A};
\node [ circle, draw ] (B) at (-1, 0) {B};
\node [ circle, draw ] (C) at ( 0, 1) {C};
\draw (A) -- (B) -- (C) -- (A);
% Bonus question: Why doesn't cycle work here?
\fill [black] (A) -- (B) -- (C) -- (A);
\end{tikzpicture}
\end{document}
但填充三角形不起作用!我希望填充三角形所包围的区域,不包括节点圆。(我不能只提取坐标并填充由此定义的三角形,它会覆盖节点。)
答案1
节点是扩展对象,这就是它cycle
不起作用的原因,但是一旦指定锚点它就会起作用,正如 Torbjørn T. 所指出的那样。
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{backgrounds}
\begin{document}
\begin{tikzpicture}
\node [ circle, draw,fill=white ] (A) at ( 1, 0) {A};
\node [ circle, draw,fill=white ] (B) at (-1, 0) {B};
\node [ circle, draw,fill=white ] (C) at ( 0, 1) {C};
\draw (A) -- (B) -- (C) -- (A);
% Bonus question: Why doesn't cycle work here?
\begin{scope}[on background layer]
\fill [black] (A.center) -- (B.center) -- (C.center) -- cycle;
\end{scope}
\end{tikzpicture}
\end{document}
答案2
只是为了好玩:
- 剪切而不是用白色填充圆形节点。
- 蓝色圆圈线。
- 红色连接线。
- 三角形填充绿色。
问题:
三角形可以用以下简单的方法填充:
\fill (A.center) -- (B.center) -- (C.center) -- cycle;
剪切区域由当前整个绘图区域减去使用奇偶填充规则的圆形区域定义。
半径可以通过 TikZ 库计算
calc
。由于舍入问题,圆圈线和填充区域之间可能存在背景像素。
outer sep=0pt
因此,圆半径可以做得小一点,作为节点使用时可以达到效果。但是outer sep=0pt
由于连接线会卡在圆线上,所以不能使用。填充的三角形放在背景层上,重叠部分被圆圈线遮住。
完整示例:
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{backgrounds}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\path[every node/.style={circle, draw=blue}]
(1, 0) node (A) {A}
(-1, 0) node (B) {B}
(0, 1) node (C) {C}
;
\draw[red] (A) -- (B) -- (C) -- (A);
\begin{pgfonlayer}{background}
\begin{scope}{even odd rule}
\clip
(current bounding box.south west)
rectangle (current bounding box.north east)
let
\p{A} := ($(A.north) - (A.center)$),
\p{B} := ($(B.north) - (B.center)$),
\p{C} := ($(C.north) - (C.center)$)
in
(A) circle[radius=\y{A}-.5\pgflinewidth]
(B) circle[radius=\y{B}-.5\pgflinewidth]
(C) circle[radius=\y{C}-.5\pgflinewidth]
;
\fill [green] (A.center) -- (B.center) -- (C.center) -- cycle;
\end{scope}
\end{pgfonlayer}
\end{tikzpicture}
\end{document}
如果没有颜色,该示例可以简化:
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\path[every node/.style={circle, draw}]
(1, 0) node (A) {A}
(-1, 0) node (B) {B}
(0, 1) node (C) {C}
;
% \draw (A) -- (B) -- (C) -- (A);
\begin{scope}{even odd rule}
\clip
(current bounding box.south west)
rectangle (current bounding box.north east)
let
\p{A} := ($(A.north) - (A.center)$),
\p{B} := ($(B.north) - (B.center)$),
\p{C} := ($(C.north) - (C.center)$)
in
(A) circle[radius=\y{A}-.5\pgflinewidth]
(B) circle[radius=\y{B}-.5\pgflinewidth]
(C) circle[radius=\y{C}-.5\pgflinewidth]
;
\fill (A.center) -- (B.center) -- (C.center) -- cycle;
\end{scope}
\end{tikzpicture}
\end{document}
顺便说一句,cycle
需要的是点,而不是节点,请参阅\fill
上面示例中的命令。