2

I have two paths which are to be drawn with dotted lines (shown in figure) and they intersect at some point C.

Now I need to draw a resultant path of these two paths with solid lines. The resultant path should follow the original paths till they intersect. So in figure the resultant path will be following path 1 from A to C and then it should take path 2 to reach B.

The paths need not be straight lines as given in the MWE. So I am looking for a method to draw path from intersection of two paths to the origin of those paths in general.

enter image description here

\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}

3 Answers 3

4

The pgfplots (!) library fillbetween allows you to use intersection segments of paths for all sorts of things, including (re)drawing.

\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}

enter image description here

6
  • Thank you. It works fine. Just curious: Does the node names A0 and B0 has to do with the node names A and B that is used in the code?
    – nidhin
    Commented Nov 5, 2018 at 19:51
  • @nidhin No, it does not. I rewrote my answer (also because the L and R syntax is newer. L refers to the first path and R to the second one. L1, L2 etc. are the intersection segments of the first path, similarly for R. See section 5.7.6 Intersection Segment Recombination of the pgfplots manual for more details. Note that this works for arbitrary paths, curves etc., and also for several intersections.
    – user121799
    Commented Nov 5, 2018 at 19:58
  • So L1 means second segment of 1st path? If so, L0 is the upper segment of 1st path. So the numbering is done is reverse order?
    – nidhin
    Commented Nov 5, 2018 at 20:04
  • @nidhin No, AFAIK for the L and R syntax the numbering starts at 1 while it starts at 0 for the A and B syntax. (I can see how this is confusing.)
    – user121799
    Commented Nov 5, 2018 at 20:06
  • 1
    @nidhin Yes, it is somewhat confusing. Notice that L0 -- R0 works sort of by accident, the path between A' and C is drawn twice in two directions. You can check that by drawing the path dashed.
    – user121799
    Commented Nov 5, 2018 at 20:32
2

If like in your code, you know what's the path to be drawn, you can redraw it by hand (1st example). Another option could be to clip the original figure to just show the desired part (2nd example). In this second case, the join at C.center is shorter than in first case.

\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}

enter image description here

2
  • Thank you. Both works fine for this example. The paths that I need to draw include shapes other than straight lines. Can we follow the first method in that case also ?
    – nidhin
    Commented Nov 5, 2018 at 19:48
  • 1
    @nidhin If "other shapes" means well know arcs probably yes, but for other kind of curves probably not.
    – Ignasi
    Commented Nov 6, 2018 at 8:45
0

Here's a solution using v2.0 of the spath3 library (as of Jan 2021). It's very similar to the fillbetween solution in another answer.

\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}

Composite path created from splitting other paths at intersection point

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .