Vous êtes sur la page 1sur 63

CG PRACTICAL FILE

Computer Graphics
(Practical File)

Submitted in partial fulfillment of the requirements


for the award of the degree of

Master of Computer Application (MCA)


To
Guru Gobind Singh Indraprastha University, Delhi

Under Supervision:

Submitted by:

Anu Taneja

Name: Ravi Kumar


Roll No:- 49
Semester: 3rd

Banarsidas Chandiwala Institute of Information Technology,


New Delhi - 110019
Batch (2013-2016)

COMPUTER GRAPHICS
06311104413

RAVI KUMAR

CG PRACTICAL FILE

INDEX
S.no

Program Name

1.

WAP to Implement Graphics Function

2.

WAP to draw hut using graphic primitives

3.

WAP to implement DDA Line Algorithm using functions

4.

WAP to implement Bresenham Line Algorithm using


functions

12

5.

WAP to implement Circle using Cartesian Coordinate


Algorithm

15

6.

WAP to implement Circle using Polar Coordinate


Algorithm

18

7.

WAP to implement Breshenham Circle Algorithm

21

8.

WAP to implement Mid-Point Circle Algorithm

25

9.

WAP to draw nested rectangles using for loop

28

10.

WAP to draw nested circles by filling with different


patterns using loops

30

11.

Create a menu-driven program to fill the shape of


circle/rectangle by different patterns

33

12.

WAP to divide rectangle into 4 parts and each part should


have different color

36

13.

WAP to implement all text-styles at once

38

14.

WAP to show flying colored balloons

41

15.

WAP to design traffic light by toggling its colors

44

16.

WAP to implement Translation Transformation

48

17.

WAP to implement Rotation Transformation

52

18.

WAP to implement Scaling Transformation

55

19.

WAP to implement Reflection Transformation

59

20.

WAP to divide circle into 4 parts .each part should have

62

COMPUTER GRAPHICS
06311104413

RAVI KUMAR

Pageno. Signature

CG PRACTICAL FILE

different colors initially and then color of 1 part should be


visible at at time.
21.

WAP to show moving ball

66

22.

WAP to implement mid-point ellipse Algorithm

70

23.

WAP to show sun-rising scene

75

24.

WAP to show a tap filling the bucket

78

25.

WAP to show flying butterfly

82

26.

WAP to rotate a fan

85

Q1. WAP to implement graphics functions


Ans:
#include<stdio.h>
COMPUTER GRAPHICS
06311104413

RAVI KUMAR

CG PRACTICAL FILE

#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm;
int maxx,maxy,c;
char d[100];
initgraph(&gd,&gm,"c:\\TURBOC3\\bgi");
clrscr();
setcolor(BLACK);
outtext("jaskeerat singh ubhi - q1 cg");
outtextxy(1,20,"GRAPHICS FUNCTION");
maxx=getmaxx();
maxy=getmaxy();
printf("\n\nmax x=%d max y=%d",maxx,maxy);
c=getgraphmode();
printf("\n\n\nmodename=%s",getmodename(c));
setcolor(BLUE);
putpixel(80,110,RED);
line(100,100,100,200);
rectangle(150,100,200,200);
circle(250,150,45);
ellipse(350,150,0,360,45,95);
arc(450,150,45,145,45);
getch();
}

COMPUTER GRAPHICS
06311104413

RAVI KUMAR

CG PRACTICAL FILE

Q2. WAP to illustrate graphics functions


Ans:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"c://TURBOC3//bgi");
setcolor(BLACK);
setcolor(BLUE);
rectangle(250,300,500,450);
rectangle(160,300,250,450);
line(160,300,205,250);
COMPUTER GRAPHICS
06311104413

RAVI KUMAR

CG PRACTICAL FILE

line(205,250,250,300);
line(205,250,455,250);
line(455,250,500,300);
rectangle(270,350,320,400);
rectangle(430,350,480,400);
rectangle(285,320,305,340);
rectangle(445,320,465,340);
rectangle(350,320,400,445);
line(355,390,355,400);
line(350,450,370,480);
line(400,450,420,480);
getch();
}

Q3.

WAP to implement DDA Line using functions

COMPUTER GRAPHICS
06311104413

RAVI KUMAR

CG PRACTICAL FILE

Ans:
#include <stdio.h>
#include <conio.h>
#include <dos.h>
#include <graphics.h>
void lineDDA(int, int, int, int);
void main()
{
int x1, y1, xn, yn;
int gd = DETECT, gm;
initgraph(&gd, &gm, "c:\\tc\\bgi");
printf("Enter the starting coordinates of line: ");
scanf("%d %d", &x1, &y1);
printf("Enter the ending coordinates of line: ");
scanf("%d %d", &xn, &yn);
lineDDA(x1, y1, xn, yn);
getch();
}
void lineDDA(int x1, int y1, int xn, int yn)
{
int dx, dy, m, i;
m = (yn-y1)/(xn-x1);
for (i=x1; i<=xn; i++)
{
if (m <= 1)
{
dx = 1;
dy = m * dx;
}
COMPUTER GRAPHICS
06311104413

RAVI KUMAR

CG PRACTICAL FILE

else
{
dy = 1;
dx = dy / m;
}
x1 = x1 + dx;
y1 = y1 + dy;
putpixel(x1, y1, RED);
//delay(20);
}
}

Q4.

WAP to implement Bresenhem Line using functions.

COMPUTER GRAPHICS
06311104413

RAVI KUMAR

CG PRACTICAL FILE

Ans:
#include <stdio.h>
#include <dos.h>
#include <graphics.h>
void lineBres(int, int, int, int);
void main()
{
int x1, y1, xn, yn;
int gd = DETECT, gm;
initgraph(&gd, &gm, "");
printf("Enter starting coordinates of line: ");
scanf("%d %d", &x1, &y1);
printf("Enter ending coordinates of line: ");
scanf("%d %d", &xn, &yn);
lineBres(x1, y1, xn, yn);
getch();
}
void lineBres(int x1, int y1, int xn, int yn)
{
int dx = xn - x1, dy = yn - y1;
int di = 2 * dy - dx;
int ds = 2 * dy, dt = 2 * (dy - dx);
putpixel(x1, y1, RED);
while (x1 < xn)
{
x1++;
if (di < 0)
di = di + ds;
else
{
COMPUTER GRAPHICS
06311104413

RAVI KUMAR

CG PRACTICAL FILE

y1++;
di = di + dt;
}
putpixel(x1, y1, RED);
delay(20);
}
}

Q5.

WAP to create a menu-driven program for filling shapes with different


patterns and colors according to the choice entered by the user.

Ans:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm,ch;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
setfillstyle(SOLID_FILL,MAGENTA);
setcolor(BLUE);
COMPUTER GRAPHICS
06311104413

RAVI KUMAR

CG PRACTICAL FILE

setbkcolor(8);
circle(469,150,100);
rectangle(100,60,300,240);
floodfill(519,150,BLUE);
floodfill(120,150,BLUE);
outtextxy(230,260,"*********MENU*********");
outtextxy(232,280,"1.DOT LINE FILL");
outtextxy(232,300,"2.XHATCH FILL");
outtextxy(232,320,"3.SLASH FILL");
outtextxy(232,340,"4.BACKSLASH FILL");
outtextxy(232,360,"5.EXIT");
outtextxy(230,370,"______________________");
outtextxy(232,387,"ENTER CHOICE:-");
for(;;)
{
gotoxy(45,25);
scanf("%d",&ch);
switch(ch)
{
case 1:
setfillstyle(INTERLEAVE_FILL,MAGENTA);
floodfill(519,150,BLUE);
floodfill(120,150,BLUE);
break;
case 2:
setfillstyle(XHATCH_FILL,MAGENTA);
floodfill(519,150,BLUE);
floodfill(120,150,BLUE);
break;
case 3:
setfillstyle(SLASH_FILL,MAGENTA);
COMPUTER GRAPHICS
06311104413

RAVI KUMAR

CG PRACTICAL FILE

floodfill(519,150,BLUE);
floodfill(120,150,BLUE);
break;
case 4:
setfillstyle(BKSLASH_FILL,MAGENTA);
floodfill(519,150,BLUE);
floodfill(120,150,BLUE);
break;
case 5:
exit(0);
default:
gotoxy(35,27);
printf("Wrong Choice Try Again");
}
}
}

COMPUTER GRAPHICS
06311104413

RAVI KUMAR

CG PRACTICAL FILE

COMPUTER GRAPHICS
06311104413

RAVI KUMAR

CG PRACTICAL FILE

Q6.

WAP to draw a rectangle divide it in 4 parts with different patterns and


then one part should be visible at a time.

Ans:
#include<conio.h>
#include<stdio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm,ch;
clrscr();
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
outtextxy(60,20,"RECTANGULAR DIVIDED IN 4 PARTS AND FILL WITH
DIFFERENT COLOURS");
rectangle(50,50,570,450);
line(310,50,310,450);
line(50,250,570,250);
outtextxy(580,260,"*MENU*");
outtextxy(580,280,"1->");
outtextxy(580,300,"2->");
outtextxy(580,320,"3->");
outtextxy(580,340,"4->");
outtextxy(580,360,"5.EXIT");
outtextxy(580,370,"__");
//

outtextxy(580,387,"");
for(;;)
{
gotoxy(45,25);
scanf("%d",&ch);
switch(ch)
{

COMPUTER GRAPHICS
06311104413

RAVI KUMAR

CG PRACTICAL FILE

case 1:
{
setfillstyle(9,BLUE);
floodfill(250,240,WHITE);
setfillstyle(9,BLACK);
floodfill(330,240,WHITE);
setfillstyle(9,BLACK);
floodfill(280,320,WHITE);
setfillstyle(9,BLACK);
floodfill(330,320,WHITE);
break;
}
case 2:
{
setfillstyle(9,GREEN);
floodfill(330,240,WHITE);
setfillstyle(9,BLACK);
floodfill(280,320,WHITE);
setfillstyle(9,BLACK);
floodfill(250,240,WHITE);
setfillstyle(9,BLACK);
floodfill(330,320,WHITE);
break;
}
case 3:
{
setfillstyle(9,YELLOW);
floodfill(280,320,WHITE);
setfillstyle(9,BLACK);
floodfill(330,240,WHITE);
setfillstyle(9,BLACK);
COMPUTER GRAPHICS
06311104413

RAVI KUMAR

CG PRACTICAL FILE

floodfill(250,240,WHITE);
setfillstyle(9,BLACK);
floodfill(330,320,WHITE);
break;
}
case 4:
{
setfillstyle(9,RED);
floodfill(330,320,WHITE);
setfillstyle(9,BLACK);
floodfill(330,240,WHITE);
setfillstyle(9,BLACK);
floodfill(280,320,WHITE);
setfillstyle(9,BLACK);
floodfill(250,240,WHITE);
break;
}
case 5:
exit(0);
default:
gotoxy(35,27);
printf("Wrong Choice Try Again");
}
}
}

COMPUTER GRAPHICS
06311104413

RAVI KUMAR

CG PRACTICAL FILE

COMPUTER GRAPHICS
06311104413

RAVI KUMAR

CG PRACTICAL FILE

Q7. WAP to move a ball horizontally


Ans:
#include<graphics.h>
#include<stdio.h>
#include<dos.h>
#include<conio.h>
void main()
{
int i, j = 0, gd = DETECT, gm;
initgraph(&gd,&gm,"C:\\TurboC3\\BGI");
settextstyle(DEFAULT_FONT,HORIZ_DIR,2);
COMPUTER GRAPHICS
06311104413

RAVI KUMAR

CG PRACTICAL FILE

outtextxy(25,240,"Press any key to view the moving Ball");


getch();
setviewport(0,0,639,440,1);
for( i = 0 ; i < getmaxx(); i = i + 10, j++ )
{
circle(25+i,35,25);
setfillstyle(SOLID_FILL,LIGHTRED);
setcolor(j);
delay(20);
if( i == 570 )
break;
clearviewport();
}
getch();
closegraph();
}

Q8. WAP to move a ball back and forth horizontally


Ans:
#include <stdio.h>
#include <conio.h>
#include <graphics.h>
#include <dos.h>
void main()
{
/* request auto detection */
int gdriver = DETECT, gmode;
COMPUTER GRAPHICS
06311104413

RAVI KUMAR

CG PRACTICAL FILE

int i ,j, x = 20, y = 20, flag = 0;


/* initialize graphic mode */
initgraph(&gdriver, &gmode, "C:/TURBOC3/BGI");
/* ball movement */
for (i=0,j=0;i<550;i=i+10,j=j+2)
{

circle(20+i,20+j,20);
delay(100);
cleardevice();
}
for (i=550,j=120;i>100;i=i-10,j=j+2)
{
circle(20+i,20+j,20);
delay(100);
cleardevice();
}
for (i=0,j=0;i<550;i=i+10,j=j+3)
{
circle(20+i,240+j,20);
delay(100);
cleardevice();
}
closegraph();
getch();
}

COMPUTER GRAPHICS
06311104413

RAVI KUMAR

CG PRACTICAL FILE

Q9. WAP to move a ball along the four sides of screen


Ans:
#include<graphics.h>
#include<stdio.h>
#include<dos.h>
#include<conio.h>
void main()
{
int i, j = 0, gd = DETECT, gm;
initgraph(&gd,&gm,"C:\\TurboC3\\BGI");
settextstyle(DEFAULT_FONT,HORIZ_DIR,2);
outtextxy(25,240,"Press any key to view the moving Ball");
getch();
// setviewport(0,0,639,440,1);
for( i = 0 ; i < getmaxx(); i = i + 10, j++ )
COMPUTER GRAPHICS
06311104413

RAVI KUMAR

CG PRACTICAL FILE

{
circle(25+i,25,25);
setfillstyle(SOLID_FILL,LIGHTRED);
//

floodfill(180+i,410,RED);
setcolor(j);
delay(20);
if( i == 570 )
{
cleardevice();
break;
}
clearviewport();

}
for( i = 0 ; i < getmaxy(); i = i + 10, j++ )
{
circle(570,25+i,25);
setfillstyle(SOLID_FILL,LIGHTRED);
//

floodfill(180+i,410,RED);
setcolor(j);
delay(20);
if( i == 370 )
{
// cleardevice();
break;
}
clearviewport();

}
for( i = getmaxx(); i >0; i = i - 10, j++ )
{
circle(25+i,370,25);
setfillstyle(SOLID_FILL,LIGHTRED);
COMPUTER GRAPHICS
06311104413

RAVI KUMAR

CG PRACTICAL FILE

//

floodfill(180+i,410,RED);
setcolor(j);
delay(20);
if( i == 370 )
{
// cleardevice();
break;
}
clearviewport();

}
for( i = 0; i >getmaxy(); i = i - 10, j++ )
{
circle(25,370-i,25);
setfillstyle(SOLID_FILL,LIGHTRED);
//

floodfill(180+i,410,RED);
setcolor(j);
delay(20);
if( i == 37 )
{
// cleardevice();
break;
}
clearviewport();

}
getch();
closegraph();
}

COMPUTER GRAPHICS
06311104413

RAVI KUMAR

CG PRACTICAL FILE

Q10. WAP to implement all the 2D Transformations


Ans:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void translate(int,int);
void scale(int,int);
void rotate(float);
void main()
{
COMPUTER GRAPHICS
06311104413

RAVI KUMAR

CG PRACTICAL FILE

int ch;
int gd=DETECT,gm;
int tx,ty,sx,sy;
float theta;
clrscr();
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
//

setbkcolor(WHITE);
setcolor(6);
outtextxy (100,88,"Object.");
rectangle(100,250,150,200);
printf("---MENU---");
printf("\n 1)Translate\n 2)Scale\n 3)Rotate");
printf("\nEnter your choice: ");
scanf("%d",&ch);
cleardevice();
switch(ch)
{
case 1:
{
clrscr();
cleardevice();
outtextxy(10,45,"Enter value of tx and ty:");
scanf("%d %d",&tx,&ty);
translate(tx,ty);
break;
}
case 2:
{
outtextxy(10,45,"Enter the value of sx and sy:");

COMPUTER GRAPHICS
06311104413

RAVI KUMAR

CG PRACTICAL FILE

scanf("%d%d",&sx,&sy);
scale(sx,sy);
break;
}
case 3:
{
outtextxy(10,50,"Enter the angle for rotation: ");
scanf("%f",&theta);
rotate(theta);
break;
}
default:
{
printf("you have enterd wrong choice");
break;
}
}
getch();
closegraph();
}
void translate(int tx,int ty)
{
setcolor(2);
outtextxy(240,10,"TRANSLATION");
outtextxy(238,20,"------------");
rectangle(100,250,150,200);
rectangle(100+tx,250+ty,150+tx,200+ty);
}
void scale(int sx,int sy)
COMPUTER GRAPHICS
06311104413

RAVI KUMAR

CG PRACTICAL FILE

{
setcolor(2);
outtextxy(240,10,"SCALING");
outtextxy(238,20,"--------");
rectangle(100,250,150,200);
rectangle(100*sx,250*sy,150*sx,200*sy);
}
void rotate(float theta)
{
int x1,x2,x3,x4;
int y1,y2,y3,y4;
int ax1,ax2,ax3,ax4,ay1,ay2,ay3,ay4;
int refx,refy;
theta=theta*(3.14/180);
setcolor(2);
outtextxy(240,10,"ROTATE");
outtextxy(238,20,"-------");
refx=100;
refy=100;
x1=100;
y1=100;
x2=150;
y2=100;
x3=150;
y3=150;
x4=100;
y4=150;
ax1=refy+(x1-refx)*cos(theta)-(y1-refy)*sin(theta);
COMPUTER GRAPHICS
06311104413

RAVI KUMAR

CG PRACTICAL FILE

ay1=refy+(x1-refx)*sin(theta)+(y1-refy)*cos(theta);
ax2=refy+(x2-refx)*cos(theta)-(y2-refy)*sin(theta);
ay2=refy+(x2-refx)*sin(theta)+(y2-refy)*cos(theta);
ax3=refy+(x3-refx)*cos(theta)-(y3-refy)*sin(theta);
ay3=refy+(x3-refx)*sin(theta)+(y3-refy)*cos(theta);
ax4=refy+(x4-refx)*cos(theta)-(y4-refy)*sin(theta);
ay4=refy+(x4-refx)*sin(theta)+(y4-refy)*cos(theta);
rectangle(100,150,150,100);
line(ax1,ay1,ax2,ay2);
line(ax2,ay2,ax3,ay3);
line(ax3,ay3,ax4,ay4);
line(ax4,ay4,ax1,ay1);
}

COMPUTER GRAPHICS
06311104413

RAVI KUMAR

CG PRACTICAL FILE

Q11. WAP to implement traffic light by toggling its colors along with countdown timer
Ans:
#include <stdio.h>
#include <conio.h>
#include <graphics.h>
#include <dos.h>
#include <string.h>
int main()
COMPUTER GRAPHICS
06311104413

RAVI KUMAR

CG PRACTICAL FILE

{
int gd = DETECT, gm;
int i, j, k, x, y, color;
char str[64];
/* initialize graphic mode */
initgraph(&gd, &gm, "C:/TURBOC3/BGI");
err = graphresult();

for (i = 0; i < 3; i++)


{
cleardevice();
setcolor(DARKGRAY);
setfillstyle(SOLID_FILL, DARKGRAY);
rectangle(50, 45, 150, 300);
rectangle(90, 300, 110, getmaxy());
floodfill(55, 55, DARKGRAY);
floodfill(91, 301, DARKGRAY);
setcolor(BLACK);
setfillstyle(SOLID_FILL, BLACK);
circle(100, 90, 30);
floodfill(100, 90, BLACK);
circle(100, 170, 30);
floodfill(100, 170, BLACK);
circle(100, 250, 30);
floodfill(100, 250, BLACK);
x = 150 + (getmaxx() - 150) / 2;
y = 125;
moveto(x, y);
COMPUTER GRAPHICS
06311104413

RAVI KUMAR

CG PRACTICAL FILE

settextstyle(TRIPLEX_FONT, HORIZ_DIR, 4);


settextjustify(CENTER_TEXT, CENTER_TEXT);
if (i == 0)
{
setcolor(LIGHTRED);
outtext("STOP");
color = LIGHTRED;
y = 90;
}
else if (i == 1)
{
setcolor(YELLOW);
outtext("READY");
color = YELLOW;
y = 170;
}
else
{
setcolor(GREEN);
outtext("START");
color = GREEN;
y = 250;
}
x = 100;
/* for red & green timer is 10 seconds */
if (color != YELLOW) {
k = 10;
}
else
{
COMPUTER GRAPHICS
06311104413

RAVI KUMAR

CG PRACTICAL FILE

k = 5;
}
for (j = k; j > 0; j--)
{
setcolor(BLACK);
setfillstyle(SOLID_FILL, BLACK);
circle(x, y, 30);
floodfill(x, y + 25, BLACK);
setcolor(color);
setfillstyle(SOLID_FILL, color);
circle(x, y, 30);
floodfill(x, y + 25, color);
setcolor(BLACK);
settextstyle(TRIPLEX_FONT, HORIZ_DIR, 3);
sprintf(str, "%d", j);
moveto(x, y);
outtext(str);
sleep(1);
}
}
cleardevice();
getch();
closegraph();
return 0;
}

COMPUTER GRAPHICS
06311104413

RAVI KUMAR

CG PRACTICAL FILE

Q12. WAP to implement Cartesian-Circle Algorithm


Ans:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void polycircle(int r,int h,int k,int value);
void circle1(int x,int y, int h, int k,int v);
void main()
{
int r1,h1,k1;
int gd=DETECT,gm;
clrscr();
printf("Enter the radius");
COMPUTER GRAPHICS
06311104413

RAVI KUMAR

CG PRACTICAL FILE

scanf("%d",&r1);
printf("Enter the centre point for the circle");
scanf("%d%d",&h1,&k1);
initgraph(&gd,&gm,"c:\\turboc3\\bgi ");
polycircle(r1,h1,k1,YELLOW);
getch();
closegraph();
}
void polycircle(int r,int h,int k,int value)
{
int x=0;
int y=r;
circle1(x,y,h,k,value);
while(x<=y)
{
circle1(x,y,h,k,value);
x++;
y=sqrt((r*r)-(x*x));
}
}
void circle1(int x,int y,int h,int k,int v)
{
putpixel(x+h,y+k,v);
putpixel(x+h,-y+k,v);
putpixel(-x+h,y+k,v);
putpixel(-x+h,-y+k,v);
putpixel(y+h,x+k,v);
putpixel(y+h,-x+k,v);
putpixel(-y+h,x+k,v);
COMPUTER GRAPHICS
06311104413

RAVI KUMAR

CG PRACTICAL FILE

putpixel(-y+h,-x+k,v);
}

Q13. WAP to implement Trignometric-Circle Algorithm


Ans:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void trigocircle(int r,int h,int k,int value);
void circlepoint(double x,double y, int h, int k,int v);
void main()
{
int r1,h1,k1;
int gd=DETECT,gm;
printf("Enter the radius");
COMPUTER GRAPHICS
06311104413

RAVI KUMAR

CG PRACTICAL FILE

scanf("%d",&r1);
printf("Enter the centre point for the circle");
scanf("%d%d",&h1,&k1);
initgraph(&gd,&gm,"c:\\turboc3\\bgi ");
trigocircle(r1,h1,k1,YELLOW);
getch();
closegraph();
}
void trigocircle(int r,int h,int k,int value)
{
int theta=90;
double x,y;
while(theta>=45)
{
x=r*cos(theta);
y=r*sin(theta);
theta=theta-1;
circlepoint(x,y,h,k,value);
}
}
void circlepoint(double x,double y,int h,int k,int v)
{
putpixel(x+h,y+k,v);
putpixel(x+h,-y+k,v);
putpixel(-x+h,y+k,v);
putpixel(-x+h,-y+k,v);
putpixel(y+h,x+k,v);
putpixel(y+h,-x+k,v);
putpixel(-y+h,x+k,v);
COMPUTER GRAPHICS
06311104413

RAVI KUMAR

CG PRACTICAL FILE

putpixel(-y+h,-x+k,v);
}

Q14. WAP to implement Bresenhem-Circle Algorithm


Ans:
# 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++)
COMPUTER GRAPHICS
06311104413

RAVI KUMAR

CG PRACTICAL FILE

{
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();
}

COMPUTER GRAPHICS
06311104413

RAVI KUMAR

CG PRACTICAL FILE

Q15. WAP to implement Mid-Point Circle Algorithm


Ans:
#include <stdio.h>
#include <conio.h>
#include <graphics.h>
#include <dos.h>
int main()
{
int gd = DETECT, gm;
int midx, midy, x, y, radius, dp;
initgraph(&gd, &gm, "C:/TURBOC3/BGI");
radius = 100;
midx = getmaxx() / 2;
midy = getmaxy() / 2;
dp = 1 - radius;
x = 0, y = radius;
do
{
COMPUTER GRAPHICS
06311104413

RAVI KUMAR

CG PRACTICAL FILE

putpixel(midx + x, midy + y, WHITE);


putpixel(midx - x, midy + y, WHITE);
putpixel(midx + x, midy - y, WHITE);
putpixel(midx - x, midy - y, WHITE);
putpixel(midx + y, midy + x, WHITE);
putpixel(midx - y, midy + x, WHITE);
putpixel(midx + y, midy - x, WHITE);
putpixel(midx - y, midy - x, WHITE);
delay(100);
x = x + 1;
if (dp < 0)
{
dp = dp + 2 * x + 1;
}
else
{
y = y - 1;
dp = dp + 2 * (x - y) + 1;
}
} while(x < y);
getch();
closegraph();
return 0;
}

