You are on page 1of 87

COMPUTER GRAPHICS

Program no: 1 Date: 13/7/2013

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

DEPARTMENT OF COMPUTER APPLICATIONS

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

int i,j,k,l; for(i=230,k=150;i>100;i--,k++)

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

DEPARTMENT OF COMPUTER APPLICATIONS

Page 3

COMPUTER GRAPHICS

OUTPUT

DEPARTMENT OF COMPUTER APPLICATIONS

Page 4

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 DEPARTMENT OF COMPUTER APPLICATIONS Page 5 .COMPUTER GRAPHICS Program No: 2 Date: 17/7/2013 ANIMATED ROCKET LAUNCHING AIM: Write a C program to draw an animated rocket launching.

h> #include<conio.i.420-y. rectangle(290. line(315.400-y).COMPUTER GRAPHICS PROGRAM: #include<stdio.300-y).y=y+5) { rectangle(300.330.260-y.y<=300. closegraph(). cleardevice().i<=340.300-y.330. setcolor(YELLOW).&m.h> #include<graphics.h> void main() { int d.i=i+3) line(i.m.300-y). delay(100). initgraph(&d. d=DETECT. line(315.440-y).i+3."c:\\tc\\bgi").340.420-y). } getch(). for(y=10.300.y. setcolor(RED).260-y. clrscr(). } DEPARTMENT OF COMPUTER APPLICATIONS Page 6 .400-y. for(i=290.

COMPUTER GRAPHICS OUTPUT: DEPARTMENT OF COMPUTER APPLICATIONS Page 7 .

j. ii) Set x1=x1+15.x3.x2=220.y1=100. iii)Set x2=x2+15.y2. h) Initialize i to 0 and repeat the steps until i<24 i) Execute the pieslice function.then decrement j by 1. d) Initialize x1=60. c) Draw a polygon using polygon function.repeat the following steps until i>2. e) Initialize i to 0 and execute the following.x3=340.x2. a) Check whether i>30. Step 3: Initialize j to 50.x2=15. DEPARTMENT OF COMPUTER APPLICATIONS Page 8 .y1. g) Set x1=0. b) Set the fill pattern as solid fill.COMPUTER GRAPHICS Program No: 3 Date: 21/7/2013 ANIMATED FLAG HOISTING AIM: Write a C program to draw an animated flag hoisting ALGORITHM: Step 1: Start Step 2: Declare the variables i. 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..y2=140.x1.i to 82. iv) Call the delay() and cleardevice() Step 4: Stop.y3=180.

x1.180.y2=140.180.y2-53+3*l. ellipse(x2-2*k.3+l). void main() { int i. ellipse(x2-2*k. initgraph(&gd.480.0.3+l).BROWN).100-k.x2.80.x2=220.3+l).l=82. clrscr().60-k.270.k.i++) { if(i<60) setcolor(RED).y1=100.80."c:\\tc\\bgi").0.x3.h> int back[10]={59.y3. ellipse(x3-4*k.59.80}. DEPARTMENT OF COMPUTER APPLICATIONS Page 9 .l--) { cleardevice(). for(i=0.3+l).COMPUTER GRAPHICS PROGRAM: #include<stdio.y1-20+l.y2. ellipse(x3-4*k.i<180.54.gm.l>2.l. gd=DETECT.r1.h> #include<conio.y1-20+l.3+l). for(k=50. fillpoly(5.y2-52+3*l. x1=60.60-k.60-k.59.y3=180.y1. ellipse(x1.0. if(i>60&&i<120) setcolor(WHITE).60-k.480.&gm. if(i>120) setcolor(GREEN). if(l>30)k--.j.y3-86+5*l.back).h> #include<graphics.r2. setfillstyle(9.90.x3=340.gd.90.270.54.90.

y2++.x1.i<24. x1=x1+15.i++) { pieslice(220-2*k.30-k/2).15).y3++.x2. x1=0.210-37+2*l. for(i=0.x2=15. setfillstyle(1. x2=x2+15. } setcolor(BLUE). } DEPARTMENT OF COMPUTER APPLICATIONS Page 10 .COMPUTER GRAPHICS y1++. } } getch().

COMPUTER GRAPHICS OUTPUT: DEPARTMENT OF COMPUTER APPLICATIONS Page 11 .

COMPUTER GRAPHICS Program No: 4 Date: 25/7/2013 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 DEPARTMENT OF COMPUTER APPLICATIONS Page 12 .

h> void drawfan(int. printf("\n Enter the choice"). break. case 2:Switch(100).int).&gmode. switch(speed) { case 1:Switch(300)."c:\\tc\\bgi").Fast"). printf("\n2.gmode.Slow").h> #include<conio. do { cleardevice().5). drawfan(0.COMPUTER GRAPHICS PROGRAM: #include<stdio.15). initgraph(&gd.MEdium"). printf("\n4. printf("\n3. DEPARTMENT OF COMPUTER APPLICATIONS Page 13 .OFF").&speed) . break. int gd=DETECT.h> #include<graphics. scanf("%d". case 3:Switch(50). void Switch(int).h> #include<math. gotoxy(30. printf("\n1. void main() { int speed. break.

320).int color) { int i.i+=3) circle(315.i<39.240.COMPUTER GRAPHICS } }while(speed!=4). delay(speed).35). }} DEPARTMENT OF COMPUTER APPLICATIONS Page 14 . setfillstyle(SOLID_FILL.i<=1400. } void Switch(speed) { int i=0. closegraph().15).WHITE).240+rotate.240.240. drawfan(i+80.35).240.320.317.330.330).i+=80) { drawfan(i. pieslice(315.120+rotate. } void drawfan(int rotate. bar(300.0).280+rotate. bar(313. setcolor(YELLOW).160+rotate. for(i=0. getch().0+rotate. setcolor(color). for(i=0.40+rotate.i).35). pieslice(315.240. pieslice(315.RED). setfillstyle(SOLID_FILL.

FAST 4. MEDIUM 3. SLOW 2. EXIT Enter the choice: 1 DEPARTMENT OF COMPUTER APPLICATIONS Page 15 .COMPUTER GRAPHICS OUTPUT: ELECTRIC FAN 1.

100+x.set x=x+50 Step 11:Increment flag.y.set y=y+50 Step 12:Stop DEPARTMENT OF COMPUTER APPLICATIONS Page 16 .100+y Step 8: Check whether flag%2=0.COMPUTER GRAPHICS Program No: 5 Date: 29/7/2013 CHESS BOARD AIM: Write a C program to draw a chess board.x. ALGORITHM: Step 1: Start Step 2: Initialize graphics components Step 3: Declare variables flag.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.i.then set color to white else set to black Step 9: Fill the rectangle Step 10:Increment flag.

"c:\\tc\\bgi").j++) { rectangle(50+x. closegraph().COMPUTER GRAPHICS PROGRAM: #include<stdio.15).100+y).gm.gd.BLACK).y=0. for(i=0.j<8. flag++.h> #include<graphics.y. } flag++.i++) { for(j=0.100+x.j. } getch(). x=x+50. initgraph(&gd. y=y+50.WHITE). floodfill(52+x.x.i<8. gd=DETECT.x=0. clrscr().i.50+y.h> void main() { int flag=0.52+y.h> #include<conio. } DEPARTMENT OF COMPUTER APPLICATIONS Page 17 . if(flag%2==0) setfillstyle(1.&gm. else setfillstyle(1.

COMPUTER GRAPHICS OUTPUT: DEPARTMENT OF COMPUTER APPLICATIONS Page 18 .

COMPUTER GRAPHICS Program No: 6 Date: 4/8/2013 FLIGHT TAKEOFF & LANDING AIM: Write a C program to draw the takeoff and landing of a flight. Step8: Stop. DEPARTMENT OF COMPUTER APPLICATIONS Page 19 . Step5: Set a loop and call the plane function for take off animation. 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. Step6: Set a loop and call the plane function for landing animation. Step4: Initialize the graphic function. Step7: Set the suitable color using setfillstyle() function.

for(i=0.&gmode.i<400.int j) DEPARTMENT OF COMPUTER APPLICATIONS Page 20 . } for(i=0. } for(i=0. closegraph().gmode. delay(100).COMPUTER GRAPHICS PROGRAM: #include<stdio. j--.h> #include<graphics.h> #include<conio.j=0.i+=2) {cleardevice().i++) { plane(0. } void plane(int i. plane(i-300. delay(75). } getch(). int gd=DETECT."c:\\tc\\bgi"). delay(75).i<50. plane(i.j).h> void plane(int.j). void main() { int i.i+=2) { cleardevice().int). initgraph(&gd. j=j++.i<500.0).

380.380.body).120.400. setcolor(GREEN). body[4]=150+i.body).110. w1[9]=380-j. body[5] =380-j.120. body[0]=50+i. w1[6]=110+i.120. w1[2]=140+i. body[2]=200+i. setcolor(RED). w2[9]=380-j. w2[6]=110+i. int w2[]={120.120.450. setcolor(RED).380}. body[7] =380-j.330. body[1] =400-j.400. body[6]=70+i. } DEPARTMENT OF COMPUTER APPLICATIONS Page 21 . w1[0]=120+i.200.140. drawpoly(5. w2[2]=140+i. setcolor(YELLOW). setfillstyle(SOLID_FILL. body[8]=30+i. w1[5]=450-j. w1[8]=120+i. w2[8]=120+i. w2[1]=380-j.380} . drawpoly(6.30.400} .380.140.50.110. w1[3]=380-j. w2[5]=330-j. w2[4]=120+i.330.380.COMPUTER GRAPHICS { int body[]={50. int w1[]={120.380.w2). body[3]=400-j. w1[4]=120+i. drawpoly(5.450. w2[3]=380-j. w2[0]=120+i.150.300. w2[7]=330-j. w1[7]=450-j. body[9] =300-j. body[10]=50+i. w1[1]=380-j. body[11]=400-j.BLACK). fillpoly(6.w1).380.70.

COMPUTER GRAPHICS OUTPUT: DEPARTMENT OF COMPUTER APPLICATIONS Page 22 .

Step 2: Declare and initialize variables such as word. Step 3: Initialize graphics driver and initgraph. Step 7: Call cleardevice().size. Step 4: Read the input string. Step 6: Print input string in different font style stored in the array. ALGORITHM: Step 1: Start.COMPUTER GRAPHICS Program No: 7 Date: 7/8/2013 TEXT ANIMATION AIM: Write a C program to animate text. Step 9: Stop.style. Step 5: Store the font style in an array.fname. DEPARTMENT OF COMPUTER APPLICATIONS Page 23 . Step 8: Call closegraph().

HORIZ_DIR. gets(word)."GOTHIC font"}. style<=GOTHIC_FONT.300. printf("Enter the Word").midx. settextstyle(style. } DEPARTMENT OF COMPUTER APPLICATIONS Page 24 . getch(). for (style=DEFAULT_FONT.h> #include<graphics. midy.COMPUTER GRAPHICS PROGRAM: #include<stdio. closegraph().h> void main() { char word[100]. initgraph(&gd."SANS SERIF font".gmode."TRIPLEX font". } getch(). style++) { cleardevice().h> #include<conio. char *fname[] = { "DEFAULT font".midy."c:\\tc\\bgi").size. if (style == TRIPLEX_FONT) size = 4. outtextxy(midx.&gmode. setcolor(style). midy = getmaxy() / 2.word). size). fname[style]). int gd=DETECT. "SMALL font". outtextxy(400. int style. midx = getmaxx() / 2.

COMPUTER GRAPHICS OUTPUT: DEPARTMENT OF COMPUTER APPLICATIONS Page 25 .

colour) End: Step 8 : Stop. 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).round(Y). a Y=Y .Y ) a b b b Step 2: Set dx=X -X and dy=Y -Y a b b a Step 3: X= X .Y ) and (X .colour) Step 7 : Repeat following for k=1 to steps begin: =X+X increment Y=Y+Y increment Putpixel(round(X). DEPARTMENT OF COMPUTER APPLICATIONS Page 26 .COMPUTER GRAPHICS Program No: 8 Date: 10/8/2013 DDA LINE DRAWING ALGORITHM AIM: Write a C program to draw a line using DDA Algorithm ALGORITHM: Step 1: Read the end points of the line (X .abs(Y).

y2. getch(). scanf("%d%d%d%d". printf("Enter the end points").y1.h> #define ROUND(a) ((int)(a+.&y2). } y=y+yincrement.COMPUTER GRAPHICS PROGRAM: #include<stdio.ROUND(y).x2. } xincrement=dx/(float)s.y2). closegraph().5).h> #include<conio. putpixel(ROUND(x).x=x1.&x2. linedda(x1.y=y1.gmode. int gd=DETECT.&x1.&y1. } void linedda(int x1.k<s.ROUND(y).int y2) { int dx=x2-x1.int y1. . float xincrement.k++) { x=x+xincrement.int.&gmode.k. }} DEPARTMENT OF COMPUTER APPLICATIONS Page 27 s=abs(dx). void main() { int x1. yincrement= dy/(float)s.5)) void linedda(int. putpixel(ROUND(x).yincrement.dy=y2-y1. if(abs(dx)>abs(dy)) { else { s=abs(dy).int x2.int).5)."C:\\TC\\BGI").h> #include<math.x2.y1.s.int. for(k=0.h> #include<graphics. initgraph(&gd.

COMPUTER GRAPHICS OUTPUT: Enter the end points: 100 100 300 250 DEPARTMENT OF COMPUTER APPLICATIONS Page 28 .

Otherwise check 5. 6.2. otherwise set y=y-1 and calculate p=p+twodydx. 5. p=2*dy-dx.COMPUTER GRAPHICS Program No: 9 Date: 14/8/2013 BRESENHAMS LINE DRAWING ALGORITHM AIM: Write a C program to draw a line using Bresenham’s Algorithm ALGORITHM: Step 1: Start. y=yb and xend=xa.1. y=ya and yend=yb.2.1. 6. Step 3: Read the starting coordinates as (xa. y=yb and yend=ya.1.if flag=true.x.flag.4.1. if p < 0 set p=p+twody.2. Otherwise check 6.3.1.2.y).if ya > yb set flag =1 otherwise set flag=0.yb).p. dy=ya-yb. set x=xa.xend. then set x=x+1 6.ya) and ending coordinate as (xb.ya.6.3. Increment variable x by 1. if yb > ya set flag =1 otherwise set flag=0. 5.3.3.if xa > xb.1 set x=xb.if ya < yb.yb. 5.3.3.3. y=ya and xend=xb. Step 2: Declare the variables xa.1. otherwise set x=x-1 and calculate py=py+twodxdy. 5.xb.1.if ya > yb. Step 7: Stop.2.then 6. 6. set x=xa.3. go to step 7 Step 6 : if dy > dx then check 6. Put a pixel in (x.2.3. 5. 6.3.2.y).2.3.2. Put a pixel in (x.dx.py. 5.if xa < xb. then set y=y+1 5.then 5. Increment variable y by 1.3.2. then 5. 6.yend.2.3. twody=2*dy.3.4. if flag=true.5. py=2*dx-dy.if xb < xa set flag =1 otherwise set flag=0. twodx=2*dx and twodydx=2*(dy-dx). 5.dy. if py < 0 set py=py+twodx.1. 6.3. DEPARTMENT OF COMPUTER APPLICATIONS Page 29 . if xa < yb set flag =1 otherwise set flag=0.twodxdy=2*(dx-dy) Step 5: if dx > dy then check 5.1 set x=xb.end. 5. Repeat following steps until x < xend 5.1.6. then 6.4. Step 4: set dx=xa-xb.5. Repeat following steps until y < yend 6. 6.3.2.1.y.

COMPUTER GRAPHICS PROGRAM: #include<stdio. dyy=2*dy. int gd=DETECT.int). void main() { int x1.p. } void linebrhm(int x1."C:\\TC\\BGI").int y1.x2.y1.gmode.x. scanf("%d%d%d%d". initgraph(&gd.&x2. getch().y1. linebrhm(x1.int x2.h> #include<graphics.dy.y.int.xEnd. dx=abs(x1-x2). dy=abs(y1-y2).y2.&gmode.&x1.h> #include<conio.dxx.int.x2.dyy.&y2).&y1. p=2*dy-dx. printf("Enter the end points"). DEPARTMENT OF COMPUTER APPLICATIONS Page 30 . y=y2. xEnd=x1.int y2) { float dx. dxy=2*(dy-dx). closegraph().h> void linebrhm(int. if(x1>x2) { x=x1.dxy.y2).h> #include<math.

} putpixel(x. } else { y++. } } DEPARTMENT OF COMPUTER APPLICATIONS Page 31 . xEnd=x2.y.COMPUTER GRAPHICS } else { x=x1. p=p+dxy.5). y=y1.y. if(p<0) { p=p+dyy. while(x<xEnd) { x++.5). } putpixel(x.

COMPUTER GRAPHICS OUTPUT: DEPARTMENT OF COMPUTER APPLICATIONS Page 32 .

Y ) = (0.0) is K (X K+1 .r) 0 0 Step 2: Calculate the initial value of the decision parameter as P = 5/4 – r 0 At each X position .Y) on the circle path centered on (X Y ) and plot the co-ordinate values X =X +X .Y ) and obtain the first point on the circumference of a C C circle centered on the origin as (X . perform the following test: K Step 3: If P <0.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.starting at k=0.Y = Y+Y C C C C Step 6: Repeat steps 3 through 5 until X≥Y.Y)and P K+1 = P + 2X K (K+1) +1 Otherwise . Step 7: Stop DEPARTMENT OF COMPUTER APPLICATIONS Page 33 .the next pont along the circle is (X +1. the next pont along the circle centered on (0.COMPUTER GRAPHICS Program No: 10 Date: 19/8/2013 MIDPOINT – CIRCLE ALGORITHM 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=r.int r) { int x. while(x<y) { if(p<0) { p=p+2*(x+1)+1. initgraph(&gd.h> #include<stdlib. x=0. DEPARTMENT OF COMPUTER APPLICATIONS Page 34 .yc. circlemid(xc.int).5)) void circlemid(int xc. } void circlemid(int x0.int yc.h> #include<stdio. p=1-r.y0.r.int r). printf("Enter the center and radius").y). void main() { int gd=DETECT.int. closegraph().&xc.x.yc.y. getch().&yc.r).int.&gmode. circlepts(x0.int y0.COMPUTER GRAPHICS PROGRAM: #include<graphics.gmode. void circlepts(int.p.&r). scanf("%d%d%d".xc."c:\\tc\\bgi").h> #include<math.h> #include<conio.h> #define ROUND(a) ((int)(a+0.

COMPUTER GRAPHICS x++. putpixel(x0+y. } DEPARTMENT OF COMPUTER APPLICATIONS Page 35 . putpixel(x0-y. putpixel(x0+y.6).6).6).y0-x. putpixel(x0-x.6).int x.6).y0.y).y0+y.6).y0+x.y0-y.x.int y) { putpixel(x0+x. putpixel(x0-y. putpixel(x0+x.6).int y0.6). y--. x++.y0-y. } circlepts(x0.y0-x. putpixel(x0-x.y0+x.y0+y. } else { p=p+2*(x+1)+1-2*(y-1). }} void circlepts(int x0.

COMPUTER GRAPHICS OUTPUT: Enter the center and radius x: 300 y: 200 r: 50 DEPARTMENT OF COMPUTER APPLICATIONS Page 36 .

y+1.oldcolor) recursively call floodfill(x.old.y.fillcolor.fillcolor) call floodfill(x-1.oldcolor) recursively Step 4: Stop DEPARTMENT OF COMPUTER APPLICATIONS Page 37 .fillcolor.3) if the current pixel!= fill color then putpixel(x.1) Draw the circle and fill it with the specified old color 3.y.fillcolor.oldcolor) recursively call floodfill(x.y.y-1.oldcolor) recursively call floodfill(x+1.fillcolor.fill and the function floddfill() Step 3:Read the old color and the fill color 3.COMPUTER GRAPHICS Program No: 11 Date: 24/8/2013 FLOOD FILL ALGORITHM AIM: Write a C program to fill a polygon using Flood fill algorithm ALGORITHM: Step 1: Start Step 2: Declare the variables x.2) Get the color of the current pixel 3.y.

