SlideShare una empresa de Scribd logo
1 de 7
Descargar para leer sin conexión
Bresenham algorithm for a circle

Applet Select one point on circle.

Bresenham's algorithm for circle rastering:
view plaincopy to clipboardprint


     1. // circle center xi,yi and point on circle xf,yf
     2. public void circle(int xi, int yi, int xf, int yf){
     3.     // calculate radius of circle
     4.     int radius=(int)(Math.sqrt((xf-xi)*(xf-xi)+(yf-yi)*(yf-yi)));
     5.
     6.     int d1 = 3 - (2 * radius);
     7.     int x = 0,i1=x1,i2=x2;
     8.
     9.     int y = radius;
     10.    boolean rov=true;
     11.
     12.    // for one eights, while x is not >= y
     13.    while (rov){
     14.        if (x>=y){rov=false;}
     15.        if (d1 < 0) { d1 = d1 + (4 * x) + 6; }
     16.        else{ d1 = d1 + 4 * (x - y) + 10; // (1)
     17.            y = y - 1;
     18.        }
     19.
     20.        // draw following circle points
     21.        setpix((x1+x),(y1+y));
     22.        setpix((x1+x),(y1-y));
     23.        setpix((x1-x),(y1+y));
     24.        setpix((x1-x),(y1-y));
     25.
     26.        setpix((x1+y),(y1+x));
     27.        setpix((x1+y),(y1-x));
     28.        setpix((x1-y),(y1+x));
     29.        setpix((x1-y),(y1-x));
     30.        x++;
     31.    }
     32. }
Circle Mid point algorithm




Rasterisation of a circle by the Bresenham algorithm

                                                   [2]                   [3]
The algorithm is related to work by Pitteway             and Van Aken.

The algorithm

          This article may be confusing or unclear to readers. Please help us clarify the article;
          suggestions may be found on the talk page. (February 2009)

The algorithm starts with the circle equation                              . For simplicity, assume the center of
the circle is at         . We consider first only the first octant and draw a curve which starts at
point          and proceeds counterclockwise, reaching the angle of 45.

The "fast" direction here (the basis vector with the greater increase in value) is the direction. The
algorithm always takes a step in the positive direction (upwards), and occasionally takes a step in
the "slow" direction (the negative    direction).

From the circle equation we obtain the transformed equation                                       , where     is
computed only a single time during initialization.

Let the points on the circle be a sequence of coordinates of the vector to the point (in the usual basis).
Points are numbered according to the order in which they are drawn, with              assigned to the
first point        .

For each point, the following holds:



     This can be rearranged as follows:
And likewise for the next point:



    In general, it is true that:




              So we refashion our next-point-equation into a recursive one by
              substituting                     :



                   Because of the continuity of a circle and because the maxima along both
                   axes is the same, we know we will not be skipping x points as we advance
                   in the sequence. Usually we will stay on the same x coordinate, and
                   sometimes advance by one.

                   The resulting co-ordinate is then translated by adding the midpoint
                   coordinates. These frequent integer additions do not limit
                   the performance much, as we can spare those square (root) computations
                   in the inner loop in turn. Again, the zero in the transformed circle equation is
                   replaced by the error term.

                   The initialization of the error term is derived from an offset of ½ pixel at the
                   start. Until the intersection with the perpendicular line, this leads to an
                   accumulated value of in the error term, so that this value is used for
                   initialization.

                   The frequent computations of squares in the circle
                   equation, trigonometric expressions and square roots can again be avoided
                   by dissolving everything into single steps and using recursive computation
                   of the quadratic terms from the preceding iterations.

                   Below is an implementation of the Bresenham Algorithm for a full circle in
                   C. Here another variable for recursive computation of the quadratic terms is
                   used, which corresponds with the term            above. It just has to be
                   increased by 2 from one step to the next:
                   void rasterCircle(int x0, int y0, int radius)
                   {
                     int f = 1 - radius;
                     int ddF_x = 1;
                     int ddF_y = -2 * radius;
                     int x = 0;
                     int y = radius;

                      setPixel(x0, y0 + radius);
                      setPixel(x0, y0 - radius);
                      setPixel(x0 + radius, y0);
                      setPixel(x0 - radius, y0);
while(x < y)
      {
        // ddF_x == 2 * x +       1;
        // ddF_y == -2 * y;
        // f == x*x + y*y -       radius*radius + 2*x - y + 1;
        if(f >= 0)
        {
          y--;
          ddF_y += 2;
          f += ddF_y;
        }
        x++;
        ddF_x += 2;
        f += ddF_x;
        setPixel(x0 + x, y0       +    y);
        setPixel(x0 - x, y0       +    y);
        setPixel(x0 + x, y0       -    y);
        setPixel(x0 - x, y0       -    y);
        setPixel(x0 + y, y0       +    x);
        setPixel(x0 - y, y0       +    x);
        setPixel(x0 + y, y0       -    x);
        setPixel(x0 - y, y0       -    x);
      }
}
It is interesting to note that there is correlation between this algorithm and
the sum of first      odd numbers, which this one basically does. That


is,


    In summary, when we compare the sum of N odd numbers to
this algorithm we have:
    ddF_y = -2 * radius                is connected to last member
of sum of N odd numbers.
                                        This member has index equal
to value of radius (integral).
                                        Since odd number is 2*n + 1
there is 1 handled elsewhere
                                        or it should be -2*radius - 1
    ddF_x = 0                          should be 1. Because
difference between two consecutive odd numbers is 2.
                                        If so f += ddF_y + 1 is f+=
ddF_y. Saving one operation.
    f = - radius + 1                    Initial error equal to half
of "bigger" step.
In case of saving one
addition it should be either -radius or -radius + 2.
 In any case there should be addition of 1 driven out of
outer loop.
 So.
 f += ddF_y                          Adding odd numbers from Nth
to 1st.
 f += ddF_x                          Adding odd numbers from 1st
to Nth. 1 is missing because it can be moved outside of
loop.

[edit]Optimization
The following adaption of the circle algorithm is useful for machines where
registers are scarce. Only three variables are needed in the main loop to
perform the calculation. Although written in C, the code has been written to
better reflect how it might be implemented in assembly code.

// 'cx' and 'cy' denote the offset of the circle center
from the origin.
void circle(int cx, int cy, int radius)
{
  int error = -radius;
  int x = radius;
  int y = 0;

  // The following while loop may be altered to 'while
(x > y)' for a
  // performance benefit, as long as a call to
'plot4points' follows
  // the body of the loop. This allows for the
elimination of the
  // '(x != y)' test in 'plot8points', providing a
further benefit.
  //
  // For the sake of clarity, this is not shown here.
  while (x >= y)
  {
     plot8points(cx, cy, x, y);

       error += y;
       ++y;
       error += y;

    // The following test may be implemented in assembly
language in
    // most machines by testing the carry flag after
adding 'y' to
// the value of 'error' in the previous step, since
'error'
    // nominally has a negative value.
    if (error >= 0)
    {
      error -= x;
      --x;
      error -= x;
    }
  }
}

void plot8points(int cx, int cy, int x, int y)
{
  plot4points(cx, cy, x, y);
  if (x != y) plot4points(cx, cy, y, x);
}

// The '(x != 0 && y != 0)' test in the last line of
this function
// may be omitted for a performance benefit if the
radius of the
// circle is known to be non-zero.
void plot4points(int cx, int cy, int x, int y)
{
   setPixel(cx + x, cy + y);
   if (x != 0) setPixel(cx - x, cy + y);
   if (y != 0) setPixel(cx + x, cy - y);
   if (x != 0 && y != 0) setPixel(cx - x, cy - y);
}
[edit]Drawing incomplete octants
The implementations above always only draw complete octants or circles.
To draw only a certain arc from an angle        to an angle , the algorithm
needs first to calculate the     and coordinates of these end points, where
it is necessary to resort to trigonometric or square root computations
(see Methods of computing square roots). Then the Bresenham algorithm is
run over the complete octant or circle and sets the pixels only if they fall into
the wanted interval. After finishing this arc, the algorithm can be ended
prematurely.

Note that if the angles are given as slopes, then no trigonometry or square
roots are required: one simply checks that          is between the desired
slopes.

[edit]Ellipses
It is possible to generalize the algorithm to handle ellipses (of which circles
are a special case). These algorithms involve calculating a full quadrant of
the ellipse, as opposed to an octant as explained above, since non-circular
ellipses lack the x-y symmetry of a circle.

One such algorithm is presented in the paper "A Fast Bresenham Type
Algorithm For Drawing Ellipses" by John Kennedy. [1]

[edit]Parabola
It is also possible to use the same concept to plot a parabola using any
programming language.

[edit]References


    1.   ^ Donald Hearn; M. Pauline Baker. Computer graphics. Prentice-

         Hall. ISBN 978-0-13-161530-4.

    2.   ^ Pitteway, M.L.V., "Algorithm for Drawing Ellipses or Hyperbolae with a

         Digital Plotter", Computer J., 10(3) November 1967, pp 282-289

    3.   ^ Van Aken, J.R., "An Efficient Ellipse Drawing Algorithm", CG&A, 4(9),

         September 1984, pp 24-3

Más contenido relacionado

La actualidad más candente

Introduction to differentiation
Introduction to differentiationIntroduction to differentiation
Introduction to differentiationShaun Wilson
 
12.5. vector valued functions
12.5. vector valued functions12.5. vector valued functions
12.5. vector valued functionsmath267
 
Application of differentiation
Application of differentiationApplication of differentiation
Application of differentiationLily Maryati
 
Applications of maxima and minima
Applications of maxima and minimaApplications of maxima and minima
Applications of maxima and minimarouwejan
 
The Application of Derivatives
The Application of DerivativesThe Application of Derivatives
The Application of Derivativesdivaprincess09
 
Newton-Raphson Method
Newton-Raphson MethodNewton-Raphson Method
Newton-Raphson MethodJigisha Dabhi
 
Lesson 19: Maximum and Minimum Values
Lesson 19: Maximum and Minimum ValuesLesson 19: Maximum and Minimum Values
Lesson 19: Maximum and Minimum ValuesMatthew Leingang
 
Applications of Differentiation
Applications of DifferentiationApplications of Differentiation
Applications of DifferentiationJoey Valdriz
 
Applied numerical methods lec12
Applied numerical methods lec12Applied numerical methods lec12
Applied numerical methods lec12Yasser Ahmed
 
Calculus AB - Slope of secant and tangent lines
Calculus AB - Slope of secant and tangent linesCalculus AB - Slope of secant and tangent lines
Calculus AB - Slope of secant and tangent linesKenyon Hundley
 
Intelligent Process Control Using Neural Fuzzy Techniques ~陳奇中教授演講投影片
Intelligent Process Control Using Neural Fuzzy Techniques ~陳奇中教授演講投影片Intelligent Process Control Using Neural Fuzzy Techniques ~陳奇中教授演講投影片
Intelligent Process Control Using Neural Fuzzy Techniques ~陳奇中教授演講投影片Chyi-Tsong Chen
 
Lesson 28: The Fundamental Theorem of Calculus
Lesson 28: The Fundamental Theorem of CalculusLesson 28: The Fundamental Theorem of Calculus
Lesson 28: The Fundamental Theorem of CalculusMatthew Leingang
 
Application of derivatives
Application of derivativesApplication of derivatives
Application of derivativesindu thakur
 

La actualidad más candente (16)

Introduction to differentiation
Introduction to differentiationIntroduction to differentiation
Introduction to differentiation
 
Application of derivative
Application of derivativeApplication of derivative
Application of derivative
 
12.5. vector valued functions
12.5. vector valued functions12.5. vector valued functions
12.5. vector valued functions
 
Application of differentiation
Application of differentiationApplication of differentiation
Application of differentiation
 
Applications of maxima and minima
Applications of maxima and minimaApplications of maxima and minima
Applications of maxima and minima
 
The Application of Derivatives
The Application of DerivativesThe Application of Derivatives
The Application of Derivatives
 
Newton-Raphson Method
Newton-Raphson MethodNewton-Raphson Method
Newton-Raphson Method
 
Lesson 19: Maximum and Minimum Values
Lesson 19: Maximum and Minimum ValuesLesson 19: Maximum and Minimum Values
Lesson 19: Maximum and Minimum Values
 
Applications of Differentiation
Applications of DifferentiationApplications of Differentiation
Applications of Differentiation
 
Applied numerical methods lec12
Applied numerical methods lec12Applied numerical methods lec12
Applied numerical methods lec12
 
Calculus AB - Slope of secant and tangent lines
Calculus AB - Slope of secant and tangent linesCalculus AB - Slope of secant and tangent lines
Calculus AB - Slope of secant and tangent lines
 
Intelligent Process Control Using Neural Fuzzy Techniques ~陳奇中教授演講投影片
Intelligent Process Control Using Neural Fuzzy Techniques ~陳奇中教授演講投影片Intelligent Process Control Using Neural Fuzzy Techniques ~陳奇中教授演講投影片
Intelligent Process Control Using Neural Fuzzy Techniques ~陳奇中教授演講投影片
 
Differentiation
DifferentiationDifferentiation
Differentiation
 
Lesson 28: The Fundamental Theorem of Calculus
Lesson 28: The Fundamental Theorem of CalculusLesson 28: The Fundamental Theorem of Calculus
Lesson 28: The Fundamental Theorem of Calculus
 
Rules of derivative
Rules of derivativeRules of derivative
Rules of derivative
 
Application of derivatives
Application of derivativesApplication of derivatives
Application of derivatives
 

Destacado

Bresenham's line algo.
Bresenham's line algo.Bresenham's line algo.
Bresenham's line algo.Mohd Arif
 
Line drawing algo.
Line drawing algo.Line drawing algo.
Line drawing algo.Mohd Arif
 
Computer Graphics Notes (B.Tech, KUK, MDU)
Computer Graphics Notes (B.Tech, KUK, MDU)Computer Graphics Notes (B.Tech, KUK, MDU)
Computer Graphics Notes (B.Tech, KUK, MDU)Rajesh Kamboj
 
Bresenham Line Drawing Algorithm
Bresenham Line Drawing AlgorithmBresenham Line Drawing Algorithm
Bresenham Line Drawing AlgorithmMahesh Kodituwakku
 
Computer Graphics - Bresenham's line drawing algorithm & Mid Point Circle alg...
Computer Graphics - Bresenham's line drawing algorithm & Mid Point Circle alg...Computer Graphics - Bresenham's line drawing algorithm & Mid Point Circle alg...
Computer Graphics - Bresenham's line drawing algorithm & Mid Point Circle alg...Saikrishna Tanguturu
 

Destacado (6)

Bresenham's line algo.
Bresenham's line algo.Bresenham's line algo.
Bresenham's line algo.
 
Bresenham circle
Bresenham circleBresenham circle
Bresenham circle
 
Line drawing algo.
Line drawing algo.Line drawing algo.
Line drawing algo.
 
Computer Graphics Notes (B.Tech, KUK, MDU)
Computer Graphics Notes (B.Tech, KUK, MDU)Computer Graphics Notes (B.Tech, KUK, MDU)
Computer Graphics Notes (B.Tech, KUK, MDU)
 
Bresenham Line Drawing Algorithm
Bresenham Line Drawing AlgorithmBresenham Line Drawing Algorithm
Bresenham Line Drawing Algorithm
 
Computer Graphics - Bresenham's line drawing algorithm & Mid Point Circle alg...
Computer Graphics - Bresenham's line drawing algorithm & Mid Point Circle alg...Computer Graphics - Bresenham's line drawing algorithm & Mid Point Circle alg...
Computer Graphics - Bresenham's line drawing algorithm & Mid Point Circle alg...
 

Similar a Rasterisation of a circle by the bresenham algorithm

The Day You Finally Use Algebra: A 3D Math Primer
The Day You Finally Use Algebra: A 3D Math PrimerThe Day You Finally Use Algebra: A 3D Math Primer
The Day You Finally Use Algebra: A 3D Math PrimerJanie Clayton
 
Open GL T0074 56 sm4
Open GL T0074 56 sm4Open GL T0074 56 sm4
Open GL T0074 56 sm4Roziq Bahtiar
 
Computer_Graphics_circle_drawing_techniq.ppt
Computer_Graphics_circle_drawing_techniq.pptComputer_Graphics_circle_drawing_techniq.ppt
Computer_Graphics_circle_drawing_techniq.pptAliZaib71
 
8.further calculus Further Mathematics Zimbabwe Zimsec Cambridge
8.further calculus   Further Mathematics Zimbabwe Zimsec Cambridge8.further calculus   Further Mathematics Zimbabwe Zimsec Cambridge
8.further calculus Further Mathematics Zimbabwe Zimsec Cambridgealproelearning
 
Line Drawing Algorithms - Computer Graphics - Notes
Line Drawing Algorithms - Computer Graphics - NotesLine Drawing Algorithms - Computer Graphics - Notes
Line Drawing Algorithms - Computer Graphics - NotesOmprakash Chauhan
 
Computer Graphics Unit 2
Computer Graphics Unit 2Computer Graphics Unit 2
Computer Graphics Unit 2SanthiNivas
 
cgrchapter2version-1-200729063505 (1).pdf
cgrchapter2version-1-200729063505 (1).pdfcgrchapter2version-1-200729063505 (1).pdf
cgrchapter2version-1-200729063505 (1).pdfmeenasp
 
Computer graphic
Computer graphicComputer graphic
Computer graphicnusratema1
 
Computer Graphics in Java and Scala - Part 1
Computer Graphics in Java and Scala - Part 1Computer Graphics in Java and Scala - Part 1
Computer Graphics in Java and Scala - Part 1Philip Schwarz
 
Applications of Differential Calculus in real life
Applications of Differential Calculus in real life Applications of Differential Calculus in real life
Applications of Differential Calculus in real life OlooPundit
 
Output primitives in Computer Graphics
Output primitives in Computer GraphicsOutput primitives in Computer Graphics
Output primitives in Computer GraphicsKamal Acharya
 
Trapezoidal Method IN Numerical Analysis
Trapezoidal Method IN  Numerical AnalysisTrapezoidal Method IN  Numerical Analysis
Trapezoidal Method IN Numerical AnalysisMostafijur Rahman
 
Chapter 3 Output Primitives
Chapter 3 Output PrimitivesChapter 3 Output Primitives
Chapter 3 Output PrimitivesPrathimaBaliga
 
IN PYTHON 1134 final project 1 ellipses For this lab.pdf
IN PYTHON 1134 final project 1  ellipses  For this lab.pdfIN PYTHON 1134 final project 1  ellipses  For this lab.pdf
IN PYTHON 1134 final project 1 ellipses For this lab.pdfadithiyaatextile
 
Natural and Clamped Cubic Splines
Natural and Clamped Cubic SplinesNatural and Clamped Cubic Splines
Natural and Clamped Cubic SplinesMark Brandao
 
chapter 3 , foley.pptxhuujjjjjjjkjmmmm. Ibibhvucufucuvivihohi
chapter 3 , foley.pptxhuujjjjjjjkjmmmm.  Ibibhvucufucuvivihohichapter 3 , foley.pptxhuujjjjjjjkjmmmm.  Ibibhvucufucuvivihohi
chapter 3 , foley.pptxhuujjjjjjjkjmmmm. Ibibhvucufucuvivihohi54MahakBansal
 
Erin catto numericalmethods
Erin catto numericalmethodsErin catto numericalmethods
Erin catto numericalmethodsoscarbg
 

Similar a Rasterisation of a circle by the bresenham algorithm (20)

The Day You Finally Use Algebra: A 3D Math Primer
The Day You Finally Use Algebra: A 3D Math PrimerThe Day You Finally Use Algebra: A 3D Math Primer
The Day You Finally Use Algebra: A 3D Math Primer
 
Open GL T0074 56 sm4
Open GL T0074 56 sm4Open GL T0074 56 sm4
Open GL T0074 56 sm4
 
Cs580
Cs580Cs580
Cs580
 
Computer_Graphics_circle_drawing_techniq.ppt
Computer_Graphics_circle_drawing_techniq.pptComputer_Graphics_circle_drawing_techniq.ppt
Computer_Graphics_circle_drawing_techniq.ppt
 
Line circle draw
Line circle drawLine circle draw
Line circle draw
 
10994479.ppt
10994479.ppt10994479.ppt
10994479.ppt
 
8.further calculus Further Mathematics Zimbabwe Zimsec Cambridge
8.further calculus   Further Mathematics Zimbabwe Zimsec Cambridge8.further calculus   Further Mathematics Zimbabwe Zimsec Cambridge
8.further calculus Further Mathematics Zimbabwe Zimsec Cambridge
 
Line Drawing Algorithms - Computer Graphics - Notes
Line Drawing Algorithms - Computer Graphics - NotesLine Drawing Algorithms - Computer Graphics - Notes
Line Drawing Algorithms - Computer Graphics - Notes
 
Computer Graphics Unit 2
Computer Graphics Unit 2Computer Graphics Unit 2
Computer Graphics Unit 2
 
cgrchapter2version-1-200729063505 (1).pdf
cgrchapter2version-1-200729063505 (1).pdfcgrchapter2version-1-200729063505 (1).pdf
cgrchapter2version-1-200729063505 (1).pdf
 
Computer graphic
Computer graphicComputer graphic
Computer graphic
 
Computer Graphics in Java and Scala - Part 1
Computer Graphics in Java and Scala - Part 1Computer Graphics in Java and Scala - Part 1
Computer Graphics in Java and Scala - Part 1
 
Applications of Differential Calculus in real life
Applications of Differential Calculus in real life Applications of Differential Calculus in real life
Applications of Differential Calculus in real life
 
Output primitives in Computer Graphics
Output primitives in Computer GraphicsOutput primitives in Computer Graphics
Output primitives in Computer Graphics
 
Trapezoidal Method IN Numerical Analysis
Trapezoidal Method IN  Numerical AnalysisTrapezoidal Method IN  Numerical Analysis
Trapezoidal Method IN Numerical Analysis
 
Chapter 3 Output Primitives
Chapter 3 Output PrimitivesChapter 3 Output Primitives
Chapter 3 Output Primitives
 
IN PYTHON 1134 final project 1 ellipses For this lab.pdf
IN PYTHON 1134 final project 1  ellipses  For this lab.pdfIN PYTHON 1134 final project 1  ellipses  For this lab.pdf
IN PYTHON 1134 final project 1 ellipses For this lab.pdf
 
Natural and Clamped Cubic Splines
Natural and Clamped Cubic SplinesNatural and Clamped Cubic Splines
Natural and Clamped Cubic Splines
 
chapter 3 , foley.pptxhuujjjjjjjkjmmmm. Ibibhvucufucuvivihohi
chapter 3 , foley.pptxhuujjjjjjjkjmmmm.  Ibibhvucufucuvivihohichapter 3 , foley.pptxhuujjjjjjjkjmmmm.  Ibibhvucufucuvivihohi
chapter 3 , foley.pptxhuujjjjjjjkjmmmm. Ibibhvucufucuvivihohi
 
Erin catto numericalmethods
Erin catto numericalmethodsErin catto numericalmethods
Erin catto numericalmethods
 

Rasterisation of a circle by the bresenham algorithm

  • 1. Bresenham algorithm for a circle Applet Select one point on circle. Bresenham's algorithm for circle rastering: view plaincopy to clipboardprint 1. // circle center xi,yi and point on circle xf,yf 2. public void circle(int xi, int yi, int xf, int yf){ 3. // calculate radius of circle 4. int radius=(int)(Math.sqrt((xf-xi)*(xf-xi)+(yf-yi)*(yf-yi))); 5. 6. int d1 = 3 - (2 * radius); 7. int x = 0,i1=x1,i2=x2; 8. 9. int y = radius; 10. boolean rov=true; 11. 12. // for one eights, while x is not >= y 13. while (rov){ 14. if (x>=y){rov=false;} 15. if (d1 < 0) { d1 = d1 + (4 * x) + 6; } 16. else{ d1 = d1 + 4 * (x - y) + 10; // (1) 17. y = y - 1; 18. } 19. 20. // draw following circle points 21. setpix((x1+x),(y1+y)); 22. setpix((x1+x),(y1-y)); 23. setpix((x1-x),(y1+y)); 24. setpix((x1-x),(y1-y)); 25. 26. setpix((x1+y),(y1+x)); 27. setpix((x1+y),(y1-x)); 28. setpix((x1-y),(y1+x)); 29. setpix((x1-y),(y1-x)); 30. x++; 31. } 32. }
  • 2. Circle Mid point algorithm Rasterisation of a circle by the Bresenham algorithm [2] [3] The algorithm is related to work by Pitteway and Van Aken. The algorithm This article may be confusing or unclear to readers. Please help us clarify the article; suggestions may be found on the talk page. (February 2009) The algorithm starts with the circle equation . For simplicity, assume the center of the circle is at . We consider first only the first octant and draw a curve which starts at point and proceeds counterclockwise, reaching the angle of 45. The "fast" direction here (the basis vector with the greater increase in value) is the direction. The algorithm always takes a step in the positive direction (upwards), and occasionally takes a step in the "slow" direction (the negative direction). From the circle equation we obtain the transformed equation , where is computed only a single time during initialization. Let the points on the circle be a sequence of coordinates of the vector to the point (in the usual basis). Points are numbered according to the order in which they are drawn, with assigned to the first point . For each point, the following holds: This can be rearranged as follows:
  • 3. And likewise for the next point: In general, it is true that: So we refashion our next-point-equation into a recursive one by substituting : Because of the continuity of a circle and because the maxima along both axes is the same, we know we will not be skipping x points as we advance in the sequence. Usually we will stay on the same x coordinate, and sometimes advance by one. The resulting co-ordinate is then translated by adding the midpoint coordinates. These frequent integer additions do not limit the performance much, as we can spare those square (root) computations in the inner loop in turn. Again, the zero in the transformed circle equation is replaced by the error term. The initialization of the error term is derived from an offset of ½ pixel at the start. Until the intersection with the perpendicular line, this leads to an accumulated value of in the error term, so that this value is used for initialization. The frequent computations of squares in the circle equation, trigonometric expressions and square roots can again be avoided by dissolving everything into single steps and using recursive computation of the quadratic terms from the preceding iterations. Below is an implementation of the Bresenham Algorithm for a full circle in C. Here another variable for recursive computation of the quadratic terms is used, which corresponds with the term above. It just has to be increased by 2 from one step to the next: void rasterCircle(int x0, int y0, int radius) { int f = 1 - radius; int ddF_x = 1; int ddF_y = -2 * radius; int x = 0; int y = radius; setPixel(x0, y0 + radius); setPixel(x0, y0 - radius); setPixel(x0 + radius, y0); setPixel(x0 - radius, y0);
  • 4. while(x < y) { // ddF_x == 2 * x + 1; // ddF_y == -2 * y; // f == x*x + y*y - radius*radius + 2*x - y + 1; if(f >= 0) { y--; ddF_y += 2; f += ddF_y; } x++; ddF_x += 2; f += ddF_x; setPixel(x0 + x, y0 + y); setPixel(x0 - x, y0 + y); setPixel(x0 + x, y0 - y); setPixel(x0 - x, y0 - y); setPixel(x0 + y, y0 + x); setPixel(x0 - y, y0 + x); setPixel(x0 + y, y0 - x); setPixel(x0 - y, y0 - x); } } It is interesting to note that there is correlation between this algorithm and the sum of first odd numbers, which this one basically does. That is, In summary, when we compare the sum of N odd numbers to this algorithm we have: ddF_y = -2 * radius is connected to last member of sum of N odd numbers. This member has index equal to value of radius (integral). Since odd number is 2*n + 1 there is 1 handled elsewhere or it should be -2*radius - 1 ddF_x = 0 should be 1. Because difference between two consecutive odd numbers is 2. If so f += ddF_y + 1 is f+= ddF_y. Saving one operation. f = - radius + 1 Initial error equal to half of "bigger" step.
  • 5. In case of saving one addition it should be either -radius or -radius + 2. In any case there should be addition of 1 driven out of outer loop. So. f += ddF_y Adding odd numbers from Nth to 1st. f += ddF_x Adding odd numbers from 1st to Nth. 1 is missing because it can be moved outside of loop. [edit]Optimization The following adaption of the circle algorithm is useful for machines where registers are scarce. Only three variables are needed in the main loop to perform the calculation. Although written in C, the code has been written to better reflect how it might be implemented in assembly code. // 'cx' and 'cy' denote the offset of the circle center from the origin. void circle(int cx, int cy, int radius) { int error = -radius; int x = radius; int y = 0; // The following while loop may be altered to 'while (x > y)' for a // performance benefit, as long as a call to 'plot4points' follows // the body of the loop. This allows for the elimination of the // '(x != y)' test in 'plot8points', providing a further benefit. // // For the sake of clarity, this is not shown here. while (x >= y) { plot8points(cx, cy, x, y); error += y; ++y; error += y; // The following test may be implemented in assembly language in // most machines by testing the carry flag after adding 'y' to
  • 6. // the value of 'error' in the previous step, since 'error' // nominally has a negative value. if (error >= 0) { error -= x; --x; error -= x; } } } void plot8points(int cx, int cy, int x, int y) { plot4points(cx, cy, x, y); if (x != y) plot4points(cx, cy, y, x); } // The '(x != 0 && y != 0)' test in the last line of this function // may be omitted for a performance benefit if the radius of the // circle is known to be non-zero. void plot4points(int cx, int cy, int x, int y) { setPixel(cx + x, cy + y); if (x != 0) setPixel(cx - x, cy + y); if (y != 0) setPixel(cx + x, cy - y); if (x != 0 && y != 0) setPixel(cx - x, cy - y); } [edit]Drawing incomplete octants The implementations above always only draw complete octants or circles. To draw only a certain arc from an angle to an angle , the algorithm needs first to calculate the and coordinates of these end points, where it is necessary to resort to trigonometric or square root computations (see Methods of computing square roots). Then the Bresenham algorithm is run over the complete octant or circle and sets the pixels only if they fall into the wanted interval. After finishing this arc, the algorithm can be ended prematurely. Note that if the angles are given as slopes, then no trigonometry or square roots are required: one simply checks that is between the desired slopes. [edit]Ellipses It is possible to generalize the algorithm to handle ellipses (of which circles are a special case). These algorithms involve calculating a full quadrant of
  • 7. the ellipse, as opposed to an octant as explained above, since non-circular ellipses lack the x-y symmetry of a circle. One such algorithm is presented in the paper "A Fast Bresenham Type Algorithm For Drawing Ellipses" by John Kennedy. [1] [edit]Parabola It is also possible to use the same concept to plot a parabola using any programming language. [edit]References 1. ^ Donald Hearn; M. Pauline Baker. Computer graphics. Prentice- Hall. ISBN 978-0-13-161530-4. 2. ^ Pitteway, M.L.V., "Algorithm for Drawing Ellipses or Hyperbolae with a Digital Plotter", Computer J., 10(3) November 1967, pp 282-289 3. ^ Van Aken, J.R., "An Efficient Ellipse Drawing Algorithm", CG&A, 4(9), September 1984, pp 24-3