我在给下图中两个红色圆环之间的区域上色时遇到了问题。有什么基本的办法吗?
谢谢
代码:
\begin{tikzpicture}[scale=2]
%contour
\path[name path=c1] (-2,0) arc[start angle=-180,end angle=180,x radius=1,y radius=.5];
\path[name path=c2] (0,0) arc[start angle=-180,end angle=180,x radius=1,y radius=.5];
\path[name path=r1] (-.5,-1)--(-.5,1);
\path[name path=r2] (.5,-1)--(.5,1);
\draw (-2,0) arc[start angle=-180,end angle=-60,x radius=1,y radius=.5];
\draw (-2,0) arc[start angle=180,end angle=60,x radius=1,y radius=.5];
\draw (2,0) arc[start angle=0,end angle=120,x radius=1,y radius=.5];
\draw (2,0) arc[start angle=0,end angle=-120,x radius=1,y radius=.5];
\draw[name intersections={of=c1 and r1, by={a1,b1}},name intersections={of=c2 and r2, by={a2,b2}}] [name path=arco](a1) ..controls(0,-.25) ..(a2)
(b1) ..controls(0,.25) ..(b2);
%rings
\path[name path=r1] (-.15,-1)--(-.15,1);
\draw[name path=anillo1,thick,draw=red,name intersections={of=arco and r1, by={arcor1}}] (arcor1) arc[start angle=270,end angle=90,x radius=.12,y radius=.315];
\path[name path=r1] (.15,-1)--(.15,1);
\filldraw[name path=anillo2,thick,draw=red,fill=red!20,name intersections={of=arco and r1, by={arcor1}}] (arcor1) arc[start angle=270,end angle=90,x radius=.12,y radius=.315];
\filldraw[thick,dashed,draw=red,fill=red!20,name intersections={of=arco and r1, by={arcor1}}] (arcor1) arc[start angle=-90,end angle=90,x radius=.08,y radius=.315];
%one hole
\path[xshift=10mm,name path=c] (-.5,.25) arc[start angle=-180,end angle=0,x radius=.5,y radius=.25];
\path[xshift=10mm,name path=r1] (-.45,-1)--(-.45,1);
\draw[xshift=10mm,name intersections={of=c and r1, by={r1c}}] (r1c) arc[start angle=-154,end angle=-26,x radius=.5,y radius=.25];
\path[xshift=10mm,name path=r2] (-.22,-1)--(-.22,1);
\draw[xshift=10mm,name intersections={of=c and r2, by={r2c}}] (r2c) arc[start angle=-160,end angle=-20,x radius=.22,y radius=-.10];
%another hole
\path[xshift=-10mm,name path=c] (-.5,.25) arc[start angle=-180,end angle=0,x radius=.5,y radius=.25];
\path[xshift=-10mm,name path=r1] (-.45,-1)--(-.45,1);
\draw[xshift=-10mm,name intersections={of=c and r1, by={r1c}}] (r1c) arc[start angle=-154,end angle=-26,x radius=.5,y radius=.25];
\path[xshift=-10mm,name path=r2] (-.22,-1)--(-.22,1);
\draw[xshift=-10mm,name intersections={of=c and r2, by={r2c}}] (r2c) arc[start angle=-160,end angle=-20,x radius=.22,y radius=-.10];
\end{tikzpicture}
答案1
我不知道这是否“基本”,但下面的代码显示了一种方法。它本质上是第二次绘制一些圆弧,并第二次绘制形状轮廓的一部分以用作剪切路径。我重命名了其中一个交叉点坐标,并在其中一个圆弧的末尾添加了一个新坐标。代码中有一些注释,如果有任何不清楚的地方,只需询问即可。
\documentclass[border=5mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{intersections, backgrounds}
\begin{document}
\begin{tikzpicture}[scale=2]
%contour
\path[name path=c1] (-2,0) arc[start angle=-180,end angle=180,x radius=1,y radius=.5];
\path[name path=c2] (0,0) arc[start angle=-180,end angle=180,x radius=1,y radius=.5];
\path[name path=r1] (-.5,-1)--(-.5,1);
\path[name path=r2] (.5,-1)--(.5,1);
\draw (-2,0) arc[start angle=-180,end angle=-60,x radius=1,y radius=.5];
\draw (-2,0) arc[start angle=180,end angle=60,x radius=1,y radius=.5];
\draw (2,0) arc[start angle=0,end angle=120,x radius=1,y radius=.5];
\draw (2,0) arc[start angle=0,end angle=-120,x radius=1,y radius=.5];
\draw[name intersections={of=c1 and r1, by={a1,b1}},name intersections={of=c2 and r2, by={a2,b2}}] [name path=arco](a1) ..controls(0,-.25) ..(a2)
(b1) ..controls(0,.25) ..(b2);
%rings
\path[name path=r1] (-.15,-1)--(-.15,1);
% change the coordinate name of the intersection here, because you need it later
\draw[name path=anillo1,thick,draw=red,name intersections={of=arco and r1, by={arcor0}}] (arcor0) arc[start angle=270,end angle=90,x radius=.12,y radius=.315];
\path[name path=r1] (.15,-1)--(.15,1);
% I removed all filling in the next to paths
\draw[name path=anillo2,thick,draw=red,name intersections={of=arco and r1, by={arcor1}}] (arcor1) arc[start angle=270,end angle=90,x radius=.12,y radius=.315];
\draw[thick,dashed,draw=red,name intersections={of=arco and r1, by={arcor1}}] (arcor1) arc[start angle=-90,end angle=90,x radius=.08,y radius=.315]
coordinate(arcor2); % add a new coordinate at the end of this arc
% here we do the filling
% on the background layer so it doesn't cover the outline
\begin{scope}[on background layer]
% use the coordinates defined above, and draw an identical path
% just with the order of the b-coordinates swapped so you go around the corners in order.
\clip (a1) ..controls(0,-.25) ..(a2) -- (b2) ..controls(0,.25) ..(b1) -- cycle;
% fill using the same arcs as above, just one of them drawn in the opposite direction
\fill [red!20]
(arcor0) arc[start angle=270,end angle=90,x radius=.12,y radius=.315] --
(arcor2) arc[start angle=90,end angle=-90,x radius=.08,y radius=.315] -- cycle;
\end{scope}
%one hole
\path[xshift=10mm,name path=c] (-.5,.25) arc[start angle=-180,end angle=0,x radius=.5,y radius=.25];
\path[xshift=10mm,name path=r1] (-.45,-1)--(-.45,1);
\draw[xshift=10mm,name intersections={of=c and r1, by={r1c}}] (r1c) arc[start angle=-154,end angle=-26,x radius=.5,y radius=.25];
\path[xshift=10mm,name path=r2] (-.22,-1)--(-.22,1);
\draw[xshift=10mm,name intersections={of=c and r2, by={r2c}}] (r2c) arc[start angle=-160,end angle=-20,x radius=.22,y radius=-.10];
%another hole
\path[xshift=-10mm,name path=c] (-.5,.25) arc[start angle=-180,end angle=0,x radius=.5,y radius=.25];
\path[xshift=-10mm,name path=r1] (-.45,-1)--(-.45,1);
\draw[xshift=-10mm,name intersections={of=c and r1, by={r1c}}] (r1c) arc[start angle=-154,end angle=-26,x radius=.5,y radius=.25];
\path[xshift=-10mm,name path=r2] (-.22,-1)--(-.22,1);
\draw[xshift=-10mm,name intersections={of=c and r2, by={r2c}}] (r2c) arc[start angle=-160,end angle=-20,x radius=.22,y radius=-.10];
\end{tikzpicture}
\end{document}
答案2
至少对我来说,您的代码很难理解。
这是一个渐近线解!
unitsize(2cm); // for scale=2
path c1=scale(1,.5)*arc((-2,0),1,0,360),
c2=scale(1,.5)*arc((0,0),1,0,360),
r1=(-.5,-1)--(-.5,1),
r2=(-1.5,-1)--(-1.5,1);
pair p[]=intersectionpoints(c1,r2),
q[]=intersectionpoints(c2,r1);
path ahihi=p[0]{dir(-20)}..(-1,.3)..{dir(20)}q[0],
dongoc=p[1]{dir(20)}..(-1,-.3)..{dir(-20)}q[1];
pair M=relpoint(ahihi,.3),N=relpoint(ahihi,.7),
P=relpoint(dongoc,.3),Q=relpoint(dongoc,.7);
//--------------
path EllipseArc(pair c, real a, real b, real Eangle1, real Eangle2, bool direction=CCW)
{
return shift(c)*scale(a,b)*arc((0,0),1,Eangle1,Eangle2,direction);
}
//--------------
pair MidPoint=(M+P)/2,MidPoint2=(N+Q)/2;
path ahihidongoc=subpath(ahihi,reltime(ahihi,.3),reltime(ahihi,.7))..
reverse(EllipseArc(MidPoint2,0.1,abs(MidPoint2-N),-90,90))..
subpath(dongoc,reltime(ahihi,.7),reltime(ahihi,.3))..
reverse(EllipseArc(MidPoint,0.1,abs(MidPoint-M),90,270))--cycle;
fill(ahihidongoc,red+opacity(.5));
draw(EllipseArc(MidPoint,0.1,abs(MidPoint-M),90,270));
draw(EllipseArc(MidPoint2,0.1,abs(MidPoint2-N),90,270));
draw(EllipseArc(MidPoint2,0.1,abs(MidPoint2-N),-90,90),dashed);
draw(EllipseArc((-2,0),.2,.1,10,170)^^EllipseArc((0,0),.2,.1,10,170));
draw(EllipseArc((-2,0)+(0,.2),.5,.2,-160,-20)^^EllipseArc((0,0)+(0,.2),.5,.2,-160,-20));
draw(scale(1,.5)*(reverse(arc((-2,0),1,-180,-60))..arc((-2,0),1,180,60))..
ahihi..
scale(1,.5)*(reverse(arc((0,0),1,0,120))..arc((0,0),1,0,-120))..reverse(dongoc));
shipout(bbox(3mm,white));