computer graphics

39
Manav Rachna College of Engineering, Faridabad MCA IIIrd Semester MCA-301, Computer Graphics and Multimedia LIST OF PRACTICALS S.No. NAME OF PRACTICAL 1. Write a program to draw a “STAR”. done 2. Write a program to draw a “HUT”. done 3. Write a program to draw a “CLOCK” with a moving pendulum. Done 4. Write a program to draw “VARIOUS FACES”. Done 5. Write a program to divide axes into small portions or Grids. Done 6. Write a program to scan converting a straight line using DDA Algorithm. Done 7. Write a program to draw a “LINE” using Bresenham’s Algorithm. Done 8. Write a program to draw a “CIRCLE” using Bresenham’s Algorithm. Done 9. Write a program to draw a “CIRCLE” using Mid point circle Algorithm. Done 10. Write a program to draw a “PIE SLICE”. 11. Write a program to design a “MOVING FAN” with a regulator having different speeds. Done

Upload: hemant-sharma

Post on 02-Dec-2014

492 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Computer Graphics

Manav Rachna College of Engineering, FaridabadMCA IIIrd Semester

MCA-301, Computer Graphics and Multimedia

LIST OF PRACTICALS

S.No. NAME OF PRACTICAL

1. Write a program to draw a “STAR”. done

2. Write a program to draw a “HUT”. done

3. Write a program to draw a “CLOCK” with a moving pendulum. Done

4. Write a program to draw “VARIOUS FACES”. Done

5. Write a program to divide axes into small portions or Grids. Done

6. Write a program to scan converting a straight line using DDA Algorithm. Done

7. Write a program to draw a “LINE” using Bresenham’s Algorithm. Done

8. Write a program to draw a “CIRCLE” using Bresenham’s Algorithm. Done

9. Write a program to draw a “CIRCLE” using Mid point circle Algorithm. Done

10. Write a program to draw a “PIE SLICE”.

11. Write a program to design a “MOVING FAN” with a regulator having different speeds. Done

12. Write a program to draw animation of a moving plane from left to right of a screen. Done

13. Write a program to shift the origin to the centre of the screen and display a rectangle in the fourth quadrant. Done

14. Write a program to draw a “JOKER”. Done

15. Write a program to find the product of two matrices.

16. Write a program to translate a 2-dimensional object.

Page 2: Computer Graphics

17. Write a program to scale a 2-dimensional object.

18. Write a program to draw a HUT in a given boundary. Done

16. Write a program to draw a “POLYGONAL”.

17. Write a program to design a “MOVING Bicycle”.

18. Write a program in MATLAB to draw a Circle of unit radius.

19. Write a program in MATLAB for Creating a Plot.

20. Write a program in MATLAB for implementing Multiple Axis Regions (subplot).

21. Write a program in MATLAB for implementing Fill function for creating graph of a well-known object.

Page 3: Computer Graphics

1. Write a program to draw a “STAR”.

#include<stdio.h>#include<conio.h>#include<graphics.h>void main(){int gd,gm;gd=DETECT;initgraph(&gd,&gm,"c:\\tc\\bgi");clrscr();line(300,150,200,300);line(300,150,400,300);line(200,300,400,300);line(200,200,400,200);line(200,200,300,350);line(400,200,300,350);getch();closegraph();}

Page 4: Computer Graphics

2. Write a program to draw a “HUT”.

#include<stdio.h>#include<conio.h>#include<graphics.h>void main(){ int gd,gm; gd=DETECT; initgraph(&gd,&gm,"c:\\tc\\bgi"); clrscr(); line(200,100,100,250); line(200,100,300,250); line(100,250,300,250); line(200,100,600,100); line(300,250,600,250); line(600,100,600,250); line(100,250,100,450); line(100,450,300,450); line(300,250,300,450); line(600,250,600,450);

Page 5: Computer Graphics

line(300,450,600,450); line(170,350,225,350); line(170,350,170,450); line(225,350,225,450); line(400,325,475,325); line(400,325,400,390); line(475,325,475,390); line(400,390,475,390); line(438,325,438,390); line(400,355,475,355); circle(200,200,12); line(275,100,375,250); line(350,100,450,250); line(425,100,525,250); line(500,100,600,250); line(233,150,600,150); line(267,200,600,200);

