You are on page 1of 14

Computer Graphics

Prepared by: Mam Amna Javed 4


Bresenham’s Circle Algorithm

Given the centre point and radius of circle,


Bresenham Circle Drawing Algorithm attempts to generate the points of one
octant.

for calculation of the locations of the pixels in the first octant of 45 degrees. It
assumes that the circle is centred on the origin. So for every pixel (x, y) it
calculates, we draw a pixel in each of the 8 octants of the circle.
The points generation using Bresenham Circle Drawing Algorithm involves the
following steps-
Step # 1:
Assign the starting point coordinates (X, Y) as-
● X=0
● Y=R
Step # 2:
Calculate the value of initial decision parameter P as-
P= 3 – 2 R
Step # 3:
Suppose the current point is (Xi, Yi) and the next point is (Xi+1, Yi+1).
Find the next point of the first octant depending on the value of decision
parameter Pi.
Follow the below two cases:
if(p<0){
Xi+1 = Xi + 1
Yi+1 = Yi
Pi+1 = Pi + 4.Xi+1 + 6
}
if(P>=0)
{
Xi+1 = Xi + 1
Yi+1 = Yi -1
Pi+1 = Pi + 4(Xi+1 - Yi+1 ) + 10
}

Step # 4:
Keep repeating Step-02 and Step-03 until Xi+1 >= Yi+1

Step # 5:
Step-04 generates all the points for one octant.
To find the points for the other seven octants for all Quadrants from 90 degree
to 45 degree, follow the eight symmetry property of the circle.

Q-I(x,y) Q-II(-x,y) Q-III(-x,-y) Q-IV(x.-y)

Limitations with Bresenham’s Algorithm:


● Accuracy of the generating points is an issue in this algorithm.
● This algorithm suffers when used to generate complex and high
graphical images.
● There is no significant enhancement with respect to performance.
Program to draw a circle using Bresenham’s Algorithm

#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <math.h>

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


{
putpixel(x+xc,y+yc,RED);
putpixel(x+xc,-y+yc,YELLOW);
putpixel(-x+xc,-y+yc,GREEN);
putpixel(-x+xc,y+yc,YELLOW);
putpixel(y+xc,x+yc,12);
putpixel(y+xc,-x+yc,14);
putpixel(-y+xc,-x+yc,15);
putpixel(-y+xc,x+yc,6);
}

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


{
int x=0,y=r,d=3-(2*r);
EightWaySymmetricPlot(xc,yc,x,y);

while(x<=y)
{
if(d<=0)
{
d=d+(4*x)+6;
}
else
{
d=d+(4*x)-(4*y)+10;
y=y-1;
}
x=x+1;
EightWaySymmetricPlot(xc,yc,x,y);
}
}

int main(void)
{
/* request auto detection */
int xc,yc,r,gdriver = DETECT, gmode, errorcode;
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "C:\\TURBOC3\\BGI");

/* read result of initialization */


errorcode = graphresult();

if (errorcode != grOk) /* an error occurred */


{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error code */
}
printf("Enter the values of xc and yc :");
scanf("%d%d",&xc,&yc);
printf("Enter the value of radius :");
scanf("%d",&r);
BresenhamCircle(xc,yc,r);

getch();
closegraph();
return 0;
}
Output

Midpoint Circle Drawing Algorithm

It is based on the following function for testing the spatial relationship between
the arbitrary point (x, y) and a circle of radius r centred at the origin

According to equation of Circle:


x2 - y2 = r2
x2 - y2 - r2 = 0
Let's suppose m is the midpoint between point A and B, so the coordinates at m
point will be (xm,, ym).
Now put these coordinates in equation:
(xm,)2 + (, ym)2- r2 = 0
If the midpoint is 0 which will be the ideal case then the midpoint will be on
exactly on circle
If it's < 0 then it means the midpoint lies inside the circle.
xi+1 = xi + 1
yi+1 = yi
If it's > 0 then it means the midpoint lies outside the circle.
xi+1 = xi + 1
yi+1 = yi - 1
Let’s calculate the midpoint
Midpoint = { ( xi + 1 + xi + 1) / 2 , (yi + yi - 1 )/2 }
Midpoint = { xi + 1, yi - 1/2 }

This is called midpoint (xi+1,yi- ) and we use it to define a decision parameter


The initial decision parameter will be:
Pi = (xi + 1)2 - ( yi - 1/2 )2 - r2

The decision parameter for the next step(incremental step) is:

Pi+1=(xi+1+1)2+(yi+1- )2- r2.

Now,

Pi+1 - Pi = { (xi+1+1)2+(yi+1- )2- r2} - {(xi + 1)2 - ( yi - 1/2 )2 - r2}

As we know xi+1 = xi+1

After solving this equation

Pi+1 = Pi + 2(xi + 1) + (y2 i+1 - yi2) -( yi+1 - yi) + 1


If the case is

Pi+1 < 0, then

yi+1 = yi

xi+1 = xi + 1

Put these values in Pi+1 = Pi + 2(xi + 1) + (y2 i+1 - yi2) -( yi+1 - yi) + 1

= Pi + 2xi + 2 + yi2 - yi2 - yi + yi + 1

= Pi + 2xi + 2 + 1

Pi+1 = Pi + 2xi + 3

If the case is

Pi+1 > = 0, then

yi+1 = yi - 1

xi+1 = xi + 1

Put these values in Pi+1 = Pi + 2(xi + 1) + (y2 i+1 - yi2) -( yi+1 - yi) + 1

= Pi + 2xi + 2 + (( yi - 1)2 - yi2) - (yi -1 - yi) + 1

= Pi + 2xi + 2 + (yi2 - 2yi + 1 - yi2) + 1 + 1

= Pi + 2xi + 2 - 2yi + 1 + 2

Pi+1 = Pi + 2xi - 2yi + 5

At initial

X = 0, y = R

Pi=(xi+1)2+(yi- )2- r2
= (0 + 1) 2 + ( r - ½)2 - r2

= 1 + r2 + 1/ 4 -r- r2
= 1/4 + 1 - r
= 5/4 - r
As 5/4 approximately equals to 1 (5/4 ~ 1)
Pi = 1 - r

Algorithm (to solve numerical)

Step1: Put x =0, y =r in equation 2

We have p=1-r

Step2: Repeat steps while x ≤ y


Plot (x, y)
If (p<0)
Then set p = p + 2x + 3
Else
p = p + 2(x-y)+5
y =y - 1 (end if)
x =x+1 (end loop)

Step3: End

Program to draw a circle using Midpoint Algorithm:

#include <graphics.h>

#include <stdlib.h>

#include <math.h>

#include <stdio.h>
#include <conio.h>

#include <iostream.h>

class bresen

float x, y,a, b, r, p;

public:

void get ();

void cal ();

};

void main ()

bresen b;

b.get ();

b.cal ();

getch ();

Void bresen :: get ()

cout<<"ENTER CENTER AND RADIUS";

cout<< "ENTER (a, b)";

cin>>a>>b;

cout<<"ENTER r";

cin>>r;
}

void bresen ::cal ()

/* request auto detection */

int gdriver = DETECT,gmode, errorcode;

int midx, midy, i;

/* initialize graphics and local variables */

initgraph (&gdriver, &gmode, " ");

/* read result of initialization */

errorcode = graphresult ();

if (errorcode ! = grOK) /*an error occurred */

printf("Graphics error: %s \n", grapherrormsg (errorcode);

printf ("Press any key to halt:");

getch ();

exit (1); /* terminate with an error code */

x=0;

y=r;

putpixel (a, b+r, RED);

putpixel (a, b-r, RED);

putpixel (a-r, b, RED);

putpixel (a+r, b, RED);

p=5/4)-r;
while (x<=y)

If (p<0)

p+= (4*x)+6;

else

p+=(2*(x-y))+5;

y--;

x++;

putpixel (a+x, b+y, RED);

putpixel (a-x, b+y, RED);

putpixel (a+x, b-y, RED);

putpixel (a+x, b-y, RED);

putpixel (a+x, b+y, RED);

putpixel (a+x, b-y, RED);

putpixel (a-x, b+y, RED);

putpixel (a-x, b-y, RED);

} :

Output
PAST PAPER QUESTIONS

Question # 1:
Consider three different raster systems with resolutions 640 by 480,1280 by
1024 and 2560 by 2048 .
a) What frame size will require in byte to these systems to store 12 bits per
pixel.
b) How much storage will be required if 24 bits per pixel are to be stored?

Solution:

To solve these questions, we need to calculate the storage required for each
raster system based on the given resolutions and bits per pixel (bpp).

a) Storage required for 12 bits per pixel (bpp)

Formula:
Storage (in bytes)=(Resolution (width x height) in pixels×Bits per pixel (bpp)) / 8

For 12 bpp:

​ For 640x480 resolution:


​ Storage= (640×480×12​) / 8 = 460800 bytes

​ For 1280x1024 resolution:
​ Storage=(1280×1024×12) / 8 = 1966080 bytes
​ ​
​ For 2560x2048 resolution:
​ Storage=(2560×2048×12) / 8 = 7864320 bytes

b) Storage required for 24 bits per pixels (bpp)

​ For 640x480 resolution:


​ Storage= (640×480×24​) / 8 = 921600 bytes

​ For 1280x1024 resolution:


​ Storage=(1280×1024×24) / 8 = 3932160 bytes
​ ​
​ For 2560x2048 resolution:
​ Storage=(2560×2048×24) / 8 = 15728640 bytes

Question # 2:
Develop a mid point algorithm which scan converts a circle from start at pixel
location x=R, y= 0, Assume you're scan converting in the clockwise direction,
find equations, along with choices should be made for the next pixel.
Do the same derivation like mentioned in the above heading (MID POINT
ALGORITHM CIRCLE DRAWING) till this equation Pi+1 = Pi + 2xi + 2yi -
5
After this , At initial x=R, y= 0
Pi = (xi + 1)2 - ( yi - 1/2 )2 - r2
= (R + 1)2 - (0 - ½ )2 - r2
= R2 + 2R + 1 + ¼ - r2
= 2R + 1 + ¼
= 2R + 5/4 (5/4 ~ 1)
= 2R + 1

You might also like