Fill a triangle with nodes on the corners
Nodes are extended objects, that's why cycle
does not work, but it works once you specify the anchors, as pointed out by 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}
Just for fun:
- Clipping instead of filling the circle nodes with white.
- Blue circle lines.
- Red connection lines.
- Triangle filled with green.
Issues:
The triangle can be filled with a simple:
\fill (A.center) -- (B.center) -- (C.center) -- cycle;
The clipping region is defined by the full current drawing area minus the circle areas using the even odd filling rule.
The radii can be calculated via TikZ library
calc
.There can be background pixels between the circle lines and the filled area due to rounding issues.
Therefore, the circle raddii are made a little smaller, the effect of using
outer sep=0pt
for the nodes. Butouter sep=0pt
cannot be used because of the connection lines that would stick into the circle lines.The filled triangle is put on the background layer, thus the overlapping part is covered by the circle lines.
Full example:
\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}
Without colors, the example can be simplified:
\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}
BTW, cycle
needs points, not nodes, see the \fill
command in the examples above.