getch(); closegraph();

Page 6: Computer Graphics

3. Write a program to draw a “CLOCK”.

#include<stdio.h>#include<conio.h>#include<graphics.h>#include<dos.h> void main() {

int gd,gm,i,j; gd=DETECT;initgraph(&gd,&gm,"C:\\tc\\bgi");clrscr();clearviewport();

for(i=200;i<=400;i=i+10){

clearviewport(); line(304,300,i,380);line(300,300,i,380);circle(i,380,10);circle(300,200,100);circle(300,200,2);

outtextxy(293,110,"12");outtextxy(299,285,"6");

Page 7: Computer Graphics

outtextxy(385,196,"3");outtextxy(211,196,"9");

outtextxy(335,120,"1");outtextxy(369,153,"2");outtextxy(370,238,"4");outtextxy(340,270,"5");outtextxy(255,270,"7");outtextxy(223,237,"8");outtextxy(220,153,"10");outtextxy(248,120,"11");

line(300,200,239,159);line(300,200,350,166);line(275,300,275,410);line(330,300,330,410);

line(275,410,304,450);line(330,410,304,450); delay(200);

}//end of forfor(i=400;i>=200;i=i-10){

clearviewport();line(304,300,i,380);line(300,300,i,380);circle(i,380,10);circle(300,200,100);circle(300,200,2);

outtextxy(293,110,"12");outtextxy(299,285,"6"); outtextxy(385,196,"3");outtextxy(211,196,"9");

outtextxy(335,120,"1");outtextxy(369,153,"2");outtextxy(370,238,"4");outtextxy(340,270,"5");outtextxy(255,270,"7");outtextxy(223,237,"8");

Page 8: Computer Graphics

outtextxy(220,153,"10");outtextxy(248,120,"11");

line(300,200,239,159);line(300,200,350,166);line(275,300,275,410);line(330,300,330,410);

line(275,410,304,450);line(330,410,304,450);

delay(200);

}//end of for

getch();closegraph();

}

Page 9: Computer Graphics

4. Write a program to draw “FACE”

#include<stdio.h>#include<conio.h>#include<graphics.h>void main(){ int gd,gm,i,x=40,y=20; gd=DETECT; initgraph(&gd,&gm,"c:\\tc\\bgi"); clrscr(); for(i=1;i<=17;i++,x=x+15,y=y+25) { circle(x,y,60); line(x,y-5,x,y+5); circle(x-30,y-10,7); circle(x+30,y-10,7); arc(x,y+20,200,340,20); setcolor(18); delay(500);

cleardevice(); }

Page 10: Computer Graphics

if(x>=280&&y>=420) for(i=1;i<=10;i++,x=x+32,y=y-23) { circle(x,y,60); line(x,y-5,x,y+5); circle(x-30,y-10,7); circle(x+30,y-10,7); arc(x,y+20,200,340,20);

setcolor(19); setbkcolor(25); delay(500); cleardevice(); }

if(x>=583&&y>=195) for(i=1;i<=9;i++,x=x-35,y=y-20) { circle(x,y,60); line(x,y-5,x,y+5); circle(x-30,y-10,7); circle(x+30,y-10,7); arc(x,y+20,200,340,20); setcolor(22); setbkcolor(26);

delay(500); cleardevice(); } if(x>=20&&y>=35) for(i=1;i<=8;i++,x=x-35,y=y+20) { circle(x,y,60); line(x,y-5,x,y+5); circle(x-30,y-10,7); circle(x+30,y-10,7); arc(x,y+20,200,340,20); setcolor(28); delay(500); cleardevice();

Page 11: Computer Graphics

} getch(); closegraph();

}

Page 12: Computer Graphics

5. Write a program to divide axes into small portions or Grids.

#include<graphics.h>#include<conio.h>void main(){

int gm,gd=DETECT,x=0,i,y=0;initgraph(&gd,&gm,"c:\\tc\\bgi");clrscr();for(i=1;i<=80;i++,x=x+20,y=y+10){

line(x,5,x,640);line(5,y,640,y);

}getch();

}

Page 13: Computer Graphics

6. Write a program to scan converting a straight line using DDA Algorithm.

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

void main(){

int gd=DETECT,gm,x1,x2,y1,y2,length,dx,dy,X,Y,i,absx,absy;initgraph(&gd,&gm,"c:\\tc\\bgi");printf("enter the value of x1");scanf("%d",&x1);printf("enter the value of x2");scanf("%d",&x2);printf("enter the value of y1");scanf("%d",&y1);printf("enter the value of y2");

Page 14: Computer Graphics

scanf("%d",&y2);absx=abs(x2-x1);absy=abs(y2-y1);if(absy>=absy){

length=absx;}else{

length=absy;}dx=(x2-x1)/length;dy=(y2-y1)/length;X=x1+0.5;Y=y1+0.5;for(i=1;i<=length;i++){

putpixel(X,Y,YELLOW);X=X+dx;Y=Y+dy;

}getch();closegraph();

}

Page 15: Computer Graphics
Page 16: Computer Graphics

7. PROGRAM TO IMPLEMENT LINE using Bresenham’s ALGORITHM

#include<conio.h>#include<graphics.h>#include<stdio.h>#include<math.h>void main(){ int gd,gm,x1,x2,y1,y2,x,y; int dy,dx,dT,dS,d; gd=DETECT; initgraph(&gd,&gm,"C:\\tc\\bgi"); clrscr(); cleardevice();//setbkcolor(10); printf("\nEnter coordinates of 1st end point: \n"); printf("x1: "); scanf(“%d”,&x1); printf("\ty1: "); scanf(“%d”,&y1); printf("\nEnter coordinates of 2nd point: \n"); printf("x2: "); scanf(“%d”,&x2); printf("\ty2: "); scanf(“%d”,,&y2);

x=x1; y=y1; dx=x2-x1; dy=y2-y1; dT=2*(dy-dx); dS=2*dy; d=2*(dy)-dx;

putpixel(x,y,13); while(x<x2) { x++; if(d<0) d=d+dS; else { y++;

d=d+dT; } putpixel(x,y,13);

} getch(); closegraph();

Page 17: Computer Graphics

}

Page 18: Computer Graphics

8. Write a program to draw a “CIRCLE” using Bresenham’s Algorithm

#include<stdio.h>#include<conio.h>#include<graphics.h>void main(){ int gd,gm,x,y,r,d,x1,y1; gd=DETECT; initgraph(&gd,&gm,"c:\\tc\\bgi"); clrscr(); printf("Enter X coordinate:-"); scanf("%d",&x1); printf("Enter Y coordinate:-"); scanf("%d",&y1); printf("enter value of radius"); scanf("%d",&r); x=0; y=r; d=3-2*r; while(x<=y) { putpixel(x+x1,y+y1,1); putpixel(y+x1,x+y1,3); putpixel(-y+x1,x+y1,3); putpixel(-x+x1,y+y1,4); putpixel(-x+x1,-y+y1,5); putpixel(-y+x1,-x+y1,6); putpixel(y+x1,-x+y1,4); putpixel(x+x1,-y+y1,3); if(d<0) { d=d+4*x+6; } else { d=d+4*(x-y)+10; y--; } x++; } getch(); closegraph(); }

Page 19: Computer Graphics
Page 20: Computer Graphics

9. PROGRAM TO IMPLEMENT MID-POINT CIRCLE ALGORITHM

#include<conio.h>#include<graphics.h>#include<stdio.h>#include<math.h>void main(){ int gd,gm,x1,y1,r,d;

int x=0,y=r,x2,y2; gd=DETECT; initgraph(&gd,&gm,"C:\\tc\\bgi"); clrscr(); cleardevice();

printf("Enter the coordinates of centre :"); scanf("%d%d",&x1,&y1); printf("Enter the radius of the circle :"); scanf("%d",&r);

x2=x1; y2=y1; d=1-r; while(x<=y) { putpixel(x2+x,y2+y,4); putpixel(x2+y,y2+x,4); putpixel(x2-y,y2+x,4); putpixel(x2-x,y2+y,4); putpixel(x2-x,y2-y,4); putpixel(x2-y,y2-x,4); putpixel(x2+y,y2-x,4); putpixel(x2+y,y2-x,4); putpixel(x2+x,y2-y,4);

if(d<0) d=d+(2*x)+3; else { d=d+2*(x-y)+5;

y--; } x++;

} getch(); closegraph();}

Page 21: Computer Graphics
Page 22: Computer Graphics

10. Write a program to draw a “PIE SLICE”.

Page 23: Computer Graphics

11. Write a program to design a “MOVING FAN” with a regulator having different speeds.

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

void main(){ int gd,gm,x,y,i,j,a; gd=DETECT; initgraph(&gd,&gm,"C:\\TC\\BGI"); clrscr(); cleardevice();

while(!kbhit()) {x=0,a=0; for(i=0;i<10;i++) { clearviewport();

setfillstyle(1,10);

//foreground movement circle(300,150,10); circle(300,150,80); circle(300,150,85); pieslice(300,150,0+x,40+x,70); pieslice(300,150,90+x,130+x,70); pieslice(300,150,180+x,220+x,70); pieslice(300,150,270+x,310+x,70); x=x+5;

line(300,150,300,400); ellipse(300,400,0,360,10,5); ellipse(300,400,0,360,15,10);

delay(1000);

//background movement circle(300,150,10); circle(300,150,80); circle(300,150,85);

Page 24: Computer Graphics

pieslice(300,150,40+a,90+a,70); pieslice(300,150,130+a,180+a,70); pieslice(300,150,220+a,270+a,70); pieslice(300,150,330+a,0+a,70); a=a+5;

line(300,150,300,400); ellipse(300,400,0,360,10,5); ellipse(300,400,0,360,15,10);

} }

getch(); closegraph();

Page 25: Computer Graphics

12. Write program to draw a “AEROPLANE”.

#include<stdio.h>#include<conio.h>#include<graphics.h>void main(){ int gd,gm,i,x=10,y=10; gd=DETECT; initgraph(&gd,&gm,"c:\\tc\\bgi"); clrscr(); for(i=1;i<=13;i++,x=x+25,y=y+25) { if(x>=630) line(x,y+20,x+10,y+20); line(x,y+20,x+10,y+50); line(x+10,y+50,x,y+80); line(x,y+80,x+10,y+80); line(x+10,y+80,x+20,y+60); line(x+10,y+20,x+20,y+40); line(x+20,y+40,x+50,y+40); line(x+20,y+60,x+50,y+60); line(x+50,y+40,x+35,y+10); line(x+35,y+10,x+45,y+10); line(x+45,y+10,x+60,y+40); line(x+50,y+60,x+35,y+90); line(x+35,y+90,x+45,y+90); line(x+45,y+90,x+60,y+60); line(x+60,y+40,x+100,y+40); line(x+60,y+60,x+100,y+60); line(x+100,y+40,x+115,y+50); line(x+100,y+60,x+115,y+50); delay(300); cleardevice(); }

getch(); closegraph();}

Page 26: Computer Graphics
Page 27: Computer Graphics

13. Write a program to draw a rectangle and circle in quadrant

#include<stdio.h>#include<conio.h>#include<graphics.h>void main(){

int gd,gm,x,y;gd=DETECT;initgraph(&gd,&gm,"c:\\tc\\bgi");x=getmaxx()/2;y=getmaxy()/2;line(0,y,getmaxx(),y);line(x,0,x,getmaxy());rectangle(x+80,80,getmaxx()-80,y-80);circle(x-100,400,30);getch();closegraph();

}

Page 28: Computer Graphics

Q 14. Write a program to draw a “JOKER”.

Sol. #include<stdio.h>#include<conio.h>#include<graphics.h>#include<dos.h>#include<math.h>

const float PI=3.14;

void main(){

int gd,gm,i=0,j,k;gd=DETECT;initgraph(&gd, &gm,"c:\\tc\\bgi");

int x=200,y=200,k;

int dx=1,dy=1,c=-1;

initgraph(&driver,&mode,”c:tcbgi”);

circle(x,y,50); circle(x-20,y-10,10);

circle(x+20,y-10,10); line(x+1,y-5,x,y+15);

line(x,y+15,x+6,y+11); arc(x,y+5,240, 300, 30);

line(x,y-80,x-50,y-19); line(x,y-80,x+50,y-19);

k=getcolor();

while(!kbhit())

{

setcolor(k);

circle(x-25+(dx*c),y-10+(dy*c),2);

circle(x+25+(dx*c*-1),y-10+(dy*c),2);

delay(100);

setcolor(0);

Page 29: Computer Graphics

circle(x-25+(dx*c),y-10+(dy*c),2);

circle(x+25+(dx*c*-1),y-10+(dy*c),2);

c=c*-1;

}

getch();

closegraph();

}

Output:

Page 30: Computer Graphics

Q 21.Write a program to design a “MOVING Bicycle”.Sol.#include<stdio.h>#include<conio.h>#include<graphics.h>#include<dos.h>#include<math.h>

const float PI=3.14;

void main(){

int gd,gm,i=0,j,k;gd=DETECT;initgraph(&gd, &gm,"c:\\tc\\bgi");

do{if(i<430){circle(50+i,300,20);circle(150+i,300,20);line(50+i,300,150+i,300);line(50+i,300,80+i,260);line(150+i,300,140+i,260);line(80+i,260,140+i,260);line(140+i,260,140+i,250);line(140+i,250,137+i,250);arc(132+i,250,270,90,5);line(80+i,260,80+i,250);ellipse(82+i,250,0,360,7,2);delay(100);i++;}if(i>=430)break;clrscr();}while(!kbhit());closegraph();getch();

}

Page 31: Computer Graphics

Output:

Q 16. Write a program to translate a 2-dimensional object.Sol.#include<stdio.h>#include<conio.h>#include<graphics.h>void main(){ int gd,gm,x,y,x1,y1,x2,y2,x3,y3; gd=DETECT; initgraph(&gd,&gm,"c:\\tc\\bgi"); clrscr(); printf("Enter the 1coordinates of triangle"); scanf("%d%d",&x1,&y1); printf("Enter the 2coordinates of triangle"); scanf("%d%d",&x2,&y2); printf("Enter the coordinates of triangle"); scanf("%d%d",&x3,&y3);

Page 32: Computer Graphics

line(x1,y1,x2,y2); line(x2,y2,x3,y3); line(x1,y1,x3,y3); getch(); cleardevice(); printf("Enter the translation factors"); scanf("%d%d",&x,&y);

x1+=x; y1+=y; x2+=x; y2+=y; x3+=x; y3+=y;

line(x1,y1,x2,y2); line(x2,y2,x3,y3); line(x1,y1,x3,y3); getch();

closegraph();}

Output:

Q17. Write a program to scale a 2-dimensional object.Sol. #include<graphics.h>#include<stdio.h>#include<conio.h>#include<math.h>#include<process.h>

void main(){

int gd,gp,x1,x2,x3,x4,y1,y2,y3,y4;float sx,sy;gd=DETECT;initgraph(&gd,&gp,"c:\\tc\\bgi");clrscr();printf("Enter the coordinates of triangle");scanf("%d%d",&x1,&y1);printf("Enter the coordinates of triangle");scanf("%d%d",&x2,&y2);

Page 33: Computer Graphics

printf("Enter the coordinates of triangle");scanf("%d%d",&x3,&y3);

line(x1,y1,x2,y2);line(x2,y2,x3,y2);line(x3,y3,x1,y1);getch();cleardevice();printf("Enter the scaling factors");scanf("%d%d",&sx,&sy);x4=sx*x1-x1;y4=sy*y1-y1;

x1=sx*x1-x4;y1=sy*y1-y4;

x2=sx*x2-x4;y2=sy*y2-y4;

x3=sx*x3-x4;y3=sy*y3-y4;

getch(); cleardevice();

line(x1,y1,x2,y2);line(x2,y2,x3,y2);line(x3,y3,x1,y1);getch();

closegraph();}

Output:

Q 15.Write a program to find the product of two matrices.Q 16. Write a program to translate a 2-dimensional object.

Page 34: Computer Graphics

Q 18.Write a program to draw a HUT in a given boundary. DoneQ 19.Write a program to draw a “POLYGONAL”.