computer graphics using c - lab record

87
COMPUTER GRAPHICS DEPARTMENT OF COMPUTER APPLICATIONS Page 1 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

Upload: vortexape

Post on 23-Oct-2015

1.004 views

Category:

Documents


5 download

DESCRIPTION

Computer Graphics using C Lab record. This documents contain basic graphics programs like DDA, Bresenhams etc and many complex animations.

TRANSCRIPT

COMPUTER GRAPHICS

DEPARTMENT OF COMPUTER APPLICATIONS Page 1

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

COMPUTER GRAPHICS

DEPARTMENT OF COMPUTER APPLICATIONS Page 2

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()

{ int i,j,k,l;

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

{

setcolor(RED);

for(j=30;j>0;j--)

circle(340,i,j);

COMPUTER GRAPHICS

DEPARTMENT OF COMPUTER APPLICATIONS Page 3

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

}

COMPUTER GRAPHICS

DEPARTMENT OF COMPUTER APPLICATIONS Page 4

OUTPUT

COMPUTER GRAPHICS

DEPARTMENT OF COMPUTER APPLICATIONS Page 5

Program No: 2

Date: 17/7/2013

ANIMATED ROCKET LAUNCHING

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

COMPUTER GRAPHICS

DEPARTMENT OF COMPUTER APPLICATIONS Page 6

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

}

COMPUTER GRAPHICS

DEPARTMENT OF COMPUTER APPLICATIONS Page 7

OUTPUT:

COMPUTER GRAPHICS

DEPARTMENT OF COMPUTER APPLICATIONS Page 8

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,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.

COMPUTER GRAPHICS

DEPARTMENT OF COMPUTER APPLICATIONS Page 9

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

COMPUTER GRAPHICS

DEPARTMENT OF COMPUTER APPLICATIONS Page 10

y1++;y2++;y3++;

}

setcolor(BLUE);

setfillstyle(1,15);

x1=0;x2=15;

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

{

pieslice(220-2*k,210-37+2*l,x1,x2,30-k/2);

x1=x1+15;

x2=x2+15;

}

}

getch();

}

COMPUTER GRAPHICS

DEPARTMENT OF COMPUTER APPLICATIONS Page 11

OUTPUT:

COMPUTER GRAPHICS

DEPARTMENT OF COMPUTER APPLICATIONS Page 12

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

COMPUTER GRAPHICS

DEPARTMENT OF COMPUTER APPLICATIONS Page 13

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;

COMPUTER GRAPHICS

DEPARTMENT OF COMPUTER APPLICATIONS Page 14

}

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

}}

COMPUTER GRAPHICS

DEPARTMENT OF COMPUTER APPLICATIONS Page 15

OUTPUT:

ELECTRIC FAN

1. SLOW

2. MEDIUM

3. FAST

4. EXIT

Enter the choice:

1

COMPUTER GRAPHICS

DEPARTMENT OF COMPUTER APPLICATIONS Page 16

Program No: 5

Date: 29/7/2013

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

COMPUTER GRAPHICS

DEPARTMENT OF COMPUTER APPLICATIONS Page 17

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

}

COMPUTER GRAPHICS

DEPARTMENT OF COMPUTER APPLICATIONS Page 18

OUTPUT:

COMPUTER GRAPHICS

DEPARTMENT OF COMPUTER APPLICATIONS Page 19

Program No: 6

Date: 4/8/2013

FLIGHT TAKEOFF & LANDING

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.

COMPUTER GRAPHICS

DEPARTMENT OF COMPUTER APPLICATIONS Page 20

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)

COMPUTER GRAPHICS

DEPARTMENT OF COMPUTER APPLICATIONS Page 21

{

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[1] =400-j;

body[2]=200+i; body[3]=400-j;

body[4]=150+i; 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);

}

COMPUTER GRAPHICS

DEPARTMENT OF COMPUTER APPLICATIONS Page 22

OUTPUT:

COMPUTER GRAPHICS

DEPARTMENT OF COMPUTER APPLICATIONS Page 23

Program No: 7

Date: 7/8/2013

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.

COMPUTER GRAPHICS

DEPARTMENT OF COMPUTER APPLICATIONS Page 24

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

}

COMPUTER GRAPHICS

DEPARTMENT OF COMPUTER APPLICATIONS Page 25

OUTPUT:

COMPUTER GRAPHICS

DEPARTMENT OF COMPUTER APPLICATIONS Page 26

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 (Xa,Y

b) and (X

b,Y

b)

Step 2: Set dx=Xa-X

b and dy=Y

b-Y

a

Step 3: X= Xa,

Y=Ya.

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.

COMPUTER GRAPHICS

DEPARTMENT OF COMPUTER APPLICATIONS Page 27

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

{ s=abs(dx); }

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; y=y+yincrement;

