前段时间我想创建一个半圆节点,我使用了下面的解决方案。但是,这个解决方案有一个缺点,如果你填充节点,整个圆都会被填充,而不仅仅是一半。是否可以确保只填充绘制的节点的一半?
梅威瑟:
\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}
答案1
(总结评论。)虽然这并不能回答你的节点形状定义出了什么问题,但是你正在重新发明轮子,因为库shapes.geometric
已经定义了一个semicircle
形状。
该形状的默认方向是圆弧在上,但您可以使用 将其转向另一个方向shape border rotate=180
。默认锚点是center
,位于半圆内部。要使用直径的中点作为锚点,请设置anchor=chord center
。
\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}