如何在 tikz 中使用此代码绘制一个圆圈:
\documentclass{article}
\usepackage{tikz,pgfplots}
%\usepackage[x11names]{xcolor}
\usepackage{tikz}
\usetikzlibrary{intersections}
\pgfdeclarelayer{bg} % declare background
\pgfsetlayers{bg,main} % order of layers (main = standard layer)
\pgfplotsset{compat=1.13}
\usepackage{amsmath}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}
\draw[black, line width = 0.50mm] plot[smooth,domain=0:2] (\x, {sqrt(1-(x-1)^2)});
\end{tikzpicture}
\end{document}
答案1
呃,那是半个圆圈。你的目的到底是什么?
绘制函数pgfplots
比基本tikz
\documentclass{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
axis equal
]
\addplot [smooth,domain=0:2,samples=101] {sqrt(1-(\x-1)^2)};
\addplot [smooth,domain=0:2,samples=101] {-sqrt(1-(\x-1)^2)};
\end{axis}
\end{tikzpicture}
\end{document}
答案2
您可能希望圆是平滑的,因此可能希望一次性绘制圆。这可以通过使用从 0 到 2 再返回的函数来实现,
(\x<2 ? \x : 4-\x)
然后你需要用 来决定你是在上分支还是下分支sign(2-\x)
,这样 plot 命令就变成了
\draw[black, line width = 0.50mm]
plot[smooth cycle,domain=0:4,samples=101]
({(\x<2 ? \x : 4-\x)}, {sign(2-\x)*sqrt(1-((\x<2 ? \x : 4-\x)-1)^2)});
这比标准的圆形路径需要付出的努力要多得多。
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[nodes={text width=2.5cm,align=center}]
\begin{scope}[local bounding box=A]
\draw[black, line width = 0.50mm]
plot[smooth cycle,domain=0:4,samples=101]
({(\x<2 ? \x : 4-\x)}, {sign(2-\x)*sqrt(1-((\x<2 ? \x : 4-\x)-1)^2)});
\end{scope}
\path (A.south) node[below] {engineering a function};
%
\begin{scope}[xshift=3cm,local bounding box=B,
declare function={xcheat(\x)=(\x<2 ? \x : 4-\x);}]
\draw[black, line width = 0.50mm]
plot[smooth cycle,domain=0:4,samples=101]
({xcheat(\x)}, {sign(2-\x)*sqrt(1-(xcheat(\x)-1)^2)});
\end{scope}
\path (B.south) node[below] {engineering and declaring a function};
%
\begin{scope}[xshift=6cm,local bounding box=C]
\draw[black, line width = 0.50mm] (1,0) circle[radius=1cm];
\end{scope}
\path (C.south) node[below] {use normal circle};
\end{tikzpicture}
\end{document}