![关于 pgf 绘图的一个问题:圆形图形](https://linux22.com/image/307318/%E5%85%B3%E4%BA%8E%20pgf%20%E7%BB%98%E5%9B%BE%E7%9A%84%E4%B8%80%E4%B8%AA%E9%97%AE%E9%A2%98%EF%BC%9A%E5%9C%86%E5%BD%A2%E5%9B%BE%E5%BD%A2.png)
例如,如何在圆形上画四个矩形,每个矩形分别压住另一个矩形?看我的图。我无法绘制左上角红色矩形后面的紫色矩形。如果你知道方法,请告诉我。
而且我想用球的颜色给形状着色,所以我不能只在左上角画一个小矩形然后将其设置为紫色。
答案1
在……的帮助下保罗·加博瑞特invclip
可以写
\documentclass[tikz,border=2mm]{standalone}
\begin{document}
\tikzset{invclip/.style={clip,insert path={{[reset cm]
(-\maxdimen,-\maxdimen) rectangle (\maxdimen,\maxdimen)
}}}}
\begin{tikzpicture}
\fill[red] (-1,-1) rectangle (1,1);
\fill[yellow, shift={(1.5,1.5)}] (-1,-1) rectangle (1,1);
\fill[blue, shift={(3,0)}] (-1,-1) rectangle (1,1);
\begin{pgfinterruptboundingbox}
\path[invclip] (-1,-1)-|(1,1);
\end{pgfinterruptboundingbox}
\fill[purple, shift={(1.5,-1.5)}] (-1,-1) rectangle (1,1);
\end{tikzpicture}
\end{document}
答案2
这里用 MetaPost 完成了,同样通过裁剪和恢复交叉部分。将与 LuaLaTeX 一起运行。
我使用了 MetaPost 的 Metafun 格式提供的阴影功能。我选择了线性阴影,因为它似乎更适合矩形。
\documentclass{standalone}
\usepackage{luamplib}
\mplibsetformat{metafun}
\begin{document}
\begin{mplibcode}
input mpcolornames;
vardef myshade(expr debut, fin, mycolor) =
define_linear_shade(debut, fin, .1[white, mycolor], mycolor)
enddef;
def fillit_withshade(expr pat, mycolor) =
fill pat withshade myshade(ulcorner pat, lrcorner pat, mycolor);
enddef;
beginfig(1);
numeric a, b; a = 2.75cm; b = 3cm;
picture intersect; intersect = nullpicture;
path rect[], fullrect, bb_intersect; fullrect = fullsquare xyscaled (a, b);
rect1 = fullrect xshifted .75a; rect2 = fullrect yshifted .75b;
rect3 = fullrect xshifted -.75a; rect4 = fullrect yshifted -.75b;
bb_intersect = buildcycle(rect4, subpath (3, 5) of rect1)
leftenlarged .5bp bottomenlarged .5bp;
% extends the intersection to its lower-left boundary (.5bp is the default pen width)
fillit_withshade(rect4, Purple);
clip currentpicture to bb_intersect; addto intersect also currentpicture;
fillit_withshade(rect4, Purple); fillit_withshade(rect3, red);
fillit_withshade(rect2, yellow); fillit_withshade(rect1, blue);
draw intersect;
endfig;
\end{mplibcode}
\end{document}
答案3
您可以简单地使用 再次绘制矩形的一部分clip
。
\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}
\shade[ball color=red] (-1,-1) rectangle (1,1);
\shade[ball color=yellow] (0.5,0.5) rectangle (2.5,2.5);
\shade[ball color=blue] (2,-1) rectangle (4,1);
\shade[ball color=purple] (0.5,-2.5) rectangle (2.5,-0.5);
\clip (-1,-1) rectangle (1,0); %clip the the top half away
\shade[ball color=red] (-1,-1) rectangle (1,1); %and draw the red rectangle again
\end{tikzpicture}
\end{document}