3

I wanted to draw an ellipse with top half dashed and bottom half solid but also filled. Is there a direct way to do this using the ellipse command from \usetikzlibrary{shapes}?

What I do now, which feels like a trick, is to draw 2 arcs for the top and bottom half this way I can set one arc dashed and the other not. Then next draw an ellipse on top of them but without an edge, just filled. Like this

\documentclass[tikz]{standalone}
\usetikzlibrary{shapes}
\begin{document}
\begin{tikzpicture}

\def\xR{1.25}; %x radius of ellipse
\def\yR{0.5};  %y radius of ellipse

\draw [dashed,domain=0:180] plot ({\xR*cos(\x)}, {\yR*sin(\x)}); %top half
\draw [domain=-180:0] plot ({\xR*cos(\x)}, {\yR*sin(\x)}); %bottom half

\fill[gray!20] (0,0) ellipse ({\xR} and {\yR}); %overlay with ellipse

\end{tikzpicture}

\end{document}

Mathematica graphics

Is there a better way do this?

1 Answer 1

5

With arcs. I'd also fill the area first, this looks arguably better.

\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}

\def\xR{1.25}; %x radius of ellipse
\def\yR{0.5};  %y radius of ellipse

\fill[gray!20] (0,0) ellipse ({\xR} and {\yR}); %overlay with ellipse
\draw [dashed]  (-\xR,0) arc[start angle=180,end angle=0,x radius=1.25,y radius=0.5]; %top half
\draw (-\xR,0) arc[start angle=180,end angle=360,x radius=1.25,y radius=0.5]; %bottom half
\end{tikzpicture}
\end{document}

enter image description here

Or using two commands "only".

\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}
\def\xR{1.25}; %x radius of ellipse
\def\yR{0.5};  %y radius of ellipse
\draw [dashed,fill=gray!20] (0,0) ellipse ({\xR} and {\yR});
\draw (-\xR,0) arc[start angle=180,end angle=360,x radius=1.25,y radius=0.5]; %bottom half
\end{tikzpicture}
\end{document}

You could write styles that dash the first 50% (or whatever %) of the path:

\documentclass[tikz]{standalone}
\usetikzlibrary{decorations.markings}
\begin{document}
\begin{tikzpicture}[draw only after/.style={preaction={decorate,decoration={markings,mark=at
position 0 with {\pgfmathsetmacro\mypoff{\pgfdecoratedpathlength*#1}\xdef\mypoff{\mypoff}
\pgfmathsetmacro\mypl{\pgfdecoratedpathlength}\xdef\mypl{\mypl}}}},
postaction={draw,dash pattern=on 0pt off \mypoff on \mypl}}]
\draw [dashed,fill=gray!20,draw only after=1/2] (1.25,0)
arc[start angle=00,end angle=360,x radius=1.25,y radius=0.5]; 
\end{tikzpicture}
\end{document}
2
  • dash the first 50% (or whatever %) of the path Yes, that will be good. How would one do this? Thanks.
    – Nasser
    Commented May 18, 2019 at 2:00
  • @Nasser I added this to the answer.
    – user121799
    Commented May 18, 2019 at 2:09

You must log in to answer this question.

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