[Tex/LaTex] Unit Circle and Circle of Radius R

tikz-pgf

In order to complete a handout I was preparing, I wrote the code below (MWE):

\documentclass[border=10pt,tikz]{standalone}
\usetikzlibrary{calc,angles,positioning,decorations.pathreplacing}
\begin{document}
\newcounter{angle}
\setcounter{angle}{135}
\begin{tikzpicture}
    %--------------------------------------------------------------------------------------------
    %Axis
    \draw[thick,-stealth,black] (-3.7,0)--(3.7,0) node[below] {$x$}; % x axis
    \draw[thick,-stealth,black] (0,-3.7)--(0,3.7) node[left] {$y$}; % y axis
    %--------------------------------------------------------------------------------------------
    % Unit Circle
    \draw[black] (0,0) circle (2cm);
    \node[black,below] at (2.2,0) {1};
    %--------------------------------------------------------------------------------------------
    % Circle of Radius R
    \draw[black] (0,0) circle (3cm);
    \node[black,below] at (3.2,0) {$r$};
    %--------------------------------------------------------------------------------------------
    % 
    \draw[-stealth] (1,0) arc (0:\theangle:1) node at ($(\theangle/2:0.7)$) {$\theta$};
    %--------------------------------------------------------------------------------------------
    \draw[dashed] (\theangle:2cm) -- (\theangle:2cm |- 0,0) coordinate (A) node [midway, left]{$y^\prime$}; % vertical line
    \draw[thick,rotate=\theangle] (0,0) -- (2,0);%  
    \filldraw[black] (\theangle:2cm) circle(1pt);
    \filldraw[black] (A) circle(1pt);
    \draw (\theangle:2cm) node[above=2pt,xshift=2pt] {$P^\prime$};
    \node[xshift=-0.75cm, below] (A) {$x^\prime$};
    %--------------------------------------------------------------------------------------------
    \draw[thick,rotate=\theangle] (0,0) -- (3,0);
    \filldraw[black] (\theangle:3cm) circle(1pt);
    \draw (\theangle:3cm) node[above=2pt] {$P$};
    \draw[dashed] (\theangle:3cm) -- (\theangle:3cm |- 0,0) coordinate (B) node [midway, left]{$y$};
    \filldraw[black] (B) circle(1pt);
    \draw [decoration={brace,raise=17pt}, decorate,thick,rotate=\theangle] (0,0) -- (B)
        node [midway, below=4.5ex]{$x$};%
\end{tikzpicture}

\end{document}

But I feel the code is a bit messy. Can you assist me in achieving the same result but with a more compact and robust code?

Here is an image of the result above:

enter image description here

Best Answer

This makes the code a bit shorter.

\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{decorations.pathreplacing,arrows.meta,bending}
\begin{document}
\begin{tikzpicture}[declare function={angle=135;},bullet/.style={inner
    sep=1pt,fill,draw,circle,solid}]
    %--------------------------------------------------------------------------------------------
    %Axis
    \draw[thick,-stealth,black] (-3.7,0)--(3.7,0) node[below] {$x$}; % x axis
    \draw[thick,-stealth,black] (0,-3.7)--(0,3.7) node[left] {$y$}; % y axis
    %--------------------------------------------------------------------------------------------
    % Rest
    \path (0,0) coordinate (O);
    \draw foreach \rad/\lab in {2/1,3/r}
    { (O) circle [radius=\rad*1cm]  (\rad,0) node[below right]{$\lab$}};
    \draw[thick] (O) -- (angle:3);
    \draw[dashed] foreach \rad/\lab in {2/',3/}
    {(angle:\rad) node[bullet,label=above:$P\lab$](P\lab){} -- 
     node[left]{$y\lab$}    (P\lab|-O)node[bullet]{} };
    \draw[-{Stealth[bend]}] (angle/2:0.6) node{$\theta$} (0:1) arc(0:angle:1) ;
    \path (O) -- node[below] {$x'$}(P'|-O); 
    \draw [decoration={brace}, decorate,thick] 
    (0,-0.5) -- (P|-0,-0.5) node[midway,below=0.1ex]{$x$};
\end{tikzpicture}
\end{document}

enter image description here

Let me mention that overall your code is fine. I feel that the things that you really should do is to avoid positioning the brace with raise and to use styles for repeating things like the bullet here. The \foreach thingy does make the code shorter but for two repetitions it is not that big of a deal.

Here is an annotated version:

\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{decorations.pathreplacing,arrows.meta,bending}
\begin{document}
\begin{tikzpicture}[declare function={angle=135;}, % This defines a 
                    % parameterless function which as opposed to
                    % \newcounter{angle}\setcounter{angle}{135}
                    % also allows to fractional values and is local.
                    % Of course there is nothing wrong with counters.
    bullet/.style={inner sep=1pt,fill,draw,circle,solid} % This 
    ] % defines a style, which you may think of as short cut.
    %  Instead of "inner sep=1pt,fill,draw,circle,solid" you only need to
    % say "bullet". And if you universally want to change something,
    % such as the radius which is controlled by inner sep, you only
    % need to change it once instead of several times.
    %--------------------------------------------------------------------------------------------
    %Axis
    \draw[thick,-stealth,black] (-3.7,0)--(3.7,0) node[below] {$x$}; % x axis
    \draw[thick,-stealth,black] (0,-3.7)--(0,3.7) node[left] {$y$}; % y axis
    %--------------------------------------------------------------------------------------------
    % Rest
    \path (0,0) coordinate (O);
    % You repeat the circles, which differ in their radii and labels, 
    % so one can do a loop that runs over the latter. 
    \draw foreach \rad/\lab in {2/1,3/r}
    { (O) circle [radius=\rad*1cm]  (\rad,0) node[below right]{$\lab$}};
    % Just a line. 
    \draw[thick] (O) -- (angle:3);
    % The P-type nodes and dashed lines are repetitive, too, so we do 
    % another loop (in which we name the P nodes for later use).
    \draw[dashed] foreach \rad/\lab in {2/',3/}
    {(angle:\rad) node[bullet,label=above:$P\lab$](P\lab){} -- 
     node[left]{$y\lab$}    (P\lab|-O)node[bullet]{} };
    % This is just an arc but it looks IMHO better if the arrow head
    % is bent. In any case, loading the bending library improves the 
    % angles of the arrow heads regardless of whether you use it explicitly
    % by saying "bent".
    \draw[-{Stealth[bend]}] (angle/2:0.6) node{$\theta$} (0:1) arc(0:angle:1) ;
    % add the x' label
    \path (O) -- node[below] {$x'$}(P'|-O); 
    % Add the brace. Rather than raising it we just connect the appropriate
    % points. Here and above we use the |- notation, e.g. 
    % (P|-0,-0.5) means "take the x value of (P) and the y value of (0,-0.5)",
    % see https://tex.stackexchange.com/a/401429 for more details.
    \draw [decoration={brace}, decorate,thick] 
     (0,-0.5) -- (P|-0,-0.5) node[midway,below=0.1ex]{$x$};
\end{tikzpicture}
\end{document}