COMPUTER GRAPHICS
06311104413

RAVI KUMAR

CG PRACTICAL FILE

Q16. WAP to implement Mid-Pint ellipse Algorithm.


Ans:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void ellipse(int xc,int yc,int rx,int ry)
{
int gm=DETECT,gd;
int x, y, p;
clrscr();
initgraph(&gm,&gd,"C:\\TC\\BGI");
x=0;
y=ry;
p=(ry*ry)-(rx*rx*ry)+((rx*rx)/4);
while((2*x*ry*ry)<(2*y*rx*rx))
{
COMPUTER GRAPHICS
06311104413

RAVI KUMAR

CG PRACTICAL FILE

putpixel(xc+x,yc-y,WHITE);
putpixel(xc-x,yc+y,WHITE);
putpixel(xc+x,yc+y,WHITE);
putpixel(xc-x,yc-y,WHITE);
if(p<0)
{
x=x+1;
p=p+(2*ry*ry*x)+(ry*ry);
}
else
{
x=x+1;
y=y-1;
p=p+(2*ry*ry*x+ry*ry)-(2*rx*rx*y);
}
}
p=((float)x+0.5)*((float)x+0.5)*ry*ry+(y-1)*(y-1)*rx*rx-rx*rx*ry*ry;
while(y>=0)
{
putpixel(xc+x,yc-y,WHITE);
putpixel(xc-x,yc+y,WHITE);
putpixel(xc+x,yc+y,WHITE);
putpixel(xc-x,yc-y,WHITE);
if(p>0)
{
y=y-1;
p=p-(2*rx*rx*y)+(rx*rx);

COMPUTER GRAPHICS
06311104413

RAVI KUMAR

CG PRACTICAL FILE

}
else
{
y=y-1;
x=x+1;
p=p+(2*ry*ry*x)-(2*rx*rx*y)-(rx*rx);
}
}
getch();
closegraph();
}
void main()
{
int xc,yc,rx,ry;
clrscr();
printf("Enter Xc=");
scanf("%d",&xc);
printf("Enter Yc=");
scanf("%d",&yc);
printf("Enter Rx=");
scanf("%d",&rx);
printf("Enter Ry=");
scanf("%d",&ry);
ellipse(xc,yc,rx,ry);
getch();
}

COMPUTER GRAPHICS
06311104413

RAVI KUMAR

CG PRACTICAL FILE

Q17. WAP to show an accident scene


Ans:
#include <graphics.h>
#include <dos.h>
#include <conio.h>
void main()
{
int i, j = 0, gd = DETECT, gm;
initgraph(&gd,&gm,"C:\\TurboC3\\BGI");
setviewport(0,0,639,440,1);
for( i = 0,j=150 ; i <= 420 ; i = i + 10, j=j-10 )
{
COMPUTER GRAPHICS
06311104413

RAVI KUMAR

CG PRACTICAL FILE

rectangle(50+i,275,150+i,400);
rectangle(150+i,350,200+i,400);
circle(75+i,410,10);
circle(175+i,410,10);
setcolor(j);
delay(70);
rectangle(540+j,275,620+j,400);
rectangle(490+j,350,540+j,400);
circle(575+j,410,10);
circle(515+j,410,10);
setcolor(j);
delay(70);
if( i == 220 )
{
cleardevice();
break;
}
clearviewport();
}
outtextxy(235,140,"AFTER ACCIDENT");
rectangle(100,150,150,200);
circle(125,215,10);
circle(135,105,10);
rectangle(240,205,380,300);
rectangle(490,350,540,400);
circle(205,410,10);
circle(245,430,10);

COMPUTER GRAPHICS
06311104413

RAVI KUMAR

CG PRACTICAL FILE

getch();
closegraph();
}

COMPUTER GRAPHICS
06311104413

RAVI KUMAR

CG PRACTICAL FILE

Q18. WAP to hit the wickets using a ball


Ans:
#include<graphics.h>
#include<stdio.h>
#include<conio.h>
#include<dos.h>
#include<stdlib.h>
void main()
{
int i;
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
for(i=500;i>220;i=i-50)
{
setfillstyle(1,14);
rectangle(210,150,220,250);
floodfill(212,168,WHITE);
rectangle(225,150,235,250);
floodfill(227,248,WHITE);
rectangle(240,150,250,250);
floodfill(242,249,WHITE);
setfillstyle(1,2);
circle(230,i,10);
floodfill(232,i-2,WHITE);
delay(200);
cleardevice();
}
setfillstyle(1,14);
rectangle(90,242,200,250);
floodfill(92,244,WHITE);
COMPUTER GRAPHICS
06311104413

RAVI KUMAR

CG PRACTICAL FILE

setfillstyle(1,14);
rectangle(260,242,380,250);
floodfill(262,244,WHITE);
setfillstyle(1,YELLOW);
rectangle(225,150,235,250);
floodfill(227,248,WHITE);
outtextxy(400,150,"CLEAN BOLD");
getch();
}

COMPUTER GRAPHICS
06311104413

RAVI KUMAR

CG PRACTICAL FILE

Q19. WAP to show a tap filling the bucket


Ans:
#include<stdio.h>
#include<conio.h>
#include<dos.h>
#include<graphics.h>
void main()
{
int gd = DETECT, gm = DETECT, i, j;
initgraph(&gd, &gm, "c:\\turboc3\\bgi");
ellipse(300, 200, 0, 360, 50, 25);
ellipse(300, 300, 0, 360, 50, 25);
line(250, 200, 250, 300);
line(350, 200, 350, 300);
ellipse(300, 100, 180, 360, 5, 2);
line(295, 100, 295, 80);
line(305, 100, 305, 86);
arc(300, 80, 90, 180, 5);
putpixel(306, 85, 15);
COMPUTER GRAPHICS
06311104413

RAVI KUMAR

CG PRACTICAL FILE

putpixel(307, 84, 15);


line(308, 84, 630, 84);
line(300, 75, 303, 75);
line(314, 75, 630, 75);
putpixel(304, 74, 15);
putpixel(305, 73, 15);
line(306, 72, 306, 65);
line(311, 72, 311, 65);
putpixel(312, 73, 15);
putpixel(313, 74, 15);
pieslice(309, 62, 0, 360, 5);
setfillstyle(SOLID_FILL, BLUE);
setcolor(LIGHTBLUE);
for (i = 0; i < 7; i++)
{
line(297 + i, 103, 297 + i, 300);
}
for (i = 1; i < 100; i++)
{
setcolor(LIGHTBLUE);
ellipse(300, 300 - i, 180, 360, 4, 2);
delay(30);
fillellipse(300, 300 - i, 49, 25);
setcolor(1);
line(297, 275 - i, 303, 275 - i);
setcolor(15);
ellipse(300, 200, 180, 360, 50, 25);
delay(50);
}
ellipse(300, 200, 0, 360, 50, 25);
setcolor(0);
COMPUTER GRAPHICS
06311104413

RAVI KUMAR

CG PRACTICAL FILE

for (i = 0; i < 7; i++)


line(297 + i, 103, 297 + i, 174);
getch();
}

COMPUTER GRAPHICS
06311104413

RAVI KUMAR

CG PRACTICAL FILE

Q20. WAP to show flying colored balloons


Ans.
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm,mode,moden;
int x,i,j,k;
initgraph(&gd,&gm ,"c:\\TURBOC3\\BGI");
x=50;
for(i=250;i>100;i--)
{
j = rand()%10;
k= rand()%10;
setfillstyle(j,k);
circle(x,i,50);
floodfill(x,i,WHITE);
line(x,i+50,x,i+120);
delay(10);
cleardevice();
}
getch();
}

COMPUTER GRAPHICS
06311104413

RAVI KUMAR

CG PRACTICAL FILE

COMPUTER GRAPHICS
06311104413

RAVI KUMAR

CG PRACTICAL FILE

Q21. WAP to show sun-rising scene


Ans .
#include<stdio.h>
#include<conio.h>
#include<dos.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm;
int maxx,maxy,c,i,ch=0;
char d[100];
initgraph(&gd,&gm,"c:\\TURBOC3\\bgi");
setfillstyle(1,BLUE);
floodfill(0,0,WHITE);
setfillstyle(1,BROWN);
line(30,300,150,100);
line(150,100,270,300);
line(30,300,270,300);
floodfill(50,280,WHITE);
setfillstyle(1,BROWN);
line(270,300,390,100);
line(390,100,510,300);
line(270,300,510,300);
floodfill(320,280,WHITE);
//arc(250,200,15,140,100);
setfillstyle(1,YELLOW);
arc(250,200,15,140,100);
floodfill(300,210,WHITE);
setcolor(RED);
line(170,130,140,0);
COMPUTER GRAPHICS
06311104413

RAVI KUMAR

CG PRACTICAL FILE

line(190,120,170,50);
line(220,100,200,0);
line(250,100,270,50);
line(280,100,310,0);
line(320,130,350,50);
line(350,160,370,0);
// line(
getch();
}

COMPUTER GRAPHICS
06311104413

RAVI KUMAR

CG PRACTICAL FILE

Q22. WAP to show a man moving in the rain.


Ans.
#include<stdio.h>
#include<dos.h>
#include<conio.h>
#include<graphics.h>
#include<stdlib.h>
void rain(int x1, int y1, int x2, int y2)
{
int s, dx, dy, m, c = 0, t = 1;
float xi, yi, x, y;
dx = x2 - x1;
dy = y2 - y1;
if (abs(dx) > abs(dy))
s = abs(dx);
else
s = abs(dy);
xi = dx / (float) s;
yi = dy / (float) s;
x = x1;
y = y1;
putpixel(x1 + 0.5, y1 + 0.5, 9);
for (m = 0; m < s; m++) {
c++;
x += xi;
y += yi;
if (getpixel(x, y) == 4)
break;
if (c % 10 == 0)
t++;
putpixel(x + 0.5, y + 0.5, 0);
COMPUTER GRAPHICS
06311104413

RAVI KUMAR

CG PRACTICAL FILE

if (t % 2 == 0)
putpixel(x + 0.5, y + 0.5, 9);
}
}
void main()
{
int gd = DETECT, gm = DETECT, c = -200, i = 0, x = 40, l = 15, h = 15, ht = 0;
initgraph(&gd, &gm, "");
cleardevice();
// DRAW THE GROUND
setcolor(BROWN);
line(0, 201, 600, 201);
cont:
while (!kbhit()) { // UNLESS a KEY is pressed
// DRAW THE MAN
setcolor(4);
ellipse(x, 100, 0, 180, 50, 30);
line(x - 50, 100, x + 50, 100);
line(x, 100, x, 150);
circle(x - 20, 115, 15);
line(x - 20, 130, x - 20, 175);
line(x - 20, 175, x - 20 - l, 200);
line(x - 20, 175, x - 20 + l, 200);
line(x - 20, 140, x, 150);
line(x - 20, 140, x - 20 - h, 160);
// DRAW THE RAIN
for (i = 0; i < 620; i += 20) {
rain(i, c, i, 200);
}
c++;
if (c == 0)
COMPUTER GRAPHICS
06311104413

RAVI KUMAR

CG PRACTICAL FILE

c = -100;
setcolor(0);
delay(50);
// REMOVE THE MAN
ellipse(x, 100, 0, 180, 50, 30);
line(x - 50, 100, x + 50, 100);
line(x, 100, x, 150);
circle(x - 20, 115, 15);
line(x - 20, 130, x - 20, 175);
line(x - 20, 175, x - 20 - l, 200);
line(x - 20, 175, x - 20 + l, 200);
line(x - 20, 140, x, 150);
line(x - 20, 140, x - 20 - h, 160);
line(x + 50, 100, x + 50, 200);
// MOVE THE COORDINATES OF THE MAN
x++;
// CODE for MOVING the LEG
l--;
if (l == -15)
l = 15;
// CODE for MOVING the HAND
if (ht == 1)
h++;
else
h--;
if (h == 15)
ht = 0;
else if (h == -15)
ht = 1;
}

COMPUTER GRAPHICS
06311104413

RAVI KUMAR

CG PRACTICAL FILE

// PAUSE THE MOVEMENT IF 'SPACEBAR' IS PRESSED, EXIT OTHERWISE


if (getch() == ' ') {
while (!kbhit());
getch();
goto cont;
}
}

COMPUTER GRAPHICS
06311104413

RAVI KUMAR

CG PRACTICAL FILE

Q23. WAP to show a rotating fan.


Ans.
#include<stdlib.h>
#include<stdio.h>
#include<conio.h>
#include<dos.h>
#include<graphics.h>
void main()
{
int j,gd=DETECT,gm,i,ch=0;
initgraph(&gd,&gm,"c:\\TURBOC3\\bgi");
setbkcolor(YELLOW);
for(i=0;i<=40;i++)
{
setfillstyle(1, 4);
pieslice(300,300, 0+i, 30+i,150);
floodfill(300,300,15);
setfillstyle(1, 1);
pieslice(300,300, 50+i, 80+i,150);
floodfill(300,300,15);
setfillstyle(1, 3);
pieslice(300,300, 110+i, 140+i,150);
floodfill(300,300,15);
setfillstyle(1, 2);
pieslice(300,300, 180+i, 210+i,150);
floodfill(300,300,15);
setfillstyle(1, 6);
pieslice(300,300, 235+i, 265+i,150);
floodfill(300,300,15);
setfillstyle(1, 5);
COMPUTER GRAPHICS
06311104413

RAVI KUMAR

CG PRACTICAL FILE

pieslice(300,300, 290+i, 320+i,150);


floodfill(300,300,15);
delay(50);
cleardevice();
}
getch();
closegraph();
}

COMPUTER GRAPHICS
06311104413

RAVI KUMAR

CG PRACTICAL FILE

Q25. WAP to show flying butterfly


Ans.
#include<graphics.h>
#include<stdio.h>
#include<conio.h>
void main()
{
int i,gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
for(i=10;i<getmaxy();i+=10)
{
ellipse(240+i,80+i,115,65,10,40);//body
///////////////
circle(240+i,40+i,10);
ellipse(215+i,40+i,15,100,20,40);
ellipse(265+i,40+i,85,165,20,40);
///////////////////////
ellipse(160+i,90+i,270,0,80,25); //left
ellipse(160+i,60+i,0,90,80,25);
ellipse(160+i,75+i,270,90,15,40);
ellipse(320+i,75+i,90,270,15,40);
////////////////////////////////////////////
ellipse(320+i,90+i,180,270,80,25); // right
ellipse(320+i,60+i,90,180,80,25);
setfillstyle(SOLID_FILL,YELLOW);
floodfill(240+i,40+i,WHITE);
setfillstyle(SOLID_FILL,BLUE);
floodfill(200+i,80+i,WHITE);
floodfill(290+i,60+i,WHITE);
COMPUTER GRAPHICS
06311104413

RAVI KUMAR

CG PRACTICAL FILE

setfillstyle(SOLID_FILL,BROWN);
floodfill(240+i,80+i,WHITE);
setfillstyle(SOLID_FILL,GREEN);
floodfill(240+i,110+i,WHITE);
delay(10);
cleardevice();
}
getch();
}

COMPUTER GRAPHICS
06311104413

RAVI KUMAR

Vous aimerez peut-être aussi