tikz:在给定坐标之间绘制一条路径

tikz:在给定坐标之间绘制一条路径

我的应用程序中有一条路径,我想将其拆分成更小的路径。我想在这些路径上附加标签来绘制它们。以下图为例:

我想要实现的目标

上方的圆弧被水平线和从圆心延伸出的两条线分成三段。我画出了路径的所有三部分,并给它们贴上标签。

现在,我这样做的方式有点繁琐。对于每一段,我都必须手动重建路径:我必须指定路径在每个交叉点处的延续方式。在这种情况下,交叉点很简单,延续性也很明显,但如果路径很复杂,这会变得更加困难。

我想要做的事情如下:

  1. 定义一条完整路径,在本例中是从左侧一直到右侧的上部弧线。此路径可以任意复杂。
  2. 通过与其他路径相交来找到路径中的坐标。
  3. 发出命令:绘制从第一个交叉点坐标到第二个交叉点坐标的路径,并为该段指定标签。绘制从第二个交叉点到第三个交叉点的路径,等等。

我怎样才能实现这个目标?

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc,shapes.multipart,fit,shapes,calc,backgrounds,decorations.pathreplacing,decorations.markings,intersections}

\begin{document}

\begin{tikzpicture}[scale=2]
\path[name path=t] (1,0) to[out=90,in=180] (2,1) to[out=0,in=90] (3,0); % top arc
\path [name path=ml] (2,0) -- (1,1);
\path [name intersections={of=ml and t}];
\coordinate (A) at (intersection-1);
\path [name path=l] (2,0) -- (3,1);
\path [name intersections={of=l and t}];
\coordinate (B) at (intersection-1);

\draw[thick,draw=blue,] (3,0) to[out=90,in=315] node[midway,above right] {$c$} (B) to[out=135,in=45] node[midway,above] {$b$} (A) to[out=225,in=90] node[midway,above left] {$a$} (1,0);
\draw[thick,draw=blue,] (1,0) -- (0,0) node[midway,above] {};

\draw[thick,draw=blue,] (1,0) to[out=-90,in=180] node[midway,below left] {} (2,-1);
\draw[thick,draw=blue,] (2,-1) to[out=0,in=-90] node[midway,below right] {} (3,0);
\draw[thick,draw=blue,] (4,0) -- (3,0) node[midway,above] {};

\draw[thick,draw=blue,] (2,0) -- node[midway,right] {} (2,-1);
\draw[thick,draw=blue,] (A) -- node[midway,below left] {} (2,0);
\draw[thick,draw=blue,] (2,0) -- node[midway,below right] {} (B);
\end{tikzpicture}
\end{document}

答案1

这是一个非常晚的答案,如果我没记错的话,其中使用的工具在你问这个问题的时候还没有。我发现这个问题此查询,这个答案的目的只是提到现在有相当方便的方法来做你建议的事情。新工具是 pgfplots 库 fillbetween,它允许您将路径拆分成段,您可以重新绘制、填充或以其他方式使用它们。

\documentclass[tikz,border=3.14mm]{standalone}
\usepackage{pgfplots}
\usetikzlibrary{calc,shapes.multipart,fit,shapes,calc,backgrounds,decorations.pathreplacing,decorations.markings,intersections}
\usepgfplotslibrary{fillbetween}
\begin{document}

\begin{tikzpicture}[scale=2]
\path[name path=t] (1,0) to[out=90,in=180] (2,1) to[out=0,in=90] (3,0); % top arc
\path [name path=ml] (2,0) -- (1,1);
\path [name intersections={of=ml and t}];
\coordinate (A) at (intersection-1);
\path [name path=l] (2,0) -- (3,1);
\path [name intersections={of=l and t}];
\coordinate (B) at (intersection-1);


\draw[thick,draw=blue,] (3,0) to[out=90,in=315] node[midway,above right] {$c$} (B) to[out=135,in=45] node[midway,above] {$b$} (A) to[out=225,in=90] node[midway,above left] {$a$} (1,0);
\draw[thick,draw=blue,] (1,0) -- (0,0) node[midway,above] {};

\draw[thick,draw=blue,] (1,0) to[out=-90,in=180] node[midway,below left] {} (2,-1);
\draw[thick,draw=blue,] (2,-1) to[out=0,in=-90] node[midway,below right] {} (3,0);
\draw[thick,draw=blue,] (4,0) -- (3,0) node[midway,above] {};

\draw[thick,draw=blue,] (2,0) -- node[midway,right] {} (2,-1);
\draw[thick,draw=blue,] (A) -- node[midway,below left] {} (2,0);
\draw[thick,draw=blue,] (2,0) -- node[midway,below right] {} (B);

% draw the left segment
\draw[thick,red,
        intersection segments={of=t and ml,sequence={L1}}];
% construct an auxiliary path
\path[name path=aux,%draw=green,thick,
        intersection segments={of=t and ml,sequence={L0}}];
% draw the middle segment
\draw[thick,green!60!black,
        intersection segments={of=aux and l,sequence={L1}}];
% draw the right segment
\draw[thick,purple,
        intersection segments={of=aux and l,sequence={L0}}];
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

我意识到这是一个很晚的答案,但这看起来是一个很好的问题,可以展示最新的(截至 2021 年 2 月)更新spath3该包引入了许多用于分割路径和使用其组件的例程。

\documentclass{article}
%\url{https://tex.stackexchange.com/q/211995/86}
\usepackage{tikz}
\usetikzlibrary{spath3, intersections}

\begin{document}

\begin{tikzpicture}
\begin{scope}[scale=2]
% Save the upper part of the arc
\path[spath/save=t] (1,0) to[out=90,in=180] (2,1) to[out=0,in=90] (3,0);
% These two are used to find the splitting points
\path [spath/save=ml] (2,0) -- (1,1);
\path [spath/save=l] (2,0) -- (3,1);

% Split the arc at the points where it intersects the
% other two paths, and then separate it into components
\tikzset{
  spath/split at intersections with={t}{ml},
  spath/split at intersections with={t}{l},
  spath/get components of={t}\cpts
}

% Draw the Y-shaed central bit, using a coordinate system
% defined from the components to get their endpoints
\draw[thick,draw=blue] (spath cs:\getComponentOf\cpts{1} 1) -- (2,0) -- (spath cs:\getComponentOf\cpts{3} 0) (2,0) -- (2,-1);

% Draw the left-hand part (in red), reversing the initial
% arc so that the join is correct
\draw[thick,red, spath/restore reverse=\getComponentOf\cpts{1}] -- (0,0);
% Label it half way along
\node[above left,spath/transform to={\getComponentOf\cpts{1}}{.5}] {\(a\)};

% Draw the middle part
\draw[thick,green, spath/restore=\getComponentOf\cpts{2}];
% And label it
\node[above,spath/transform to={\getComponentOf\cpts{2}}{.5}] {\(b\)};

% Draw the right-hand part
\draw[thick,orange, spath/restore=\getComponentOf\cpts{3}] -- (4,0);
% And label it
\node[above right,spath/transform to={\getComponentOf\cpts{3}}{.5}] {\(c\)};


\draw[thick,draw=blue] (1,0) to[out=-90,in=180] (2,-1) to[out=0,in=-90] (3,0);

\end{scope}
\end{tikzpicture}
\end{document}

渲染分割路径

相关内容