Bresenham's circle drawing algorithm | Code in C++ and Python

 
Bresenham's circle drawing algorithm

Introduction to Bresenham's Circle Algorithm

The usage of trigonometric and power functions must be avoided if a circle is to be plotted efficiently. It's also preferable to execute the computations required to identify the scan-converted points using only integer addition, subtraction, and multiplication by powers of two, as with the construction of a straight line. These objectives can be satisfied due to Bresenham's circle drawing algorithm. Bresenham's algorithm for scanning and converting a circle works as follows. When the eight-way symmetry of a circle is applied to make a circle, just a 45-degree angle is required to generate points. Furthermore, if points are formed from 90 degrees to 45 degrees, just the +x and -y directions are used, as illustrated in Figure 01. 


Circle's scan converted with Bresenham's algorithm
Fig. 01: Circle's scan converted with Bresenham's algorithm


Advantages of Bresenham's circle drawing algorithm

  1. It is fast to draw a circle.
  2. It is an incremental process.
  3. It needs only integer values.
  4. Only three arithmetic operations are needed to perform the algorithm (addition, subtraction, and multiplication)
  5. It used only fixed points generated based on the distance variable.


Disadvantages of Bresenham's circle drawing algorithm

  1. The circle generated by this algorithm is not accurate.
  2. The edge of Bresenham's circle drawing algorithm's circle is not smooth.



Bresenham's circle drawing algorithm

step 1: Firstly, take the input of the circle's radius. The parameter of the radius is set to be 'r'.

step 2: set x->0, y->r, and the distance d-> 3 - 2r. 

step 3: Set the initial points of the x and y coordinates in the frame for drawing the circle in the frame. (These variables are declared in the Python code as 'xc' and 'yc'. In c++, these variables are auto default in the center position for the 'graphics.h' heading file)

step 4: Run a while loop until the condition is satisfied (condition: while the x is less than or equal to y). The while loop runs until the circle is drawn. ( until step 4, 5, and 6 is satisfied).

step 5: Set the pixel to the x and y points under step 2.

step 6: increase the x variable with 1 which is x->x+1

step 7: Set conditions for the distance variable 'd'. If the distance 'd' is less than zero than,

    d-> d + 4x + 6

else,

    d-> d + 4(x-y) + 10, and y-> y-1


Bresenham's circle drawing algorithm using C++

#include <bits/stdc++.h>

using namespace std;

int main()

{

    int r;

    cin>>r;

    int x = 0, y = r, d = 3-2r;

    while(x <= y) 

        {

            setPixel(x, y);

                if (d < 0) {

                    d = d + 4x + 6; 

                }

                else {

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

                    y--;

                }

        x++;

    }

    return 0;

}


It's worth noting that at the start of each iteration of the while loop, we set a pixel whose position has already been calculated (0, r). Then, in order to update d and establish the correct y coordinate of the next pixel, we examine the current value of decision variable d. Finally, we raise the value of the variable x. 


Bresenham's circle drawing algorithm using python

from PIL import Image as img

def drawCircle(xc, yc ,x, y,im):

im.putpixel((xc+x, yc+y), 0)
im.putpixel((xc-x, yc+y), 0)
im.putpixel((xc+x, yc-y), 0)
im.putpixel((xc-x, yc-y), 0)
im.putpixel((xc+y, yc+x), 0)
im.putpixel((xc-y, yc+x), 0)
im.putpixel((xc+y, yc-x), 0)
im.putpixel((xc-y, yc-x), 0)

def circleBres(xc,yc,r) :
im = img.new(mode='1', size=(1000,1000),color=1)
x,y = 0,r
d = 3 - 2 * r
drawCircle(xc, yc, x, y,im)
while (y >= x):
x+=1
if (d > 0):
y-=1
d = d + 4 * (x - y) + 10

else:
d = d + 4 * x + 6
drawCircle(xc, yc, x, y,im)
im.save('Bresenhams Circle Output.png')
im.show()


if __name__=='__main__':
r=int(input())
circleBres(500,400,r)

একটি মন্তব্য পোস্ট করুন

0 মন্তব্যসমূহ