putpixel(ROUND(x),ROUND(y),5);

}}

COMPUTER GRAPHICS

DEPARTMENT OF COMPUTER APPLICATIONS Page 28

OUTPUT:

Enter the end points:

100

100

300

250

COMPUTER GRAPHICS

DEPARTMENT OF COMPUTER APPLICATIONS Page 29

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.

Step 2: Declare the variables xa,xb,ya,yb,dx,dy,flag,end,xend,yend,x,y,p,py.

Step 3: Read the starting coordinates as (xa,ya) and ending coordinate as (xb,yb).

Step 4: set dx=xa-xb, dy=ya-yb, p=2*dy-dx, py=2*dx-dy, twody=2*dy, twodx=2*dx and

twodydx=2*(dy-dx),twodxdy=2*(dx-dy)

Step 5: if dx > dy then check

5.1.if xa > xb, then

5.1.1 set x=xb, y=yb and xend=xa;

5.1.2.if ya > yb set flag =1 otherwise set flag=0.

5.2.if xa < xb,then

5.2.1. set x=xa, y=ya and xend=xb;

5.2.2. if yb > ya set flag =1 otherwise set flag=0.

5.3. Repeat following steps until x < xend

5.3.1. Put a pixel in (x,y).

5.3.2. Increment variable x by 1.

5.3.3. if p < 0 set p=p+twody.

5.3.4. Otherwise check

5.3.5. if flag=true, then set y=y+1

5.3.6. otherwise set y=y-1 and calculate p=p+twodydx.

5.4. go to step 7

Step 6 : if dy > dx then check

6.1.if ya < yb, then

6.1.1. set x=xa, y=ya and yend=yb;

6.1.2. if xa < yb set flag =1 otherwise set flag=0.

6.2.if ya > yb,then

6.2.1 set x=xb, y=yb and yend=ya;

6.2.2.if xb < xa set flag =1 otherwise set flag=0.

6.3. Repeat following steps until y < yend

6.3.1. Put a pixel in (x,y).

6.3.2. Increment variable y by 1.

6.3.3. if py < 0 set py=py+twodx.

6.3.4. Otherwise check

6.3.5.if flag=true, then set x=x+1

6.3.6. otherwise set x=x-1 and calculate py=py+twodxdy.

Step 7: Stop.

COMPUTER GRAPHICS

DEPARTMENT OF COMPUTER APPLICATIONS Page 30

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;

COMPUTER GRAPHICS

DEPARTMENT OF COMPUTER APPLICATIONS Page 31

}

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

}

}

COMPUTER GRAPHICS

DEPARTMENT OF COMPUTER APPLICATIONS Page 32

OUTPUT:

COMPUTER GRAPHICS

DEPARTMENT OF COMPUTER APPLICATIONS Page 33

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 (XC,Y

C) and obtain the first point on the circumference of a

circle centered on the origin as (X0,Y

0) = (0,r)

Step 2: Calculate the initial value of the decision parameter as P0 = 5/4 – r

At each XK

position ,starting at k=0, perform the following test:

Step 3: If PK<0, the next pont along the circle centered on (0,0) is

(XK+1

,Y)and PK+1

= PK

+ 2X(K+1)

+1

Otherwise ,the next pont along the circle is

(XK+1,Y

K-1) and P

K+1 = P

K+ 2 X

K+1+1 – 2 Y

K+1

Where 2 XK+1

= 2 XK+2

and 2 YK+1

= 2 YK -2

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

(XCY

C) and plot the co-ordinate values X =X +X

C ,Y = Y+Y

C

Step 6: Repeat steps 3 through 5 until X≥Y.

Step 7: Stop

COMPUTER GRAPHICS

DEPARTMENT OF COMPUTER APPLICATIONS Page 34

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;

COMPUTER GRAPHICS

DEPARTMENT OF COMPUTER APPLICATIONS Page 35

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

}

COMPUTER GRAPHICS

DEPARTMENT OF COMPUTER APPLICATIONS Page 36

OUTPUT:

Enter the center and radius

x: 300

y: 200

r: 50

COMPUTER GRAPHICS

DEPARTMENT OF COMPUTER APPLICATIONS Page 37

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,y,old,fill and the function floddfill()

Step 3:Read the old color and the fill color

3.1) Draw the circle and fill it with the specified old color

3.2) Get the color of the current pixel

3.3) if the current pixel!= fill color then

putpixel(x,y,fillcolor)

call floodfill(x-1,y,fillcolor,oldcolor) recursively

call floodfill(x+1,y,fillcolor,oldcolor) recursively

call floodfill(x,y-1,fillcolor,oldcolor) recursively

call floodfill(x,y+1,fillcolor,oldcolor) recursively

Step 4: Stop

COMPUTER GRAPHICS

