4

Some time ago I wanted to create a half-circle node and I used the solution below. However, this solution has one drawback that if you fill the node, the whole circle is filled, not only a half. Is it possible to make sure that only half of the node that is drawn is filled?

MWE:

\documentclass{minimal}
\usepackage{tikz}

\begin{document}

\makeatletter
\tikzset{arc style/.initial={}}
\pgfdeclareshape{half circle}{
  \inheritsavedanchors[from=circle]
  \inheritanchorborder[from=circle]

  \inheritanchor[from=circle]{center}
  \inheritanchor[from=circle]{south}
  \inheritanchor[from=circle]{west}
  \inheritanchor[from=circle]{north}
  \inheritanchor[from=circle]{east}

  \inheritbackgroundpath[from=circle]

  \beforebackgroundpath{
    \pgfkeys{/tikz/arc style/.get=\tmp}
    \expandafter\tikzset\expandafter{\tmp}
    \tikz@options

    \radius \pgf@xa=\pgf@x
    \centerpoint \pgf@xb=\pgf@x \pgf@yb=\pgf@y

    \advance\pgf@xb by \pgf@xa
    \pgfpathmoveto{\pgfpoint{\pgf@xb}{\pgf@yb}}
    \pgfpatharc{0}{-180}{\pgf@xa}

    \pgfusepath{draw}
  }
}
\makeatother

\begin{tikzpicture}

\fill[fill=black!10] (0,0) rectangle (6,4);
\node[half circle,inner sep=9pt] (a) at (2,2) {};
\node[half circle,inner sep=9pt,fill=white] (b) at (4,2) {};

\end{tikzpicture}

\end{document}
6
  • There is already a semicircle shape in the shapes.geometric library. (Which doesn't answer your question, but might be of interest.) Jan 20, 2022 at 20:10
  • @TorbjørnT. That would do if I can turn semicircle for 180 degrees.
    – Pygmalion
    Jan 20, 2022 at 20:20
  • shape border rotate=180 Jan 20, 2022 at 20:20
  • @TorbjørnT. It works as a quick fix. The only problem is that the specified node position is not the centre of the half-circle, so I have to fix that manually.
    – Pygmalion
    Jan 20, 2022 at 20:30
  • There are lots of predefined anchors for that shape, see the figure in the TikZ manual for an overview. The default anchor is center, you might be after anchor=north, which is the center of the full circle. Jan 20, 2022 at 20:33

1 Answer 1

10

(Summing up comments.) While this doesn't answer the question of what is wrong with your node shape definition, you are re-inventing the wheel a bit, as the shapes.geometric library already defines a semicircle shape.

The default orientation of that shape is with the arc on top, but you can turn it the other way with shape border rotate=180. And the default anchor is center, which is in the interior of the half circle. To use the midpoint of the diameter as anchor, set anchor=chord center.

enter image description here

\documentclass[border=5mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric}
\tikzset{
  half circle/.style={
      semicircle,
      shape border rotate=180,
      anchor=chord center,
      minimum size=5mm
      }
}
\begin{document}
\begin{tikzpicture}
    \node [half circle, draw=blue, fill=red] at (1,1) {};
    \fill (1,1) circle[radius=1pt];
\end{tikzpicture}
\end{document}
2
  • It is long time but I found a problem with your solution. Drawing from outside to the node is not going to point to the center of the circle but to the centre of the semicircle. See example \node [half circle, draw] (a) at (1,1) {}; \draw (0,0) -- (a); \draw (0,0) -- (1,1);
    – Pygmalion
    Mar 15 at 11:30
  • @Pygmalion Try removing anchor=chord center. Mar 15 at 18:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .