Question

In: Computer Science

How can you generate a circle of center C and radius R using Bresenhem’s algorithm? Also,...

How can you generate a circle of center C and radius R using Bresenhem’s algorithm? Also, make flow chart for this algorithm. Write a C/C++ program to implement Bresenhem’s circle algorithm.

Solutions

Expert Solution

The Bresenham's circle drawing algorithm.

the whole 360 degree of circle we will divide it in 8-parts each octant of 45 degree. In order to that we will use Bresenham's Circle Algorithm for calculation of the locations of the pixels in the first octant of 45 degrees. It assumes that the circle is centered on the origin. So for every pixel (x, y) it calculates, we draw a pixel in each of the 8 octants of the circle as shown below :-


Explanation:

Now, we will see how to calculate the next pixel location from a previously known pixel location (x, y). In Bresenham's algorithm at any point (x, y) we have two option either to choose the next pixel in the east i.e. (x+1, y) or in the south east i.e. (x+1, y-1).

And this can be decided by using the decision parameter d as:

  • If d > 0, then (x+1, y-1) is to be chosen as the next pixel as it will be closer to the arc.
  • else (x+1, y) is to be chosen as next pixel.

Now to draw the circle for a given radius 'r' and centre (xc, yc) We will start from (0, r) and move in first quadrant till x=y (i.e. 45 degree). We should start from listed initial condition:

d = 3 - (2 * r)
x = 0
y = r

Now for each pixel, we will do the following operations:

  1. Set initial values of (xc, yc) and (x, y)
  2. Set decision parameter d to d = 3 - (2 * r).
  3. call drawCircle(int xc, int yc, int x, int y) function.
  4. Repeat steps 5 to 8 until x < = y
  5. Increment value of x.
  6. If d < 0, set d = d + (4*x) + 6
  7. Else, set d = d + 4 * (x - y) + 10 and decrement y by 1.
  8. call drawCircle(int xc, int yc, int x, int y) function

Draw circle() function.

// function to draw all other 7 pixels 

// present at symmetric position 

drawCircle(int xc, int yc, int x, int y) 

{ 

    putpixel(xc+x, yc+y, RED); 

    putpixel(xc-x, yc+y, RED); 

    putpixel(xc+x, yc-y, RED); 

    putpixel(xc-x, yc-y, RED); 

    putpixel(xc+y, yc+x, RED); 

    putpixel(xc-y, yc+x, RED); 

    putpixel(xc+y, yc-x, RED); 

    putpixel(xc-y, yc-x, RED); 

} 



Below is C implementation of above approach.



// C-program for circle drawing 

// using Bresenham's Algorithm 

// in computer-graphics 

#include <stdio.h> 

#include <dos.h> 

#include <graphics.h> 

  

// Function to put pixels 

// at subsequence points 

void drawCircle(int xc, int yc, int x, int y) 

{ 

    putpixel(xc+x, yc+y, RED); 

    putpixel(xc-x, yc+y, RED); 

    putpixel(xc+x, yc-y, RED); 

    putpixel(xc-x, yc-y, RED); 

    putpixel(xc+y, yc+x, RED); 

    putpixel(xc-y, yc+x, RED); 

    putpixel(xc+y, yc-x, RED); 

    putpixel(xc-y, yc-x, RED); 

} 

  

// Function for circle-generation 

// using Bresenham's algorithm 

void circleBres(int xc, int yc, int r) 

{ 

    int x = 0, y = r; 

    int d = 3 - 2 * r; 

    drawCircle(xc, yc, x, y); 

    while (y >= x) 

    { 

        // for each pixel we will 

        // draw all eight pixels 

          

        x++; 

  

        // check for decision parameter 

        // and correspondingly  

        // update d, x, y 

        if (d > 0) 

        { 

            y--;  

            d = d + 4 * (x - y) + 10; 

        } 

        else

            d = d + 4 * x + 6; 

        drawCircle(xc, yc, x, y); 

        delay(50); 

    } 

} 

  

  

// driver function 

int main() 

{ 

    int xc = 50, yc = 50, r2 = 30; 

    int gd = DETECT, gm; 

    initgraph(&gd, &gm, "");  // initialize graph 

    circleBres(xc, yc, r);    // function call 

    return 0; 

} 

Output:


Related Solutions

a- If the radius r of a circle is measured to be r  0.325 +\-...
a- If the radius r of a circle is measured to be r  0.325 +\- 0.001 m, then calculate the area of the circle A =  3.14r 2 , as well as the uncertainty in the area. b-Explain the difference between precision and accuracy. c-Do systematic errors affect precision or accuracy? Do random errors affect precision or accuracy?
1. If the radius r of a circle is measured to be r  0.325 +\-...
1. If the radius r of a circle is measured to be r  0.325 +\- 0.001 m, then calculate the area of the circle A =3.14 r^2, as well as the uncertainty in the area. 2. Explain the difference between precision and accuracy. 3. Do systematic errors affect precision or accuracy? Do random errors affect precision or accuracy?
3. Use the mid-point circle algorithm to draw the circle centred at origin with radius 12....
3. Use the mid-point circle algorithm to draw the circle centred at origin with radius 12. 4. Use midpoint ellipse algorithm with radius rx=4 and ry=10 and centred at the origin.
1) A circle of radius r has area A = π r2. If a random circle...
1) A circle of radius r has area A = π r2. If a random circle has a radius that is evenly distributed in the interval (0, 1), what are the mean and variance of the area of ​​the circle? choose the correct answer A) 1/3 and 1/12 B) Pi/3 and 1/12 C) Pi/3 and 1/5 D) Pi/3 and (4/45)*Pi^2
The questions in this assignment are about a circle with center at (-9, -12) and radius...
The questions in this assignment are about a circle with center at (-9, -12) and radius 15. The equation for the circle in function form is: y = ±√(225 - (x - 9)^2) - 12, however there may be mistakes in the equation. Identify all of the locations of mistakes. a) Find the intersections of the given circle with the y axis. What number is the y coordinate of the lower intersection? b) What number is the y coordinate of...
If a circle C with radius 1 rolls along the outside of the circle x2 +...
If a circle C with radius 1 rolls along the outside of the circle x2 + y2 = 36, a fixed point P on C traces out a curve called an epicycloid, with parametric equations x = 7 cos(t) − cos(7t), y = 7 sin(t) − sin(7t). Graph the epicycloid. Find the area it encloses.
15. Let r be a positive real number. The equation for a circle of radius r...
15. Let r be a positive real number. The equation for a circle of radius r whose center is the origin is (x^2)+(y^2)= r^2 . (a) Use implicit differentiation to determine dy/dx . (b) Let (a,b) be a point on the circle with a does not equal 0 and b does not equal 0. Determine the slope of the line tangent to the circle at the point (a,b). (c) Prove that the radius of the circle to the point (a,b)...
Write a C++ program that prompts the user for the radius of a circle and then...
Write a C++ program that prompts the user for the radius of a circle and then calls inline function circleArea to calculate the area of that circle. It should do it repeatedly until the user enters -1. Use the constant value 3.14159 for π Sample: Enter the radius of your circle (-1 to end): 1 Area of circle with radius 1 is 3.14159 Enter the radius of your circle (-1 to end): 2 Area of circle with radius 2 is...
(C++ programming) Assignment *Circle Class -Radius r (private) as an attribute variable -Member function -Get(): Function...
(C++ programming) Assignment *Circle Class -Radius r (private) as an attribute variable -Member function -Get(): Function that returns r value of property variable -Put(int d): A function that stores d in attribute variable r *Declare a one-dimensional array of type Circle and in each array element Read and store integers from standard input device *Declare the swap() function to swap two elements with each other *Write a program that sorts the elements of a one-dimensional array of circle type in...
Solve as much as you can Using the Lehmer algorithm (Zi = (a Zi-1 + c)...
Solve as much as you can Using the Lehmer algorithm (Zi = (a Zi-1 + c) (mod m)      0 < Zi < m-1                    Ui = Zi / m, with m = 16, a = 5, c = 3, and Z0 = 7, generate three random numbers (U1,U2,U3 ) ………….(SO (a)) subject is Modeling and Simulations
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT