Вы находитесь на странице: 1из 46

# PRACTICAL FILE OF COMPUTER GRAPHICS

## PRACTICAL FILE OF COMPUTER GRAPHICS

INDEX
Aim
1. 2. 3. 4. 5. 6 7. 8. 9. 10. 11. 12. 13. Write a program to draw a stick man Write a program to draw a rectangle using line function Write a program to draw a line using DDAs line drawing algorithm Write a program to draw a line using Bresenhams line drawing algorithm Write a program to draw a circle using equation of circle Write a program to draw a circle using Bresenhams circle drawing algorithm Write a program to draw a circle using midpoint circle drawing algorithm Write a program to draw a circle using polar coordinates Write a program to fill a circle using Boundary Fill Algorithm Write a program to fill a circle using Flood Fill Algorithm Write a program for line clipping using cohenSutherland algorithm Write a program to translate a triangle about the origin Write a program to scale a triangle about a fixed point taken as one of the vertex of the triangle Write a program to rotate a triangle about a fixed point taken as one of the vertex of the triangle

Page
2 4 6 9 12 14 17 20 23 27 30 36 39

Remarks

14.

42

PANKAJ GILL

1
11/CSE/168

## PRACTICAL FILE OF COMPUTER GRAPHICS

PRACTICAL NO.1
Write a program to draw a stick man
#include<math.h> #include<conio.h> #include<graphics.h> void main() { intgd=DETECT,gm; int x,y,r,c1; initgraph(&gd,&gm,""); circle(150,70,70); circle(120,50,10); circle(190,50,10); line(155,60,155,80); arc(155,100,180,360,20); line(130,140,130,170); line(170,140,170,170); rectangle(80,170,230,260); line(110,260,110,360); line(205,260,205,360); line(80,190,55,240); line(230,190,255,240); getch(); }

PANKAJ GILL

2
11/CSE/168

OUTPUT

PANKAJ GILL

3
11/CSE/168

## PRACTICAL FILE OF COMPUTER GRAPHICS

PRACTICAL NO. 2
Write a program to draw a rectangle using line function
#include<graphics.h> void main() { intgd=DETECT,gm; initgraph(&gd,&gm," "); line(100,100,100,300); line(100,100,300,100); line(100,300,300,300); line(300,100,300,300); getch(); }

PANKAJ GILL

4
11/CSE/168

OUTPUT

PANKAJ GILL

5
11/CSE/168

## Write a program to draw a line using DDAs line drawing algorithm

#include<stdio.h> #include<conio.h> #include<graphics.h> #include<dos.h> voidlineDDA(int,int,int,int); void main() { int x1,y1,xn,yn; intgd=DETECT,gm; initgraph(&gd,&gm,""); printf("enter the starting coordinates of the line:"); scanf("%d%d",&x1,&y1); printf("enter the ending coordinates of the line:"); scanf("%d%d",&xn,&yn); lineDDA(x1,y1,xn,yn); getch(); } voidlineDDA(int x1,int y1,int xn,intyn) { intdx,dy,m,i; m=(yn-y1)/(xn-x1); for(i=x1;i<=xn;i++)

PRACTICAL NO. 3

PANKAJ GILL

6
11/CSE/168

## PRACTICAL FILE OF COMPUTER GRAPHICS

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

PANKAJ GILL

7
11/CSE/168

OUTPUT

PANKAJ GILL

8
11/CSE/168

## Write a program to draw a line usi ng Bresenhams line drawing algorithm

#include<conio.h> #include<stdio.h> #include<graphics.h> #include<dos.h> voidlineBres(int,int,int,int); void main() { int x1,y1,xn,yn; intgd=DETECT,gm; initgraph(&gd,&gm,""); printf("Enter the starting coordinate at line:"); scanf("%d%d", &x1, &y1); printf("Enter the ending coordinate at line:"); scanf("%d%d", &xn, &yn); lineBres(x1,y1,xn,yn); getch(); } voidlineBres(int x1,int y1,int xn,intyn) { int dx = xn-x1,dy=yn-y1; int di = 2*dy-dx; int ds = 2*dy,dt = 2*(dy-dx);

PROGRAM NO.4

PANKAJ GILL

9
11/CSE/168

## PRACTICAL FILE OF COMPUTER GRAPHICS

putpixel(x1,y1,RED); while(x1<xn) { x1++; if(di<0) { di=di+ds; } else { y1++; di=di+dt; } putpixel(x1,y1,RED); delay(20); } }

PANKAJ GILL

10
11/CSE/168

OUTPUT

PANKAJ GILL

11
11/CSE/168

## PRACTICAL FILE OF COMPUTER GRAPHICS

PRACTICAL NO. 5
Write a program to draw a circle using equation of circle
#include<conio.h> #include<graphics.h> void main() { intgd=DETECT,gm; int x,y,r,c1; initgraph(&gd,&gm,""); circle(200,200,50); getch(); closegraph(); }

PANKAJ GILL

12
11/CSE/168

OUTPUT

PANKAJ GILL

13
11/CSE/168

## Write a program to draw a circle using Bresenhams circle drawing algorithm

#include<stdio.h> #include<conio.h> #include<dos.h> #include<graphics.h> voidcircleBres(int,int,int); voiddrawcircle(int,int,int,int); void main() { intxc,yc,r; intgd=DETECT,gm; initgraph(&gd,&gm,""); printf("Enter the centre coordinates of the circle"); scanf("%d%d",&xc,&yc); printf("Enter radius of circle:"); scanf("%d",&r); circleBres(xc,yc,r); getch(); } voidcircleBres(intxc,intyc,int r) { int x=0,y=r; int d=3-2*r;

PRACTICAL NO. 6

PANKAJ GILL

14
11/CSE/168

## PRACTICAL FILE OF COMPUTER GRAPHICS

while(x<y) { drawcircle(xc,yc,x,y); x++; if(d<0) d=d+4*(x)+6; else { y--; d=d+4*(x-y)+10; drawcircle(xc,yc,x,y); delay(50); } } } voiddrawcircle(intxc,intyc,intx,int y) { putpixel(xc+x,yc+y,RED); putpixel(xc+y,yc+x,YELLOW); putpixel(xc-x,yc+y,BLUE); putpixel(xc-y,yc+x,GREEN); putpixel(xc-x,yc-y,GREEN); putpixel(xc-y,yc-x,YELLOW); putpixel(xc+y,yc-x,RED); putpixel(xc+x,yc-y,YELLOW); }

PANKAJ GILL

15
11/CSE/168

OUTPUT

PANKAJ GILL

16
11/CSE/168

## Write a program to draw a circle using midpoint circle drawing algorithm

#include<stdio.h> #include<conio.h> #include<graphics.h> #include<dos.h> voidcirclemidpoint(int,int,int); voiddrawcircle(int,int,int,int); void main() { intxc,yc,r; intgd=DETECT,gm; initgraph(&gd,&gm,""); printf("Enter center coordinates of the circle: "); scanf("%d%d",&xc,&yc); printf("Enter radius of the circle: "); scanf("%d",&r); circlemidpoint(xc,yc,r); getch(); } voidcirclemidpoint(intxc,intyc,int r) { int x=0,y=r; int p=1-r; while(x<y) { drawcircle(xc,yc,x,y); x++; if(p<0) {

PRACTICAL NO. 7

PANKAJ GILL

17
11/CSE/168

## PRACTICAL FILE OF COMPUTER GRAPHICS

p=p+2*x+1; } else { y--; p=p+2*(x-y)+1; } drawcircle(xc,yc,x,y); delay(50); } } voiddrawcircle(intxc,intyc,intx,int y) { putpixel(xc+x,yc+y,RED); putpixel(xc-x,yc+y,BLUE); putpixel(xc+x,yc-y,GREEN); putpixel(xc-x,yc-y,RED); putpixel(xc+y,yc+xGREEN); putpixel(xc-y,yc+x,YELLOW); putpixel(xc+y,yc-x, YELLOW); putpixel(xc-y,yc-x, YELLOW); }

PANKAJ GILL

18
11/CSE/168

OUTPUT

PANKAJ GILL

19
11/CSE/168

## Write a program to draw a circle using polar coordinates

#include<graphics.h> #include<math.h> #include<conio.h> voidacircle(inth,intk,int r); voiddpixel(intx,inty,inth,int k); void main() { intgd=DETECT,gm; initgraph(&gd,&gm,""); setbkcolor(YELLOW); acircle(100,100,100); getch(); closegraph(); } voidacircle(inth,intk,int r) { inty,x; int theta; for(theta=0;theta<=360;theta+=1) { x=r*cos(theta); y=r*sin(theta); dpixel(x,y,h,k);

PRACTICAL NO. 8

PANKAJ GILL

20
11/CSE/168

## PRACTICAL FILE OF COMPUTER GRAPHICS

} } voiddpixel(intx,inty,inth,int k) { putpixel(x+h,y+k,RED); putpixel(y+h,x+k,RED); putpixel(-y+h,x+k,RED); putpixel(-x+h,y+k,RED); putpixel(-x+h,-y+k,RED); putpixel(-y+h,-x+k,RED); putpixel(y+h,-x+k,RED); putpixel(x+h,-y+k,RED); }

PANKAJ GILL

21
11/CSE/168

OUTPUT

PANKAJ GILL

22
11/CSE/168

## Write a program to fill a circle using Boundary Fill Algorithm

#include<graphics.h> #include<math.h> #include<conio.h> voiddcircle(inth,intk,int r); voiddpixel(intx,inty,inth,int k); voidcfill(intx,int y, intfcolor, intbcolor); void main() { intgd=DETECT,gm; initgraph(&gd,&gm,""); setbkcolor(YELLOW); dcircle(30,30,27); cfill(30,30,BLUE,RED); getch(); closegraph(); } voiddcircle(inth,intk,int r) { inty,i;

PRACTICAL NO. 9

PANKAJ GILL

23
11/CSE/168

## PRACTICAL FILE OF COMPUTER GRAPHICS

for(i=0;i<=r;i++) { y=sqrt((r*r-(i)*(i))); dpixel(i,y,h,k); } } voiddpixel(intx,inty,inth,int k) { putpixel(x+h,y+k,RED); putpixel(y+h,x+k,RED); putpixel(-y+h,x+k,RED); putpixel(-x+h,y+k,RED); putpixel(-x+h,-y+k,RED); putpixel(-y+h,-x+k,RED); putpixel(y+h,-x+k,RED); putpixel(x+h,-y+k,RED); } voidcfill(intx,int y, intfcolor, intbcolor) { int current; current=getpixel(x,y); if(current!=bcolor&& current!=fcolor)

PANKAJ GILL

24
11/CSE/168

## PRACTICAL FILE OF COMPUTER GRAPHICS

{ putpixel(x,y,fcolor); cfill(x+1,y,BLUE,RED); cfill(x-1,y,BLUE,RED); cfill(x,y+1,BLUE,RED); cfill(x,y-1,BLUE,RED); } }

PANKAJ GILL

25
11/CSE/168

OUTPUT

PANKAJ GILL

26
11/CSE/168

## PRACTICAL FILE OF COMPUTER GRAPHICS

PRACTICAL NO. 10
Write a program to fill a circle using Flood Fill Algorithm
#include<graphics.h> #include<math.h> #include<conio.h> voiddcircle(inth,intk,int r); voiddpixel(intx,inty,inth,int k); voidffill(intx,int y, intfcolor, intbcolor); void main() { intgd=DETECT,gm; initgraph(&gd,&gm,""); setbkcolor(YELLOW); dcircle(30,30,27); ffill(30,30,YELLOW,BLACK); getch(); closegraph(); } voiddcircle(inth,intk,int r) { inty,i; for(i=0;i<=r;i++) { y=sqrt((r*r-(i)*(i))); dpixel(i,y,h,k); } }

PANKAJ GILL

27
11/CSE/168

## PRACTICAL FILE OF COMPUTER GRAPHICS

voiddpixel(intx,inty,inth,int k) { putpixel(x+h,y+k,RED); putpixel(y+h,x+k,RED); putpixel(-y+h,x+k,RED); putpixel(-x+h,y+k,RED); putpixel(-x+h,-y+k,RED); putpixel(-y+h,-x+k,RED); putpixel(y+h,-x+k,RED); putpixel(x+h,-y+k,RED); } voidffill(intx,int y, intfcolor, intbcolor) { if(getpixel(x,y)==bcolor) { putpixel(x,y,fcolor); ffill(x+1,y,YELLOW,BLACK); ffill(x-1,y,YELLOW,BLACK); ffill(x,y+1,YELLOW,BLACK); ffill(x,y-1,YELLOW,BLACK); } }

PANKAJ GILL

28
11/CSE/168

OUTPUT

PANKAJ GILL

29
11/CSE/168

## Write a program for line clipping using cohen Sutherland algorithm

#include<stdio.h> #include<graphics.h> #include<conio.h> typedef unsigned intoutcode; enum { TOP=0x1, BOTTOM=0x2, RIGHT=0x4, LEFT=0x8 }; voidlineclip(x0,y0,x1,y1,xwmin,ywmin,xwmax,ywmax ) float x0,y0,x1,y1,xwmin,ywmin,xwmax,ywmax; { intgd,gm; outcode code0,code1,codeout; int accept = 0, done=0; code0 = calcode(x0,y0,xwmin,ywmin,xwmax,ywmax); code1 = calcode(x1,y1,xwmin,ywmin,xwmax,ywmax); do{ if(!(code0 | code1)) { accept =1 ; done =1; } else if(code0 & code1) done = 1; else

PROGRAM NO. 11

PANKAJ GILL

30
11/CSE/168

## PRACTICAL FILE OF COMPUTER GRAPHICS

{ floatx,y; codeout = code0 ? code0 : code1; if(codeout& TOP) { x = x0 + (x1-x0)*(ywmax-y0)/(y1-y0); y = ywmax; } else if(codeout& BOTTOM) { x = x0 + (x1-x0)*(ywmin-y0)/(y1-y0); y = ywmin; } else if ( codeout& RIGHT) { y = y0+(y1-y0)*(xwmax-x0)/(x1-x0); x = xwmax; } else { y = y0 + (y1-y0)*(xwmin-x0)/(x1-x0); x = xwmin; } if(codeout == code0)

PANKAJ GILL

31
11/CSE/168

## PRACTICAL FILE OF COMPUTER GRAPHICS

{ x0 = x; y0 = y; code0=calcode(x0,y0,xwmin,ywmin,xwmax,ywma x); } else { x1 = x; y1 = y; code1 = calcode(x1,y1,xwmin,ywmin,xwmax,ywmax); } } } while( done == 0); if(accept) line(x0,y0,x1,y1); rectangle(xwmin,ywmin,xwmax,ywmax); getch(); } intcalcode (x,y,xwmin,ywmin,xwmax,ywmax) floatx,y,xwmin,ywmin,xwmax,ywmax; { int code =0; if(y>ywmax) code |=TOP; else if( y<ywmin) code |= BOTTOM;

PANKAJ GILL

32
11/CSE/168

## PRACTICAL FILE OF COMPUTER GRAPHICS

else if(x >xwmax) code |= RIGHT; else if ( x<xwmin) code |= LEFT; return(code); } main() { float x2,y2,x1,y1,xwmin,ywmin,xwmax,ywmax; intgd=DETECT,gm; clrscr(); initgraph(&gd,&gm,"e:\\tc\\bgi"); printf("\n\n\tEnter the co-ordinates of Line :"); printf("\n\n\tX1 Y1 : "); scanf("%f %f",&x1,&y1); printf("\n\n\tX2 Y2 : "); scanf("%f %f",&x2,&y2); printf("\n\tEnter the co_ordinates of window :\n "); printf("\n\txwmin , ywmin : "); scanf("%f %f",&xwmin,&ywmin); printf("\n\txwmax , ywmax : "); scanf("%f %f",&xwmax,&ywmax); clrscr(); line(x1,y1,x2,y2); rectangle(xwmin,ywmin,xwmax,ywmax); getch();

PANKAJ GILL

33
11/CSE/168

## PRACTICAL FILE OF COMPUTER GRAPHICS

clrscr(); lineclip(x1,y1,x2,y2,xwmin,ywmin,xwmax,ywmax ); getch(); closegraph(); }

PANKAJ GILL

34
11/CSE/168

OUTPUT

PANKAJ GILL

35
11/CSE/168

## PRACTICAL FILE OF COMPUTER GRAPHICS

PRACTICAL NO. 12
Write a program to translate a triangle about the origin
#include<stdio.h> #include<conio.h> #include<graphics.h> #include<process.h> #include<math.h> voidRectAngle(intx,inty,intHeight,int Width); void Translate(intx,inty,intHeight,int Width); void main() { intgd=DETECT,gm; intx,y,Height,Width; initgraph(&gd,&gm," "); printf("Enter the First point for the Rectangle:"); scanf("%d%d",&x,&y); printf("Enter the Height&Width for the Rectangle:"); scanf("%d%d",&Height,&Width); RectAngle(x,y,Height,Width); getch(); cleardevice(); Translate(x,y,Height,Width); RectAngle(x,y,Height,Width); getch();

PANKAJ GILL

36
11/CSE/168

## PRACTICAL FILE OF COMPUTER GRAPHICS

} voidRectAngle(intx,inty,intHeight,int Width) { line(x,y,x+Width,y); line(x,y,x,y+Height); line(x+Width,y,x+Width,y+Height); line(x,y+Height,x+Width,y+Height); } void Translate(intx,inty,intHeight,int Width) { intNewx,Newy,a,b; printf("Enter the Transaction coordinates"); scanf("%d%d",&Newx,&Newy); cleardevice(); a=x+Newx; b=y+Newy; RectAngle(a,b,Height,Width); }

PANKAJ GILL

37
11/CSE/168

OUTPUT

PANKAJ GILL

38
11/CSE/168

## PRACTICAL FILE OF COMPUTER GRAPHICS

Write a program to scale a triangle about a fixed point taken as one of the vertex of the triangle
#include<stdio.h> #include<conio.h> #include<graphics.h> void main() { int x1,y1,x2,y2,x3,y3,x4,y4; intsx,sy; int poly[8]; intgd=DETECT,gm; initgraph(&gd,&gm,""); cleardevice(); printf("Enter the first coordinates of triangle: "); scanf("%d%d",&x1,&y1); printf("Enter the second coordinates of triangle: "); scanf("%d%d",&x2,&y2); printf("Enter the third coordinates of triangle: "); scanf("%d%d",&x3,&y3); poly[0]=x1; poly[1]=y1; poly[2]=x2; poly[3]=y2; poly[4]=x3; poly[5]=y3; poly[6]=x1; poly[7]=y1; cleardevice(); drawpoly(4,poly); getch(); printf("Enter the scaling factors: ");

PRACTICAL NO. 13

PANKAJ GILL

39
11/CSE/168

## PRACTICAL FILE OF COMPUTER GRAPHICS

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; poly[0]=x1; poly[1]=y1; poly[2]=x2; poly[3]=y2; poly[4]=x3; poly[5]=y3; poly[6]=x1; poly[7]=y1; getch(); cleardevice(); drawpoly(4,poly);2 getch(); closegraph(); }

PANKAJ GILL

40
11/CSE/168

OUTPUT

After Scaling

PANKAJ GILL

41
11/CSE/168

## PRACTICAL FILE OF COMPUTER GRAPHICS

Write a program to rotate a triangle about a fixed point taken as one of the vertex of the triangle
#include<stdio.h> #include<conio.h> #include<graphics.h> #include<process.h> #include<math.h> void TriAngle(int x1,int y1,int x2,int y2,int x3,int y3); void Rotate(int x1,int y1,int x2,int y2,int x3,int y3); void main() { intgd=DETECT,gm; int x1,y1,x2,y2,x3,y3; initgraph(&gd,&gm," "); printf("Enter the 1st point for the triangle: "); scanf("%d%d",&x1,&y1); printf("Enter the 2nd point for the triangle: "); scanf("%d%d",&x2,&y2); printf("Enter the 3rd point for the triangle: "); scanf("%d%d",&x3,&y3); TriAngle(x1,y1,x2,y2,x3,y3); getch(); cleardevice(); Rotate(x1,y1,x2,y2,x3,y3); setcolor(5); TriAngle(x1,y1,x2,y2,x3,y3); getch(); } voidTriAngle(int x1,int y1,int x2,int y2,int x3,int y3) {

Program No. 14

PANKAJ GILL

42
11/CSE/168

## PRACTICAL FILE OF COMPUTER GRAPHICS

line(x1,y1,x2,y2); line(x2,y2,x3,y3); line(x3,y3,x1,y1); } void Rotate(int x1,int y1,int x2,int y2,int x3,int y3) { int x,y,a1,b1,a2,b2,a3,b3; float Angle; printf("Enter the angle for rotation: "); scanf("%f",&Angle); cleardevice(); Angle=(Angle*3.14)/180; a1=x2+(x1-x2)*cos(Angle)-(y1-y2)*sin(Angle); b1=y2+(x1-x2)*sin(Angle)+(y1-y2)*cos(Angle); a2=x2+(x2-x2)*cos(Angle)-(y2-y2)*sin(Angle); b2=y2+(x2-x2)*sin(Angle)+(y2-y2)*cos(Angle); a3=x2+(x3-x2)*cos(Angle)-(y3-y2)*sin(Angle); b3=y2+(x3-x2)*sin(Angle)+(y3-y2)*cos(Angle); printf("Rotated: "); TriAngle(a1,b1,a2,b2,a3,b3); }

PANKAJ GILL

43
11/CSE/168

OUTPUT

PANKAJ GILL

44
11/CSE/168

PANKAJ GILL

45
11/CSE/168