1

How to draw Elliptical Arc in tikz or tkz-euclide

enter image description here

If we know center, X-Radius, Y-Radius , Start Angle - the start angle of the arc and End Angle - the end angle of the arc

MWE

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage{amssymb}
\def\centerarc[#1](#2)(#3:#4:#5)% Syntax: [draw options] (center) (initial angle:final angle:radius)
{ \draw[#1] ($(#2)+({#5*cos(#3)},{#5*sin(#3)})$) arc (#3:#4:#5); }

\tikzset{elliparc/.style args={#1:#2:#3}{%
insert path={(#1:#3) arc (#1:#2:#3)}}}

\begin{document}
\begin{tikzpicture}
\begin{scope}[rotate=0]
\draw[dashed] (0,0) [elliparc=0:180:1cm and .5cm];
\draw[] (0,0) [elliparc=-180:0:1cm and .5cm];

\draw[dashed] (0,3) [elliparc=0:180:1cm and .5cm];
\draw[] (0,3) [elliparc=-180:0:1cm and .5cm];% not working

%\draw (0,3) ellipse ({1cm} and {0.5cm});

\draw[] (1,0) -- ++(0,3);
\draw[] (-1,0) -- ++(0,3);

\path (0,0) -- node[pos=0.5,above] {$r$} ++(1,0);
\draw[thin,color=brown] (0,0) --  ++(0,3.05);
\draw[thin,color=brown] (0,0) -- ++(1,0);
\path (0,0) -- node[pos=0.5,above,sloped] {$h$} ++(0,3.05);
\end{scope}
\end{tikzpicture}
\end{document}
  • 2
    Sorry, what is the question here? – user121799 Sep 1 '18 at 5:06
  • \draw[] (0,0) [elliparc=-180:0:1cm and .5cm]; Works well...\draw[] (0,3) [elliparc=-180:0:1cm and .5cm];% not working (Center of ellipse changed) – sandu Sep 1 '18 at 5:24
  • isn't \tikz\draw (3,0) arc [start angle=0, end angle=80, x radius=3, y radius=1]; ? – touhami Sep 1 '18 at 6:41
  • 2
    @sandu Try \draw (3,0) ++ ({3*cos(80)},{1*sin(80)}) arc (80:180:3 and 1); for an arc with center at (3,0), start angle of 80, end angle of 180, x radius of 3 and y radius of 1. Compare with \draw[dashed,gray] (3,0) circle (3 and 1); to see the complete ellipse. – Max Sep 1 '18 at 10:38
  • 1
    @marmot Ah now I see. The solution is to change \tikzset{elliparc/.style args={#1:#2:#3}{insert path={(#1:#3) arc (#1:#2:#3)}}} to \tikzset{elliparc/.style args={#1:#2:#3}{insert path={++(#1:#3) arc (#1:#2:#3)}}}. Notice the ++ at the start of insert path, which indicates that you use the last point as reference position. – Max Sep 1 '18 at 14:01
2

You can fix this simply by adding ++ to the first coordinate in the insert path key of elliparc.

enter image description here

MWE:

\documentclass[tikz,margin=2mm]{standalone}
\usepackage{tikz}
\usepackage{amssymb}

\tikzset{elliparc/.style args={#1:#2:#3}{%
insert path={++(#1:#3) arc (#1:#2:#3)}}}

\begin{document}
\begin{tikzpicture}
\begin{scope}[rotate=0]
\draw[dashed] (0,0) [elliparc=0:180:1cm and .5cm];
\draw (0,0) [elliparc=-180:0:1cm and .5cm];

\draw[dashed] (0,3) [elliparc=0:180:1cm and .5cm];
\draw (0,3) [elliparc=-180:0:1cm and .5cm];% working

\draw (1,0) -- ++(0,3);
\draw (-1,0) -- ++(0,3);

\path (0,0) -- node[pos=0.5,above] {$r$} ++(1,0);
\draw[thin,color=brown] (0,0) -- ++(0,3.05);
\draw[thin,color=brown] (0,0) -- ++(1,0);
\path (0,0) -- node[pos=0.5,above,sloped] {$h$} ++(0,3.05);
\end{scope}
\end{tikzpicture}
\end{document}

Just for fun I also edited your definition of \centerarc such that the optional first argument is really optional, and it does not need the calc library anymore, because that seemed a bit overkill.

\makeatletter
\def\centerarci[#1](#2) (#3:#4:#5)% Syntax: [draw options] (center) (initial angle:final angle:radius)
{\draw[#1] (#2) ++(#3:#5) arc (#3:#4:#5);}
\def\centerarc{\@ifnextchar[{\centerarci}{\centerarci[]}}
\makeatother

MWE with \centerarc and drawing the cylinder with both options:

\documentclass[tikz,margin=2mm]{standalone}
\usepackage{tikz}
\usepackage{amssymb}

\makeatletter
\def\centerarci[#1](#2) (#3:#4:#5)% Syntax: [draw options] (center) (initial angle:final angle:radius)
{\draw[#1] (#2) ++(#3:#5) arc (#3:#4:#5);}
\def\centerarc{\@ifnextchar[{\centerarci}{\centerarci[]}}
\makeatother

\tikzset{elliparc/.style args={#1:#2:#3}{%
insert path={++(#1:#3) arc (#1:#2:#3)}}}

\begin{document}
\begin{tikzpicture}
\begin{scope}[rotate=0]
\draw[dashed] (0,0) [elliparc=0:180:1cm and .5cm];
\draw (0,0) [elliparc=-180:0:1cm and .5cm];

\draw[dashed] (0,3) [elliparc=0:180:1cm and .5cm];
\draw (0,3) [elliparc=-180:0:1cm and .5cm];% working

\draw (1,0) -- ++(0,3);
\draw (-1,0) -- ++(0,3);

\path (0,0) -- node[pos=0.5,above] {$r$} ++(1,0);
\draw[thin,color=brown] (0,0) --  ++(0,3.05);
\draw[thin,color=brown] (0,0) -- ++(1,0);
\path (0,0) -- node[pos=0.5,above,sloped] {$h$} ++(0,3.05);
\end{scope}
\end{tikzpicture}
\begin{tikzpicture}
\begin{scope}[rotate=0]
\centerarc[dashed](0,0) (0:180:1cm and 0.5cm);
\centerarc(0,0) (-180:0:1cm and 0.5cm);

\centerarc[dashed](0,3) (0:180:1cm and 0.5cm);
\centerarc(0,3)  (-180:0:1cm and 0.5cm);

\draw (1,0) -- ++(0,3);
\draw (-1,0) -- ++(0,3);

\path (0,0) -- node[pos=0.5,above] {$r$} ++(1,0);
\draw[thin,color=brown] (0,0) --  ++(0,3.05);
\draw[thin,color=brown] (0,0) -- ++(1,0);
\path (0,0) -- node[pos=0.5,above,sloped] {$h$} ++(0,3.05);
\end{scope}
\end{tikzpicture}
\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.