gm.h> void floodfill1(int.int fill.int old) { printf("Enter the coordinates").old.int.COMPUTER GRAPHICS PROGRAM: #include<graphics.int). setcolor(old).int.int. void main() { int gd=DETECT. } void floodfill1(int x.y.&old).y.int y.old). } void floodfill4(int x.y.h> #include<stdio.y.y.&y).old). getch().int old) { DEPARTMENT OF COMPUTER APPLICATIONS Page 38 .int). closegraph().fill. int x. floodfill4(x.int y. scanf("%d%d".&fill.&x. floodfill1(x."c:\\tc\\bgi").old). initgraph(&gd. floodfill(x.h> #include<conio. setfillstyle(1.&gm. printf("Enter the fill color and the old color"). void floodfill4(int. circle(x. scanf("%d%d". int fill.old).30).

y-1.old). floodfill4(x+1.y.old).fill).y)==old) { putpixel(x.y.y+1.COMPUTER GRAPHICS if(getpixel(x. floodfill4(x-1.fill.old).y.fill. floodfill4(x.old).fill. } } DEPARTMENT OF COMPUTER APPLICATIONS Page 39 . floodfill4(x.fill.

COMPUTER GRAPHICS OUTPUT: Enter the co-ordinates: 300 200 Enter the fill color and the old color DEPARTMENT OF COMPUTER APPLICATIONS Page 40 .

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.y0) to (xk.TRANSILATION AIM: Write a C program to transilate a 2D object.COMPUTER GRAPHICS Program No: 12 Date: 29/8/2013 2D.yk) and move the pixel to line end points(xk. 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. Step 11: Call clear device function Step 12: Stop DEPARTMENT OF COMPUTER APPLICATIONS Page 41 .

mode.y[5]. outtextxy(200. printf("Enter the no: of vertices:").&x[i]. for(i=0. } lineto(x[0].i<n. printf("\n\n\n\n\n\n\n\n\n Enter the translation factors:"). int tx.y[0]). for(i=1. DEPARTMENT OF COMPUTER APPLICATIONS Page 42 .&tx. getch(). scanf("%d".h> #include<stdio. setcolor(1).i<n.x[5].&n). } setcolor(1). scanf("%d%d". moveto(x[0].ty. driver=DETECT.i++) { printf("\n Enter x[%d] y[%d]->".&y[i]).i.i+1.i+1).i++) { lineto(x[i].y[0]).y[i]).&mode.COMPUTER GRAPHICS PROGRAM: #include<graphics.&ty).150. clrscr()."Befor translation"). scanf("%d%d".h> #include<conio.h> void main() { int driver. initgraph(&driver.h> #include<math."c:\\tc\\bgi").n.

} lineto(x[0].i++) { x[i]=x[i]+tx. closegraph().330. moveto(x[0].COMPUTER GRAPHICS for(i=0.i<n. for(i=1. y[i]=y[i]+ty. } DEPARTMENT OF COMPUTER APPLICATIONS Page 43 ."After translation").i<n.y[0]).i++) { lineto(x[i].y[i]). } outtextxy(190. getch().y[0]).

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 Enter the translation factors 50 30 AFTER TRANSLATION DEPARTMENT OF COMPUTER APPLICATIONS Page 44 .

theta Step 4: For i=0 to n read x[i] and y[i] Step 5: Move to (x0. n.COMPUTER GRAPHICS Program No: 13 Date: 4/9/2013 2D.5 and 6 Step 12: Call clear device function Step 13: Stop. 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. y[0]) Step 9: Enter the pivot point Step 10: For r= 0 to theta. y[i]) Step 8: Line to (x[0].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. DEPARTMENT OF COMPUTER APPLICATIONS Page 45 . yr.y0) Step 6: For i=0 to n Step 7: Line to (x[i].

x1[5]. getch().&x[i].&n).150."c:\\tc\\bgi"). scanf("%f". moveto(x[0].x[5]. } lineto(x[0].y1[5].&theta).h> void main() { int driver.mode.y[0]).i++) { lineto(x[i]. setcolor(1). DEPARTMENT OF COMPUTER APPLICATIONS Page 46 . printf("Enter the no: of vertices:"). } setcolor(1).xr.&mode."Befor rotation").h> #include<math. printf("\n\n\n\n\n\n\n\n\n Enter the rotation angle:").i<n.y[i]). outtextxy(180.yr.i+1. scanf("%d%d".y[0]).h> #include<conio. driver=DETECT.i<n. for(i=0. scanf("%d". clrscr().h> #include<stdio.n.i++) { printf("\n Enter x[%d] y[%d]->".COMPUTER GRAPHICS PROGRAM: #include<graphics.y[5]. float theta.&y[i]).i. for(i=1. initgraph(&driver.i+1).

y1[0]).0)/180. moveto(x1[0]. getch(). } lineto(x1[0]. for(i=1. y1[i]=yr+(x[i]-xr)*sin(theta)+(y[i]-yr)*cos(theta). } DEPARTMENT OF COMPUTER APPLICATIONS Page 47 . for(i=0.COMPUTER GRAPHICS xr=getmaxx()/2.i<n.330. closegraph().y1[i]).i++) { x1[i]=xr+(x[i]-xr)*cos(theta)-(y[i]-yr)*sin(theta).i++) { lineto(x1[i].0*theta.i<n.0/7.y1[0])."After rotation"). yr=getmaxx()/2. theta=(22. } outtextxy(190.

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 Enter the rotation angle: 180 AFTER ROTATION DEPARTMENT OF COMPUTER APPLICATIONS Page 48 .

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.COMPUTER GRAPHICS Program No: 14 Date: 8/9/2013 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.yk) and move the pixel to line end points(xk. Step 11: Call clear device function Step 12: Stop DEPARTMENT OF COMPUTER APPLICATIONS Page 49 .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.y0) to (xk.

&x[i].xf.i<n.i. } printf("\nenter the Scaling factors:").y[5].&y[i]).&ch).&sx.h> #include<stdio.i+1)."c:\\tc\\bgi"). clrscr(). for(i=0.Fixed point\n3. do { printf("\n1.i++) { printf("\n enter x[%d] y[%d]->".At orgin\n2. setcolor(1).COMPUTER GRAPHICS PROGRAM: #include<graphics. scanf("%d%d".n.i+1. driver=DETECT.&sy).h> #include<conio.&n).y[0]). moveto(x[0].h> void main() { int driver."before Scaling").exit").&mode. initgraph(&driver.ch. scanf("%d".yf. scanf("%d%d". int sx.150. printf("Enter your choice").x[5].h> #include<math.mode. scanf("%d". DEPARTMENT OF COMPUTER APPLICATIONS Page 50 . if(ch==1) { outtextxy(200.sy. printf("enter the no: of vertices:").

} if(ch==2) { printf("enter Th Fixed Point").y[0]). getch().i<n.i<n.y[i]). scanf("%d%d".i++) { lineto(x[i].i++) { lineto(x[i].COMPUTER GRAPHICS for(i=1.i<n.y[i]).i<n.&xf."before Scaling").y[0]).100. } lineto(x[0].i++) { x[i]=xf+((x[i]-xf)*sx). for(i=1. for(i=0. y[i]=y[i]*sy. setcolor(1).&yf). for(i=0. y[i]=yf+((y[i]-yf)*sy)."After Scaling").i++) { x[i]=x[i]*sx. } lineto(x[0]. moveto(x[0]. } outtextxy(100. getch(). } outtextxy(100. DEPARTMENT OF COMPUTER APPLICATIONS Page 51 .y[0]).80.

y[0]). outtextxy(100. for(i=1.i<n. } DEPARTMENT OF COMPUTER APPLICATIONS Page 52 .y[i]).i++) { lineto(x[i]. getch(). for(i=1. } lineto(x[0].COMPUTER GRAPHICS moveto(x[0].y[0]). closegraph(). } } while(ch!=3). getch().100.y[0]).i<n. getch()."After Scaling"). moveto(x[0].y[i]).y[0]).i++) { lineto(x[i]. } lineto(x[0].

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 Enter the scaling factors 2 3 AFTER SCALING DEPARTMENT OF COMPUTER APPLICATIONS Page 53 .

COMPUTER GRAPHICS Program No: 15 Date: 14/9/2013 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 DEPARTMENT OF COMPUTER APPLICATIONS Page 54 .

370-2*dx-50. rectangle(320.0. initgraph(&gd.370.\n4:Y=-X.150+2*dy). getch().dy.h> void main() { int gd. rectangle(320."REFLECTION ABOUT Y-axis").150).h> #include<graphics.\n3:Y=X.100.50.&gm.250.250)."C:\\tc\\bgi"). } if(ch==2) { outtextxy(50.600. line(0.COMPUTER GRAPHICS PROGRAM: #include<stdio. line(300. dy=250-150.h> #include<conio. scanf("%d". rectangle(320-2*dx-50.gm. do { printf("\n1:X-axis.500).dx.&ch). detectgraph(&gd.&gm).ch. if(ch==1) { outtextxy(50.150). printf("\nEnter the choice of axis about which the object is to reflect:"). dx=320-300.\n5:Exit").50. DEPARTMENT OF COMPUTER APPLICATIONS Page 55 . clrscr().370.300.\n2:Y-axis.100+2*dy. clrscr().100."REFLECTION ABOUT X-axis").

50.370).100+50.390+50. getch().320. rectangle(100.50."REFLECTION ABOUT Y=-X-axis")."REFLECTION ABOUT Y=X-axis"). rectangle(340+50.150. } if(ch==4) { outtextxy(50.150+50).COMPUTER GRAPHICS } if(ch==3) { outtextxy(50. } } while(ch!=5). } DEPARTMENT OF COMPUTER APPLICATIONS Page 56 .

Y = -X 5. X – axis 2. Exit 2 Reflection about Y-axis DEPARTMENT OF COMPUTER APPLICATIONS Page 57 .COMPUTER GRAPHICS OUTPUT: Enter the choice of axis about which the object is to reflect: 1. X = Y 4. X – axis 2. Y – axis 3. Y – axis 3. X = Y 4. Exit 1 Reflection about X-axis Enter the choice of axis about which the object is to reflect: 1. Y = -X 5.

Exit 4 Reflection about Y=-X DEPARTMENT OF COMPUTER APPLICATIONS Page 58 . Y – axis 3. Y – axis 3. X = Y 4.COMPUTER GRAPHICS Enter the choice of axis about which the object is to reflect: 1. Y = -X 5. X = Y 4. X – axis 2. X – axis 3 Reflection about Y=X 2. Y = -X 5. Exit Enter the choice of axis about which the object is to reflect: 1.

COMPUTER GRAPHICS Program No: 16 Date: 25/9/2013 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 DEPARTMENT OF COMPUTER APPLICATIONS Page 59 .

x2.h> #include<process.&y1.y3=200.y1.&y3. scanf("%d". initgraph(&gd. detectgraph(&gd.h> #include<conio.float*.int).sh).x1.x2=200.x3=200.x4=100.y3). x_shear(&x3. printf("enter the shear factor"). if(c==1) { x_shear(&x1. line(x1.y4=200.y2).&gm.sh).y4).y4. printf("\n enter your choice\n (1) x-axis\n (2) y-axis").h> #include<graphics.float*.COMPUTER GRAPHICS PROGRAM: #include<stdio.h> void x_shear(float*.&y4. x_shear(&x4. int c.gd.x4.&y2.&gm).y1=100.int)."c:\\tc\\bgi"). x_shear(&x2. getch().&c).gm. line(x4. } if(c==2) { DEPARTMENT OF COMPUTER APPLICATIONS Page 60 .x3.y3. line(x2. scanf("%d". void y_shear(float*.&sh).y2=100.sh).y2. line(x3.y1). void main() { float x1=100.sh).sh.

} void x_shear(float *x. } DEPARTMENT OF COMPUTER APPLICATIONS Page 61 . } void y_shear(float *x.y1.&y1.&y2. line(x3.50. line(x2. y_shear(&x2.y1)."SHEARING").sh).y2. } outtextxy(50.&y3.sh).y2).&y4. y_shear(&x3.x4.sh).x3.float *y.int sh) { (*x)=(*x)+sh*(*y).x2. getch().y4).sh). line(x4.y3.float*y.y4.x1.COMPUTER GRAPHICS y_shear(&x1. line(x1. y_shear(&x4.int sh) { (*y)=(*y)+sh*(*x).y3).

Y-axis 1 Enter the shear factor 2 Enter your choice 1.axis 2.COMPUTER GRAPHICS OUTPUT: Enter your choice 1. X.axis 2. X. Y-axis 2 Enter the shear factor 1 DEPARTMENT OF COMPUTER APPLICATIONS Page 62 .

Step 7. return else if both outcodes are non zero and their logical and # 0 than reject the line segment exit the loop. Enter the loop.COMPUTER GRAPHICS Program No: 17 Date: 28/9/2013 COHEN-SUTHERLAND LINE CLIPPING ALGORITHM AIM: Write a C program to implement Cohen-Sutherland Line Clipping Algorithm ALGORITHM: Step 1: start Step 2: consider the line segment p1. Stop DEPARTMENT OF COMPUTER APPLICATIONS Page 63 .then find the point of intersection of the line segment with right boundary 4) the lower bit of outcode (p1)! =0.then find the point of intersection of the line segment with low boundary 3) the third most significant bit of outcode (p1)! =0. p2 Step 4: enter into a loop Step 5: loop if both outcode are zero. return else if outcode (p1) =0 then swap (p1. then display line segment exit the loop. p2 Step 3: findout outcode of p1. then find the point of intersection of the line segment with top boundary 2) the second most significant bit of outcode (p1)! =0. step 9. find the outcode Step 8. p2) end-if 1) the most significant bit of outcode (p1)! =0.then find the point of intersection of the line segment with left boundary step 6 replace the points p1 with the intersection point.

rectangle(xmin. line(x0.ymin. printf("\nenter theco-ordinates of the line").int.y1). printf("\nbefore clipping").int.&ymax).gmode.int. closegraph().LEFT=0X8 }.COMPUTER GRAPHICS PROGRAM: #include<stdio.&x0.h> #include<conio.&x1.xmax.&xmax.int).x1.int. void lineclip(int."C:\\TC\\BGI"). enum { TOP=0X1.int).ymax). rectangle(xmin.int.ymin. initgraph(&gd.ymax.xmax.RIGHT=0X4.&gmode. scanf("%d%d%d%d".int.int.y0. int gd=DETECT. initgraph(&gd.y1.int.&ymin.x1.&gmode. getch().int.&xmin.ymin.h> #include<graphics."C:\\TC\\BGI").int.&y0. scanf("%d%d%d%d". printf("\n\n\nafter clipping").x1.y0.xmax. DEPARTMENT OF COMPUTER APPLICATIONS Page 64 .ymin.&y1).xmin.ymax).h> #define TRUE 1 #define FALSE 0 typedef unsigned int outcode.xmax. int compoutecode(int.y1. lineclip(x0.ymax). printf("enter the co-ordinates of the window").y0.x0. void main() { int xmin.BOTTOM=0X2.

outcode0=compoutecode(x0.xmax. int accept=FALSE.y. do { if(!(outcode0|outcode1)) { accept=TRUE.ymax). } else if(outcode0&outcode1) { done=TRUE.y0.xmin.ymin.int y1.int x1.ymin. } else { if(outcode0) { outcodeout=outcode0.int xmax.int ymin.ymax).outcode1. outcode1=compoutecode(x1.done=FALSE. closegraph().int y0. done=TRUE.xmin.x.int ymax) { outcode outcode0.int xmin.xmax. } else { outcodeout=outcode1. } void lineclip(int x0.COMPUTER GRAPHICS getch(). } if(outcodeout&TOP) { DEPARTMENT OF COMPUTER APPLICATIONS Page 65 .outcodeout.y1.

} else if(outcodeout&RIGHT) { y=y0+(y1-y0)*(xmax-x0)/(x1-x0). } else { y=y0+(y1-y0)*(xmin-x0)/(x1-x0).xmax. x=xmin.y0. y0=y.ymax).xmax. outcode1=compoutecode(x1.COMPUTER GRAPHICS x=x0+(x1-x0)*(ymax-y0)/(y1-y0). } else { x1=x.ymin. } } }while(done==FALSE). DEPARTMENT OF COMPUTER APPLICATIONS Page 66 .xmin. y=ymax. y1=y. y=ymin.ymax).ymin. } if(outcodeout==outcode0) { x0=x.y1. x=xmax. outcode0=compoutecode(x0.xmin. } else if(outcodeout&BOTTOM) { x=x0+(x1-x0)*(ymin-y0)/(y1-y0).

} DEPARTMENT OF COMPUTER APPLICATIONS Page 67 .int y. } else if(x<xmin) { code=code|LEFT. } return code. if(y>ymax) { code=code|TOP. } if(x>xmax) { code=code|RIGHT. } else if(y<ymin) { code=code|BOTTOM.int ymin.y1).int xmax.x1.y0.int xmin.COMPUTER GRAPHICS if(accept) { line(x0.int ymax) { outcode code=0. } } int compoutecode(int x.

COMPUTER GRAPHICS OUTPUT: BEFORE CLIPPING AFTER CLIPPING DEPARTMENT OF COMPUTER APPLICATIONS Page 68 .

clip the polygon with left boundary then pass the parameters to the right clip ii. Clip the polygon with top boundary then pass the parameters to the saved clip point procedure Step 5: Do the step 4 for the vertex of the polygon Step 6: Draw the polygon and the window after clipping. Clip the polygon with right boundary then pass the parameters to the bottom clip iii. Clip the polygon with bottom boundary then pass the parameters to the top clip iv.COMPUTER GRAPHICS Program No: 18 Date: 1/10/2013 SUTHERLAND-HODEGEMAN POLYGON CLIPPING AIM: Write a C program to implement Sutherland-Hodegeman polygon clipping ALGORITHM: Step 1: Start Step 2: Read the vertices of the polygon Step 3: Draw the polygon and the window before clipping Step 4: Clip a polygon against each window boundary i. Step 7: Stop DEPARTMENT OF COMPUTER APPLICATIONS Page 69 .

y[i+1]).&ymax).int.BOTTOM=0x2.y[i]. for(i=0. clearviewport(). } printf("\nEnter the point of the clipping rectangle").int). int compoutcode(int.x[i+1].&xmax.int.y[0]). } line(x[i].h> #include<graphics. scanf("%d".y[i].int. void CohenSutherlandLineClipAndDraw(int.int.int.x[0].i.&xmin.ymax.x[10].int.int.i<n. printf("\nEnter the points"). setcolor(8).i++) { scanf("%d%d".COMPUTER GRAPHICS PROGRAM: #include<stdio. initgraph(&gd. scanf("%d%d%d%d". for(i=0.gmode.h> #define FALSE 0 #define TRUE 1 typedef unsigned int outcode.y[10].i<n-1.xmin.int.ymin.&gmode.&y[i]). void main() { int n.int). DEPARTMENT OF COMPUTER APPLICATIONS Page 70 ."c:\\tc\\bgi").int. int gd=DETECT. printf("enter the number of vertices of the polygen").int.&ymin.LEFT=0x8}. enum{TOP=0x1.&x[i].xmax.&n).h> #include<conio.RIGHT=0x4.i++) { line(x[i].

COMPUTER GRAPHICS rectangle(xmin.xmax. rectangle(xmin.xmax.outcodeout.i++) CohenSutherlandLineClipAndDraw(x[i]. int accept=FALSE.xmin. int x.int xmax. } void CohenSutherlandLineClipAndDraw(int x0.int x1.int xmin.y[0]. closegraph(). outcode1=compoutcode(x1.ymax). getch().x[i+i].xmin.ymax).ymax). else { if(outcode0) outcodeout=outcode0. CohenSutherlandLineClipAndDraw(x[i].y.outcode1.i<n-1.ymin.done=FALSE.xmin.xmax.ymax).xmax.y1.int ymin.int y0.xmax.ymin.y[i].ymax).y0.int ymax) { outcode outcode0. done=TRUE.ymax).ymin. outcode0=compoutcode(x0. else DEPARTMENT OF COMPUTER APPLICATIONS Page 71 .int y1.ymin.y[i+1]. getch().ymin.xmin.y[i].x[0].ymin. clearviewport(). for(i=0.xmax. } else if(outcode0 & outcode1) done=TRUE. do { if(!(outcode0|outcode1)) { accept=TRUE.

x=xmin. y=ymax.xmax.ymin. outcode0=compoutcode(x0. y0=y. y1=y.xmin.xmax. DEPARTMENT OF COMPUTER APPLICATIONS Page 72 . } if(outcodeout==outcode0) { x0=x.xmin.COMPUTER GRAPHICS outcodeout=outcode1. x=xmax.y1. } else if(outcodeout & RIGHT) { y=y0+(y1-y0)*(xmax-x0)/(x1-x0). } else { x1=x.ymin. } else { y=y0+(y1-y0)*(xmin-x0)/(x1-x0).y0. outcode1=compoutcode(x1. } else if(outcodeout & BOTTOM) { x=x0+(x1-x0)*(ymin-y0)/(y1-y0). if(outcodeout&TOP) { x=x0+(x1-x0)*(ymax-y0)/(y1-y0).ymax).ymax). y=ymin.

int ymax) { outcode code=0. if(x>xmax) code=code|RIGHT.COMPUTER GRAPHICS } } }while(done==FALSE).int y. else if(x<xmin) code=code|LEFT. if(y>ymax) code=code|TOP.y0. if(accept) line(x0.int xmin.int ymin. else if(y<ymin) code=code|BOTTOM.int xmax.y1).x1. return code. } int compoutcode(int x. } DEPARTMENT OF COMPUTER APPLICATIONS 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 DEPARTMENT OF COMPUTER APPLICATIONS Page 74 .

