share
TeX - LaTeXHelp with drawing a unit circle & lines
[+13] [2] anonymous12345
[2012-10-22 20:50:49]
[ tikz-pgf ]
[ https://tex.stackexchange.com/questions/78716/help-with-drawing-a-unit-circle-lines ]

I would like to draw a unit circle with labelled coordinates of points on it as well as lines that intersect the circle like this:

The code that I pasted below does part of the job. But I want

  1. coordinates that are larger,
  2. the lines, and
  3. the slopes of the lines labelled, ideally without the lines intersecting the labels.

I would greatly appreciate help.


% Unit circle % Author: Supreme Aryal
% A unit circle with cosine and sine values for some
% common angles.

\documentclass[landscape]{article}
\usepackage{tikz} %%%<
\usepackage{verbatim}
\usepackage[active,tightpage]{preview}
\PreviewEnvironment{tikzpicture}
\setlength\PreviewBorder{5pt}% %%%>

\begin{comment} :Title: Unit circle

A unit circle with cosine and sine values for some common angles.

\end{comment}

\usepackage[top=1in,bottom=1in,right=1in,left=1in]{geometry} \begin{document}
    \begin{tikzpicture}[scale=5.3,cap=round,>=latex]
        % draw the coordinates
        \draw[->] (-1.5cm,0cm) -- (1.5cm,0cm) node[right,fill=white] {$x$};
        \draw[->] (0cm,-1.5cm) -- (0cm,1.5cm) node[above,fill=white] {$y$};

        % draw the unit circle
        \draw[thick] (0cm,0cm) circle(1cm);

        \foreach \x in {0,30,...,360} {
                % lines from center to point
                % dots at each point
                \filldraw[black] (\x:1cm) circle(0.4pt);
                % draw each angle in degrees
        }


        \foreach \x/\xtext/\y in {
            % the coordinates for the first quadrant
            30/\frac{\sqrt{3}}{2}/\frac{1}{2},
            45/\frac{\sqrt{2}}{2}/\frac{\sqrt{2}}{2},
            60/\frac{1}{2}/\frac{\sqrt{3}}{2},
            % the coordinates for the second quadrant
            150/-\frac{\sqrt{3}}{2}/\frac{1}{2},
            135/-\frac{\sqrt{2}}{2}/\frac{\sqrt{2}}{2},
            120/-\frac{1}{2}/\frac{\sqrt{3}}{2},
            % the coordinates for the third quadrant
            210/-\frac{\sqrt{3}}{2}/-\frac{1}{2},
            225/-\frac{\sqrt{2}}{2}/-\frac{\sqrt{2}}{2},
            240/-\frac{1}{2}/-\frac{\sqrt{3}}{2},
            % the coordinates for the fourth quadrant
            330/\frac{\sqrt{3}}{2}/-\frac{1}{2},
            315/\frac{\sqrt{2}}{2}/-\frac{\sqrt{2}}{2},
            300/\frac{1}{2}/-\frac{\sqrt{3}}{2}}
                \draw (\x:1.45cm) node[fill=white] {$\left(\xtext,\y\right)$};

        % draw the horizontal and vertical coordinates
        % the placement is better this way
        \draw (-1.25cm,0cm) node[above=1pt] {$(-1,0)$}
              (1.25cm,0cm)  node[above=1pt] {$(1,0)$}
              (0cm,-1.25cm) node[fill=white] {$(0,-1)$}
              (0cm,1.25cm)  node[fill=white] {$(0,1)$};
    \end{tikzpicture} \end{document}
[+14] [2012-10-22 22:15:05] percusse

Maybe a little clean up would be beneficial

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{fpu}
\begin{document}
\begin{tikzpicture}[cap=round,>=latex,every node/.style={scale=0.5}]
        \draw[->] (-1.5cm,0cm) -- (1.5cm,0cm) node[right,fill=white]{$x$};
        \draw[->] (0cm,-1.5cm) -- (0cm,1.5cm) node[above,fill=white]{$y$};
        \draw[thick] (0cm,0cm) circle(1cm);
% Source of rays
\coordinate (s) at (-1cm,0);
\node[above left] at (-1cm,0) {$(-1,0)$};
\foreach \x in {2,1,0.5,0.333333,-0.25,-1}{
\draw[orange] (s)--++({atan2(1,\x)}:2) node[black,right] {$m=\pgfmathprintnumber[frac]{\x}$};
}
\end{tikzpicture}

\end{document}

enter image description here

Note that the fractions that are placed via fpu library are really, I mean really, sensitive to the number input. If you remove a few 3s from 0.333333 you'll probably understand what I'm warning for :)

Also if you want to scale up, the node shapes needs the key transform shape option to follow the current transformation. So it's maybe better to scale the nodes independently as I did via every node/.style={...}.


For Luigi's question in the comments, we can simply use the half angle relation to get where it crosses the circle and then extend a fix amount from that point such that the extensions are always the same length.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{fpu}
\begin{document}
\begin{tikzpicture}[cap=round,>=latex,every node/.style={scale=0.5}]
        \draw[->] (-1.5cm,0cm) -- (1.5cm,0cm) node[right,fill=white]{$x$};
        \draw[->] (0cm,-1.5cm) -- (0cm,1.5cm) node[above,fill=white]{$y$};
        \draw[thick] (0cm,0cm) circle(1cm);
% Source of rays
\coordinate (s) at (-1cm,0);
\node[above left] at (-1cm,0) {$(-1,0)$};
\foreach \x in {2,1,0.5,0.333333,-0.25,-1}{
\pgfmathsetmacro\myhalfangle{2*atan2(1,\x)}
\draw[orange] (s)-- ({cos(\myhalfangle)},{sin(\myhalfangle)}) 
-- ++(0.5*\myhalfangle:1.2cm) node[black,right] {$m=\pgfmathprintnumber[frac]{\x}$};
}
\end{tikzpicture}

\end{document}

enter image description here


Though why doesn't the m = 2 line go through the point (0, 1)...? - anonymous12345
@anonymous12345 It was wrong. Now corrected. - percusse
@percusse, what if the length of the arrows varied in such a way that the arrow tips protruded from the circle by the same length? - Luigi
@Luigi See the second example :) - percusse
+1 @percusse, pardon! I saw it yesterday (and upvoted) but didn't reply. I don't know which is better. I thought it would have been neater, but due to the m=2 line, they are all too long. Anyway, I did it so:\pgfmathparse{sqrt(2+2*cos(2*atan2(1,\x)))+0.2} in your original code. Thanks. - Luigi
1
[+4] [2012-10-24 00:00:56] cmhughes

@percusse's answer is great (+1 of course), but when it comes to drawing pictures that use axis I would definitely consider pgfplots [1]. The output is very similar

enter image description here

but the construction is slightly different

\documentclass{article}

\usepackage{pgfplots}
\usetikzlibrary{fpu}

\pgfplotsset{every axis/.append style={
                    axis x line=middle,    % put the x axis in the middle
                    axis y line=middle,    % put the y axis in the middle
                    axis line style={<->}, % arrows on the axis
                    xlabel={$x$},          % default put x on x-axis
                    ylabel={$y$},          % default put y on y-axis
                    }}


% set the arrows as stealth fighters
\tikzset{>=stealth}

\begin{document}

\begin{tikzpicture}
  \begin{axis}[axis equal,
            xmin=-1.5,xmax=1.5,
            ymin=-1.5,ymax=1.5,
            xtick={-10},
            ytick={-10},
            ]
    \addplot [domain=0:2*pi,samples=50] ({cos(deg(x))},{sin(deg(x))});
    \node  at (axis cs:-1,0)[anchor=south east]{$(-1,0)$};
    \pgfplotsinvokeforeach{1,0.5,0.333333,-0.25,-1}{
        \pgfmathparse{2*cos(atan2(1,#1))-1}
        \addplot[red,->] expression[domain=-1:{\pgfmathresult}]{#1*x+#1}node[black,right] {$m=\pgfmathprintnumber[frac]{#1}$};
    }
  \end{axis}
\end{tikzpicture}

\end{document}

I like this approach because of the global settings for the axis. With a little work in the preamble, you can then spice up the picture (using globally defined settings), for example

% framing
\pgfplotsset{framed/.style={axis background/.style ={draw=blue,fill=yellow!20,rounded corners=3ex}}}

enter image description here

\begin{tikzpicture}
  \begin{axis}[axis equal,
            xmin=-1.5,xmax=1.5,
            ymin=-1.5,ymax=1.5,
            xtick={-10},
            ytick={-10},
            minor xtick={-1.5,-1,...,1.5},
            minor ytick={-1.5,-1,...,1.5},
            framed,
            grid=both,
            ]
    \addplot [domain=0:2*pi,samples=50] ({cos(deg(x))},{sin(deg(x))});
    \node  at (axis cs:-1,0)[anchor=south east]{$(-1,0)$};
    \pgfplotsinvokeforeach{1,0.5,0.333333,-0.25,-1}{
        \pgfmathparse{2*cos(atan2(1,#1))-1}
        \addplot[red,->] expression[domain=-1:{\pgfmathresult}]{#1*x+#1}node[black,right] {$m=\pgfmathprintnumber[frac]{#1}$};
    }
  \end{axis}
\end{tikzpicture}
[1] http://texdoc.net/pkg/pgfplots

Great use of pgfplots +1. - user11232
@cmhughes, why the four arrows for the axes? And what if the length of the arrows varied in such a way that the arrow tips protruded from the circle by the same length? Hint: \pgfmathparse{cos(2*atan2(1,#1))+0.15*cos(atan2(1,#1))}. P.S. I always set samples=2 for straight lines. - Luigi
(1) @Luigi I'm British, but currently live in America- the Americans put arrows on their axis - cmhughes
2