You are on page 1of 9

BCA VI Semester

Computer Graphics Programs

IMPORTANT NOTE: For all the programs see the code


initgraph(&gd,&gm,"c:\\TURBOC3\\BGI");
Here the path C:\\TURBOC3\\BGI is the path of directory BGI where your turboc++ is
installed, the path could also be C:\\TC\\BGI (so just check your c drive and see the path
where the BGI folder is located on your system)

1. Write a program based in the Bresenham line drawing algorithm to draw a line with two
endpoints on a raster pixel screen.
LINE.CPP
#include<conio.h>
#include<graphics.h>
#include<math.h>
void main()
{
//Graphics mode initialization to 640x480 resolution
int gd=VGA, gm=VGAHI;
initgraph(&gd,&gm,"c:\\TURBOC3\\BGI");
//variables type declaration
int dx,dy,x,y,xend,p;
int xa,xb,ya,yb;
//points defined
xa=100;
ya=100;
xb=500;
yb=205;
//beginning of Bresenham algorithm
dx=abs(xa-xb);
dy=abs(ya-yb);
p=2*dy-dx;
//determine which point to start and which point to end
if(xa>xb)
{
x=xb;
y=yb;

xend=xa;
}
else
{
x=xa;
y=ya;
xend=xb;
}
putpixel(x,y,7);
do
{
x++;
if(p<0)
{
p+=2*dy;
}
else
{
y++;
p+=2*(dy-dx);
}
putpixel(x,y,7);
}while(x<xend);
getch();
//wait for a key
restorecrtmode(); //exit to previous mode
}
OUTPUT:

2. Write a program to draw a circle using the general circle formula x 2 + y2 = r2


CIRCLE.CPP
//xc = x coordinate of the centre of the circle =320.
//yc = x coordinate of the centre of the circle =240.
//r = radius of the circle =100
#include<conio.h>
#include<graphics.h>
#include<math.h>
void main()
{
int gd = DETECT, gm;
float x,y,r=100;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
do
{
//too large increments will cause discontinuity near x axis
x+=0.5;
//square root can calculate values only in positive first quadrant
y=sqrt(r*r - x*x);
//symmetrically plotting to all four quadrants, locating it at centre of
screen
putpixel(320+x,240+y,2);
putpixel(320+x,240-y,2);
putpixel(320-x,240+y,2);
putpixel(320-x,240-y,2);
}while(x<100);
getch();
restorecrtmode();
}

OUTPUT:

3. Write a program to draw a circle based on the midpoint circle algorithm.


MIDPOINTCIRCLE.CPP
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void plot_symmetric_pts(int,int,int,int);
void main()
{
int x,y,xc,yc;
float p,r;
int gd = DETECT, gm;
printf("\n\t Enter the coordinates of centre xc,yc : ");
scanf("%d,%d",&xc,&yc);
printf("\n\t Enter the radius r : ");
scanf("%f",&r);
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
x=0; //starting point
y=r;

p=5/4 - r; //initial decision parameter Po.


do
{
plot_symmetric_pts(xc,yc,x,y);
if(p<y)
{
p+=2*x+1;
}
else
{
p+=2*(x-y)+1;
y--;
}
x++;
}while(x<y);
if(x==y)
{
plot_symmetric_pts(xc,yc,x,y);
}
getch();
restorecrtmode();
}
//subroutine to plot symmetrical points in other octants.
void plot_symmetric_pts(int x,int y,int x1,int y1)
{
putpixel(x+x1,y+y1,2);
putpixel(x-x1,y+y1,2);
putpixel(x+x1,y-y1,2);
putpixel(x-x1,y-y1,2);
putpixel(x+y1,y+x1,2);
putpixel(x+y1,y-x1,2);
putpixel(x-y1,y+x1,2);
putpixel(x-y1,y-x1,2);
}
OUTPUT:

4. Write a program to fill a circle with any colour using the boundary filling algorithm.
BOUNDARYFILL.CPP
#include<graphics.h>
#include<conio.h>
void boundary_fill(int,int,int,int);
void main()
{
int gd = DETECT, gm;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
setcolor(WHITE);
//notice this red point will be deleted
putpixel(310,230,RED);
circle(320,240,40);
boundary_fill(320,240,LIGHTGREEN,WHITE);
getch();
}
void boundary_fill(int seedx,int seedy,int fill_color,int boundary_color)
{
if(getpixel(seedx,seedy)!=boundary_color &&
getpixel(seedx,seedy)!=fill_color)
{
putpixel(seedx,seedy,fill_color);
boundary_fill((seedx+1),seedy,fill_color,boundary_color);

boundary_fill((seedx-1),seedy,fill_color,boundary_color);
boundary_fill(seedx,(seedy+1),fill_color,boundary_color);
boundary_fill(seedx,(seedy-1),fill_color,boundary_color);
}
}
OUTPUT:

5. Write a program for polygon filling based on the scan line polygon filling algorithm.
SCANLINE.CPP
#include<graphics.h>
#include<conio.h>
void ScanFill(float x1,float y1, float x2,float y2,float x3,float y3,float x4,
float y4,int color);
void EdgeDetect(float x1,float y1, float x2,float y2,int *leftedge,int
*rightedge);
void main()
{
int gd = DETECT, gm;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
float x1,y1,x2,y2,x3,y3,x4,y4,color;
x1=100.0;
y1=50.0;
x2=150.0;

y2=380.0;
x3=500.0;
y3=400.0;
x4=550.0;
y4=100.0;
color=12;
ScanFill(x1,y1,x2,y2,x3,y3,x4,y4,color);
getch();
closegraph();
}
void EdgeDetect(float x1,float y1, float x2,float y2,int *leftedge,int
*rightedge)
{
float mx;
//x slope dx/dy
float x;
//x pixel coordinate
int i;
//looping variable
float temp;
if((y2-y1)<0)
{
temp=y1; y1=y2; y2=temp;
temp=x1; x1=x2; x2=temp;
//swapping operations
}
if((y2-y1)!=0)
{
mx=(x2-x1)/(y2-y1);
// dx/dy
}
else
{
mx=(x2-x1); //dx
}
x=x1; //starting x coordinate
for(i=y1;i<=y2;i++)
{
if(x<(float)leftedge[i])
leftedge[i]=(int)x;
if(x>(float)rightedge[i])
rightedge[i]=(int)x;
x+=mx;
// x= x + dx/dy
}
}

void ScanFill(float x1,float y1, float x2,float y2,float x3,float y3,float x4,
float y4,int color)
{
int leftedge[480];
int rightedge[480];
int y,i;
for(i=0;i<480;i++)
//initialization
{
leftedge[i]=640;
rightedge[i]=0;
}
EdgeDetect(x1,y1,x2,y2,leftedge,rightedge); //detecting the edges of the
triangle
EdgeDetect(x2,y2,x3,y3,leftedge,rightedge);
EdgeDetect(x3,y3,x4,y4,leftedge,rightedge);
EdgeDetect(x4,y4,x1,y1,leftedge,rightedge);
//scanfill the horizontal scanlines
for(y=0;y<480;y++)
{
if(leftedge[y]<=rightedge[y])
{
for(i=(int)leftedge[y];i<(int)rightedge[y];i++)
{
putpixel(i,y,color);
}
}
}
}
OUTPUT:

You might also like