0% found this document useful (0 votes)
20 views6 pages

Bresenham Circle

The document explains Bresenham's Circle Drawing Algorithm, which efficiently draws a circle by leveraging symmetry and decision parameters to determine pixel placement. It outlines the mathematical derivation of the algorithm, including the decision parameter calculations and the iterative process to select points on the circle. A C program example is provided to illustrate the implementation of the algorithm.

Uploaded by

AFIFA SYED
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views6 pages

Bresenham Circle

The document explains Bresenham's Circle Drawing Algorithm, which efficiently draws a circle by leveraging symmetry and decision parameters to determine pixel placement. It outlines the mathematical derivation of the algorithm, including the decision parameter calculations and the iterative process to select points on the circle. A C program example is provided to illustrate the implementation of the algorithm.

Uploaded by

AFIFA SYED
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Bresenham Circle Drawing

Symmetry:

The quality of being made up of exactly similar parts around an axis.


A circle is made up of 8 octants as seen in the figure below.

We can find any point's symmetric complement about these lines by permuting the indices. For example the point (x,y)
has a complementary point (y,x) about the line x=y. And the total set of complements for the point (x,y) are get point's
symmetric complement about these lines by permuting the indices :
{(x,-y), (-x,y), (-x,-y), (y,x), (y,-x), (-y,x),(-y,-x)}

The algorithm loops over one-eighth of the circle. Specifically, it starts at the top of the circle and goes to the right by 45
degrees, where the slope of a radial line is 1.

Bresenham Circle Drawing Derivation:

The equation of circle is x2 + y2 = r2, where r is the radius of the circle. So using this, we can write a simple circle drawing
algorithm by solving the above equation for y at unit x intervals using:

Using the above equation:

However, unsurprisingly this is not a brilliant solution!


Firstly, the resulting circle has large gaps where the slope approaches vertical.
Secondly, the calculations are not very efficient:
• Square multiply operation
• Square root operation

So, we need a more efficient and more accurate solution.


Bresenham circle drawing algorithm works by first generating from top towards right upto the 45o of the circle.

Suppose the first pixel drawn is P(xk,yk). Now to get the next pixel we will increment xk+1 but for y we have two options,
either we can select point N(xk+1,yk) or point S (xk+1,yk-1).
Now the distance between point N and arc of the circle is d1 & between point S and arc of the circle is d2 & which ever
distance is less we will select that point. For that we will derive a decision parameter and on the basis of it we will decide
which distance is less.

Equation of the circle is given by: x2 + y2 = r2 or we can say that:


f(c) = x2 + y2 – r2

we can derive the distance d1 by using the above equation on point N

f(N) = (xk+1)2 + (yk)2 - r2 value of this function will be +ve, since distance of point N is greater than radius in above fig
f(S) = (xk+1)2 + (yk-1)2 - r2 value of this function will be –ve, since distance of point S is less than radius.

Let Pk be the decision parameter


Pk = f(N) + f(S)

If(Pk < 0) i.e. f(S)>f(N) then


Select point N
Else
Select point S

Now calculate Pk:


Pk = f(N) + f(S)
Pk = (xk+1)2 + (yk)2 - r2 + (xk+1)2 + (yk-1)2 - r2

Pk = 2(xk+1)2 + (yk)2 + (yk-1)2 - 2r2 this equation will be used later to derive initial decision parameter

Now calculate Pk+1: simply replace xk with xk+1 and since we still don’t know what to take yk or yk+1 for the next pixel
represent next y as yk+1 and not as (yk+1) only in the above equation of Pk

Pk+1 = 2(xk+1+1)2 + (yk+1)2 + (yk+1 – 1)2 - 2r2

Pk+1 = 2(xk+2)2 + (yk+1)2 + (yk+1 – 1)2 - 2r2


Now, Pk+1 – Pk = 2(xk+2)2 + (yk+1)2 + (yk+1 – 1)2 - 2r2 – [2(xk+1)2 + (yk)2 + (yk-1)2 - 2r2]

Pk+1 – Pk = 2((xk)2+4xk+4)2 + (yk+1)2 + (yk+1)2 –2yk+1 +1 – 2r2 – 2((xk)2+2xk +1) – (yk)2 – ((yk)2 –2yk + 1) + 2r2

Pk+1 – Pk = 2(xk)2 + 8xk + 8 + (yk+1)2 + (yk+1)2 – 2yk+1 + 1 – 2r2 – 2(xk)2 – 4xk – 2 – (yk)2 – (yk)2 +2yk – 1 + 2r2

Pk+1 – Pk = 4xk + 6 + 2(yk+1)2 – 2yk+1 – 2(yk)2 + 2yk

Pk+1 = Pk + 4xk + 6 + 2(yk+1)2 – 2yk+1 – 2(yk)2 + 2yk - ------- (x)

If(Pk < 0) then


Select point N(xk+1,yk) -> replace yk+1 with yk in above derived equation (x)

Pk+1 = Pk + 4xk + 6 + 2(yk)2 – 2yk – 2(yk)2 + 2yk

Pk+1 = Pk + 4xk + 6

Else
Select point S(xk+1,yk-1) -> replace yk+1 with yk-1 in above derived equation (x)

Pk+1 = Pk + 4xk + 6 + 2(yk-1)2 – 2yk-1 – 2(yk)2 + 2yk

Now replace yk-1 as (yk – 1) in the above equation

Pk+1 = Pk + 4xk + 6 + 2(yk – 1)2 – 2(yk – 1) – 2(yk)2 + 2yk

Pk+1 = Pk + 4xk + 6 + 2(yk)2 – 4yk + 2 – 2yk + 2 – 2(yk)2 + 2yk

Pk+1 = Pk + 4xk + 6 + 2(yk)2 – 4yk + 2 – 2yk + 2 – 2(yk)2 + 2yk

Pk+1 = Pk + 4xk – 4yk + 10

Pk+1 = Pk + 4(xk – yk) + 10

Now we will derive initial decision parameter

Pk = 2(xk+1)2 + (yk)2 + (yk-1)2 - 2r2

The initial value for the equation is (0 , r) i.e. x = 0, y = r

Pk = 2(0+1)2 + (r)2 + (r-1)2 - 2r2

Pk = 2 + r2 + r2 – 2r + 1 – 2r2

Pk = 3 + 2r2 – 2r – 2r2

P0 = 3 – 2r
Bresenham Circle Drawing Algorithm:

1. Accept center of circle (xc, yc) , radius (r)


2. Set initial values x = 0, y = r
3. Set decision parameter Pk = 3 – 2r
4. Call drawCircle(int xc, int yc, int x, int y) function
5. while x<=y
a. Increment value of x
b. If Pk<0, set Pk = Pk + 4x + 6
c. Else, set Pk = Pk + 4(x-y) + 10 & y = y – 1
d. Call drawCircle(int xc, int yc, int x, int y) function

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)

Example:
Plot a circle for Bresenham algorithm for radius = 8 & center (xc, yc) = (30, 40)

x= 0, y = 8
xc = 30, yc = 40 //center
x y P Remark x+xc y+yc
0 8 p = 3-2r = 3 – 2(8) = 3-16 = -13 p<0, x++ & change p 30 48
1 8 p = p + 4x + 6 = -13 + 4(1) +6 = -3 p<0, x++ & change p 31 48
2 8 p = p + 4x + 6 = -3 + 4(2) +6 = 11 p>0, x++, y-- & change p 32 48
3 7 p = p + 4(x-y) + 10 = 11+4(-4)+10=5 p>0, x++, y-- & change p 33 47
4 6 p = p + 4(x-y) + 10 = 5+4(-2)+10=7 p>0, x++, y-- & change p 34 46
5 5 p = p + 4(x-y) + 10 = 7+4(0)+10=17 Since x has become equal to y so 35 45
stop

Program:

// C-program for circle drawing


// using Bresenham’s Algorithm

#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 p = 3 - 2 * r;
drawCircle(xc, yc, x, y);
while (x <= y)
{
// for each pixel we will
// draw all eight pixels

x++; //since x will increment in any scenario

// check for decision parameter and correspondingly update d, x, y


if (p < 0)
{
p = p + 4 * x + 6;
}
Else
{
y--;
p = p + 4 * (x - y) + 10;
}

drawCircle(xc, yc, x, y);


delay(50);
}
}

// driver function
int main()
{
int xc = 50, yc = 50, r2 = 30;
int gd, gm;
detectgraph(&gd,&gm);
initgraph(&gd, &gm, "c:\\Turboc4\\tc\\bgi"); // initialize graph
circleBres(xc, yc, r); // function call
return 0;
}

Output:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy