\documentclass{article}
\usepackage{amssymb}
\usepackage{amsmath}
%\usepackage{slide-article-tom}
\usepackage[landscape]{slide-article-tom}

\usepackage{amsthm}
\newtheorem{thm}{Theorem}

\ifx\pdfoutput\undefined
     \usepackage[dvips]{graphicx}
\else
     \usepackage[pdftex]{graphicx}
     \pdfcompresslevel 9
\fi

\usepackage[all]{xy}
\setlength{\unitlength}{1cm}


\usepackage{fixltx2e}

\usepackage{wrapfig}


\usepackage{tikz}
\usetikzlibrary{backgrounds,calc,arrows,shapes.arrows,shapes.gates.logic.US,shapes.gates.logic.IEC}
\usepackage{calc}
\usepackage{circuitikz}

\tikzset{
%MyCube/.style={scale=1.0,x={(-.4cm,-0.3cm)},y={(0.6cm,-0.3cm)},z={(0.2cm,0.7cm)}},	
MyCube/.style={scale=1.0,x={(-0.55cm,-0.3cm)},y={(1cm,0cm)},z={(0cm,1cm)}},
	MyFaceA/.style={green!70!,draw=black},
	MyFaceB/.style={blue!50!,draw=black},
	MyFaceC/.style={blue!30!green!30!,draw=black}
	}

\newcommand{\cube}[4]{%
	% parameters #1,#2,#3 = coordinates of the center, parameter #4 = length of edge
 	\pgfmathparse{0.5*#4}%
	\let\sd\pgfmathresult{}%
 	\coordinate (A) at ({#1+\sd},{#2+\sd},{#3+\sd});
 	\coordinate (E) at ({#1+\sd},{#2+\sd},{#3-\sd});
	\coordinate (B) at  ({#1+\sd},{#2-\sd},{#3+\sd});
	\coordinate (F) at  ({#1+\sd},{#2-\sd},{#3-\sd});
	\coordinate (C) at  ({#1-0.7*\sd},{#2-0.7*\sd},{#3+\sd});
	\coordinate (G) at  ({#1-\sd},{#2-\sd},{#3-\sd});
 	\coordinate (D) at ({#1-0.7*\sd},{#2+\sd},{#3+\sd});
 	\coordinate (H) at ({#1-0.7*\sd},{#2+\sd},{#3-0.7*\sd});
	\fill[MyFaceA] (A)--(E)--(H)--(D)--cycle;
	\fill[MyFaceB] (A)--(B)--(C)--(D)--cycle;
	\fill[MyFaceC] (A)--(B)--(F)--(E)--cycle;
	\draw (A)--(E)--(H)--(D)--cycle;
	\draw (D)--(C)--(B)--(A);
	\draw (B)--(F)--(E);
}

\usepackage[titles]{tocloft}
\setlength{\cftbeforesecskip}{-2ex}
\setlength{\cftbeforesubsecskip}{-2ex}

\usepackage{hyperref}

\hypersetup{colorlinks,
            linkcolor=blue,
            citecolor=blue,
            %pdfpagemode=FullScreen
            pdfpagemode=UseNone
            }


%\newcommand\@linkcolor{red}
%\newcommand\@linkcolor{blue}
%\newcommand\@anchorcolor{black}
%\newcommand\@citecolor{red}
%\newcommand\@filecolor{cyan}
%\newcommand\@urlcolor{magenta}
%\newcommand\@menucolor{red}
%\newcommand\@pagecolor{red}


\newcommand\pagedone{\newpage}

\newcommand\tthdump[1]{#1}


\newcommand\sectionhead[1]{\begin{center}\section*{#1 \hyperlink{Contents}{\hfil$\leftarrow$}}
             \end{center}
     \addcontentsline{toc}{section}{#1}
     \vspace{-\baselineskip}
     }
     
\newcommand\subsectionhead[1]{\begin{center}\subsection*{#1 \hyperlink{Contents}{\hfil$\leftarrow$}}
             \end{center}
     \addcontentsline{toc}{subsection}{#1}
     \vspace{-\baselineskip}
     }
     
\newcommand\cyrsubsectionhead[1]{\begin{center}\subsection*{#1 \hyperlink{Contents}{\hfil$\leftarrow$}}
             \end{center}
     \addcontentsline{toc}{subsection}{#1}
     \vspace{-\baselineskip}
     }


\newcommand\exercises[1]{\begin{center}\subsection*{#1 -~exercises
            \hyperlink{Contents}{\hfil$\leftarrow$}}\end{center}
    \addcontentsline{toc}{subsection}{#1 (exercises)}
     \vspace{-\baselineskip}
     }
     

\newcommand\quotesection[1]{\begin{center}{\LARGE\hypertarget{#1}
     {#1}\hyperlink{The quotes}{\hfil$\twoheadleftarrow$}}\end{center}}

\newcommand\httimes{\times}

\newcommand\mybigtriangledown{\bigtriangledown}

\newcommand\htvec[1]{\vec{#1}}

\newcommand\myspan{\mathrm{span}}
\newcommand\mymatrix{\mathrm{matrix}}

\newcommand\myreal{\mathbb{R}}

\newcommand\mycomplex{\mathbb{C}}
\newcommand\myfield{\mathbb{F}}
\newcommand\myquaternions{\mathbb{H}}
\newcommand\myrationals{\mathbb{Q}}
\newcommand\myintegers{\mathbb{Z}}

%\newcommand\dim\mathrm{dim}
\newcommand\im{\mathrm{im}}
\newcommand\deriv{\mathrm{D}}
\newcommand\trace{\mathrm{trace}}
\newcommand\detm{\mathrm{det}_m}
\newcommand\detv{\mathrm{det}_v}
\newcommand\perm{\mathrm{perm}}
\newcommand\sign{\mathrm{sign}}

% kets and bras etc.
\newcommand\ket[1]{|{#1}\rangle}
\newcommand\bra[1]{\langle{#1}|}
\newcommand\braket[2]{\langle{#1}|{#2}\rangle}

%boolean operators
\newcommand\myand{$\wedge$ }
\newcommand\myor{$\vee$ }

% defines a 2 element column vector.
\newcommand\col[2]{\left(\begin{array}{c}#1\\#2\end{array}\right)}
\newcommand\tcol[2]{(#1, #2)^T}

\begin{document}
\raggedright

\pagestyle{myfooters}
%\pagestyle{plain}

\thispagestyle{empty}

%Slide 1
\title{{\LARGE\bf to Draw a Circle}\newline
with a Digital Differential Algorithm\newline
}
\author{
\begin{wrapfigure}{r}{0.35\textwidth}
  \vspace{-2cm}
  \begin{center}
    {\fontsize{5}{5}\selectfont  
       http://csustan.csustan.edu/\~\ tom
    }
   {\hyperref{http://csustan.csustan.edu/\~tom}{}{}
     {    \includegraphics[width=0.2\textwidth]{tom-carter-QR-pic.png}
     }
   }
  \end{center}
  \end{wrapfigure}
      Tom Carter \newline
      \newline
     Computer Science \newline
     CSU Stanislaus\newline
\newline
{\hyperref{mailto:tom@csustan.csustan.edu}{}{}
     {tom@csustan.csustan.edu}}
\vfill
{\hyperref{http://csustan.csustan.edu/\~tom/Lecture-Notes/Graphics/DDA-circle/DDA-circle.pdf}{}{}
     {http://csustan.csustan.edu/\~\ tom/Lecture-Notes/Graphics/DDA-circle/DDA-circle.pdf}}
}

%\date{Fall, 2011}
\date{\today}


\maketitle

\renewcommand\contentsname{Our general topics:\hypertarget{Contents}{}}
%% \newcommand\listfigurename{List of Figures}
%% \newcommand\listtablename{List of Tables}

\tableofcontents

\pagedone




\sectionhead{Drawing a Circle in a Raster}

     There are times when we want to draw a circle on a raster device.  We'll develop a
     reasonably optimized algorithm for doing this.  The approach we'll take is to develop
     a {\em digital differential algorithm}.
     
     The {\em raster} is a two-dimensional array of pixels.  We will assume we are given
     a center $(CX, CY)$ (in pixel coordinates) and a radius $R$ (also in pixel coordinates).
     
     One nice thing about a circle is that it has many symmetries.  In fact, we can draw a
     circle by specifying pixels we want to turn on in just one octant, say $X = 0$ to
     $X = Y$.

\pagedone

     Here's an example of what this will look like:
     
     \vspace{1.5cm}
      \centerline{\includegraphics[width = 4.8in]{Circle-1}}
      \centerline{\bf \large A circle in a raster}
\pagedone

     Using symmetries, we only work in one octant:
     
     \vspace{1.5cm}
      \centerline{\includegraphics[width = 4.8in]{Circle-2}}
      \centerline{\bf  Symmetries - just one octant}

\pagedone
     We'll make some simplifying assumptions in the derivation.
     \begin{enumerate}
          \item Our pixel coordinates go from left to right, and bottom to top, the way they
               do in the first quadrant in mathematics.
          \item Our pixels are square (so we don't need to worry about aspect ratio \ldots).
          \item Our pixels are either off or on (i.e., no gray-scale \ldots).
     \end{enumerate}
\pagedone
   
\sectionhead{The DDA Circle Algorithm (first steps)}

     To keep things simple, we'll do our calculations as though center of the circle is at $(0,0)$.
     We'll assume we have a procedure (circlePoints($CX, CY, X, Y$)) that takes advantage of
     the symmetries, and turns on 8 (or 4 \ldots) pixels once we specify the center of the circle $(CX, CY)$
     and the pixel to start with relative to the center.
\pagedone
     With the assumptions we have made, our stepwise algorithm will have the outline:
     \begin{verbatim}
          X = 0
          Y = R
          circlePoints(CX, CY, X, Y)
          While (X < Y)
               X = X + 1
               determine Y value
               circlePoints(CX, CY, X, Y)
          EndWhile
     \end{verbatim}
     Thus, if we can figure out a fast way to determine the Y value to turn on, we will be done.
     
     We'll do this in an iterative fashion.  As we step along in the X direction, we can see that
     either Y stays the same, or Y decreases by 1.  All we really need to do is figure out when
     to decrement Y.
     
     Algorithm, version 2:
     \begin{verbatim}
          X = 0
          Y = R
          circlePoints(CX, CY, X, Y)
          While (X < Y)
               X = X + 1
               If we should decrement Y
                    Y = Y - 1
               EndIf
               circlePoints(CX, CY, X, Y)
          EndWhile
     \end{verbatim}
\pagedone
     We need to develop a {\em decision function} (or {\em decision variable}),
     sometimes called an {\em indicator variable, that
     will tell us when to decrement Y.
     
     Let's start with the function
          $$F(x, y) = x^2 + y^2 - r^2$$
     For any point $(x, y)$ on the circle, we have $F(x, y) = 0$.  If $F(x, y) < 0$, the point is
     inside the circle, and if $F(x, y) > 0$, the point is outside the circle.
     
     So, suppose the current pixel is $(X, Y)$.  We need to decide whether the next pixel
     should be $(X + 1, Y)$ or $(X + 1, Y - 1)$.  We'll create a decision variable that looks at
     the ``average'' of the two pixels we are considering, call it $M$, and checks whether
     $M$ is inside or outside the circle:
          \begin{align*}
               D & = F(M) \\
                   & = F(X + 1, Y - \frac{1}{2}) \\
                   & = (X + 1)^2 + (Y - \frac{1}{2})^2 - R^2 \\
                   & = X^2 + 2X + 1 + Y^2 - Y + \frac{1}{4} - R^2 \\
          \end{align*}
     If $D < 0$, then $M$ is still inside the circle, so we don't want to decrement $Y$ yet.
     
     We don't want to calculate $D$ at each step (floating point operations, etc.), but
     instead want to update the value incrementally.
\pagedone
     There are two possibilities.
     
     If we didn't decrement Y, then the new value of $D$
     will be
          \begin{align*}
               D_{new} & = (X + 2)^2 + (Y - \frac{1}{2})^2 - R^2 \\
                              & = X^2 + 4X + 4 + Y^2 - Y + \frac{1}{4} - R^2 \\
          \end{align*}
     and the increment in $D$ will then be
          \begin{align*}
               D_{new} - D_{old} & = X^2 + 4X + 4 + Y^2 - Y + \frac{1}{4} - R^2 \\
                              & \ \ \ \ \ \ - (X^2 + 2X + 1 + Y^2 - Y + \frac{1}{4} - R^2) \\
                              & = 2X + 3 \\
          \end{align*}
     If we did decrement Y, then the new value of $D$
     will be
          \begin{align*}
               D_{new} & = (X + 2)^2 + (Y - \frac{3}{2})^2 - R^2 \\
                              & = X^2 + 4X + 4 + Y^2 - 3Y + \frac{9}{4} - R^2 \\
          \end{align*}
     and the increment in $D$ will then be
          \begin{align*}
               D_{new} - D_{old} & = X^2 + 4X + 4 + Y^2 - 3Y + \frac{9}{4} - R^2 \\
                              & \ \ \ \ \ \ - (X^2 + 2X + 1 + Y^2 - Y + \frac{1}{4} - R^2) \\
                              & = 2X - 2Y  + 5.\\
          \end{align*}
\pagedone
     The other thing we need to do is establish the starting value for $D$:
          \begin{align*}
               D_0 & = F(1, R - \frac{1}{2}) \\
                       & = 1 + (R - \frac{1}{2})^2 - R^2 \\
                       & = 1 + R^2 - R + \frac{1}{4} - R^2 \\
                       & = \frac{5}{4} - R \\
          \end{align*}
     Given that we want to work with integers (not floating point), we will
     go ahead and use $D_0 = 1 - R$.
     
     Technically, this means that we should do the comparison $D < - \frac{1}{4}$
     for our decision variable, but since we are working with integers,
     this is no different from $D < 0$.
\pagedone
     We can thus put together our algorithm as:
      \begin{verbatim}
          X = 0
          Y = R
          D = 1 - R
          circlePoints(CX, CY, X, Y)
          While (X < Y)
               If D < 0
                    D = D + 2 * X + 3
               Else
                    Y = Y - 1
                    D = D + 2 * (X - Y) + 5
               EndIf
               X = X + 1
               circlePoints(CX, CY, X, Y)
          EndWhile
     \end{verbatim}
\pagedone
\sectionhead{The DDA Circle Algorithm}
     Our final algorithm looks like this:
     \begin{verbatim}
void circleDDA(int xCenter, int yCenter, int radius)
{
    int x;
    int y;
    int p;

    x = 0;
    y = radius;
    p = 1 - radius; /**/
    
    circlePoints(xCenter, yCenter, x, y);
    
    while (x < y) {
        x++;
        if (p < 0) {
            p += 2*x+3;  /**/
        } else {
            y--;
            p += 2*(x-y)+ 5;  /**/
        }
        circlePoints(xCenter, yCenter, x, y);
    }
}
     \end{verbatim}
\pagedone
     The one other piece is the ``circlePoints'' procedure, that takes advantage of
     the symmetries of the circle to plot 8 (or 4) points.
     \begin{verbatim}
void circlePoints(int cx, int cy, int x, int y)
{
    int cxpx = cx + x;
    int cxmx = cx - x;
    int cxpy = cx + y;
    int cxmy = cx - y;
    int cypx = cy + x;
    int cymx = cy - x;
    int cypy = cy + y;
    int cymy = cy - y;
    
    
	if (x == 0) {
		if ((0 <= cx) && (cx < RDim)
		     && (0 <= (cypy)) && ((cypy) < RDim))
            theRaster[cx][cypy] = 1;
		if ((0 <= cx) && (cx < RDim)
		     && (0 <= (cymy)) && ((cymy) < RDim))
            theRaster[cx][cymy] = 1;
		if ((0 <= (cxpy)) && ((cxpy) < RDim)
		     && (0 <= (cy)) && ((cy) < RDim))
            theRaster[cxpy][cy] = 1;
		if ((0 <= (cxmy)) && ((cxmy) < RDim)
		     && (0 <= (cy)) && ((cy) < RDim))
            theRaster[cxmy][cy] = 1;
	} else 
	if (x == y) {
		if ((0 <= (cxpx)) && ((cxpx) < RDim)
		     && (0 <= (cy + y)) && ((cy + y) < RDim))
            theRaster[cxpx][cy + y] = 1;
		if ((0 <= (cxmx)) && ((cxmx) < RDim)
		     && (0 <= (cy + y)) && ((cy + y) < RDim))
            theRaster[cxmx][cy + y] = 1;
		if ((0 <= (cxpx)) && ((cxpx) < RDim)
		     && (0 <= (cymy)) && ((cymy) < RDim))
            theRaster[cxpx][cymy] = 1;
		if ((0 <= (cxmx)) && ((cxmx) < RDim)
		     && (0 <= (cymy)) && ((cymy) < RDim))
            theRaster[cxmx][cymy] = 1;
	} else 
	if (x < y) {
		if ((0 <= (cxpx)) && ((cxpx) < RDim)
		     && (0 <= (cy + y)) && ((cy + y) < RDim))
            theRaster[cxpx][cy + y] = 1;
		if ((0 <= (cxmx)) && ((cxmx) < RDim)
		     && (0 <= (cy + y)) && ((cy + y) < RDim))
            theRaster[cxmx][cy + y] = 1;
		if ((0 <= (cxpx)) && ((cxpx) < RDim)
		     && (0 <= (cymy)) && ((cymy) < RDim))
            theRaster[cxpx][cymy] = 1;
		if ((0 <= (cxmx)) && ((cxmx) < RDim)
		     && (0 <= (cymy)) && ((cymy) < RDim))
            theRaster[cxmx][cymy] = 1;
		if ((0 <= (cx + y)) && ((cx +y) < RDim)
		     && (0 <= (cypx)) && ((cypx) < RDim))
            theRaster[cx + y][cypx] = 1;
		if ((0 <= (cxmy)) && ((cxmy) < RDim)
		     && (0 <= (cypx)) && ((cypx) < RDim))
            theRaster[cxmy][cypx] = 1;
		if ((0 <= (cx + y)) && ((cx + y) < RDim)
		     && (0 <= (cymx)) && ((cymx) < RDim))
            theRaster[cx + y][cymx] = 1;
		if ((0 <= (cxmy)) && ((cxmy) < RDim)
		     && (0 <= (cymx)) && ((cymx) < RDim)) /**/
            theRaster[cxmy][cymx] = 1;
	}
}
     \end{verbatim}

% \footnotesize
% \bibliographystyle{plain} 
% 
%
%
%\marginpar{\hyperlink{Contents}{\hfil$\leftarrow$}}
%
%\vspace{-\baselineskip}
%\vspace{-\baselineskip}
%\vspace{-\baselineskip}
% 
% \begin{thebibliography}{12}
%
%\addcontentsline{toc}{section}{References}
%
%\bibitem{feld}
%Feld, Scott L. (1991), \\
%Why your friends have more friends than you do, \\
%American Journal of Sociology 96 (6): 1464-1477, \\
%\href{http://dx.doi.org/10.1086\%2F229693}
%           {doi:10.1086/229693, JSTOR 2781907}
%
%\bibitem{newman}
%Newman, M.E.J. (2002), \\
%Random Graphs as Models of Networks,\\
%SFI WORKING PAPER: 2002-02-005, \\
%\href{http://www.santafe.edu/media/workingpapers/02-02-005.pdf}
%          {http://www.santafe.edu/media/workingpapers/02-02-005.pdf}
%
%\end{thebibliography}

\hyperlink{Contents}{\hfil To top $\leftarrow$}


\end{document}
