我需要一個以 為中心的半圓節點chord center
。定位是透過選件實現的anchor
,但外部線路仍然定向center
。也可以直接線嗎chord center
?
微量元素
\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\usetikzlibrary{shapes.geometric}
\begin{tikzpicture}
\node [semicircle,draw,shape border rotate=180,anchor=chord center,outer sep=0,inner sep=0.2cm] (a) at (1,1) {};
\draw (0,0) -- (a);
\draw[densely dashed] (0,0) -- (1,1);
\end{tikzpicture}
\end{document}
答案1
沒有定義一個新的形狀,這是一個欺騙版本,周圍有另一個圓形節點。
為此,我將把錨點添加semicircle center
到始終位於和弦中間的形狀中,獨立於外弦。
這允許您將形狀放置在semicircle center
不更改外部 sep 的情況下。
然後我們再增加另一個circle
與半圓具有相同(外)半徑的節點。這與半圓同名,但增加了一個'
。
從技術上講,我們可以從形狀本身提取半徑,但through
庫使這變得非常容易。 (它測量at
和點之間的距離through
,並將其設定為minimum size
節點的 。)
程式碼
\documentclass[tikz]{standalone}
\usetikzlibrary{shapes.geometric, through}
\makeatletter
\pgfutil@namedef{pgf@anchor@semicircle@semicircle center}{%
\pgfpointadd{\pgf@sh@reanchor{semicircle}{chord center}}{%
\installsemicircleparameters\pgfpointpolar{\rotate+90}{\outersep}}}
\makeatother
\tikzset{
circle around semi/.style={
append after command={[every node/.code=]%
node[at=(\tikzlastnode.semicircle center),
circle through=(\tikzlastnode.apex), overlay,
anchor=center, name=\tikzlastnode', inner sep=+0pt, outer sep=+0pt,
path only]{}}}}
\begin{document}
\begin{tikzpicture}[ultra thick]
\node [
semicircle, circle around semi, draw,
shape border rotate=180,
anchor=semicircle center,
inner sep=0.2cm
] (a) at (1,1) {};
\draw[red] (0,0) -- (a');
\end{tikzpicture}
\tikz\draw[ultra thick]
(0,0) .. controls +(30:1) and +(135:2) .. (2,2)
foreach[count=\i from 0, evaluate=\i as \c using \i/4*100]
\pos in {.125, .333, .5, .75, .95}{
node[fill=red!\c!blue, draw=blue!\c!red, fill, thin, sloped,
semicircle, circle around semi, pos=\pos, anchor=semicircle center](@){}
(@') foreach \ang in {0,15,...,359}{edge[thin] ++(\ang:.3)}
};
\end{document}
輸出
答案2
也許剪切可以工作(但可能不適用於您的具體情況,並且將文字定位在半圓內需要調整):
\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\begin{scope}
\clip (0.5,1) rectangle +(1,-1);
\node [circle, draw, outer sep=0, inner sep=0.25cm] (a) at (1,1) {};
\end{scope}
\draw[line cap=rect] (a.east) -- (a.west);
\draw[red] (0,0) -- (a);
\end{tikzpicture}
\end{document}
答案3
根據我的評論,我們可以解決這個問題\fill
程式碼
\documentclass[border=3mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric}
\colorlet{pagebackground}{yellow!10}
\pagecolor{pagebackground}
\begin{document}
\begin{tikzpicture}
\coordinate (a) at (1,1);
\draw [red](0,0) -- (a);
\node [semicircle,draw,shape border rotate=180,anchor=chord center,outer sep=0cm,inner sep=0.2cm,fill=pagebackground] at (a) {};
\end{tikzpicture}
\end{document}
答案4
我總是想讓程式碼盡可能簡單(KISS),並且我理解它並且能夠實際維護它。因此,根據 @Qrrbrbirlbel 的解決方案,我將使用以下程式碼:
\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\usetikzlibrary{shapes.geometric}
\newcommand{\semicircleup}[4]{%
\node[circle,outer sep=0pt,inner sep=0,minimum width=2*#3] (#1) at #2 {};
\node[semicircle,shape border rotate=180,anchor=chord center,outer sep=0pt,inner sep=0,minimum width=2*#3,#4] at (#1) {};
}
\begin{tikzpicture}
\semicircleup{a}{(1,1)}{0.5cm}{draw,fill=white}
\draw (0,0) -- (a);
\end{tikzpicture}
\end{document}
它繪製一個命名的不可見圓形節點(用於裁剪線條)和一個具有相同最小寬度(帶有inner sep=0
)的未命名可見半圓節點。
額外的優點(這對我來說很重要)是不使用額外的庫。
但我仍然會接受@Qrrbrbirlbel 的回答,作為對他想法的讚揚。