我有两条路径,用虚线绘制(如图所示),它们在某个点 C 相交。
现在我需要用实线画出这两条路径的合成路径。合成路径应沿着原始路径直到它们相交。因此,在图中,合成路径将沿着路径 1 从 A 到 C,然后应沿着路径 2 到达 B。
路径不必是 MWE 中给出的直线。因此,我正在寻找一种方法来绘制从两条路径的交点到这些路径的原点的路径。
\documentclass[margin=1cm]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc,intersections}
\begin{document}
\begin{tikzpicture}
\draw[dotted, name path=p1] (-2,0)node(A){A} -- ++(0,1) -- ++(45:5cm)node(A1){A'};
\draw[dotted, name path=p2] (2,0)node(B){B} -- ++(0,1) -- ++(135:5cm)node(B1){B'};
\path [name intersections={of = p1 and p2,by=C}];
\draw (C)node[]{C};
\end{tikzpicture}
\end{document}
答案1
该pgfplots
(!)库fillbetween
允许您使用路径的交叉段来做各种事情,包括(重新)绘制。
\documentclass[margin=1cm]{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}
\begin{document}
\begin{tikzpicture}
\draw[dotted, name path=p1] (-2,0)node(A){A} -- ++(0,1) -- ++(45:5cm)node(A1){A'};
\draw[dotted, name path=p2] (2,0)node(B){B} -- ++(0,1) -- ++(135:5cm)node(B1){B'};
\path [name intersections={of = p1 and p2,by=C}];
\draw (C)node[]{C};
\draw [intersection segments={
of=p1 and p2,
sequence={L1 -- R1[reverse]}
}];
\end{tikzpicture}
\end{document}
答案2
如果像在您的代码中一样,您知道要绘制的路径是什么,则可以手动重新绘制它(第 1 个示例)。另一个选项可能是剪辑原始图形以仅显示所需部分(第 2 个示例)。在第二种情况下,连接处C.center
比第一种情况更短。
\documentclass[margin=1cm]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc,intersections}
\begin{document}
\begin{tikzpicture}
\draw[dotted, name path=p1] (-2,0)node(A){A} -- ++(0,1) -- ++(45:5cm)node(A1){A'};
\draw[dotted, name path=p2] (2,0)node(B){B} -- ++(0,1) -- ++(135:5cm)node(B1){B'};
\path [name intersections={of = p1 and p2,by=C}] (C) node{C};
%\draw (C)node[]{C};
\draw (A.center)--++(0,1)--(C.center)--([yshift=1cm]B.center)--(B.center);
\end{tikzpicture}
\begin{tikzpicture}
\draw[dotted, name path=p1] (-2,0)node(A){A} -- ++(0,1) -- ++(45:5cm)node(A1){A'};
\draw[dotted, name path=p2] (2,0)node(B){B} -- ++(0,1) -- ++(135:5cm)node(B1){B'};
\path [name intersections={of = p1 and p2,by=C}] (C) node{C};
%\draw (C)node[]{C};
{
\clip (A.west|-C) rectangle (B.south east);
\draw (-2,0) -- ++(0,1) -- ++(45:5cm);
\draw (2,0) -- ++(0,1) -- ++(135:5cm);
}
%\draw (A.center)--++(0,1)--(C.center)--([yshift=1cm]B.center)--(B.center);
\end{tikzpicture}
\end{document}
答案3
以下是使用 v2.0 的解决方案spath3
库(截至 2021 年 1 月)。它与fillbetween
另一个答案中的解决方案非常相似。
\documentclass{article}
%\url{https://tex.stackexchange.com/q/458536/86}
\usepackage{tikz}
\usetikzlibrary{calc,intersections, spath3}
\ExplSyntaxOn
\cs_set_eq:NN \getComponentOf \clist_item:Nn
\ExplSyntaxOff
\begin{document}
\begin{tikzpicture}
\draw[dotted, spath/save=p1] (-2,0)node(A){A} -- ++(0,1) -- ++(45:5cm)node(A1){A'};
\draw[dotted, spath/save=p2] (2,0)node(B){B} -- ++(0,1) -- ++(135:5cm)node(B1){B'};
\tikzset{
spath/split at intersections={p1}{p2},
spath/get components of={p1}\Acpts,
spath/get components of={p2}\Bcpts,
}
\draw[
spath/restore=\getComponentOf\Acpts{1},
]
[
spath/append reverse=\getComponentOf\Bcpts{1}
];
\end{tikzpicture}
\end{document}