DEPARTMENT OF COMPUTER APPLICATIONS Page 38

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)

{

COMPUTER GRAPHICS

DEPARTMENT OF COMPUTER APPLICATIONS Page 39

if(getpixel(x,y)==old)

{

putpixel(x,y,fill);

floodfill4(x,y-1,fill,old);

floodfill4(x-1,y,fill,old);

floodfill4(x,y+1,fill,old);

floodfill4(x+1,y,fill,old);

}

}

COMPUTER GRAPHICS

DEPARTMENT OF COMPUTER APPLICATIONS Page 40

OUTPUT:

Enter the co-ordinates:

300

200

Enter the fill color and the old color

COMPUTER GRAPHICS

DEPARTMENT OF COMPUTER APPLICATIONS Page 41

Program No: 12

Date: 29/8/2013

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

COMPUTER GRAPHICS

DEPARTMENT OF COMPUTER APPLICATIONS Page 42

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

COMPUTER GRAPHICS

DEPARTMENT OF COMPUTER APPLICATIONS Page 43

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

}

COMPUTER GRAPHICS

DEPARTMENT OF COMPUTER APPLICATIONS Page 44

OUTPUT:

Enter the no: of vertices:

3

Enter x[1] y[1] 315 90

Enter x[2] y[2] 210 290

Enter x[3] y[3] 410 290

BEFORE TRANSLATION

Enter the translation factors

50 30

AFTER TRANSLATION

COMPUTER GRAPHICS

DEPARTMENT OF COMPUTER APPLICATIONS Page 45

Program No: 13

Date: 4/9/2013

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.

COMPUTER GRAPHICS

DEPARTMENT OF COMPUTER APPLICATIONS Page 46

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

COMPUTER GRAPHICS

DEPARTMENT OF COMPUTER APPLICATIONS Page 47

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

}

COMPUTER GRAPHICS

DEPARTMENT OF COMPUTER APPLICATIONS Page 48

OUTPUT:

Enter the no: of vertices

3

Enter x[1] y[1] 315 90

Enter x[2] y[2] 290 210

Enter x[3] y[3] 415 290

BEFORE ROTATION

Enter the rotation angle:

180

AFTER ROTATION

COMPUTER GRAPHICS

DEPARTMENT OF COMPUTER APPLICATIONS Page 49

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,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

COMPUTER GRAPHICS

DEPARTMENT OF COMPUTER APPLICATIONS Page 50

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

COMPUTER GRAPHICS

DEPARTMENT OF COMPUTER APPLICATIONS Page 51

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

COMPUTER GRAPHICS

DEPARTMENT OF COMPUTER APPLICATIONS Page 52

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

}

COMPUTER GRAPHICS

DEPARTMENT OF COMPUTER APPLICATIONS Page 53

OUTPUT:

Enter the no: of vertices:

3

Enter x[1] y[1] 315 90

Enter x[2] y[2] 210 290

Enter x[3] y[3] 410 290

BEFORE SCALING

Enter the scaling factors

2 3

AFTER SCALING

COMPUTER GRAPHICS

DEPARTMENT OF COMPUTER APPLICATIONS Page 54

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] y1[i] = -y1[i]

Draw the reflected image

If ch=2 then

Do the reflection along Y-axis

x1[i] = -x1[i] y1[i] = y[i]

Draw the reflected image

If ch=3 then

Do the reflection along line Y = X

x1[i] = x[i] y1[i] = -y[i]

Draw the reflected image

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

COMPUTER GRAPHICS

DEPARTMENT OF COMPUTER APPLICATIONS Page 55

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

COMPUTER GRAPHICS

DEPARTMENT OF COMPUTER APPLICATIONS Page 56

}

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

}

COMPUTER GRAPHICS

DEPARTMENT OF COMPUTER APPLICATIONS Page 57

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

1

Reflection about X-axis

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

2

Reflection about Y-axis

COMPUTER GRAPHICS

DEPARTMENT OF COMPUTER APPLICATIONS Page 58

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

3

Reflection about Y=X

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

4

Reflection about Y=-X

COMPUTER GRAPHICS

DEPARTMENT OF COMPUTER APPLICATIONS Page 59

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

COMPUTER GRAPHICS

DEPARTMENT OF COMPUTER APPLICATIONS Page 60

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)

{

COMPUTER GRAPHICS

DEPARTMENT OF COMPUTER APPLICATIONS Page 61

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

}

COMPUTER GRAPHICS

DEPARTMENT OF COMPUTER APPLICATIONS Page 62

OUTPUT:

Enter your choice

1. X- axis

2. Y-axis

1

Enter the shear factor

2

Enter your choice

1. X- axis

2. Y-axis

2

Enter the shear factor

1

COMPUTER GRAPHICS

DEPARTMENT OF COMPUTER APPLICATIONS Page 63

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, p2

