我已经发现如何以特定节点为中心画圆弧。所以我画了由 1 到 4 个扇区组成的圆圈。
但是,当我用 3 个扇区绘制时,即使代码相似,我也会得到额外的边距(右侧和底部)。
此件文物从何而来?
\documentclass{scrartcl}
\usepackage{tikz}
\begin{document}
\fbox{\begin{tikzpicture}%
\fill [green] (0,0) ++ (0:1) arc (0:90:1) -- (0,0) -- cycle;
\fill [yellow] (0,0) ++ (-90:1) arc (-90:0:1) -- (0,0) -- cycle;
\fill [red] (0,0) ++ (-180:1) arc (-180:-90:1) -- (0,0) -- cycle;
\fill [blue] (0,0) ++ (90:1) arc (90:180:1) -- (0,0) -- cycle;
\end{tikzpicture}}
\fbox{\begin{tikzpicture}%
\fill [orange] (0,0) ++ (-30:1) arc (-30:90:1) -- (0,0) -- cycle;
\fill [pink] (0,0) ++ (-150:1) arc (-150:-30:1) -- (0,0) -- cycle;
\fill [gray] (0,0) ++ (90:1) arc (90:210:1) -- (0,0) -- cycle;
\end{tikzpicture}}
\fbox{\begin{tikzpicture}%
\fill [purple] (0,0) ++ (-90:1) arc (-90:90:1) -- (0,0) -- cycle;
\fill [green] (0,0) ++ (90:1) arc (90:270:1) -- (0,0) -- cycle;
\end{tikzpicture}}
\fbox{\begin{tikzpicture}%
\fill [brown] (0,0) circle (1);
\end{tikzpicture}}
\end{document}
答案1
这与使用贝塞尔曲线构造弧线的方式有关pgf
。绘制算法涉及绘制尽可能多的弧线,即连续旋转 90 度的弧线以及剩余的较小弧线。
可以使用装饰绘制每个贝塞尔曲线的多边形来检查结果show path construction
。可以看出,根据起始和终止角度,由于 90 度弧的旋转,贝塞尔曲线的控制点可能会延伸到圆的“自然”边界框之外。
\documentclass[varwidth,border=5]{standalone}
\usepackage{tikz}
\usetikzlibrary{decorations.pathreplacing}
\tikzset{
show curve polygon/.style={postaction={
decoration={
show path construction,
curveto code={
\draw [black,dotted]
(\tikzinputsegmentfirst) -- (\tikzinputsegmentsupporta) --
(\tikzinputsegmentsupportb) -- (\tikzinputsegmentlast) -- cycle;
}
},decorate
}},
every picture/.style={baseline=(0:0), >=stealth}
}
\begin{document}
\fbox{\begin{tikzpicture}%
\fill [green, show curve polygon] (0,0) ++ (0:1) arc (0:90:1) -- (0,0) -- cycle;
\fill [yellow, show curve polygon] (0,0) ++ (-90:1) arc (-90:0:1) -- (0,0) -- cycle;
\fill [red, show curve polygon] (0,0) ++ (-180:1) arc (-180:-90:1) -- (0,0) -- cycle;
\fill [blue, show curve polygon] (0,0) ++ (90:1) arc (90:180:1) -- (0,0) -- cycle;
\end{tikzpicture}}
\fbox{\begin{tikzpicture}%
\fill [orange, show curve polygon] (0,0) ++ (-30:1) arc (-30:90:1) -- (0,0) -- cycle;
\fill [pink, show curve polygon] (0,0) ++ (-150:1) arc (-150:-30:1) -- (0,0) -- cycle;
\fill [gray, show curve polygon] (0,0) ++ (90:1) arc (90:210:1) -- (0,0) -- cycle;
\end{tikzpicture}}
\fbox{\begin{tikzpicture}%
\fill [purple, show curve polygon] (0,0) ++ (-90:1) arc (-90:90:1) -- (0,0) -- cycle;
\fill [green, show curve polygon
] (0,0) ++ (90:1) arc (90:270:1) -- (0,0) -- cycle;
\end{tikzpicture}}
\fbox{\begin{tikzpicture}%
\fill [brown, show curve polygon] (0,0) circle (1);
\end{tikzpicture}}
\end{document}
图片的边界框被放大以包含多边形中的所有点,因为这可以保证图片足够大以包含任意贝塞尔曲线。另一种方法(在绘制曲线时调整曲线的边界框以考虑所绘制曲线的范围)在计算上是昂贵的(至少可以这么说),因此解决这个问题的一种方法是使用路径来\useasboundingbox
设置所使用的实际边界框tikzpicture
。
答案2
答案3
为了便于比较,下面是同一个图,元帖子和luamplib。这里没有边界框问题,但您必须使用它来处理它lualatex
。
\documentclass[border=5mm]{standalone}
\usepackage{luamplib}
\everymplib{ verbatimtex \leavevmode etex; beginfig(0); }
\everyendmplib{ endfig; }
\mplibcodeinherit{enable}
\begin{document}
\fbox{\begin{mplibcode}
path C, segment; C = fullcircle scaled 2cm;
segment := subpath (0,2) of C -- origin -- cycle;
fill segment rotated 0 withcolor green;
fill segment rotated 90 withcolor blue;
fill segment rotated 180 withcolor red;
fill segment rotated 270 withcolor red+green;
\end{mplibcode}}
\fbox{\begin{mplibcode}
segment := subpath(0,8/3) of C -- origin -- cycle;
fill segment rotated 90 withcolor 1/2 white;
fill segment rotated 210 withcolor 5/6[red,white];
fill segment rotated 330 withcolor red + 1/2 green;
\end{mplibcode}}
\fbox{\begin{mplibcode}
segment := subpath(0,4) of C -- origin -- cycle;
fill segment rotated 90 withcolor green;
fill segment rotated 270 withcolor 1/3[red,blue];
\end{mplibcode}}
\fbox{\begin{mplibcode}
fill C withcolor 1/3[red,green];
\end{mplibcode}}
\end{document}