n Step 4: Get the cityname temperature of n cities Step 5: Draw barchart and piechart Step 6: Stop DEPARTMENT OF COMPUTER APPLICATIONS Page 75 .COMPUTER GRAPHICS Program No: 19 Date: 3/10/2013 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.

scanf("%s". printf("\n1.400). printf("\n-------------"). for(i=0.100).h> #include<dos. int gd. int i.&a[i]).400. for(i=0.h> #include<conio. printf("\nTEMPERATURE :").poId=0. } cleardevice().d=0. line(50.i=i+30) DEPARTMENT OF COMPUTER APPLICATIONS Page 76 . float tot=0.h> void main() { char *pa=0.NO OF CITIES: "). line(50.&n).50.&t[i]).n.i++) { printf("ENTER DETAILS ").&gm. gd=DETECT. scanf("%d".350.h> #include<process.gm. printf("\n----------------").h> #include<graphics. char a[50].i<n.ii.400.j."C:\\TC\\BGI"). printf("ENTER DETAILS ").jj. printf("1. scanf("%d". int pnew[50]={0}.t[50].COMPUTER GRAPHICS PROGRAM: #include<stdio.i<300. initgraph(&gd. NAME OF CITY :").

pieslice(500. tot=tot+t[i]. } for(i=0.j<nn.80+d.400-i. for(j=0. outtextxy(68+d.399.60. putpixel(50+i. d=d+30. if(i==n-1)pnew[i]=360. } DEPARTMENT OF COMPUTER APPLICATIONS Page 77 .100). } getch().COMPUTER GRAPHICS { putpixel(50+i.400-i.240.400-i. for(i=0.5).5). putpixel(50.400-j). pnew[i]=poId+((t[i]*100)/tot)*3.5).5).5).401.i++) { int nn=3*t[i].400. putpixel(51.400-i. putpixel(50.405. putpixel(50+i. setcolor(i+2).pnew[i].i<n.5).i<n.400-j.poId.i+2).5). *pa=a[i]. putpixel(49.i++) { setfillstyle(1.pa). } setcolor(WHITE). poId=pnew[i].j++) line(60+d.

COMPUTER GRAPHICS OUTPUT: Enter details -----------------No: of cities 2 Enter details -----------------1. Name of the city Trivandrum 2. Temperature 40 Enter details -----------------1. Temperature 60 DEPARTMENT OF COMPUTER APPLICATIONS Page 78 . Name of the city Kollam 2.

Step 3: Initialize the graphics using the initgraph() function. Step 6: Stop.b and i. DEPARTMENT OF COMPUTER APPLICATIONS Page 79 . Step 4: Using the circle(). Step 2: Declare an array a[12] with values 1 to 12 and integer variables a.outtext () fuctions to draw a clock shaped figure. Step 5: Perform animation of the clock.COMPUTER GRAPHICS Program No: 20 Date: 5/10/2013 SIMULATION OF THE CLOCK AIM: Write a C program to draw the simulation of the clock ALGORITHM: Step 1: Declare the graphics driver and graphicsmode.

circle(300."2".sec=0. gettime(&t). Page 80 struct time t.200.x3=0.b.gm.200.y3=0."11". outtextxy(a+300.i++) { a=160*cos(arg_hour*i-M_PI_2).RED). initgraph(&gd. line(300."8"."C:\\TC\\BGI")."5".y2+200).y3+200)."9".390.y1=0.h> #include<graphics.min."7".h> #include<conio. setcolor(RED).b+200. } while(!kbhit()) { setcolor(BLACK). int dig_sec. int a. b=160*sin(arg_hour*i-M_PI_2). char Time_Dig[14].200. hour=t.200).x1=0.h> #define arg_sec M_PI/30 #define arg_hour M_PI/6 #define arg_min M_PI/360 void main() { int gd=DETECT."4". floodfill(300.x2=0. char *k[12]={"1".&gm.y1+200).i<13. DEPARTMENT OF COMPUTER APPLICATIONS .h> #include<dos."12"}.x2+300. setfillstyle(1.hour.k[i-1]). for(i=1.ti_hour."6".y2=0."10". line(300."3".200.i. line(300. circle(300.x3+300.COMPUTER GRAPHICS PROGRAM: #include<stdio.RED).x1+300.h> #include<math.200.180).

98.ti_sec. outtextxy(270. setcolor(YELLOW). Time_Dig[0]=hour/10+48. setcolor(YELLOW). setcolor(WHITE).y1+200). y1=150*sin(arg_sec*sec-M_PI_2)*0.6. Time_Dig[8]='\0'.9. closegraph(). Time_Dig[4]=min%10+48. Time_Dig[7]=sec%10+48. } DEPARTMENT OF COMPUTER APPLICATIONS Page 81 . outtextxy(270.y2+200).200.9.200. x1=150*cos(arg_sec*sec-M_PI_2)*0. y3=150*sin(arg_hour*hour-M_PI_2+arg_min*min)*0.250. y2=150*sin(arg_sec*min-M_PI_2)*0. x2=150*cos(arg_sec*min-M_PI_2)*0. Time_Dig[1]=hour%10+48.y3+200).250.200.ti_min. setcolor(CYAN).98. restorecrtmode(). delay(50). line(300. min=t.""). if(hour>12) hour-=12.Time_Dig).x3+300. } getch(). Time_Dig[3]=min/10+48.COMPUTER GRAPHICS sec=t. line(300. Time_Dig[5]=':'.6. Time_Dig[6]=sec/10+48.x1+300. x3=150*cos(arg_hour*hour-M_PI_2+arg_min*min)*0. line(300.x2+300. Time_Dig[2]=':'.

COMPUTER GRAPHICS

OUTPUT:

DEPARTMENT OF COMPUTER APPLICATIONS

Page 82

COMPUTER GRAPHICS

Program No: 21 Date: 9/10/2013

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.

DEPARTMENT OF COMPUTER APPLICATIONS

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

} } void main() { int gd=DETECT. int x.&gmode. closegraph(). drawpoly(9. initgraph(&gd.body).COMPUTER GRAPHICS setcolor(WHITE).human)."c:\\tc\\bgi"). drawpoly(5. setcolor(LIGHTRED). delay(180).gmode. cycle(). getch(). } DEPARTMENT OF COMPUTER APPLICATIONS Page 86 .

COMPUTER GRAPHICS OUTPUT: DEPARTMENT OF COMPUTER APPLICATIONS Page 87 .