0% found this document useful (0 votes)
134 views22 pages

CG Lab File

The document contains programs for various computer graphics concepts in C++ like getting the graphics mode, changing graphics mode, drawing colorful lines and circles, drawing different shapes like lines, rectangles, bars, circles and ellipses using different algorithms, designing Pacman, drawing lines using DDA and Bresenham algorithms, drawing circles using Bresenham algorithm, translating, scaling and mirror reflecting 2D objects. Each program contains the aim, program code with graphics header files and functions to implement the given computer graphics concept.

Uploaded by

Er. Arvind Kumar
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
134 views22 pages

CG Lab File

The document contains programs for various computer graphics concepts in C++ like getting the graphics mode, changing graphics mode, drawing colorful lines and circles, drawing different shapes like lines, rectangles, bars, circles and ellipses using different algorithms, designing Pacman, drawing lines using DDA and Bresenham algorithms, drawing circles using Bresenham algorithm, translating, scaling and mirror reflecting 2D objects. Each program contains the aim, program code with graphics header files and functions to implement the given computer graphics concept.

Uploaded by

Er. Arvind Kumar
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 22

AIM: Write a program to get the graphics mode and its name, and driver name.

Program: #include<iostream.h> #include<conio.h> #include<graphics.h> void main() { clrscr(); int gm,gd=DETECT; int mode; char *name, *driver; initgraph(&gd,&gm,""); mode=getgraphmode(); name=getmodename(mode); driver=getdrivername(); cout<<"\n Value of Graphics mode is: "<<gm; cout<<"\n Current Graphics mode and mode name is: " <<mode<<name; cout<<"\n Graphics driver name: "<<driver; getch(); closegraph(); }

AIM: Write a program to change the graphics mode.


Computer Graphics
Page 16

Program: #include<iostream.h> #include<conio.h> #include<graphics.h> void main() { clrscr(); int gm,gd=DETECT; int mode; char *name; initgraph(&gd,&gm,""); mode=getgraphmode(); name=getmodename(mode); cout<<"\n Graph mode no is "<<mode<<" and name is "<<name; cout<<"\n Press any key to change the mode"; getch(); setgraphmode(EGALO); mode=getgraphmode(); name=getmodename(mode); cout<<"\n New Graph mode no is "<<mode<<" and name is "<<name; getch(); closegraph(); }

AIM: Write a program to display colorful lines.


Computer Graphics
Page 16

Program: #include<iostream.h> #include<conio.h> #include<graphics.h> #include<stdlib.h> #include<dos.h> 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.


Computer Graphics
Page 16

Program: #include<iostream.h> #include<conio.h> #include<graphics.h> #include<stdlib.h> #include<dos.h> 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,


Computer Graphics
Page 16

bar, 3d bar.
Program: #include<iostream.h> #include<conio.h> #include<graphics.h> 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.


Computer Graphics
Page 16

Program: #include<iostream.h> #include<conio.h> #include<graphics.h> void main() { clrscr(); int gm,gd=DETECT; int x,y,r,xr,yr; initgraph(&gd,&gm,""); cout<<"\n Enter the center point of circle \n x: "; cin>>x; cout<<"\n y: "; cin>>y; cout<<"\n Enter the radius: "; cin>>r; circle(x,y,r); cout<<"\n Enter the center point(x,y) of ellipse: "; cin>>x>>y; cout<<"\n Enter the x and y radius: "; cin>>xr>>yr; ellipse(x,y,0,360,xr,yr); getch(); closegraph(); }

AIM: Write a program to design the PACMAN.


Computer Graphics
Page 16

Program: #include<iostream.h> #include<conio.h> #include<graphics.h> 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.


Computer Graphics
Page 16

Program: #include<iostream.h> #include<conio.h> #include<graphics.h> #include<math.h> #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,""); cout<<"\n Enter the starting point "; cin>>x1>>y1; cout<<"\n Enter the ending point "; cin>>x2>>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<steps;k++) { x+=xinc; y+=yinc; putpixel(Round(x),Round(y),WHITE); Computer Graphics
Page 16

} getch(); closegraph(); }

AIM: Write a program to draw a line by Bresenham method.


Computer Graphics
Page 16

Program: #include<iostream.h> #include<conio.h> #include<graphics.h>

void main() { clrscr(); int gd=DETECT,gm,x1,y1,x2,y2,dx,dy,pk,steps,x,y; initgraph(&gd,&gm,""); cout<<"\n Enter the starting point "; cin>>x1>>y1; cout<<"\n Enter the ending point "; cin>>x2>>y2; dx=x2-x1; dy=y2-y1; x=x1; y=y1; steps=dx; pk=(2*dy)-dx; for(int k=0;k<steps;k++) { if (pk<0) { putpixel(x++,y,WHITE); pk=pk+(2*dy); } else { putpixel(x++,y++,WHITE); pk=pk+(2*dy)-(2*dx); Computer Graphics
Page 16

} } getch(); closegraph(); }

AIM: Write a program to draw a circle by Bresenham method.


Computer Graphics

Page 16

Program: #include<conio.h> #include<iostream.h> #include<graphics.h> void main() { clrscr(); int gd=DETECT,gm; initgraph(&gd,&gm,""); int x,y,p,r,xc,yc; cout<<"Enter the center of circle: "; cin>>xc>>yc; cout<<"Enter the radius of circle: "; cin>>r; x=0; y=r; p=1-r; while(x<y) { x++; if(p<0) p=p+(2*x)+1; else { y--; p+=2*(x-y)+1; } putpixel(xc+x,yc+y,15); putpixel(xc-x,yc+y,15); putpixel(xc+x,yc-y,15); putpixel(xc-x,yc-y,15); putpixel(xc+y,yc+x,15); putpixel(xc-y,yc+x,15); Computer Graphics
Page 16

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.


Computer Graphics
Page 16

Program: #include<iostream.h> #include<conio.h> #include<graphics.h> #include<math.h> void disp(); float x,y; int xc,yc; void main() { clrscr(); int gd=DETECT,gm; int a,b; float p1,p2; initgraph(&gd,&gm,""); cout<<"Enter the center of ellipse: "; cin>>xc>>yc; cout<<"Enter the radius of ellipse(rx,ry): "; cin>>a>>b; x=0;y=b; p1=(b*b)-(a*a*b)+(a*a)/4; while((2.0*b*b*x)<(2.0*a*a*y)) { x++; if(p1<=0) { p1=p1+(2.0*b*b*x)+(b*b); disp(); } else { y--; p1=p1+(2.0*b*b*x)+(b*b)-(2.0*a*a*y); Computer Graphics
Page 16

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() Computer Graphics


Page 16

{ 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.


Computer Graphics
Page 16

Program: #include<iostream.h> #include<conio.h> #include<graphics.h> void main() { clrscr(); int gm,gd=DETECT; initgraph(&gd,&gm," "); int left,top,right,bottom,tx,ty; int x=getmaxx()/2, y=getmaxy()/2; cout<<"Enter the coordinates of top-left corner of rectangle: "; cin>>left>>top; cout<<"Enter the coordinates of right bottom corner of rectangle{<=(320,239)}: "; cin>>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); cout<<"Enter translation vector (tx & ty):"; cin>>tx>>ty; cleardevice(); line(0,y,2*x,y); line(x,0,x,2*y); gotoxy(1,1); cout<<"Translated rectangle is..."; rectangle(x+left+tx,y-top-ty,x+right+tx,y-bottom-ty); getch(); Computer Graphics
Page 16

closegraph(); }

AIM: Write a program for scaling of 2D objects.


Computer Graphics
Page 16

Program: #include<iostream.h> #include<conio.h> #include<graphics.h> void main() { clrscr(); int gm,gd=DETECT; initgraph(&gd,&gm," "); int left,top,right,bottom,s1,s2; int x=getmaxx()/2,y=getmaxy()/2; cout<<"Enter the coordinates of top-left corner of rectangle: "; cin>>left>>top; cout<<"Enter the coordinates of right bottom corner of rectangle{<=(320,239): "; cin>>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); cout<<"Enter the scaling parameters (Sx & Sy): "; cin>>s1>>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<<"Scaled rectangle is..."; rectangle(x+left-scl,y-top-sct,x+right+scl,y-bottom+sct); getch(); Computer Graphics
Page 16

closegraph(); }

AIM: Write a program for mirror reflection of 2D objects.


Computer Graphics
Page 16

Program: #include<iostream.h> #include<conio.h> #include<graphics.h> void main() { clrscr(); int gm,gd=DETECT; initgraph(&gd,&gm," "); int left,top,right,bottom; char p; int x=getmaxx()/2,y=getmaxy()/2; cout<<"Enter the coordinates of top-left corner of rectangle: "; cin>>left>>top; cout<<"Enter the coordinates of right bottom corner of rectangle{<=(320,239)}: "; cin>>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); cout<<"Enter the axis of mirror reflection: "; cin>>p; cleardevice(); line(0,y,2*x,y); line(x,0,x,2*y); gotoxy(1,1); cout<<"Reflected rectangle is..."; if(p=='x'||p=='X') rectangle(x+left,y+top,x+right,y+bottom); else if(p=='y'||p=='Y') rectangle(x-left,y-top,x-right,y-bottom); getch(); closegraph(); Computer Graphics
Page 16

Computer Graphics

Page 16

You might also like