Computer Graphics Lab Manual
1. Write a Program to draw basic graphics construction like line, circle, arc,
 ellipse and rectangle.
#include<graphics.h>
#include<conio.h>
void main()
{
 intgd=DETECT,gm;
 initgraph (&gd,&gm,"c:\\tc\\bgi");
 setbkcolor(GREEN);
 printf("\t\t\t\n\nLINE");
 line(50,40,190,40);
 printf("\t\t\n\n\n\nRECTANGLE");
 rectangle(125,115,215,165);
 printf("\t\t\t\n\n\n\n\n\n\nARC");
 arc(120,200,180,0,30);
 printf("\t\n\n\n\nCIRCLE");
 circle(120,270,30);
 printf("\t\n\n\n\nECLIPSE");
 ellipse(120,350,0,360,30,20);
 getch();
}
 2. Write a Program to draw animation using increasing circles filled with
 different colors and patterns.
#include<graphics.h>
#include<conio.h>
void main()
{
 intgd=DETECT, gm, i, x, y;
 initgraph(&gd, &gm, "C:\\TC\\BGI");
 x=getmaxx()/3;
 y=getmaxx()/3;
 setbkcolor(WHITE);
 setcolor(BLUE);
 for(i=1;i<=8;i++)
 {
 setfillstyle(i,i);
 delay(20);
 circle(x, y, i*20);
 floodfill(x-2+i*20,y,BLUE);
 }
 getch();
 closegraph();
}
 3. Program to make screen saver in that display different size circles filled
 with different colors and at random places.
#include<stdio.h>
#include<conio.h>
#include"graphics.h"
#include"stdlib.h"
void main()
{
 intgd=DETECT,gm,i=0,x,xx,y,yy,r;
 //Initializes the graphics system
 initgraph(&gd,&gm,"c:\\tc\\bgi");
 x=getmaxx();
 y=getmaxy();
 while(!kbhit())
 {
 i++;
 // setfillstyle(random(i),random(30));
 circle(xx=random(x),yy=random(y),random(30));
 setfillstyle(random(i),random(30));
 floodfill(xx,yy,getmaxcolor());
 delay(200);
 }
 getch();
}
 4. Write a Program to make a moving colored car using inbuilt functions.
#include<graphics.h>
#include<conio.h>
int main()
{
 intgd=DETECT,gm, i, maxx, cy;
 initgraph(&gd, &gm, "C:\\TC\\BGI");
 setbkcolor(WHITE);
 setcolor(RED);
 maxx = getmaxx();
 cy = getmaxy()/2;
 for(i=0;i<maxx-140;i++)
 {
 cleardevice();
 line(0+i,cy-20, 0+i, cy+15);
 line(0+i, cy-20, 25+i, cy-20);
 line(25+i, cy-20, 40+i, cy-70);
 line(40+i, cy-70, 100+i, cy-70);
 line(100+i, cy-70, 115+i, cy-20);
 line(115+i, cy-20, 140+i, cy-20);
 line(0+i, cy+15, 18+i, cy+15);
 circle(28+i, cy+15, 10);
 line(38+i, cy+15, 102+i, cy+15);
 circle(112+i, cy+15,10);
 line(122+i, cy+15 ,140+i,cy+15);
 line(140+i, cy+15, 140+i, cy-20);
 rectangle(50+i, cy-62, 90+i, cy-30);
 setfillstyle(1,BLUE);
 floodfill(5+i, cy-15, RED);
 setfillstyle(1, LIGHTBLUE);
 floodfill(52+i, cy-60, RED);
 delay(10);
 }
 getch();
 closegraph();
 return 0;
}
 5. Write a Program to print your name in Hindi script on console output in C.
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
void main()
{
 int gd=DETECT,gm;
 initgraph(&gd,&gm,"c:\\tc\\bgi");
 setbkcolor(9);
 line(100,100,370,100);
 line(120,100,120,170);
 arc(143,100,0,180,23);
 line(165,100,165,155);
 arc(150,155,100,0,15);
 line(180,100,180,170);
 circle(210,140,10);
 line(210,130,250,130);
 circle(280,140,10);
 line(280,130,330,130);
 line(330,100,330,170);
 line(345,100,345,170);
 ellipse(337,100,0,180,9,18);
 getch();
}
 6. Write a Program control a ball using arrow keys.
#include<graphics.h>
#include<stdio.h>
void main()
{
 intgd=DETECT,gm,x,y,r=40;
 charch;
 initgraph(&gd,&gm,"C:/TURBOC3/BGI");
 setbkcolor(3);
 x=getmaxx()/2;
 y=getmaxy()/2;
 setfillstyle(1,RED);
 circle(x,y,r);
 floodfill(x,y,getmaxcolor());
 while((ch=getch())!=13)
 {
 switch(ch)
 {
 case 75: if(x>=r+1)
 {
 cleardevice();
 circle(x-=10,y,r);
 floodfill(x,y,getmaxcolor());
 }
 break;
 case 72: if(y>=r+1)
 {
 cleardevice();
 circle(x,y-=10,r);
 floodfill(x,y,getmaxcolor());
 }
 break;
 case 77: if(x<=(getmaxx()-r-10))
 {
 cleardevice();
 circle(x+=10,y,r);
 floodfill(x,y,getmaxcolor());
 }
 break;
 case 80: if(y<=(getmaxy()-r-10))
 {
 cleardevice();
 circle(x,y+=10,r);
 floodfill(x,y,getmaxcolor());
 }
 }
 }
 getch();
}
 7. Write a Program to implement Digital Clock.
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
struct time t;
void display(int,int,int);
void main()
{
 int i=0,gd=DETECT,gm,hr,min,sec;
 clrscr();
 initgraph(&gd,&gm,"c:\\turboc3\\bgi");
 setcolor(GREEN);
 settextstyle(4,0,7);
 while(!kbhit())
 {
 gettime(&t);
 hr=t.ti_hour;
 min=t.ti_min;
 sec=t.ti_sec;
 i++;
 display(100,100,hr);
 display(200,100,min);
 display(300,100,sec);
 sound(400);
 delay(30);
 nosound();
 delay(930);
 cleardevice();
 }
 getch();
}
void display(int x,int y,int num)
{
 char str[3];
 itoa(num,str,10);
 settextstyle(4,0,7);
 outtextxy(180,100,":");
 outtextxy(280,100,":");
 outtextxy(x,y,str);
 rectangle(90,90,380,200);
 rectangle(70,70,400,220);
 outtextxy(90,250,"Digital Clock");
}
 8. Write a Program to implement bouncing ball using sine wave form.
#include<stdio.h>
#include<graphics.h>
#define HEIGHT getmaxy()
#define WIDTH getmaxx()
#define GROUND 450
#define MAXHEIGHT 420
void main()
{
 int x,y=0,t=MAXHEIGHT,c=1;
 int gd=DETECT,gm;
 initgraph(&gd,&gm,"C:\\T urboC3\\BGI");
 for(x=40;x<=getmaxx();x=x+2)
{
 //Draw Ground
 rectangle (0,MAXHEIGHT,getmaxx(),MAXHEIGHT+5);
 floodfill (5,MAXHEIGHT+3,WHITE);
 //Draw Ball
 pieslice(x,y,0,360,20);
 //floodfill(x,y,RED);
 delay(100);
 if(y>MAXHEIGHT-20)
 {
 c=0;
 t=t-40;
 }
 if(y<=(MAXHEIGHT-t))
 {
 c=1;
 }
 if(t>=40)
 y=y+(c? 15:-15);
 cleardevice();
 //Exit upon keypress
 if(kbhit())
 break;
}
getch();
}
 9. Write a Program to implement Bouncing Ball in vertical direction.
#include <stdio.h>
#include <conio.h>
#include <graphics.h>
#include <dos.h>
int main()
{
 int gd = DETECT, gm;
 int i, x, y, flag=0;
 initgraph(&gd, &gm, "C:\\TC\\BGI");
 x = getmaxx()/2;
 y = 30;
 while (!kbhit())
 {
 if(y >= getmaxy()-30 || y <= 30)
 flag = !flag;
 /* draws the gray board */
 setcolor(RED);
 setfillstyle(SOLID_FILL, RED);
 circle(x, y, 30);
 floodfill(x, y, RED);
 delay(50);
 cleardevice();
 if(flag)
 {
 y = y + 5;
 }
 else
 {
 y = y - 5;
 }
 }
 getch();
 closegraph();
 return 0;
}
 10. Write a program of Translation, Rotation, and Scaling using Composite
 Transformation.
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
int x1,y1,x2,y2,x3,y3,a,b;
void draw();
void rotate();
int main(void)
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\TC\\BGI");
printf("Enter first co-ordinate value for triangle:");
scanf("%d%d",&x1,&y1);
printf("Enter second co-ordinatevalues for triangle:");
scanf("%d%d",&x2,&y2);
printf("Enter third co-ordinate valuesfor triangle:");
scanf("%d%d",&x3,&y3);
draw();
getch();
rotate();
getch();
return 0;
}
void draw()
{
 line(x1,y1,x2,y2);
 line(x2,y2,x3,y3);
 line(x3,y3,x1,y1);
}
void rotate()
{
 int a1,a2,a3,b1,b2,b3;
 float angle;
 printf("Enter the rotation angle co-ordinates:");
 scanf("%f",&angle);
 cleardevice();
 angle=(angle*3.14)/180;
 a1=a+(x1-a)*cos(angle)-(y1-b)*sin(angle);
 b1=b+(x1-a)*sin(angle)+(y2-b)*cos(angle);
 a2=a+(x2-a)*cos(angle)-(y1-b)*sin(angle);
 b2=b+(x2-a)*sin(angle)+(y2-b)*cos(angle);
 a3=a+(x3-a)*cos(angle)-(y1-b)*sin(angle);
 b3=b+(x3-a)*sin(angle)+(y2-b)*cos(angle);
 printf("ROTATION");
 printf("\n Changed coordinates\n");
 printf("%d %d\n%d %d\n%d %d",a1,b1,a2,b2,a3,b3);
 line(a1,b1,a2,b2);
 line(a2,b2,a3,b3);
 line(a3,b3,a1,b1);
}
 11. Write a program to draw a Circle using midpoint implementation Method.
#include<stdio.h>
#include<graphics.h>
void drawcircle(int x0, int y0, int radius)
{
 int x = radius;
 int y = 0;
 int err = 0;
 while (x >= y)
 {
 putpixel(x0 + x, y0 + y, 7);
 putpixel(x0 + y, y0 + x, 7);
 putpixel(x0 - y, y0 + x, 7);
 putpixel(x0 - x, y0 + y, 7);
 putpixel(x0 - x, y0 - y, 7);
 putpixel(x0 - y, y0 - x, 7);
 putpixel(x0 + y, y0 - x, 7);
 putpixel(x0 + x, y0 - y, 7);
 if (err <= 0)
 {
 y += 1;
 err += 2*y + 1;
 }
 if (err > 0)
 {
 x -= 1;
 err -= 2*x + 1;
 }
 }
}
void main()
{
 int gdriver=DETECT, gmode, error, x, y, r;
 initgraph(&gdriver, &gmode, "c:\\turboc3\\bgi");
 printf("Enter radius of circle: ");
 scanf("%d", &r);
 printf("Enter co-ordinates of center(x and y): ");
 scanf("%d%d", &x, &y);
 drawcircle(x, y, r);
 getch();
}
 12. Program to draw Sine, Cosine and Tangent Curves
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
#include<stdio.h>
#include<stdlib.h>
#define pi 3.14
class arc
{
 float x, y, a, xm, ym;
 int i, ch;
 public:
 void get();
 void map();
 void sine();
 void cosine();
 void tangent();
};
void arc::get()
{
 cout<<"\n ENTER THE MAXIMUM VALUE FOR Y";
 cin>>y;
 cout<<"\n MENU IS---------";
 cout<<"\n 1-> SINE CURVE";
 cout<<"\n 2->COSINE CURVE";
 cout<<"\n 3-> TANGENT CURVE";
 cout<<"\n 4-> EXIT";
 cout<<"\n ENTER YOUR CHOICE";
 cin>>ch;
 switch(ch)
 {
 case1:
 sine();
 break();
 case2:
 cosine();
 case3:
 tangent();
 case4:
 exit(0);
 }
}
void arc::sine()
{
 cleardevice();
 xm=getmaxx()/2;
 ym=getmaxy()/2;
 line(xm, 0, xm, 2*ym);
 line(0, ym, 2 *xm, ym);
 outtextxy(0, ym, "X-AXIS");
 outtextxy(xm, 0, "Y-AXIS");
 for(x=-300;x<=300;x=x+0.5)
 {
 y=a*sin((pi*x)/180);
 putpixel(x+(320),-y+240,RED);
 }
}
void arc::cosine()
{
 cleardevice();
 xm=getmaxx()/2;
 ym=getmaxy()/2;
 line(xm, 0, xm, 2*ym);
 line(0, ym, 2 *xm, ym);
 outtextxy(0, ym, "X-AXIS");
 outtextxy(xm, 0, "Y-AXIS");
 for(x=-300;x<=300;x=x+0.5)
 {
 y=a*cos((pi*x)/180);
 putpixel(x+(320),-y+240,RED);
 }
}
void arc :: map()
{
 int gd=DETECT,gm;
 initgraph (&gd, &gm, "");
 int errorcode = graphresult();
 /*an error occurred */
 if (errorcode!=grOK)
 {
 printf("Graphics error: %s \n",grapherrormsg (errorcode));
 printf("Press and key to halt: ");
 getch();
 exit(1); /* terminate with an error code */
 }
}
void main()
{
 class arc a;
 clrscr();
 a.map();
 a.get();
 getch();
}
 13. Draw shapes using C graphics
 #include<graphics.h>
 #include<conio.h>
 main()
 int gd = DETECT,gm,left=100,top=100,right=200,bottom=200,x= 300,y=150,radius=50;
 initgraph(&gd, &gm, "C:\\TC\\BGI");
 rectangle(left, top, right, bottom);
 circle(x, y, radius);
 bar(left + 300, top, right + 300, bottom);
 line(left - 10, top + 150, left + 410, top + 150);
 ellipse(x, y + 200, 0, 360, 100, 50);
 outtextxy(left + 100, top + 325, "My first C graphics program");
 getch();
 closegraph();
 return 0;
14. C program draw bar chart
#include <graphics.h>
#include <conio.h>
int main()
{
 int gd = DETECT, gm;
 initgraph(&gd, &gm, "C:\\TC\\BGI");
 setcolor(YELLOW);
 rectangle(0,30,639,450);
 settextstyle(SANS_SERIF_FONT,HORIZ_DIR,2);
 setcolor(WHITE);
 outtextxy(275,0,"Bar Chart");
 setlinestyle(SOLID_LINE,0,2);
 line(100,420,100,60);
 line(100,420,600,420);
 line(90,70,100,60);
 line(110,70,100,60);
 line(590,410,600,420);
 line(590,430,600,420);
 outtextxy(95,35,"Y");
 outtextxy(610,405,"X");
 outtextxy(85,415,"O");
 setfillstyle(LINE_FILL,BLUE);
 bar(150,100,200,419);
 setfillstyle(XHATCH_FILL,RED);
 bar(225,150,275,419);
 setfillstyle(WIDE_DOT_FILL,GREEN);
 bar(300,200,350,419);
 setfillstyle(INTERLEAVE_FILL,MAGENTA);
 bar(375,125,425,419);
 setfillstyle(HATCH_FILL,BROWN);
 bar(450,175,500,419);
 getch();
 return 0;
}
15. C program for DDA line generation
#include <graphics.h>
#include <math.h>
#include <stdio.h>
// Function for finding absolute value
int abs(int n) { return ((n > 0) ? n : (n * (-1))); }
// DDA Function for line generation
void DDA(int X0, int Y0, int X1, int Y1)
{
 // calculate dx & dy
 int dx = X1 - X0;
 int dy = Y1 - Y0;
 // calculate steps required for generating pixels
 int steps = abs(dx) > abs(dy) ? abs(dx) : abs(dy);
 // calculate increment in x & y for each steps
 float Xinc = dx / (float)steps;
 float Yinc = dy / (float)steps;
 // Put pixel for each step
 float X = X0;
 float Y = Y0;
 for (int i = 0; i <= steps; i++) {
 putpixel(round(X), round(Y),
 RED); // put pixel at (X,Y)
 X += Xinc; // increment in x at each step
 Y += Yinc; // increment in y at each step
 delay(100); // for visualization of line-
 // generation step by step
 }
 }
 // Driver program
 int main()
 {
 int gd = DETECT, gm;
 // Initialize graphics function
 initgraph(&gd, &gm, "");
 int X0 = 2, Y0 = 2, X1 = 14, Y1 = 16;
 // Function call
 DDA(2, 2, 14, 16);
 return 0;
 }
 16. Program to implement Bresenham's Line Drawing Algorithm:
#include<stdio.h>
#include<graphics.h>
void drawline(int x0, int y0, int x1, int y1)
{
 int dx, dy, p, x, y;
 dx=x1-x0;
 dy=y1-y0;
 x=x0;
 y=y0;
 p=2*dy-dx;
 while(x<x1)
 {
 if(p>=0)
 {
 putpixel(x,y,7);
 y=y+1;
 p=p+2*dy-2*dx;
 }
 else
 {
 putpixel(x,y,7);
 p=p+2*dy;}
 x=x+1;
 }
}
int main()
{
 int gdriver=DETECT, gmode, error, x0, y0, x1, y1;
 initgraph(&gdriver, &gmode, "c:\\turboc3\\bgi");
 printf("Enter co-ordinates of first point: ");
 scanf("%d%d", &x0, &y0);
 printf("Enter co-ordinates of second point: ");
 scanf("%d%d", &x1, &y1);
 drawline(x0, y0, x1, y1);
 return 0;
}
 17. DDA Circle drawing algorithm
 #include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void main()
 int xc=340,yc=280,y,r,x;
 float theta;
 int gd=DETECT,gm;
 initgraph(&gd,&gm,"C:\\turboc3\\BGI");
 //printf("Enter the center of circle:");
 //scanf("%d%d",&xc,&yc);
 printf("Enter radius of circle:");
 scanf("%d",&r);
 for(theta=0;theta<=2*3.14;theta+=0.01)
 x=(int)(xc+(r*cos(theta)));
 y=(int)(yc+(r*sin(theta)));
 putpixel(x,y,WHITE);
 getch();
 closegraph();
18. Bresenham’s Circle drawing algorithm
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void main()
 int gd=DETECT,gm;
 int r,x,y,p,xc=320,yc=240;
 initgraph(&gd,&gm,"C:\\TurboC3\\BGI");
 cleardevice();
 printf("Enter the radius");
 scanf("%d",&r);
 x=0;
 y=r;
 putpixel(xc+x,yc-y,1);
 p=3-(2*r);
 for(x=0;x<=y;x++)
 if(p<0)
 y=y;
 p=(p+(4*x)+6);
 else
 {
 y=y-1;
 p=p+((4*(x-y)+10));
 putpixel(xc+x,yc-y,1);
 putpixel(xc-x,yc-y,2);
 putpixel(xc+x,yc+y,3);
 putpixel(xc-x,yc+y,4);
 putpixel(xc+y,yc-x,5);
 putpixel(xc-y,yc-x,6);
 putpixel(xc+y,yc+x,7);
 putpixel(xc-y,yc+x,8);
 getch();
 closegraph();
19. Cohen sutherland line clipping Algorithm
//Write a program to implement Cohen Sutherland line clipping algorithm.
#include<graphics.h>
#include<conio.h>
#include<stdio.h>
const int t=1,b=2,r=2,l=8;
float xmin,ymin,xmax,ymax;
int calcode(float x,float y)
 int code=0;
 if(y>ymax)
 code |=t;
 else if(y<ymin)
 code|=b;
 else if(x>xmax)
 code|=r;
 else if(x<xmin)
 code|=l;
 return(code);
void lineclip(float x1,float y1,float x2,float y2)
 unsigned int code1,code2,codeout;
 int accept=0,done=0;
 code1=calcode(x1,y1);
 code2=calcode(x2,y2);
 do
 if(!(code1|code2))
 accept=1;
 done=1;
 else if(code1 & code2)
 done=1;
else
 float x,y;
 codeout=code1?code1:code2;
 if(codeout& t)
 x=x1+(x2-x1)*(ymax-y1)/(y2-y1);
 y=ymax;
 else if(codeout& b)
 x=x1+(x2-x1)*(ymin-y1)/(y2-y1);
 y=ymin;
 else if(codeout& r)
 y=y1+(y2-y1)*(xmax-x1)/(x2-x1);
 x=xmax;
 else
 y=y1+(y2-y1)*(xmin-x1)/(x2-x1);
 x=xmin;
 if(codeout==code1)
 {
 x1=x;
 y1=y;
 code1=calcode(x1,y1);
 else
 x2=x;
 y2=y;
 code2=calcode(x2,y2);
 while(done==0);
 if(accept)
 line(x1,y1,x2,y2);
 rectangle(xmin,ymin,xmax,ymax);
main()
 float x1,y1,x2,y2;
 int gd=DETECT,gm;
 clrscr();
 initgraph(&gd,&gm,"C:\\TurboC3\\BGI");
 printf("Enter the co-ordinates of line ::::\n\t x1");
scanf("%f",&x1);
printf("\n\t y1:");
scanf("%f",&y1);
printf("\n\t x2:");
scanf("%f",&x2);
printf("\n\t y2:");
scanf("%f",&y2);
printf("Enter the co-ordinates of window :::\n");
printf("\n\txmin");
scanf("%f",&xmin);
printf("\n\tymin");
scanf("%f",&ymin);
printf("\n\txmax");
scanf("%f",&xmax);
printf("\n\tymax");
scanf("%f",&ymax);
clrscr();
line(x1,y1,x2,y2);
rectangle(xmin,ymin,xmax,ymax);
getch();
clrscr();
lineclip(x1,y1,x2,y2);
getch();
closegraph();
return 0;
}
20. Animate national flag
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
#include<math.h>
int main()
 int gd = DETECT,gm,a,b,i,r,x,y;
 initgraph(&gd, &gm,"C:\\Turboc3\\BGI");
 //draw the bottom rectangle
 setfillstyle(SOLID_FILL,1);
 rectangle(10,320,200,340);
 floodfill(11,321,15);
 rectangle(30,300,175,320);
 floodfill(31,301,15);
 rectangle(50,280,155,300);
 floodfill(51,281,15);
 //draw the pole
 setfillstyle(SOLID_FILL,3);
 rectangle(100,38,110,280);
 floodfill(101,39,15);
 //draw the top rectangle
 setfillstyle(SOLID_FILL,RED);
 rectangle(110,40,220,58);
 floodfill(111,43,15);
 setfillstyle(SOLID_FILL,15);
 rectangle(110,58,220,78);
 floodfill(111,59,15);
 setfillstyle(SOLID_FILL,GREEN);
 rectangle(110,78,220,98);
 floodfill(111,79,15);
 //Ashok chakra
 a=160;
 b=68;
 r=13;
 setcolor(BLUE);
 circle(a,b,r);
 for(i=0;i<=360;i=i+25)
 x=r*cos(i*3.14/180);
 y=r*sin(i*3.14/180);
 line(a,b,a+x,b-y);
 getch();
 return 0;
 // closegraph();