TransWikia.com

Extracting a subpath from intersection

TeX - LaTeX Asked by Marc Boyer on July 31, 2021

I am wondering if intersections can be used to extract a subpath from a circle/ellipse path. I have two ellipses, and I am trying to plot only the perimeter of the union. Considering the following code and the associated figure. Is there a way to build a path going from 1 to 2 following the green arc, then from 2 to 3 following the red arc, etc. ?

Ellipses

pagestyle{empty}
usepackage{tikz}
usetikzlibrary{intersections}

begin{document}
begin{tikzpicture}
 draw[name path=e1,green] (0,0) ellipse [x radius=1, y radius=2,
 rotate=30];
 draw[name path=e2,red] (0,0) ellipse [x radius=.8, y radius=3, rotate=-30];
 fill [name intersections={of=e1 and e2, name=i, total=t}]
 [every node/.style={left}]
 foreach s in {1,...,t}{(i-s) circle (2pt) node {footnotesizes}};
end{tikzpicture}
end{document}```

One Answer

My spath3 TikZ library can handle this. It's not clear if you want the components put together into a single path or you want them to stay separate (so that, for example, they can be differently coloured). In this code, I've put them together.

documentclass{article}
%url{https://tex.stackexchange.com/q/585049/86}

pagestyle{empty}
usepackage{tikz}
usetikzlibrary{intersections,spath3}

begin{document}
begin{tikzpicture}
 draw[spath/save=e1,green, line width=4pt] (0,0) ellipse [x radius=1, y radius=2,
 rotate=30];
 draw[spath/save=e2, red, line width=4pt] (0,0) ellipse [x radius=.8, y radius=3, rotate=-30];
 draw [name intersections={of=e1 and e2, name=i, total=t}]
 [every node/.style={left}]
 foreach s in {1,...,t}{(i-s) circle[radius=6pt] +(-6pt,0) node {footnotesizes}};
 tikzset{
   spath/split at intersections={e1}{e2},
   spath/get components of={e1}Acpts,
   spath/get components of={e2}Bcpts,
 }
 draw[
   ultra thick,
   spath/use=getComponentOfAcpts{2},
   spath/use={getComponentOfBcpts{4},weld},
   spath/use={getComponentOfAcpts{4},weld},
   spath/use={getComponentOfBcpts{2},weld},
   ] -- cycle;
 ;
end{tikzpicture}
end{document}`

The result of this is:

Spliced and diced ellipses

I've changed your intersection decorations to hollow circles so that you can see the effect of the weld parameter in joining the path seamlessly.

There's a slight artefact at location 1. That's where the path starts and ends and there is a slight difference between the start point and the end point so the PDF viewer puts a tiny line segment in there and that causes the spur. I've thought of an idea of how to fix it, and I'll add that to the next version (the development version can be found at github).

Correct answer by Andrew Stacey on July 31, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP