Computer Graphics Using C - Lab Record
Computer Graphics Using C - Lab Record
ANIMATED SUNRISE
AIM: Write a C program to draw animated sunrise. ALGORITHM: Step 1: Start Step 2: Initialize initgraph and graphics mode Step 3: Declare function mountain() and sun() Step 4: Call the below functions i) sun() - Draw sun using the function circle() and animate it using for loop and function delay() ii) mountain() - Draw mountain using function arc() and line() Step 5: Call cleardevice() Step 6: closegraph() Step 7: Stop
Page 1
COMPUTER GRAPHICS
PROGRAM: #include<stdio.h> #include<graphics.h> #include<conio.h> void main() { void sun(); void mountain(); int gd,gm; gd=DETECT; initgraph(&gd,&gm,"c:\\tc\\bgi"); mountain(); sun(); getch(); closegraph(); } void mountain() { line(0,300,200,147); arc(225,165,30,150,30); line(250,148,400,300); line(350,250,498,151); arc(527,175,43,137,36); line(553,150,640,250); } void sun() { { setcolor(RED); for(j=30;j>0;j--) circle(340,i,j);
DEPARTMENT OF COMPUTER APPLICATIONS Page 2
COMPUTER GRAPHICS
setcolor(BROWN); mountain(); setcolor(YELLOW); arc(k,250,20,180,6); arc(k-10,250,20,180,6); l=k+50; setcolor(BLUE); arc(l,150,20,180,6); arc(l-10,150,20,180,6); setcolor(5); arc(l+100,200,20,180,6); arc(l+90,200,20,180,6); arc(l+150,170,30,140,6); arc(l+140,170,30,140,6); delay(150); cleardevice(); } cleardevice(); setcolor(BROWN); mountain(); setcolor(YELLOW); for(i=30;i>0;i--) circle(340,90,i); }
Page 3
COMPUTER GRAPHICS
OUTPUT
Page 4
COMPUTER GRAPHICS
AIM: Write a C program to draw an animated rocket launching. ALGORITHM: Step 1: Start Step 2: Initialize intigraph and graphics mode Step 3: Draw rocket using the function rectangle() and line() and animate it using for loop and function delay() Step 4: Call cleardevice() Step 5: Closegraph() Step 6: Stop
Page 5
COMPUTER GRAPHICS
PROGRAM: #include<stdio.h> #include<graphics.h> #include<conio.h> void main() { int d,m,i,y; clrscr(); d=DETECT; initgraph(&d,&m,"c:\\tc\\bgi"); for(y=10;y<=300;y=y+5) { rectangle(300,300-y,330,400-y); rectangle(290,400-y,340,420-y); line(315,260-y,300,300-y); line(315,260-y,330,300-y); setcolor(YELLOW); for(i=290;i<=340;i=i+3) line(i,420-y,i+3,440-y); setcolor(RED); delay(100); cleardevice(); } getch(); closegraph(); }
Page 6
COMPUTER GRAPHICS
OUTPUT:
Page 7
COMPUTER GRAPHICS
AIM: Write a C program to draw an animated flag hoisting ALGORITHM: Step 1: Start Step 2: Declare the variables i,j,x1,x2,x3,y1,y2. Step 3: Initialize j to 50,,i to 82,repeat the following steps until i>2. a) Check whether i>30,then decrement j by 1. b) Set the fill pattern as solid fill. c) Draw a polygon using polygon function. d) Initialize x1=60,y1=100,x2=220,y2=140,x3=340,y3=180. e) Initialize i to 0 and execute the following. i) Set the color as RED when(i<60) ii) Set the color as WHITE if i value is between 60 and 120 iii)Draw an ellipse using ellipse function f) Set the color as BLUE. g) Set x1=0,x2=15. h) Initialize i to 0 and repeat the steps until i<24 i) Execute the pieslice function. ii) Set x1=x1+15. iii)Set x2=x2+15. iv) Call the delay() and cleardevice() Step 4: Stop.
Page 8
COMPUTER GRAPHICS
PROGRAM: #include<stdio.h> #include<graphics.h> #include<conio.h> int back[10]={59,80,59,480,54,480,54,80,59,80}; void main() { int i,j,x1,x2,x3,y1,y2,y3,r1,r2,k,l,gd,gm; clrscr(); gd=DETECT; initgraph(&gd,&gm,"c:\\tc\\bgi"); for(k=50,l=82;l>2;l--) { cleardevice(); if(l>30)k--; setfillstyle(9,BROWN); fillpoly(5,back); x1=60;y1=100;x2=220;x3=340;y2=140;y3=180; for(i=0;i<180;i++) { if(i<60) setcolor(RED); if(i>60&&i<120) setcolor(WHITE); if(i>120) setcolor(GREEN); ellipse(x1,y1-20+l,0,90,100-k,3+l); ellipse(x2-2*k,y1-20+l,180,270,60-k,3+l); ellipse(x2-2*k,y2-53+3*l,0,90,60-k,3+l); ellipse(x3-4*k,y2-52+3*l,180,270,60-k,3+l); ellipse(x3-4*k,y3-86+5*l,0,90,60-k,3+l);
DEPARTMENT OF COMPUTER APPLICATIONS Page 9
COMPUTER GRAPHICS
Page 10
COMPUTER GRAPHICS
OUTPUT:
Page 11
COMPUTER GRAPHICS
ROTATING FAN
AIM: Write a C program to draw a rotating fan and control its speed. ALGORITHM: Step 1: Start Step 2: Declare function drawfan() to draw fan using bar() and pieslice() function Step 3: Declare function switch_on() to change the speed using delay() Step 4: Initialize initgraph and graphics mode Step 5: Call cleardevice() Step 6: Call drawfan() Step 7: Using switch case call switch_on() function Step 8: Closegraph() Step 9: Stop
Page 12
COMPUTER GRAPHICS
PROGRAM: #include<stdio.h> #include<conio.h> #include<graphics.h> #include<math.h> void drawfan(int,int); void Switch(int); void main() { int speed; int gd=DETECT,gmode; initgraph(&gd,&gmode,"c:\\tc\\bgi"); do { cleardevice(); drawfan(0,15); gotoxy(30,5); printf("\n1.Slow"); printf("\n2.MEdium"); printf("\n3.Fast"); printf("\n4.OFF"); printf("\n Enter the choice"); scanf("%d",&speed) ; switch(speed) { case 1:Switch(300); break; case 2:Switch(100); break; case 3:Switch(50); break;
DEPARTMENT OF COMPUTER APPLICATIONS Page 13
COMPUTER GRAPHICS
} }while(speed!=4); getch(); closegraph(); } void drawfan(int rotate,int color) { int i; setfillstyle(SOLID_FILL,WHITE); setcolor(color); bar(313,240,317,320); bar(300,320,330,330); setfillstyle(SOLID_FILL,RED); pieslice(315,240,0+rotate,40+rotate,35); pieslice(315,240,120+rotate,160+rotate,35); pieslice(315,240,240+rotate,280+rotate,35); setcolor(YELLOW); for(i=0;i<39;i+=3) circle(315,240,i); } void Switch(speed) { int i=0; for(i=0;i<=1400;i+=80) { drawfan(i,0); drawfan(i+80,15); delay(speed); }}
Page 14
COMPUTER GRAPHICS
OUTPUT: ELECTRIC FAN 1. SLOW 2. MEDIUM 3. FAST 4. EXIT Enter the choice: 1
Page 15
COMPUTER GRAPHICS
CHESS BOARD
AIM: Write a C program to draw a chess board. ALGORITHM: Step 1: Start Step 2: Initialize graphics components Step 3: Declare variables flag,x,y,i,j Step 4: Initialize flag=0 Step 5: Repeat steps 6 to 11 from i=0 to 8 Step 6: Repeat steps 7 to 10 from j=0 to 8 Step 7: Draw rectangle with arguements 50+x,50+y,100+x,100+y Step 8: Check whether flag%2=0,then set color to white else set to black Step 9: Fill the rectangle Step 10:Increment flag,set x=x+50 Step 11:Increment flag,set y=y+50 Step 12:Stop
Page 16
COMPUTER GRAPHICS
PROGRAM: #include<stdio.h> #include<graphics.h> #include<conio.h> void main() { int flag=0,x,y,i,j,gd,gm; clrscr(); gd=DETECT; initgraph(&gd,&gm,"c:\\tc\\bgi"); for(i=0,y=0;i<8;i++) { for(j=0,x=0;j<8;j++) { rectangle(50+x,50+y,100+x,100+y); if(flag%2==0) setfillstyle(1,WHITE); else setfillstyle(1,BLACK); flag++; floodfill(52+x,52+y,15); x=x+50; } flag++; y=y+50; } getch(); closegraph(); }
Page 17
COMPUTER GRAPHICS
OUTPUT:
Page 18
COMPUTER GRAPHICS
AIM: Write a C program to draw the takeoff and landing of a flight. ALGORITHM: Step1: Declare a function name plane with two arguments. Step2: Declare the variables i and initialize j=0 Step3: Declare the graphics driver and graphics mode. Step4: Initialize the graphic function. Step5: Set a loop and call the plane function for take off animation. Step6: Set a loop and call the plane function for landing animation. Step7: Set the suitable color using setfillstyle() function. Step8: Stop.
Page 19
COMPUTER GRAPHICS
PROGRAM: #include<stdio.h> #include<conio.h> #include<graphics.h> void plane(int,int); void main() { int i,j=0; int gd=DETECT,gmode; initgraph(&gd,&gmode,"c:\\tc\\bgi"); for(i=0;i<50;i++) { plane(0,0); delay(100); } for(i=0;i<400;i+=2) { cleardevice(); plane(i,j); delay(75); j=j++; } for(i=0;i<500;i+=2) {cleardevice(); plane(i-300,j); j--; delay(75); } getch(); closegraph(); } void plane(int i,int j)
DEPARTMENT OF COMPUTER APPLICATIONS Page 20
COMPUTER GRAPHICS
{ int body[]={50,400,200,400,150,380,70,380,30,300,50,400} ; int w1[]={120,380,140,380,120,450,110,450,120,380} ; int w2[]={120,380,140,380,120,330,110,330,120,380}; body[0]=50+i; body[2]=200+i; body[4]=150+i; body[1] =400-j; body[3]=400-j; body[5] =380-j;
body[6]=70+i; body[7] =380-j; body[8]=30+i; body[9] =300-j; body[10]=50+i; body[11]=400-j; w1[0]=120+i; w1[1]=380-j; w1[2]=140+i; w1[3]=380-j; w1[4]=120+i; w1[5]=450-j; w1[6]=110+i; w1[7]=450-j; w1[8]=120+i; w1[9]=380-j; w2[0]=120+i; w2[1]=380-j; w2[2]=140+i; w2[3]=380-j; w2[4]=120+i; w2[5]=330-j; w2[6]=110+i; w2[7]=330-j; w2[8]=120+i; w2[9]=380-j; setfillstyle(SOLID_FILL,BLACK); setcolor(RED); drawpoly(6,body); setcolor(YELLOW); drawpoly(5,w1); drawpoly(5,w2); setcolor(RED); setcolor(GREEN); fillpoly(6,body); }
Page 21
COMPUTER GRAPHICS
OUTPUT:
Page 22
COMPUTER GRAPHICS
TEXT ANIMATION
AIM: Write a C program to animate text. ALGORITHM: Step 1: Start. Step 2: Declare and initialize variables such as word,fname,style,size. Step 3: Initialize graphics driver and initgraph. Step 4: Read the input string. Step 5: Store the font style in an array. Step 6: Print input string in different font style stored in the array. Step 7: Call cleardevice(). Step 8: Call closegraph(). Step 9: Stop.
Page 23
COMPUTER GRAPHICS
PROGRAM: #include<stdio.h> #include<conio.h> #include<graphics.h> void main() { char word[100]; char *fname[] = { "DEFAULT font","TRIPLEX font", "SMALL font","SANS SERIF font","GOTHIC font"}; int style,midx,midy,size; int gd=DETECT,gmode; initgraph(&gd,&gmode,"c:\\tc\\bgi"); printf("Enter the Word"); gets(word); midx = getmaxx() / 2; midy = getmaxy() / 2; for (style=DEFAULT_FONT; style<=GOTHIC_FONT; style++) { cleardevice(); if (style == TRIPLEX_FONT) size = 4; settextstyle(style, HORIZ_DIR, size); outtextxy(midx, midy, fname[style]); setcolor(style); outtextxy(400,300,word); getch(); } getch(); closegraph(); }
Page 24
COMPUTER GRAPHICS
OUTPUT:
Page 25
COMPUTER GRAPHICS
Step 3: X= X ,
a
Y=Y .
a
Step 4: If abs(dx)>abs(dy) then steps=abs(dx) Else Steps=abs(dy) Step 5 : Assign X increment =dx/(float)steps Y increment =dy/(float)steps Step 6 : Call function putpixel(abs(X),abs(Y),colour) Step 7 : Repeat following for k=1 to steps begin: =X+X increment Y=Y+Y increment Putpixel(round(X),round(Y),colour) End: Step 8 : Stop.
Page 26
COMPUTER GRAPHICS
PROGRAM: #include<stdio.h> #include<conio.h> #include<graphics.h> #include<math.h> #define ROUND(a) ((int)(a+.5)) void linedda(int,int,int,int); void main() { int x1,y1,x2,y2; int gd=DETECT,gmode; initgraph(&gd,&gmode,"C:\\TC\\BGI"); printf("Enter the end points"); scanf("%d%d%d%d",&x1,&y1,&x2,&y2); linedda(x1,y1,x2,y2); getch(); closegraph(); } void linedda(int x1,int y1,int x2,int y2) { int dx=x2-x1,dy=y2-y1,k,s; float xincrement,yincrement,x=x1,y=y1; if(abs(dx)>abs(dy)) { else { s=abs(dy); } xincrement=dx/(float)s; yincrement= dy/(float)s; putpixel(ROUND(x),ROUND(y),5); for(k=0;k<s;k++) { x=x+xincrement; }}
DEPARTMENT OF COMPUTER APPLICATIONS Page 27
s=abs(dx);
y=y+yincrement;
putpixel(ROUND(x),ROUND(y),5);
COMPUTER GRAPHICS
OUTPUT:
Page 28
COMPUTER GRAPHICS
COMPUTER GRAPHICS
PROGRAM: #include<stdio.h> #include<conio.h> #include<graphics.h> #include<math.h> void linebrhm(int,int,int,int); void main() { int x1,y1,x2,y2; int gd=DETECT,gmode; initgraph(&gd,&gmode,"C:\\TC\\BGI"); printf("Enter the end points"); scanf("%d%d%d%d",&x1,&y1,&x2,&y2); linebrhm(x1,y1,x2,y2); getch(); closegraph(); } void linebrhm(int x1,int y1,int x2,int y2) { float dx,dy,dxx,dyy,dxy,p,x,y,xEnd; dx=abs(x1-x2); dy=abs(y1-y2); p=2*dy-dx; dyy=2*dy; dxy=2*(dy-dx); if(x1>x2) { x=x1; y=y2; xEnd=x1;
DEPARTMENT OF COMPUTER APPLICATIONS Page 30
COMPUTER GRAPHICS
} else { x=x1; y=y1; xEnd=x2; } putpixel(x,y,5); while(x<xEnd) { x++; if(p<0) { p=p+dyy; } else { y++; p=p+dxy; } putpixel(x,y,5); } }
Page 31
COMPUTER GRAPHICS
OUTPUT:
Page 32
COMPUTER GRAPHICS
AIM: Write a C program to draw a circle using Midpoint circle generating algorithm. ALGORITHM:
Step 1: Input radius r and circle center (X ,Y ) and obtain the first point on the circumference of a
C C
Step 3: If P <0, the next pont along the circle centered on (0,0) is
K
(X
K+1
,Y)and P
K+1
= P + 2X
K
(K+1)
+1
Otherwise ,the next pont along the circle is (X +1,Y -1) and P
K K K+1
=P +2X
K K+1
K+1
+1 2 Y
K -2
K+1
Where 2 X
K+1
=2X
K+2
and 2 Y
=2Y
Step 4: Determine symmetry points in the other seven octants Step 5: Move each calculated pixel position (X,Y) on the circle path centered on (X Y ) and plot the co-ordinate values X =X +X ,Y = Y+Y
C C C C
Page 33
COMPUTER GRAPHICS
PROGRAM: #include<graphics.h> #include<conio.h> #include<math.h> #include<stdio.h> #include<stdlib.h> #define ROUND(a) ((int)(a+0.5)) void circlemid(int xc,int yc,int r); void circlepts(int,int,int,int); void main() { int gd=DETECT,gmode,xc,yc,r; initgraph(&gd,&gmode,"c:\\tc\\bgi"); printf("Enter the center and radius"); scanf("%d%d%d",&xc,&yc,&r); circlemid(xc,yc,r); getch(); closegraph(); } void circlemid(int x0,int y0,int r) { int x,y,p; x=0; y=r; p=1-r; circlepts(x0,y0,x,y); while(x<y) { if(p<0) { p=p+2*(x+1)+1;
DEPARTMENT OF COMPUTER APPLICATIONS Page 34
COMPUTER GRAPHICS
x++; } else { p=p+2*(x+1)+1-2*(y-1); x++; y--; } circlepts(x0,y0,x,y); }} void circlepts(int x0,int y0,int x,int y) { putpixel(x0+x,y0+y,6); putpixel(x0+y,y0+x,6); putpixel(x0-x,y0+y,6); putpixel(x0-y,y0+x,6); putpixel(x0+x,y0-y,6); putpixel(x0+y,y0-x,6); putpixel(x0-x,y0-y,6); putpixel(x0-y,y0-x,6); }
Page 35
COMPUTER GRAPHICS
Page 36
COMPUTER GRAPHICS
Page 37
COMPUTER GRAPHICS
PROGRAM: #include<graphics.h> #include<conio.h> #include<stdio.h> void floodfill1(int,int,int); void floodfill4(int,int,int,int); int x,y; int fill,old; void main() { int gd=DETECT,gm; initgraph(&gd,&gm,"c:\\tc\\bgi"); floodfill1(x,y,old); getch(); closegraph(); } void floodfill1(int x,int y,int old) { printf("Enter the coordinates"); scanf("%d%d",&x,&y); printf("Enter the fill color and the old color"); scanf("%d%d",&fill,&old); setcolor(old); circle(x,y,30); setfillstyle(1,old); floodfill(x,y,old); floodfill4(x,y,fill,old); } void floodfill4(int x,int y,int fill,int old) {
DEPARTMENT OF COMPUTER APPLICATIONS Page 38
COMPUTER GRAPHICS
Page 39
COMPUTER GRAPHICS
OUTPUT:
Enter the co-ordinates: 300 200 Enter the fill color and the old color
Page 40
COMPUTER GRAPHICS
2D- TRANSILATION
AIM: Write a C program to transilate a 2D object. ALGORITHM: Step 1: Start Step 2: Initialize initgraph() and graphics mode Step 3: Read the no of vertices Step 4: Read the end points Step 5: Draw the polygon using line to command from(x0,y0) to (xk,yk) and move the pixel to line end points(xk,yk) Step 6: Repeat step 3 until the last pixel to initial and compute the polygon Step 7: Now draw the lines from final pixel to initial pixel and compute the polygon Step 8: Input the transilate factors, tx and ty Step 9: Obtain new end points as x= x + tx y= y + ty
Step 10: Repeat step 2 to step 6 to get the transilated position. Step 11: Call clear device function Step 12: Stop
Page 41
COMPUTER GRAPHICS
PROGRAM: #include<graphics.h> #include<stdio.h> #include<conio.h> #include<math.h> void main() { int driver,mode,x[5],y[5],i,n; int tx,ty; clrscr(); driver=DETECT; initgraph(&driver,&mode,"c:\\tc\\bgi"); printf("Enter the no: of vertices:"); scanf("%d",&n); for(i=0;i<n;i++) { printf("\n Enter x[%d] y[%d]->",i+1,i+1); scanf("%d%d",&x[i],&y[i]); } setcolor(1); outtextxy(200,150,"Befor translation"); setcolor(1); moveto(x[0],y[0]); for(i=1;i<n;i++) { lineto(x[i],y[i]); } lineto(x[0],y[0]); getch(); printf("\n\n\n\n\n\n\n\n\n Enter the translation factors:"); scanf("%d%d",&tx,&ty);
DEPARTMENT OF COMPUTER APPLICATIONS Page 42
COMPUTER GRAPHICS
for(i=0;i<n;i++) { x[i]=x[i]+tx; y[i]=y[i]+ty; } outtextxy(190,330,"After translation"); moveto(x[0],y[0]); for(i=1;i<n;i++) { lineto(x[i],y[i]); } lineto(x[0],y[0]); getch(); closegraph(); }
Page 43
COMPUTER GRAPHICS
OUTPUT:
Enter the no: of vertices: 3 Enter x[1] y[1] 31590 Enter x[2] y[2] 210 290 Enter x[3] y[3] 410 290 BEFORE TRANSLATION
AFTER TRANSLATION
Page 44
COMPUTER GRAPHICS
2D- ROTATION
AIM: Write a C program to rotate a 2D object with respect to orgin ALGORITHM: Step 1: Start Step 2: Initialize initgraph() and graphics mode Step 3: Declare the variables xr, yr, n, theta Step 4: For i=0 to n read x[i] and y[i] Step 5: Move to (x0,y0) Step 6: For i=0 to n Step 7: Line to (x[i], y[i]) Step 8: Line to (x[0], y[0]) Step 9: Enter the pivot point Step 10: For r= 0 to theta, obtain the new points x1[i] = xr + (x[i] xr) * cos(theta) (y[i]-yr) * sin(theta) y1[i] = yr + (x[i] xr) * sin(theta) + (y[i]-yr) * cos(theta) Step 11: Repeat step 4,5 and 6 Step 12: Call clear device function Step 13: Stop.
Page 45
COMPUTER GRAPHICS
PROGRAM: #include<graphics.h> #include<stdio.h> #include<conio.h> #include<math.h> void main() { int driver,mode,x[5],y[5],x1[5],y1[5],xr,yr,i,n; float theta; clrscr(); driver=DETECT; initgraph(&driver,&mode,"c:\\tc\\bgi"); printf("Enter the no: of vertices:"); scanf("%d",&n); for(i=0;i<n;i++) { printf("\n Enter x[%d] y[%d]->",i+1,i+1); scanf("%d%d",&x[i],&y[i]); } setcolor(1); outtextxy(180,150,"Befor rotation"); setcolor(1); moveto(x[0],y[0]); for(i=1;i<n;i++) { lineto(x[i],y[i]); } lineto(x[0],y[0]); getch(); printf("\n\n\n\n\n\n\n\n\n Enter the rotation angle:"); scanf("%f",&theta);
DEPARTMENT OF COMPUTER APPLICATIONS Page 46
COMPUTER GRAPHICS
xr=getmaxx()/2; yr=getmaxx()/2; theta=(22.0/7.0)/180.0*theta; for(i=0;i<n;i++) { x1[i]=xr+(x[i]-xr)*cos(theta)-(y[i]-yr)*sin(theta); y1[i]=yr+(x[i]-xr)*sin(theta)+(y[i]-yr)*cos(theta); } outtextxy(190,330,"After rotation"); moveto(x1[0],y1[0]); for(i=1;i<n;i++) { lineto(x1[i],y1[i]); } lineto(x1[0],y1[0]); getch(); closegraph(); }
Page 47
COMPUTER GRAPHICS
OUTPUT: Enter the no: of vertices 3 Enter x[1] Enter x[2] Enter x[3] y[1] 315 y[2] 290 y[3] 415 90 210 290
BEFORE ROTATION
Page 48
COMPUTER GRAPHICS
2D SCALING
AIM: Write a C program to scale a 2D object with respect to orgin ALGORITHM: Step 1: Start Step 2: Initialize initgraph() and graphics mode Step 3: Read the no of vertices Step 4: Read the end points Step 5: Draw the polygon using line to command from(x0,y0) to (xk,yk) and move the pixel to line end points(xk,yk) Step 6: Repeat step 3 until the last pixel to initial and compute the polygon Step 7: Now draw the lines from final pixel to initial pixel and compute the polygon Step 8: Input the scaling factors, sx and sy Step 9: Obtain new end points as x= x * sx y= y * sy
Step 10: Repeat step 4 to step 8 to get the scaling position. Step 11: Call clear device function Step 12: Stop
Page 49
COMPUTER GRAPHICS
PROGRAM: #include<graphics.h> #include<stdio.h> #include<conio.h> #include<math.h> void main() { int driver,mode,x[5],y[5],i,n; int sx,sy,ch,xf,yf; clrscr(); driver=DETECT; initgraph(&driver,&mode,"c:\\tc\\bgi"); printf("enter the no: of vertices:"); scanf("%d",&n); for(i=0;i<n;i++) { printf("\n enter x[%d] y[%d]->",i+1,i+1); scanf("%d%d",&x[i],&y[i]); } printf("\nenter the Scaling factors:"); scanf("%d%d",&sx,&sy); do { printf("\n1.At orgin\n2.Fixed point\n3.exit"); printf("Enter your choice"); scanf("%d",&ch); if(ch==1) { outtextxy(200,150,"before Scaling"); setcolor(1); moveto(x[0],y[0]);
DEPARTMENT OF COMPUTER APPLICATIONS Page 50
COMPUTER GRAPHICS
for(i=1;i<n;i++) { lineto(x[i],y[i]); } lineto(x[0],y[0]); getch(); for(i=0;i<n;i++) { x[i]=x[i]*sx; y[i]=y[i]*sy; } outtextxy(100,100,"After Scaling"); moveto(x[0],y[0]); for(i=1;i<n;i++) { lineto(x[i],y[i]); } lineto(x[0],y[0]); getch(); } if(ch==2) { printf("enter Th Fixed Point"); scanf("%d%d",&xf,&yf); for(i=0;i<n;i++) { x[i]=xf+((x[i]-xf)*sx); y[i]=yf+((y[i]-yf)*sy); } outtextxy(100,80,"before Scaling"); setcolor(1);
DEPARTMENT OF COMPUTER APPLICATIONS Page 51
COMPUTER GRAPHICS
moveto(x[0],y[0]); for(i=1;i<n;i++) { lineto(x[i],y[i]); } lineto(x[0],y[0]); getch(); outtextxy(100,100,"After Scaling"); moveto(x[0],y[0]); for(i=1;i<n;i++) { lineto(x[i],y[i]); } lineto(x[0],y[0]); getch(); } } while(ch!=3); getch(); closegraph(); }
Page 52
COMPUTER GRAPHICS
OUTPUT: Enter the no: of vertices: 3 Enter x[1] y[1] 31590 Enter x[2] y[2] 210 290 Enter x[3] y[3] 410 290 BEFORE SCALING
AFTER SCALING
Page 53
COMPUTER GRAPHICS
2D REFLECTION
AIM: Write a C program to reflect a 2D object. ALGORITHM: Step 1: Start Step 2: Initialize initgraph() and graphics mode Step 3: Read choice Step 4: Initialize a function to plot points Step 5: If ch=1 then Do the reflection along X-axis x1[i] = -x1[i] If ch=2 then Do the reflection along Y-axis x1[i] = -x1[i] If ch=3 then Do the reflection along line Y = X x1[i] = x[i] If ch=4 then Do the reflection along the line Y = -X x1[i] = x[i] y1[i] = -y[i] Draw the reflected image Step 6: Call clear device function Step 7: Stop y1[i] = -y[i] Draw the reflected image y1[i] = y[i] Draw the reflected image y1[i] = -y1[i] Draw the reflected image
Page 54
COMPUTER GRAPHICS
PROGRAM: #include<stdio.h> #include<conio.h> #include<graphics.h> void main() { int gd,gm,ch,dx,dy; clrscr(); detectgraph(&gd,&gm); initgraph(&gd,&gm,"C:\\tc\\bgi"); do { printf("\n1:X-axis,\n2:Y-axis,\n3:Y=X,\n4:Y=-X,\n5:Exit"); printf("\nEnter the choice of axis about which the object is to reflect:"); scanf("%d",&ch); clrscr(); line(0,250,600,250); line(300,0,300,500); rectangle(320,100,370,150); dx=320-300; dy=250-150; getch(); if(ch==1) { outtextxy(50,50,"REFLECTION ABOUT X-axis"); rectangle(320,100+2*dy,370,150+2*dy); } if(ch==2) { outtextxy(50,50,"REFLECTION ABOUT Y-axis"); rectangle(320-2*dx-50,100,370-2*dx-50,150);
DEPARTMENT OF COMPUTER APPLICATIONS Page 55
COMPUTER GRAPHICS
} if(ch==3) { outtextxy(50,50,"REFLECTION ABOUT Y=X-axis"); rectangle(340+50,100+50,390+50,150+50); } if(ch==4) { outtextxy(50,50,"REFLECTION ABOUT Y=-X-axis"); rectangle(100,320,150,370); } } while(ch!=5); getch(); }
Page 56
COMPUTER GRAPHICS
OUTPUT: Enter the choice of axis about which the object is to reflect: 1. X axis 2. Y axis 3. X = Y 4. Y = -X 5. Exit
Enter the choice of axis about which the object is to reflect: 1. X axis 2. Y axis 3. X = Y 4. Y = -X 5. Exit
Page 57
COMPUTER GRAPHICS
Enter the choice of axis about which the object is to reflect: 1. X axis 3 Reflection about Y=X 2. Y axis 3. X = Y 4. Y = -X 5. Exit
Enter the choice of axis about which the object is to reflect: 1. X axis 2. Y axis 3. X = Y 4. Y = -X 5. Exit
Page 58
COMPUTER GRAPHICS
2D SHEAR
AIM: Write a C program to shear a 2D object. ALGORITHM: Step 1: Start Step 2: Initialize initgraph() and graphics mode Step 3: Read Choice Step 4: Initialize a function to plot points Step 5: if ch=1 then Read the shear parameter Convert the translation parameters Do the transformation along the shearing x = x + shx * y Draw the sheared image if ch=2 then Read the shear parameter Convert the translation parameters Do the transformation along the shearing y = y + shy * x Draw the sheared image Step 6: Call clear device function Step 7: Stop
Page 59
COMPUTER GRAPHICS
PROGRAM: #include<stdio.h> #include<conio.h> #include<graphics.h> #include<process.h> void x_shear(float*,float*,int); void y_shear(float*,float*,int); void main() { float x1=100,y1=100,x2=200,y2=100,x3=200,y3=200,x4=100,y4=200; int c,gd,gm,sh; printf("\n enter your choice\n (1) x-axis\n (2) y-axis"); scanf("%d",&c); printf("enter the shear factor"); scanf("%d",&sh); detectgraph(&gd,&gm); initgraph(&gd,&gm,"c:\\tc\\bgi"); line(x1,y1,x2,y2); line(x2,y2,x3,y3); line(x3,y3,x4,y4); line(x4,y4,x1,y1); getch(); if(c==1) { x_shear(&x1,&y1,sh); x_shear(&x2,&y2,sh); x_shear(&x3,&y3,sh); x_shear(&x4,&y4,sh); } if(c==2) {
DEPARTMENT OF COMPUTER APPLICATIONS Page 60
COMPUTER GRAPHICS
y_shear(&x1,&y1,sh); y_shear(&x2,&y2,sh); y_shear(&x3,&y3,sh); y_shear(&x4,&y4,sh); } outtextxy(50,50,"SHEARING"); line(x1,y1,x2,y2); line(x2,y2,x3,y3); line(x3,y3,x4,y4); line(x4,y4,x1,y1); getch(); } void x_shear(float *x,float *y,int sh) { (*x)=(*x)+sh*(*y); } void y_shear(float *x,float*y,int sh) { (*y)=(*y)+sh*(*x); }
Page 61
COMPUTER GRAPHICS
OUTPUT: Enter your choice 1. X- axis 2. Y-axis 1 Enter the shear factor 2
Page 62
COMPUTER GRAPHICS
Page 63
COMPUTER GRAPHICS
PROGRAM: #include<stdio.h> #include<graphics.h> #include<conio.h> #define TRUE 1 #define FALSE 0 typedef unsigned int outcode; enum { TOP=0X1,BOTTOM=0X2,RIGHT=0X4,LEFT=0X8 }; void lineclip(int,int,int,int,int,int,int,int); int compoutecode(int,int,int,int,int,int); void main() { int xmin,ymin,xmax,ymax,x0,y0,x1,y1; int gd=DETECT,gmode; initgraph(&gd,&gmode,"C:\\TC\\BGI"); printf("enter the co-ordinates of the window"); scanf("%d%d%d%d",&xmin,&ymin,&xmax,&ymax); printf("\nenter theco-ordinates of the line"); scanf("%d%d%d%d",&x0,&y0,&x1,&y1); printf("\nbefore clipping"); rectangle(xmin,ymin,xmax,ymax); line(x0,y0,x1,y1); getch(); closegraph(); initgraph(&gd,&gmode,"C:\\TC\\BGI"); printf("\n\n\nafter clipping"); rectangle(xmin,ymin,xmax,ymax); lineclip(x0,y0,x1,y1,xmin,xmax,ymin,ymax);
DEPARTMENT OF COMPUTER APPLICATIONS Page 64
COMPUTER GRAPHICS
getch(); closegraph(); } void lineclip(int x0,int y0,int x1,int y1,int xmin,int xmax,int ymin,int ymax) { outcode outcode0,outcode1,outcodeout; int accept=FALSE,done=FALSE,x,y; outcode0=compoutecode(x0,y0,xmin,xmax,ymin,ymax); outcode1=compoutecode(x1,y1,xmin,xmax,ymin,ymax); do { if(!(outcode0|outcode1)) { accept=TRUE; done=TRUE; } else if(outcode0&outcode1) { done=TRUE; } else { if(outcode0) { outcodeout=outcode0; } else { outcodeout=outcode1; } if(outcodeout&TOP) {
DEPARTMENT OF COMPUTER APPLICATIONS Page 65
COMPUTER GRAPHICS
x=x0+(x1-x0)*(ymax-y0)/(y1-y0); y=ymax; } else if(outcodeout&BOTTOM) { x=x0+(x1-x0)*(ymin-y0)/(y1-y0); y=ymin; } else if(outcodeout&RIGHT) { y=y0+(y1-y0)*(xmax-x0)/(x1-x0); x=xmax; } else { y=y0+(y1-y0)*(xmin-x0)/(x1-x0); x=xmin; } if(outcodeout==outcode0) { x0=x; y0=y; outcode0=compoutecode(x0,y0,xmin,xmax,ymin,ymax); } else { x1=x; y1=y; outcode1=compoutecode(x1,y1,xmin,xmax,ymin,ymax); } } }while(done==FALSE);
DEPARTMENT OF COMPUTER APPLICATIONS Page 66
COMPUTER GRAPHICS
if(accept) { line(x0,y0,x1,y1); } } int compoutecode(int x,int y,int xmin,int xmax,int ymin,int ymax) { outcode code=0; if(y>ymax) { code=code|TOP; } else if(y<ymin) { code=code|BOTTOM; } if(x>xmax) { code=code|RIGHT; } else if(x<xmin) { code=code|LEFT; } return code; }
Page 67
COMPUTER GRAPHICS
OUTPUT:
BEFORE CLIPPING
AFTER CLIPPING
Page 68
COMPUTER GRAPHICS
Page 69
COMPUTER GRAPHICS
PROGRAM: #include<stdio.h> #include<graphics.h> #include<conio.h> #define FALSE 0 #define TRUE 1 typedef unsigned int outcode; enum{TOP=0x1,BOTTOM=0x2,RIGHT=0x4,LEFT=0x8}; void CohenSutherlandLineClipAndDraw(int,int,int,int,int,int,int,int); int compoutcode(int,int,int,int,int,int); void main() { int n,i,x[10],y[10],xmin,ymin,xmax,ymax; int gd=DETECT,gmode; initgraph(&gd,&gmode,"c:\\tc\\bgi"); printf("enter the number of vertices of the polygen"); scanf("%d",&n); printf("\nEnter the points"); for(i=0;i<n;i++) { scanf("%d%d",&x[i],&y[i]); } printf("\nEnter the point of the clipping rectangle"); scanf("%d%d%d%d",&xmin,&ymin,&xmax,&ymax); clearviewport(); for(i=0;i<n-1;i++) { line(x[i],y[i],x[i+1],y[i+1]); } line(x[i],y[i],x[0],y[0]); setcolor(8);
DEPARTMENT OF COMPUTER APPLICATIONS Page 70
COMPUTER GRAPHICS
rectangle(xmin,ymin,xmax,ymax); getch(); clearviewport(); for(i=0;i<n-1;i++) CohenSutherlandLineClipAndDraw(x[i],y[i],x[i+i],y[i+1],xmin,xmax,ymin,ymax); CohenSutherlandLineClipAndDraw(x[i],y[i],x[0],y[0],xmin,xmax,ymin,ymax); rectangle(xmin,ymin,xmax,ymax); getch(); closegraph(); } void CohenSutherlandLineClipAndDraw(int x0,int y0,int x1,int y1,int xmin,int xmax,int ymin,int ymax) { outcode outcode0,outcode1,outcodeout; int accept=FALSE,done=FALSE; int x,y; outcode0=compoutcode(x0,y0,xmin,xmax,ymin,ymax); outcode1=compoutcode(x1,y1,xmin,xmax,ymin,ymax); do { if(!(outcode0|outcode1)) { accept=TRUE; done=TRUE; } else if(outcode0 & outcode1) done=TRUE; else { if(outcode0) outcodeout=outcode0; else
DEPARTMENT OF COMPUTER APPLICATIONS Page 71
COMPUTER GRAPHICS
outcodeout=outcode1; if(outcodeout&TOP) { x=x0+(x1-x0)*(ymax-y0)/(y1-y0); y=ymax; } else if(outcodeout & BOTTOM) { x=x0+(x1-x0)*(ymin-y0)/(y1-y0); y=ymin; } else if(outcodeout & RIGHT) { y=y0+(y1-y0)*(xmax-x0)/(x1-x0); x=xmax; } else { y=y0+(y1-y0)*(xmin-x0)/(x1-x0); x=xmin; } if(outcodeout==outcode0) { x0=x; y0=y; outcode0=compoutcode(x0,y0,xmin,xmax,ymin,ymax); } else { x1=x; y1=y; outcode1=compoutcode(x1,y1,xmin,xmax,ymin,ymax);
DEPARTMENT OF COMPUTER APPLICATIONS Page 72
COMPUTER GRAPHICS
} } }while(done==FALSE); if(accept) line(x0,y0,x1,y1); } int compoutcode(int x,int y,int xmin,int xmax,int ymin,int ymax) { outcode code=0; if(y>ymax) code=code|TOP; else if(y<ymin) code=code|BOTTOM; if(x>xmax) code=code|RIGHT; else if(x<xmin) code=code|LEFT; return code; }
Page 73
COMPUTER GRAPHICS
OUTPUT: Enter the number of vertices 3 Enter the coordinates 200 280 300 200 100 200
Enter the points of the clipping rectangle 150 180 200 300
BEFORE CLIPPING
AFTER CLIPPING
Page 74
COMPUTER GRAPHICS
PIE CHART
AIM: Write a C program to draw a pie chart ALGORITHM: Step 1: Start Step 2: Initialize initgaraph and graphics mode Step 3: Get the number of cities,n Step 4: Get the cityname temperature of n cities Step 5: Draw barchart and piechart Step 6: Stop
Page 75
COMPUTER GRAPHICS
PROGRAM: #include<stdio.h> #include<conio.h> #include<graphics.h> #include<process.h> #include<dos.h> void main() { char *pa=0; int i,j,ii,jj,n,t[50],d=0; int pnew[50]={0},poId=0; float tot=0; char a[50]; int gd,gm; gd=DETECT; initgraph(&gd,&gm,"C:\\TC\\BGI"); printf("ENTER DETAILS "); printf("\n-------------"); printf("1.NO OF CITIES: "); scanf("%d",&n); for(i=0;i<n;i++) { printf("ENTER DETAILS "); printf("\n----------------"); printf("\n1. NAME OF CITY :"); scanf("%s",&a[i]); printf("\nTEMPERATURE :"); scanf("%d",&t[i]); } cleardevice(); line(50,400,50,100); line(50,400,350,400); for(i=0;i<300;i=i+30)
DEPARTMENT OF COMPUTER APPLICATIONS Page 76
COMPUTER GRAPHICS
{ putpixel(50+i,400,5); putpixel(50+i,399,5); putpixel(50+i,401,5); putpixel(50,400-i,5); putpixel(50,400-i,5); putpixel(49,400-i,5); putpixel(51,400-i,5); } for(i=0;i<n;i++) { int nn=3*t[i]; setcolor(i+2); for(j=0;j<nn;j++) line(60+d,400-j,80+d,400-j); *pa=a[i]; outtextxy(68+d,405,pa); d=d+30; tot=tot+t[i]; } setcolor(WHITE); for(i=0;i<n;i++) { setfillstyle(1,i+2); pnew[i]=poId+((t[i]*100)/tot)*3.60; if(i==n-1)pnew[i]=360; pieslice(500,240,poId,pnew[i],100); poId=pnew[i]; } getch(); }
Page 77
COMPUTER GRAPHICS
OUTPUT: Enter details -----------------No: of cities 2 Enter details -----------------1. Name of the city Trivandrum 2. Temperature 40 Enter details -----------------1. Name of the city Kollam 2. Temperature 60
Page 78
COMPUTER GRAPHICS
Page 79
COMPUTER GRAPHICS
PROGRAM: #include<stdio.h> #include<conio.h> #include<graphics.h> #include<dos.h> #include<math.h> #define arg_sec M_PI/30 #define arg_hour M_PI/6 #define arg_min M_PI/360 void main() { int gd=DETECT,gm,sec=0,hour,min,x1=0,y1=0,x2=0,y2=0,x3=0,y3=0; char *k[12]={"1","2","3","4","5","6","7","8","9","10","11","12"}; int a,b,i; int dig_sec; char Time_Dig[14]; setcolor(RED); circle(300,200,200); circle(300,200,180); setfillstyle(1,RED); floodfill(300,390,RED); for(i=1;i<13;i++) { a=160*cos(arg_hour*i-M_PI_2); b=160*sin(arg_hour*i-M_PI_2); outtextxy(a+300,b+200,k[i-1]); } while(!kbhit()) { setcolor(BLACK); line(300,200,x1+300,y1+200); line(300,200,x2+300,y2+200); line(300,200,x3+300,y3+200); gettime(&t); hour=t.ti_hour;
Page 80
struct time t;
initgraph(&gd,&gm,"C:\\TC\\BGI");
COMPUTER GRAPHICS
sec=t.ti_sec;
min=t.ti_min;
Time_Dig[0]=hour/10+48; Time_Dig[1]=hour%10+48; Time_Dig[2]=':'; Time_Dig[3]=min/10+48; Time_Dig[4]=min%10+48; Time_Dig[5]=':'; Time_Dig[6]=sec/10+48; Time_Dig[7]=sec%10+48; Time_Dig[8]='\0'; outtextxy(270,250,""); x1=150*cos(arg_sec*sec-M_PI_2)*0.98; y1=150*sin(arg_sec*sec-M_PI_2)*0.98; x2=150*cos(arg_sec*min-M_PI_2)*0.9; y2=150*sin(arg_sec*min-M_PI_2)*0.9; if(hour>12) hour-=12; x3=150*cos(arg_hour*hour-M_PI_2+arg_min*min)*0.6; y3=150*sin(arg_hour*hour-M_PI_2+arg_min*min)*0.6; setcolor(YELLOW); line(300,200,x1+300,y1+200); setcolor(CYAN); line(300,200,x2+300,y2+200); setcolor(WHITE); line(300,200,x3+300,y3+200); setcolor(YELLOW); outtextxy(270,250,Time_Dig); delay(50); } getch(); closegraph(); restorecrtmode(); }
DEPARTMENT OF COMPUTER APPLICATIONS Page 81
COMPUTER GRAPHICS
OUTPUT:
Page 82
COMPUTER GRAPHICS
BICYCLE RIDING
AIM: Write a C program to draw a person riding a bicycle ALGORITHM: Step 1: Start Step 2: Initialize the graphics driver and initgraph. Step 3: Declare a function cycle 3.1: Draw the figure of a man riding bicycle using inbuilt function such as line(),circle() Step 4: Animate the man and cycle Step 5: Call cleardevice(). Step 6: Call closegraph(). Step 7: Stop.
Page 83
COMPUTER GRAPHICS
PROGRAM: #include<graphics.h> #include<stdio.h> #include<conio.h> void cycle() { int i; int body[]={450,260,490,190,520,250,570,200,490,190}; int human[]={565,190,575,190,570,150,566,150,566,147, 563,147,563,150,560,150,565,190}; for(i=0;i<=700;i+=5) { cleardevice(); body[0]=450-i; body[2]=490-i; body[4]=520-i; body[6]=570-i; body[8]=490-i; human[0]=565-i; human[2]=575-i; human[4]=570-i; human[6]=566-i; human[8]=566-i; human[10]=563-i; human[12]=563-i; human[14]=560-i; human[16]=565-i; setcolor(WHITE); circle(450-i,260,20); //front wheel circle(450-i,260,3); // front wheel circle(600-i,260,20);//back wheel
DEPARTMENT OF COMPUTER APPLICATIONS Page 84
body[1]=260; body[3]=190; body[5]=250; body[7]=200; body[9]=190; human[1]=190; human[3]=190; human[5]=150; human[7]=150; human[9]=147; human[11]=147; human[13]=150; human[15]=150; human[17]=190;
COMPUTER GRAPHICS
circle(600-i,260,3); //back wheel circle(520-i,250,7); //pedal circle(520-i,250,3); //pedal line(520-i,258,600-i,262);//lower chain line(520-i,242,600-i,258);//upper chain line(570-i,200,600-i,260); //back stay line(490-i,190,505-i,170);//handle line(490-i,160,520-i,180);//handle // seat line(570-i,200,570-i,195); line(565-i,190,565-i,195); line(565-i,195,575-i,195); line(575-i,195,575-i,190); setcolor(RED); line(520-i,250,530-i,255); line(520-i,250,510-i,245); setcolor(WHITE); line(520-i,180,525-i,175); //handle node line(493-i,160,498-i,155); setcolor(LIGHTRED); line(565-i,190,520-i,220); line(520-i,220,510-i,245); line(575-i,190,533-i,230); line(533-i,230,530-i,255); setcolor(LIGHTRED); line(566-i,150,540-i,185);//hand1 line(540-i,185,527-i,175); line(563-i,150,530-i,165);//hand 2 line(530-i,165,498-i,155); setfillstyle(SOLID_FILL,RED); setcolor(LIGHTRED); circle(564-i,140,5);//head
DEPARTMENT OF COMPUTER APPLICATIONS Page 85
COMPUTER GRAPHICS
setcolor(WHITE); drawpoly(5,body); setcolor(LIGHTRED); drawpoly(9,human); delay(180); } } void main() { int gd=DETECT,gmode; int x; initgraph(&gd,&gmode,"c:\\tc\\bgi"); cycle(); getch(); closegraph(); }
Page 86
COMPUTER GRAPHICS
OUTPUT:
Page 87