TikZ,填充两个路径之间的空间

TikZ,填充两个路径之间的空间

我想填充中央半圆周和上方实黑线之间的空间。

\documentclass[tikz,border=1.5cm]{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\usetikzlibrary{calc,intersections,spath3,pgfplots.fillbetween}

\begin{document}
\begin{tikzpicture}
\coordinate (orig1) at (0,0);
\path [spath/save=orig1--A, line width=0.5mm, red] (orig1) arc[start angle=0, end angle=180, radius=5cm] coordinate (A);
\coordinate (orig2) at ($(orig1)!0.5!(A)$);
\path [spath/save=orig2--B, line width=0.5mm, red] (orig2) arc[start angle=0, end angle=180, radius=5cm] coordinate (B);
\path [draw,name intersections={of=orig1--A and orig2--B,by=E}];
\tikzset
{% spath3 operatios:
   spath/split at intersections={orig1--A}{orig2--B},
   spath/get components of={orig1--A}\Acpts,
   spath/get components of={orig2--B}\Bcpts
}
\draw[line width=0.5mm,gray,dashed,
      spath/use=\getComponentOf\Acpts{1},
      spath/use={\getComponentOf\Bcpts{2},weld}];
\draw[name path=smerge, line width=0.5mm,
      spath/use=\getComponentOf\Acpts{2},spath/use=\getComponentOf\Bcpts{1}];
      
\draw[line width=0.25mm,dotted] ([shift=({-1,0})]B)--([shift=({1,0})]orig1);

\path [draw, name path=s3, line width=0.5mm, gray] (orig2) arc[start angle=0, end angle=180, radius=2.5cm];

\tikzfillbetween[of=smerge and s3]{blue, opacity=0.2};

\end{tikzpicture}
\end{document}

因此,我将smerge包含两条实线和s3半圆周的路径称为。然后,我简单地尝试

\tikzfillbetween[of=smerge and s3]{blue, opacity=0.2};

但结果却是这样的:

在此处输入图片描述

所需区域已填充,但从半圆周的端点开始,也填充了一种三角形。我该如何避免这种情况并仅填充smerge和之间的空间s3

答案1

正如您当前正在使用的那样,spath3您可以将 3 个圆弧组合起来并“焊接”它们以形成一个实体(因此不需要fillbetween)。

像这样:

\documentclass[tikz,border=1.618mm]{standalone}
\usetikzlibrary{calc,intersections,spath3}

\begin{document}
\begin{tikzpicture}
\coordinate (orig1) at (0,0);
\path [spath/save=orig1--A] (orig1) arc[start angle=0, end angle=180, radius=5cm] coordinate (A);
\coordinate (orig2) at ($(orig1)!0.5!(A)$);
\path [spath/save=orig2--B] (orig2) arc[start angle=0, end angle=180, radius=5cm] coordinate (B);
\path [draw,name intersections={of=orig1--A and orig2--B,by=E}];
\node at (E)[circle,fill,red] {};
% creating the 'gray' arc and saving it (it will be gray)
\path [spath/save=s3] (orig2) arc[start angle=0, end angle=180, radius=2.5cm];
\tikzset
{% spath3 operatios:
   spath/split at intersections={orig1--A}{orig2--B},
   spath/get components of={orig1--A}\Acpts,
   spath/get components of={orig2--B}\Bcpts
}
\draw[line width=0.5mm,gray,dashed,
      spath/use=\getComponentOf\Acpts{1},
      spath/use={\getComponentOf\Bcpts{2},weld}];
\draw[name path=smerge, line width=0.5mm,
      spath/use=\getComponentOf\Acpts{2},spath/use=\getComponentOf\Bcpts{1}];
% drawing the saved gray path
\draw[spath/use=s3,line width=0.5mm, gray];
% combining various arcs in one solid
\fill[blue, opacity=0.2,
      spath/use={\getComponentOf\Acpts{2},reverse},
      spath/use={\getComponentOf\Bcpts{1},reverse,weld},
      spath/use={s3,weld}
     ];
\draw[line width=0.25mm,dotted] ([shift=({-1,0})]B)--([shift=({1,0})]orig1);
%\tikzfillbetween[of=smerge and s3]{blue, opacity=0.2};
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

这只是一个更简单的解决方案,没有不必要的花哨的包。

\documentclass[tikz, border=1cm]{standalone}
\begin{document}
\begin{tikzpicture}
\newcommand{\radius}{3cm}
\draw[gray, thick, dotted] (-2*\radius-1,0) -- (2*\radius+1,0);
\draw[gray, ultra thick, dotted] (0.5*\radius,0) arc[start angle=0, end angle=180, radius=\radius];
\draw[gray, ultra thick, dotted] (-0.5*\radius,0) arc[start angle=180, end angle=0, radius=\radius];
\draw[ultra thick, fill=blue!20] (0.5*\radius,0) arc[start angle=0, end angle=180, radius=0.5*\radius] arc[start angle=0, end angle=-60, radius=-\radius] arc[start angle=60, end angle=0, radius=\radius] -- cycle;
\end{tikzpicture}
\end{document}

带填充区域的圆弧

答案3

改编

  • \Bcpts{1}只需改变和的顺序\Acpts{2}
    \draw[name path=smerge, line width=0.5mm,
          spath/use=\getComponentOf\Bcpts{1},
          spath/use=\getComponentOf\Acpts{2}];
    
    意思是:先画右圆弧(B)的左半部分,再画左圆弧(A)的右半部分。这类问题大多可以通过调整顺序来改变。

代码

\documentclass[tikz,border=1.5cm]{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\usetikzlibrary{calc,intersections,spath3,pgfplots.fillbetween}

\begin{document}
\begin{tikzpicture}
\coordinate (orig1) at (0,0);
\path [spath/save=orig1--A, line width=0.5mm, red] (orig1) arc[start angle=0, end angle=180, radius=5cm] coordinate (A);
\coordinate (orig2) at ($(orig1)!0.5!(A)$);
\path [spath/save=orig2--B, line width=0.5mm, red] (orig2) arc[start angle=0, end angle=180, radius=5cm] coordinate (B);
\path [draw,name intersections={of=orig1--A and orig2--B,by=E}];
\tikzset
{% spath3 operatios:
   spath/split at intersections={orig1--A}{orig2--B},
   spath/get components of={orig1--A}\Acpts,
   spath/get components of={orig2--B}\Bcpts
}
\draw[line width=0.5mm,gray,dashed,
      spath/use=\getComponentOf\Acpts{1},
      spath/use={\getComponentOf\Bcpts{2},weld}];
\draw[name path=smerge, line width=0.5mm,
      spath/use=\getComponentOf\Bcpts{1},
      spath/use=\getComponentOf\Acpts{2}];
      
\draw[line width=0.25mm,dotted] ([shift=({-1,0})]B)--([shift=({1,0})]orig1);

\path [draw, name path=s3, line width=0.5mm, gray] (orig2) arc[start angle=0, end angle=180, radius=2.5cm];

\tikzfillbetween[of=smerge and s3]{blue, opacity=0.2};

\end{tikzpicture}
\end{document}

结果

在此处输入图片描述

答案4

在等待 Tikz 问题的答案时,可以lualatex使用内置的元帖子语言。

在此处输入图片描述

您需要使用以下命令进行编译lualatex

\documentclass[border=5mm]{standalone}
\usepackage{luamplib}
\begin{document}
\mplibtextextlabel{enable}
\begin{mplibcode}
input colorbrewer-rgb
beginfig(1);
  numeric r, s; r = 144; s = 60;
  path a, b, c, base, shark;
  a = halfcircle scaled 2r shifted (s * right);
  b = halfcircle scaled 2r shifted (s * left);
  c = halfcircle scaled abs (point 0 of b - point 4 of a);
  base = point 4 of b shifted 20 left -- point 0 of a shifted 20 right;
  shark = buildcycle(b, a, reverse c);

  fill shark withcolor Blues 8 3;
  fill c -- cycle withcolor Oranges 8 2;
  draw a dashed evenly;
  draw b dashed evenly;
  draw shark;
  draw base dashed withdots scaled 1/2;

endfig;
\end{mplibcode}
\end{document}

笔记

  • 彩色酿酒师MP 的颜色定义是完整 texmf 安装的一部分

  • 内置halfcircle路径有 5 个点,编号从 0(3 点钟位置)到 4(9 点钟位置)。

  • buildcycle会获取变量列表<path>,并尝试将它们组合成一条封闭路径。如果您按正确的顺序列出路径,并让它们在循环中都以相同的方向运行,则效果最佳。因此buildcycle(a, b, reverse c)

  • 您只能fill关闭路径,因此您必须执行fill c--cycle而不是仅仅执行fill c

  • 在 MP 中填充和绘制是单独的操作。

相关内容