7/29/2019 cg lab file
1/22
AIM: Write a program to get the graphics mode and its name,
and driver name.
Program:
#include#include
#include
void main()
{
clrscr();
int gm,gd=DETECT;
int mode;
char *name, *driver;initgraph(&gd,&gm,"");
mode=getgraphmode();
name=getmodename(mode);
driver=getdrivername();
cout
7/29/2019 cg lab file
2/22
Program:
#include
#include
#includevoid main()
{
clrscr();
int gm,gd=DETECT;
int mode;
char *name;
initgraph(&gd,&gm,"");
mode=getgraphmode();
name=getmodename(mode);cout
7/29/2019 cg lab file
3/22
Program:
#include
#include
#include#include
#include
void main()
{
clrscr();
int gm,gd=DETECT;
initgraph(&gd,&gm,"");
setbkcolor(12);
outtextxy(150,200,"\n Press any key to see the colorful lines");getch();
delay(100);
setbkcolor(4);
while(!kbhit())
{
setcolor(random(MAXCOLORS));
line(320,240,random(640),random(480));
}
closegraph();}
AIM: Write a program to display colorful circles.
C o m p u t e r G r a p h i c sPage 16
7/29/2019 cg lab file
4/22
Program:
#include
#include
#include#include
#include
void main()
{
clrscr();
int gm,gd=DETECT;
initgraph(&gd,&gm,"");
setbkcolor(12);
outtextxy(150,200,"\n Press any key to see the colorful circles");getch();
delay(100);
setbkcolor(15);
while(!kbhit())
{
setcolor(random(MAXCOLORS));
circle(320,240,random(200));
}
closegraph();
}
AIM: Write a program to draw different shapes as line, rectangle,
C o m p u t e r G r a p h i c sPage 16
7/29/2019 cg lab file
5/22
bar, 3d bar.
Program:
#include
#include#include
void main()
{
clrscr();
int gm,gd=DETECT;
initgraph(&gd,&gm,"");
outtextxy(30,40,"Line");
moveto(50,50);
linerel(200,100);outtextxy(400,40,"Rectangle");
rectangle(350,50,550,150);
outtextxy(130,300,"Bar");
bar(100,310,250,350);
outtextxy(430,250,"3D Bar");
bar3d(410,280,480,400,20,1);
getch();
closegraph();
}
AIM: Write a program to draw different shapes as circle and ellipse.
C o m p u t e r G r a p h i c sPage 16
7/29/2019 cg lab file
6/22
Program:
#include
#include
#include
void main(){ clrscr();
int gm,gd=DETECT;
int x,y,r,xr,yr;
initgraph(&gd,&gm,"");
coutx;
couty;
coutr;
circle(x,y,r);
coutx>>y;
coutxr>>yr;
ellipse(x,y,0,360,xr,yr);
getch();
closegraph();}
AIM: Write a program to design the PACMAN.
C o m p u t e r G r a p h i c sPage 16
7/29/2019 cg lab file
7/22
Program:
#include
#include
#include
void main(){
clrscr();
int gm,gd=DETECT;
initgraph(&gd,&gm,"");
int polyarray[]={10,190,100,300,250,300,400,250,325,225,325,175,
300,200,270,190,265,165,250,180,200,150,250,130,265,150,270,125,
295,115,325,135,325,100,400,50,200,50,100,100,10,190};
drawpoly(21,polyarray);
outtextxy(120,190,"PACAMAN");setfillstyle(SOLID_FILL,4);
floodfill(125,265,15);
circle(220,80,15);
setfillstyle(SOLID_FILL,0);
floodfill(225,85,15);
getch();
closegraph();
}
AIM: Write a program to draw a line by DDA method.
C o m p u t e r G r a p h i c sPage 16
7/29/2019 cg lab file
8/22
Program:
#include
#include
#include
#include#define Round(a) (int(a+0.5))
void main()
{
clrscr();
int gd=DETECT,gm,x1,y1,x2,y2,steps,dx,dy;
float xinc,yinc,x,y;initgraph(&gd,&gm,"");
coutx1>>y1;
coutx2>>y2;
dx=x2-x1;
dy=y2-y1;
x=x1; y=y1;
if (abs(dx)>abs(dy))
steps=abs(dx);
else
steps=abs(dy);
xinc=dx/float(steps);
yinc=dy/float(steps);
putpixel(Round(x),Round(y),WHITE);
for (int k=0;k
7/29/2019 cg lab file
9/22
}
getch();
closegraph();
}
AIM: Write a program to draw a line by Bresenham method.
C o m p u t e r G r a p h i c sPage 16
7/29/2019 cg lab file
10/22
Program:
#include
#include
#include
void main()
{
clrscr();
int gd=DETECT,gm,x1,y1,x2,y2,dx,dy,pk,steps,x,y;
initgraph(&gd,&gm,"");
coutx1>>y1;
coutx2>>y2;
dx=x2-x1;
dy=y2-y1;
x=x1; y=y1;
steps=dx;
pk=(2*dy)-dx;
for(int k=0;k
7/29/2019 cg lab file
11/22
}
}
getch();
closegraph();
}
AIM: Write a program to draw a circle by Bresenham method.
C o m p u t e r G r a p h i c sPage 16
7/29/2019 cg lab file
12/22
Program:
#include
#include
#include
void main()
{
clrscr();
int gd=DETECT,gm;
initgraph(&gd,&gm,"");
int x,y,p,r,xc,yc;
coutxc>>yc;
coutr;
x=0;
y=r;
p=1-r;
while(x
7/29/2019 cg lab file
13/22
putpixel(xc+y,yc-x,15);
putpixel(xc-y,yc-x,15);
}
getch();
closegraph();
}
AIM: Write a program to draw a circle by Bresenham method.
C o m p u t e r G r a p h i c sPage 16
7/29/2019 cg lab file
14/22
Program:
#include
#include
#include
#include
void disp();
float x,y;
int xc,yc;
void main()
{
clrscr();
int gd=DETECT,gm;int a,b;
float p1,p2;
initgraph(&gd,&gm,"");
coutxc>>yc;
couta>>b;
x=0;y=b;
p1=(b*b)-(a*a*b)+(a*a)/4;
while((2.0*b*b*x)
7/29/2019 cg lab file
15/22
disp();
}
x=-x;
disp();
x=-x;
}
x=a;
y=0;
disp();
p2=(b*b)-(a*a*b)+(a*a)/4;
while((2.0*b*b*x)>=(2.0*a*a*y))
{
y++;if(p2>0)
{
p2=p2+(a*a)-(2.0*a*a*y);
disp();
}
else
{
x--;
p2=p2+(2.0*b*b*x)-(2.0*a*a*y)+(a*a);disp();
}
y=-y;
disp();
y=-y;
}
getch();
closegraph();
}
void disp()
C o m p u t e r G r a p h i c sPage 16
7/29/2019 cg lab file
16/22
{
putpixel(xc+x,yc+y,15);
putpixel(xc-x,yc+y,15);
putpixel(xc+x,yc-y,15);
putpixel(xc+x,yc-y,15);
}
AIM: Write a program for translation of 2D objects.
C o m p u t e r G r a p h i c sPage 16
7/29/2019 cg lab file
17/22
Program:
#include
#include
#include
void main()
{
clrscr();
int gm,gd=DETECT;
initgraph(&gd,&gm," ");
int left,top,right,bottom,tx,ty;
int x=getmaxx()/2, y=getmaxy()/2;coutleft>>top;
cout>right>>bottom;
cleardevice();
line(0,y,2*x,y);
line(x,0,x,2*y);
rectangle(x+left,y-top,x+right,y-bottom);
gotoxy(1,1);
couttx>>ty;
cleardevice();
line(0,y,2*x,y);line(x,0,x,2*y);
gotoxy(1,1);
cout
7/29/2019 cg lab file
18/22
closegraph();
}
AIM: Write a program for scaling of 2D objects.
C o m p u t e r G r a p h i c sPage 16
7/29/2019 cg lab file
19/22
Program:
#include
#include
#include
void main()
{
clrscr();
int gm,gd=DETECT;
initgraph(&gd,&gm," ");
int left,top,right,bottom,s1,s2;
int x=getmaxx()/2,y=getmaxy()/2;
coutleft>>top;
cout>right>>bottom;
cleardevice();
line(0,y,2*x,y);
line(x,0,x,2*y);
rectangle(x+left,y-top,x+right,y-bottom);
gotoxy(1,1);
couts1>>s2;
cleardevice();
line(0,y,2*x,y);
line(x,0,x,2*y);int scl=((right-left)*s1)/4,sct=((top-bottom)*s2)/4;
gotoxy(1,1);
cout
7/29/2019 cg lab file
20/22
closegraph();
}
AIM: Write a program for mirror reflection of 2D objects.
C o m p u t e r G r a p h i c sPage 16
7/29/2019 cg lab file
21/22
Program:
#include
#include
#include
void main()
{
clrscr();
int gm,gd=DETECT;
initgraph(&gd,&gm," ");
int left,top,right,bottom;
char p;
int x=getmaxx()/2,y=getmaxy()/2;coutleft>>top;
cout>right>>bottom;
cleardevice();
line(0,y,2*x,y);
line(x,0,x,2*y);rectangle(x+left,y-top,x+right,y-bottom);
gotoxy(1,1);
coutp;
cleardevice();
line(0,y,2*x,y);
line(x,0,x,2*y);
gotoxy(1,1);
cout
7/29/2019 cg lab file
22/22
}