Step 3: findout outcode of p1, p2

Step 4: enter into a loop

Step 5: loop

if both outcode are zero, then display line segment

exit the loop, return

else if both outcodes are non zero and their logical and # 0

than reject the line segment

exit the loop, return

else if outcode (p1) =0 then swap (p1, p2)

end-if

1) the most significant bit of outcode (p1)! =0, then find the point of intersection of the

line segment with top boundary

2) the second most significant 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,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 left boundary

step 6 replace the points p1 with the intersection point.

Step 7. find the outcode

Step 8. Enter the loop.

step 9. Stop

COMPUTER GRAPHICS

DEPARTMENT OF COMPUTER APPLICATIONS Page 64

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

COMPUTER GRAPHICS

DEPARTMENT OF COMPUTER APPLICATIONS Page 65

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)

{

COMPUTER GRAPHICS

DEPARTMENT OF COMPUTER APPLICATIONS Page 66

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

COMPUTER GRAPHICS

DEPARTMENT OF COMPUTER APPLICATIONS Page 67

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;

}

COMPUTER GRAPHICS

DEPARTMENT OF COMPUTER APPLICATIONS Page 68

OUTPUT:

BEFORE CLIPPING

AFTER CLIPPING

COMPUTER GRAPHICS

DEPARTMENT OF COMPUTER APPLICATIONS Page 69

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. clip the polygon with left boundary then pass the parameters to the right clip

ii. 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. 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.

Step 7: Stop

COMPUTER GRAPHICS

DEPARTMENT OF COMPUTER APPLICATIONS Page 70

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

COMPUTER GRAPHICS

DEPARTMENT OF COMPUTER APPLICATIONS Page 71

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

COMPUTER GRAPHICS

DEPARTMENT OF COMPUTER APPLICATIONS Page 72

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

COMPUTER GRAPHICS

DEPARTMENT OF COMPUTER APPLICATIONS Page 73

}

}

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

}

COMPUTER GRAPHICS

DEPARTMENT OF COMPUTER APPLICATIONS Page 74

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

COMPUTER GRAPHICS

DEPARTMENT OF COMPUTER APPLICATIONS Page 75

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,n

Step 4: Get the cityname temperature of n cities

Step 5: Draw barchart and piechart

Step 6: Stop

COMPUTER GRAPHICS

DEPARTMENT OF COMPUTER APPLICATIONS Page 76

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)

COMPUTER GRAPHICS

DEPARTMENT OF COMPUTER APPLICATIONS Page 77

{

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

}

COMPUTER GRAPHICS

DEPARTMENT OF COMPUTER APPLICATIONS Page 78

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

COMPUTER GRAPHICS

DEPARTMENT OF COMPUTER APPLICATIONS Page 79

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.

Step 2: Declare an array a[12] with values 1 to 12 and integer variables a,b and i.

Step 3: Initialize the graphics using the initgraph() function.

Step 4: Using the circle(),outtext () fuctions to draw a clock shaped figure.

Step 5: Perform animation of the clock.

Step 6: Stop.

COMPUTER GRAPHICS

DEPARTMENT OF COMPUTER APPLICATIONS Page 80

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]; struct time t;

initgraph(&gd,&gm,"C:\\TC\\BGI");

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;

COMPUTER GRAPHICS

DEPARTMENT OF COMPUTER APPLICATIONS Page 81

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

}

COMPUTER GRAPHICS

DEPARTMENT OF COMPUTER APPLICATIONS Page 82

OUTPUT:

COMPUTER GRAPHICS

DEPARTMENT OF COMPUTER APPLICATIONS Page 83

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.

COMPUTER GRAPHICS

DEPARTMENT OF COMPUTER APPLICATIONS Page 84

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[1]=260;

body[2]=490-i; body[3]=190;

body[4]=520-i; body[5]=250;

body[6]=570-i; body[7]=200;

body[8]=490-i; body[9]=190;

human[0]=565-i; human[1]=190;

human[2]=575-i; human[3]=190;

human[4]=570-i; human[5]=150;

human[6]=566-i; human[7]=150;

human[8]=566-i; human[9]=147;

human[10]=563-i; human[11]=147;

human[12]=563-i; human[13]=150;

human[14]=560-i; human[15]=150;

human[16]=565-i; human[17]=190;

setcolor(WHITE);

circle(450-i,260,20); //front wheel

circle(450-i,260,3); // front wheel

circle(600-i,260,20);//back wheel

COMPUTER GRAPHICS

DEPARTMENT OF COMPUTER APPLICATIONS Page 85

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

COMPUTER GRAPHICS

DEPARTMENT OF COMPUTER APPLICATIONS Page 86

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

}

COMPUTER GRAPHICS

DEPARTMENT OF COMPUTER APPLICATIONS Page 87

OUTPUT: