Cgs Record

Download as pdf or txt
Download as pdf or txt
You are on page 1of 68

Computer Graphics

CONTENTS
PATTERNS...1
BAR CHART ............................................................................................................................ 7
PIE CHART ........................................................................................................................... 12
DDA ......................................................................................................................................... 17
BRESENHAMS LINE ......................................................................................................... 20
BRESENHAMS CIRCLE.................................................................................................... 23
TRANSFORMATION ........................................................................................................... 26
LINE CLIPPING ................................................................................................................... 32
BOUNDARY FILL AND FLOOD FILL ............................................................................. 39
TABLE FAN ........................................................................................................................... 47
BOUNCING BALL ................................................................................................................ 52
NATIONAL FLAG ................................................................................................................ 57
LANDING OF AEROPLANE ON A SHIP ......................................................................... 62
BUCKET FILLING.......66

Department of Computer Applications

Page 1

Computer Graphics

PATTERNS
AIM
Write a program to draw
1.
2.
3.
4.

LINE
CIRCLE
RECTANGLE
TRIANGLE

ALGORITHM
Step 01: start
Step 02: declare integer variables x1, x2,x3,x4,sangle,eangle,xr,yr, r
Step 03: initialize graphics mode and graphics driver
Step 04: enter the choice
Step 05: in case 1, call line function with variables x1,x2,x3,x4
Step 06: in case 2, call circle function with variables x1,x2,r
Step 07: in case 3,call line function 4 times with variables x1,x2,x3,x4,x5,x6,x7,x8
Step 08: in case 4,call line function 3 times with variables x1,x2,x3,x4,x5,x6
Step 09: stop.

PROGRAM
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{

int x1,x2,x3,x4,x5,x6,x7,x8,r,xr,yr,sangle,eangle,c;
int gm,gd=DETECT;
initgraph(&gd,&gm,"C:\\TC\\BGI");
do
{
printf("\n 1.line\n");
printf(" 2.circle\n");
printf(" 3.rectangle\n");
printf(" 4.triangle\n");

Department of Computer Applications

Page 2

Computer Graphics
printf(" enter your choice\n");
scanf("%d",&c);
switch(c)
{
case 1: printf("\n enter the values\n");
scanf("%d%d%d%d",&x1,&x2,&x3,&x4);
line(x1,x2,x3,x4);
break;
case 2: printf("\n enter the values\n");
scanf("%d%d%d",&x1,&x2,r);
circle(x1,x2,r);
break;
case 3: printf("\n enter the values\n");
scanf("%d%d%d%d%d%d%d%d",&x1,&x2,&x3,&x4,&x5,&x6,&x7,&x8);
line(x1,x2,x3,x4);
line(x3,x4,x5,x6);
line(x5,x6,x7,x8);
line(x7,x8,x1,x2);
break;
case 4: printf("\n enter the values\n");
scanf("%d%d%d%d%d%d",&x1,&x2,&x3,&x4,&x5,&x6);
line(x1,x2,x3,x4);
line(x3,x4,x5,x6);
line(x5,x6,x1,x2);
break;
default:printf("invalid choice");
break;
Department of Computer Applications

Page 3

Computer Graphics
}
}while(c<4);
getch();
closegraph();
}

OUTPUT
1.LINE

2.CIRCLE

Department of Computer Applications

Page 4

Computer Graphics

3.RECTANGLE

4.TRIANGLE

Department of Computer Applications

Page 5

Computer Graphics

Department of Computer Applications

Page 6

Computer Graphics

BAR CHART
AIM
Write a program to draw a bar chart corresponding to the sales and expense for 12 months in
an Indian firm.

ALGORITHM
Algorithm for main
Define a structure gr with float variables sal, exp, strx, endx, stry, endy and an array arr
Step 01: start
Step 02: declare integer variable x:=0
Step 03: declare float variables tot1, tot2, temp1, j, xb, yb, xb1, yb1, t, flg
Step 04: initialize tot1, tot2, temp1, flg to zero
Step 05: declare character variables str, tp, ptr, c
Step 06: initialize *c with names of 12 months
Step 07: i:=0
Step 08: repeat steps 09 to 12 until i=12
Step 09: read sales and expense, arr[i].sal, arr[i]..exp
Step 10: tot1:=tot1+arr[i].sal
Step 11: tot2:=tot2+arr[i].exp
Step 12: increment i
Step 13: initialize graphics mode and graphics driver
Step 14: set background color with white
Step 15: i:=0
Step 16: repeat steps17 to 20 until i=12
Step 17: temp1:=arr[i].sal
Step 18: flg:=(float)temp1/tot1
Step 19: arr[i].endx:=flg*400
Step 20: increment i
Step 21: temp1:=0
Step 22: flg:=0
Step 23: i:=0
Step 24: repeat steps 25 to 28 until i=12
Step 25: temp1:= arr[i].exp
Step 26: flg:=(float)temp1/tot2
Step 27: arr[i].endy:=flg*100
Step 28: increment i
Step 29: xb:=50
Step 30: xb1:=60
Step 31: draw line with parameters (40, 0, 40, 200)
Step 32: draw line with parameters (40, 200, 520, 200)
Step 33: set text style with parameters (2, 0, 4)
Step 34: x:=0
Step 35: i:=0
Step 36: repeat steps 37 to 40 until i>100
Department of Computer Applications

Page 7

Computer Graphics
Step 37: itoa (1,ptr,10)
Step 38: display text with parameters (15, (200-x), ptr)
Step 39: x:=x+40
Step 40: i:=i+10
Step 41: x:=50
Step 42: i:=0
Step 43: repeat steps 44 to 46 until i=12
Step 44: display text with parameters ((int) x, 220, c[i])
Step 45: x:=x+40
Step 46: increment i
Step 47: i:=0
Step 48: repeat steps 49 to 57 until i=12
Step 49: yb:=200-arr[i].endx
Step 50: yb1:=200-arr[i].endy
Step 51: setfillstyle(1,2)
Step 52: draw bar with parameters (xb,yb,xb+10,200)
Step 53: setfillstyle(1,4)
Step 54: draw bar with parameters (xb1,yb1,xb1+10,200)
Step 55: xb1:=xb1+40
Step 56: xb:=xb+40
Step 57: increment i
Step 58: draw rectangle with parameters (540, 10, 610, 100)
Step 59: display text with parameters (550, 20, sales)
Step 60: display text with parameters (550,60, expense)
Step 61: stop

PROGRAM
#include<conio.h>
#include<graphics.h>
#include<stdio.h>
#include<stdlib.h>
struct gr
{
float sal;
float exp;
float strx;
float endx;
float stry;
float endy;
}arr[12];
void main()
{
int gd=DETECT,gm,i,x=0;
float tot1=0,tot2=0,temp1=0,j,xb,yb,xb1,yb1,t;
float flg=0;
char str[20],tp[10],*c[20]={"jan","feb","mar","apr","may","jun",
"jul","aug","sep","oct","nov","dec"};
Department of Computer Applications

Page 8

Computer Graphics
char ptr[20];
clrscr();
for(i=0;i<12;i++)
{
printf("Enter sales and expense for %s\n",*(c+i));
scanf("%d %d",&arr[i].sal,&arr[i].exp);
tot1 = tot1 + arr[i].sal;
tot2 = tot2 + arr[i].exp;
}
clrscr();
initgraph(&gd,&gm,"c:\\tc\\bgi");
setbkcolor(WHITE);
setcolor(1);
for(i=0;i<12;i++)
{
temp1= arr[i].sal;
flg = (float ) temp1 / tot1;
arr[i].endx = flg * 400;
}
temp1=0;
flg=0;
for(i=0;i<12;i++)
{
temp1= arr[i].exp;
flg = (float ) temp1 / tot2;
arr[i].endy = flg * 100;
}
xb=50;
xb1=60;
line(40,0,40,200);
line(40,200,520,200);
settextstyle(2,0,4);
x=0;
for(i=0;i<=100;i=i+10)
{
itoa(i,ptr,10);
outtextxy(15,(200-x),ptr);
x=x+20;
}
x=50;
for(i=0;i<12;i++)
{
outtextxy((int)x,220,c[i]);
Department of Computer Applications

Page 9

Computer Graphics
x = x+40;
}
for(i=0;i<12;i++)
{
yb=200-arr[i].endx;
yb1=200-arr[i].endy;
setfillstyle(1,2);
bar(xb,yb,xb+10,200);
setfillstyle(1,4);
bar(xb1,yb1,xb1+10,200);
xb1=xb1+40;
xb=xb+40;
}
rectangle(540,10,610,100);
setcolor(2);
outtextxy(550,20,"Sales");
setcolor(4);
outtextxy(550,60,"Expesnse");
getch();
}

OUTPUT
Enter sales and expense for jan
8000
6000
Enter sales and expense for feb
4000
9000
Enter sales and expense for mar
4675
6879
Enter sales and expense for apr
2340
7456
Enter sales and expense for may
9870
7799
Enter sales and expense for jun
4999
6000
Enter sales and expense for jul
8500
7600
Enter sales and expense for aug
4500
3700
Enter sales and expense for sep
9700
Department of Computer Applications

Page 10

Computer Graphics
4689
Enter sales and expense for oct
5900
7000
Enter sales and expense for nov
8000
6000
Enter sales and expense for dec
9870
5555

Department of Computer Applications

Page 11

Computer Graphics
PIE CHART
AIM
Write a program to draw a pie chart corresponding to the sales and expenses for 12 months in
an Indian firm.

ALGORITHM
Algorithm for main
Step 01: start
Step 02: declare float array sales, exp
Step 03: declare float variable x and initialize x:=150.00
Step 04: declare character array and initialize with the name of 12 months
Step 05: initialize graphics mode and graphics driver
Step 06: i:=0
Step 07: repeat steps 08 to 10 until i=12
Step 08: read sales, sales[i]
Step 09: read expense, exp[i]
Step 10: increment i
Step 11: set background color to white
Step 12: display the text sales
Step 13: draw pie with parameters (sales, b, x)
Step 14: display the text expense
Step 15: draw pie with parameters (exp, b,x)
Step 16: close graph
Step 17: stop
Function piedraw
Step 01: start
Step 02: declare float variables sum, spnt, stangle, eangle, langle, y
Step 03: intialize y:=200.00, sum:=0, stangle:=0
Step 04: declare integer variables i, p, c
Step 05: p:=1
Step 06: c:=2
Step 07: i:=0
Step 08: repeat step 09 until i=12
Step 09: sum+:=a[i]
Step 10: i:=0
Step 11: repeat steps 12 to 27 while i<12
Step 12: if p=11 then p=6
Step 13: set fillstyle (p,c)
Step 14: spnt:=(a[i]/sum)*100
Step 15: angle:=spnt*3.6
Step 16: eangle:=stangle+angle
Step 17: langle:=((stangle+eangle)/2)*3.14/180
Step 18: draw pie slice with parameters (x, y, stangle, eangle, 100)
Department of Computer Applications

Page 12

Computer Graphics
Step 19: if (stangle+eangle/2)>90 and (stangle+eangle/2)<270 then goto step 20 else goto
step 23
Step 20: if (stangle+eangle/2)>180 then goto step 21 else goto step 22
Step 21: display (x+120*cos(langle)), (y-110*sin(langle)), b[i]
Step 22: display (x+120*cos(langle)-20), (y-110*sin(langle)), b[i]
Step 23: display (x+120*cos(langle)), (y-110*sin(langle)), b[i]
Step 24: stangle:=eangle
Step 25: increment i
Step 26: increment p
Step 27: increment c
Step 28: stop

PROGRAM:
#include<stdio.h>
#include<graphics.h>
#include<math.h>
void piedraw(float [15],char [12][4],float);
void main()
{
float sales[15],exp[15],i;
float x=150.00;
char b[12][4]={"jan","feb","mar","apr","may","jun",
"jul","aug","sep","oct","nov","dec"};
int gd=DETECT,gm;
initgraph(&gd,&gm,"..\\bgi");
printf("\nEnter sales for 12 months \n");
for (i=0;i<12;i++)
{
printf("\nEnter sales for %s :", b[i] );
scanf("%f",&sales[i]);
printf("\nEnter expense for %s : ", b[i] );
scanf("%f",&exp[i]);
}
setbkcolor(WHITE);
cleardevice();
setcolor(BLUE);
settextstyle(3,0,3);
outtextxy(100,50,"SALES");
piedraw(sales,b,x);
settextstyle(3,0,3);
setcolor(RED);
outtextxy(480,65,"EXPENSE");
x=475.00;
piedraw(exp,b,x);
getch();
closegraph();
Department of Computer Applications

Page 13

Computer Graphics
}
void piedraw(float a[15],char b[12][4],float x)
{
float sum=0,spnt,stangle=0,eangle,angle,langle,y=200.00;
int i,p=1,c=2;
for(i=0;i<12;i++)
{
sum+=a[i];
}
i=0;
while (i<12)
{
if(p==11)
{
p=6;
}
setfillstyle(p,c);
spnt=(a[i]/sum)*100;
angle=spnt*3.6;
eangle=stangle+angle;
langle=((stangle+eangle)/2)*3.14/180;
pieslice((int)x,(int)y,stangle,eangle,100);
settextstyle(2,0,0);
settextjustify(0,0);
if((stangle+eangle/2)>90&& (stangle+eangle/2) <270)
{
if((stangle+eangle/2)>180)
outtextxy( (int)x+120*cos(langle),(int)y-110*sin(langle),b[i]);
else
outtextxy( (int)x+120*cos(langle)-20,(int)y-110*sin(langle),b[i]);
}
else
{
outtextxy( (int)x+120*cos(langle),(int)y-110*sin(langle),b[i]);
}
stangle=eangle;
i++;p++;c++;
}
}

Department of Computer Applications

Page 14

Computer Graphics
OUTPUT
Enter sales for 12 months
Enter sales for jan
: 8000
Enter expense for jan : 6000
Enter sales for feb
: 4000
Enter expense for feb : 9000
Enter sales for mar : 4675
Enter expense for mar: 6879
Enter sales for apr
: 2340
Enter expense for apr : 7456
Enter sales for may : 9870
Enter expense for may: 7799
Enter sales for jun
: 4999
Enter expense for jun : 6000
Enter sales for jul
: 8500
Enter expense for jul : 7600
Enter sales for aug
: 4500
Enter expense for aug : 3700
Enter sales for sep
: 9700
Enter expense for sep : 4689
Enter sales for oct
: 5900
Enter expense for oct : 7000
Enter sales for nov : 8000
Enter expense for nov : 6000
Enter sales for dec
: 9870
Enter expense for dec : 5555

Department of Computer Applications

Page 15

Computer Graphics

Department of Computer Applications

Page 16

Computer Graphics
DDA
AIM
Write a program to implement DDA line drawing algorithm

ALGORITHM
Algorithm for main
Step 01: start
Step 02: declare integer variables x1, x2, y1, y2
Step 03: initialize graphics driver and graphics mode
Step 04: read the starting coordinates of line, x1, y1
Step 05: read the end coordinates of line, x2, y2
Step 06: call function ddaline() with parameters x1, y1, x2, y2
Step 07:stop
Function ddaline
Step 01: start
Step 02: declare integer variables dx, dy, f, xin,yin
Step 03: decalre float variable m
Step 04: dx:=x2-x1
Step 05: dy:=y2-y1
Step 06: m=(float)dy/dx
Step 07: if abs(m)<=1 then f=dx
Else f=dy
Step 08: xin=dx/(float)f
Step 09: yin=dy/(float)f
Step 10: x=x1
Step 11: y=y1
Step 12: point to plot is (x, y, 1)
Step 13: initialize i=1
Step 14: repeat steps 14 to 17 until i=f
Step 15: x+:= xin
Step 16: y+:= yin
Step 17: point to plot is (x, y, 8)
Step 18: increment i
Step 19: stop

Department of Computer Applications

Page 17

Computer Graphics
PROGRAM
#include<graphics.h>
#include<conio.h>
#include<math.h>
#include<stdio.h>
void ddaline(int,int,int,int);
void main()
{
int gd=DETECT,gm=DETECT;
int x1,y1,x2,y2;
initgraph(&gd,&gm,"d:\\tc\\bgi");
printf("Enter the end coordinates\n");
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
ddaline(x1,y1,x2,y2);
getch();
}
void ddaline(int x1,int y1,int x2,int y2)
{
int dy,dx,f,i,x,y,xin,yin;
float m;
dy=y2-y1;
dx=x2-x1;
m=(float)dy/dx;
if(abs(m)<=1)
f=dx;
else
f=dy;
xin=dx/(float)f;
yin=dy/(float)f;
x=x1;
y=y1;
putpixel(x,y,1);
for(i=1;i<=f;i++)
{
x=x+xin;
y=y+yin;
putpixel(x,y,8);
}
}

Department of Computer Applications

Page 18

Computer Graphics
OUTPUT

Department of Computer Applications

Page 19

Computer Graphics
BRESENHAMS LINE
AIM
Write a program to implement Bresenhams line drawing algorithm

ALGORITHM
Algorithm for main
Step 01: start
Step 02: initialize graphics driver and graphics mode
Step 03: declare integer variables x1, x2, y1, y2
Step 04: read the start coordinates of line, x1, y1
Step 05: read the end coordinates of line, x2, y2
Step 06: call function Bresenham with parameters x1, y1, x2, y2
Step 07: stop
Function bresenham
Step 01: start
Step 02: declare integer variables p, dx, k, dy
Step 03: declare a float variable m, slope
Step 04: m:=(y2-y1)/(x2-x1)
Step 05: dx:=x2-x1
Step 06: dy:=y2-y1
Step 07: if slope is less than 1 then goto step 08 else goto step 18
Step 08: p:=2*dy-dx
Step 09: k:=0
Step 10: repeat steps 11 to 17 until k=dx
Step 11: if p<0 then goto step 12 else goto step 15
Step 12: point to plot is (x1++,y1,2)
Step 13: p+:=2*dy;
Step 14: goto step 17
Step 15: point to plot is (x1++,y1++,2)
Step 16: p+:=2*dy-2*dx
Step 17: increment k
Step 18: if slope is greater than 1 goto step 19
Step 19: p:=2*dx-dy
Step 20: k:=0
Step 21: repeat steps 22 to 28 until k=dy
Step 22: if p<0 then goto step 23 else goto step 26
Step 23: point to plot is (x1,y1++,2)
Step 24: p+:=2*dx;
Step 25: goto step 17
Step 26: point to plot is (x1++,y1++,2)
Step 27: p+:=2*dx-2*dy
Step 28: increment k
Step 30: stop
Department of Computer Applications

Page 20

Computer Graphics
PROGRAM
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void bresenham(int x1,int y1,int x2,int y2)
{
float m;
int p,dx=x2-x1,k,dy=y2-y1;
m=(y2-y1)/(x2-x1);
if(m<1)
{
p=2*dy-dx;
for(k=0;k<dx;k++)
{
if(p<0)
{
putpixel(x1++,y1,2);
p+=2*dy;
}
else
{
putpixel(x1++,y1++,2);
p+=2*dy-2*dx;
}
}
}
else
{
p=2*dx-dy;
for(k=0;k<dy;k++)
{
if(p<0)
{
putpixel(x1,y1++,2);
p+=2*dx;
}
else
{
putpixel(x1++,y1++,2);
p+=2*dx-2*dy;
}
}
}
}
void main()
{
Department of Computer Applications

Page 21

Computer Graphics
int gd=DETECT,gm;
int x1,y1,x2,y2;
printf("\nEnter the start coordinates of line:\n");
scanf("%d%d",&x1,&y1);
printf("\nEnter the end coordinates of line:\n");
scanf("%d%d",&x2,&y2);
initgraph(&gd,&gm,"c:\\tc\\bgi");
bresenham(x1,y1,x2,y2);
getch();
}

OUTPUT
Enter the end coordinates of line:
100 100 400 400

Department of Computer Applications

Page 22

Computer Graphics
BRESENHAMS CIRCLE
AIM
Write a program to implement Bresenhams circle algorithm

ALGORITHM
Algorithm for main
Step 01: start
Step 02: declare integer variables x, y, r
Step 03: initialize graphics mode and graphics driver
Step 04: read the center coordinates of circle, x, y
Step 05: read the radius, r
Step 06: call function brescircle() with parameters (x, y, r)
Step 07: stop
Function brescircle(xc,yc,r)
Step 01: start
Step 02: declare integer variables x, y, p
Step 03: x:=0
Step 04: y:=r
Step 05: p:=(5/4)-r
Step 06: repeat steps 07 to 11 while x<=y
Step 07: increment x
Step 08: if p<0 then goto step 09 else goto step 10
Step 09: p+:=2*x+1
Step 10: decrement y
Step 11: p+:=2*(x-y)+1
Step 12: call function plotpoint() with parameters (xc, yc, x, y)
Step 13: stop
Function plotpoint(xc, yc, x, y)
Step 01: start
Step 02: point to plot is (xc+x, yc+y,3)
Step 03: point to plot is (xc-x, yc+y,3)
Step 04: point to plot is (xc+x, yc-y,3)
Step 05: point to plot is (xc-x, yc-y,3)
Step 06: point to plot is (xc+y, yc+x,3)
Step 07: point to plot is (xc-y, yc+x,3)
Step 08: point to plot is(xc+y, yc-x,3)
Step 09: point to plot is (xc-y, yc-x,3)
Step 10: stop

Department of Computer Applications

Page 23

Computer Graphics
PROGRAM
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void brescircle(int,int,int);
void plotpoint(int,int,int,int);
void main()
{
int x,y,r;
int gd=DETECT,gm,gerror;
clrscr();
initgraph(&gd,&gm,"d:\\tc\\bgi");
printf("Enter the center coordinates: ");
scanf("%d%d",&x,&y);
printf("Enter the radius: ");
scanf("%d",&r);
brescircle(x,y,r);
getch();
}
void brescircle(int xc,int yc,int r)
{
int x,y,p;
x=0;y=r;
p=(5/4)-r;
while(x<=y)
{
x++;
if(p<0)
p=p+(2*x)+1;
else
{
y--;
p=p+(2*x)-(2*y)+1;
}
plotpoint(xc,yc,x,y);
}
}
void plotpoint(int xc,int yc,int x,int y)
{
putpixel(xc+x,yc+y,WHITE);
putpixel(xc-x,yc+y,WHITE);
putpixel(xc+x,yc-y,WHITE);
putpixel(xc-x,yc-y,WHITE);
putpixel(xc+y,yc+x,WHITE);
putpixel(xc-y,yc+x,WHITE);
putpixel(xc+y,yc-x,WHITE);
putpixel(xc-y,yc-x,WHITE);
}
Department of Computer Applications

Page 24

Computer Graphics
OUTPUT

Department of Computer Applications

Page 25

Computer Graphics
TRANSFORMATION
AIM
Write a program to implement the basic transformations

ALGORITHM
Algorithm for main
Step 01: start
Step 02: declare an integer variable ch
Step 03: declare graphics drivers and modules
Step 04: repeat steps 05 to until ch==4
Step 05: clear screen
Step 06: read the choice, ch
Step 07: if ch=1 then call function translation() and goto step
Step 08: if ch=2 then call function rotation() and goto step
Step 09: if ch=3 then call function scaling() and goto step
Step 10: if ch=4 then return
Step 11: stop
Function translation()
Step 01: start
Step 02: declare integer variables tx, ty, x1, y1, x2, y2, x3, y3
Step 03: read values for coordinates of triangle x1, y1, x2, y2, x3, y3
Step 04: read the translation vectors, tx, ty
Step 05: plot line with parameters x1, y1, x2, y2
Step 06: plot line with parameters x1, y1, x3, y3
Step 07: plot line with parameters x3, y3, x2, y2
Step 08: plot line with parameters x1+tx, y1+ty, x2+tx, y2+ty
Step 09: plot line with parameters x1+tx, y1+ty, x3+tx, y3+ty
Step 10: plot line with parameters x3+tx, y3+ty, x2+tx, y2+ty
Step 11: stop
Function scaling()
Step 01: start
Step 02: declare integer variables sx, sy, x1, y1, x2, y2, x3, y3
Step 03: read values of the end points of triangle x1, y1, x2, y2, x3, y3
Step 04: read the scaling factors, sx, sy
Step 05: plot line with parameters x1, y1, x2, y2
Step 06: plot line with parameters x1, y1, x3, y3
Step 07: plot line with parameters x3, y3, x2, y2
Step 08: x2=x2+(x2-x1)*sx, y2=y2+(y2-y1)*sy
Step 09: x3=x3+(x3-x1)*sx, y3=y3+(y3-y1)*sy
Step 10: plot line with parameters x1, y1, x2, y2
Step 11: plot line with parameters x1, y1, x3, y3
Department of Computer Applications

Page 26

Computer Graphics
Step 12: plot line with parameters x3, y3, x2, y2
Step 13: stop
Function rotation()
Step 01: start
Step 02: declare integer variables x1,y1,x2,y2,xn,yn
Step 03: declare double variables r11,r12,th
Step 04: read the line end coordinates x1, y1, x2, y2
Step 05: read the angle to rotate, th
Step 06: th=(th*3.14)/180
Step 07: plot line with parameters x1, y1, x2, y2
Step 08: r11=cos(th*3.14/180);
Step 09: r12=sin(th*3.14/180);
Step 10:xn=(x2*r11)-(y2*r12);
Step 11: yn=(x2*r12)+(y2*r11);
Step 12: plot line with parameters x1, y1, xn, yn
Step 13: stop

PROGRAM
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void translation();
void rotation();
void scaling();
void main()
{
int ch;
int gd=DETECT,gm;
do
{
clrscr();
printf("Enter your choice\n1=translation\n2=rotation\n3=scaling\n4=exit\n");
scanf("%d",&ch);
initgraph(&gd,&gm,"e:\\tc\\bgi");
switch(ch)
{
case 1: translation();
break;
case 2: rotation();
break;
case 3: scaling();
break;
case 4: exit(0);
getch();
default: printf("wrong choice\n");
break;
Department of Computer Applications

Page 27

Computer Graphics
}
}while(ch!=4);
}
void translation()
{
int x1,x2,y1,y2,tx,ty,x3,y3;
printf("Enter the 3 end coordinates of triangle\n");

scanf("%d%d%d%d%d%d",&x1,&y1,&x2,&y2,&x3,&y3);
printf("Enter the translation vector values\n");
scanf("%d%d",&tx,&ty);
line(x1,y1,x2,y2);
line(x1,y1,x3,y3);
line(x2,y2,x3,y3);
line(x1+tx,y1+ty,x2+tx,y2+ty);
line(x1+tx,y1+ty,x3+tx,y3+ty);
line(x2+tx,y2+ty,x3+tx,y3+ty);
getch();
}
void rotation()
{
int x2,y2,x1,y1,xn,yn;
double r11,r12,th;
printf("Enter the line end coordinates\n");
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
printf("Enter the rotation angle\n");
scanf("%lf",&th);
line(x1,y1,x2,y2);
r11=cos(th*3.14/180);
r12=sin(th*3.14/180);
xn=(x2*r11)-(y2*r12);
yn=(x2*r12)+(y2*r11);
line(x1,y1,xn,yn);
getch();
}
void scaling()
{
int x1,x2,y1,y2,sx,sy,x3,y3;
printf("Enter the 3 end coordinates of triangle\n");

scanf("%d%d%d%d%d%d",&x1,&y1,&x2,&y2,&x3,&y3);
printf("Enter the scaling factors\n");
scanf("%d%d",&sx,&sy);
line(x1,y1,x2,y2);
line(x1,y1,x3,y3);
line(x2,y2,x3,y3);
line(x1,y1,(x2+(x2-x1)*sx),(y2+(y2-y1)*sy));
line(x1,y1,(x3+(x3-x1)*sx),(y3+(y3-y1)*sy));
Department of Computer Applications

Page 28

Computer Graphics

line((x2+(x2-x1)*sx),(y2+(y2-y1)*sy),(x3+(x3-x1)*sx),(y3+(y3-y1
)*sy));
getch();
}

OUTPUT
1.Translation
2.Rotation
3.Scaling
4.Exit
1
Enter the number of coordinates: 2
Enter coordinate (x1,y1) :
100
50
Enter coordinate (x2,y2) :
200
50
Enter the translation vectors, tx and ty:
5
10

Department of Computer Applications

Page 29

Computer Graphics
1.Translation
2.Rotation
3.Scaling
4.Exit
2
Enter the number of coordinates: 3
Enter coordinate (x1,y1) :
100
50
Enter coordinate (x2,y2) :
150
50
Enter coordinate (x3,y3) :
100
100
Enter the angle to rotate:
90
Enter Rotationpoint:
100
100

1.Translation
2.Rotation
Department of Computer Applications

Page 30

Computer Graphics
3.Scaling
4.Exit
3
Enter the number of coordinates: 3
Enter coordinate (x1,y1) :
100
50
Enter coordinate (x2,y2) :
150
50
Enter coordinate (x3,y3) :
100
100
Enter the scaling factors, sx and sy:
2
2

Department of Computer Applications

Page 31

Computer Graphics
LINE CLIPPING
AIM
Write a program to implement line clipping

ALGORITHM
Algorithm for main
Step 01: start
Step 02: declare integer variables n, x, i
Step 03: declare integer array x[10][2], y[10][2]
Step 04: initialize graphics mode and graphics driver
Step 05: read the number of lines, n
Step 06: i:=0
Step 07: repeat the steps 08 to 09 until i=n
Step 08: read the coordinates, x[i][0], y[i][0], x[i][1], y[i][1]
Step 09: increment i
Step 10: read window coordinates, xwmin, ywmin, xwmax, ywmax
Step 11: clear viewport
Step 12: display the text before clipping
Step 13: i:=0
Step 14: repeat steps 15 to 16 until i=n
Step 15: draw line with parameters (x[i][0],y[i][0],x[i][1],y[i][1])
Step 16: increment i
Step 17: draw a rectangle using xwmin, ywmin, xwmax and ywmax
Step 18: clear viewport
Step 19: display the text after clipping
Step 20: draw a rectangle using xwmin, ywmin, xwmax and ywmaxv
Step 21: repeat steps 22 to 24 until i=n
Step 22: call function clipline with parameters (x[i][0],y[i][0],x[i][1],y[i][1])
Step 23: stop
Function clipline
Step 01: start
Step 02: declare integer array ca[4], cb[4], c[4]
Step 03: call function code with parameters (x1, y1,ca)
Step 04: call function code with parameters (x2, y2,cb)
Step 05: if (((ca[0] | cb[0])=0) and ((ca[1] | cb[1])=0) and
((ca[2] | cb[2])=0) and ((ca[3] | ca[3])=0)) then goto step 06
Step 06: draw line with parameters (x1, y1, x2, y2) and return
Step 07: if (((ca[0] | cb[0])!=0) and ((ca[1] | cb[1])!=0) and
((ca[2] | cb[2])!=0) and ((ca[3] | cb[3])!=0)) then goto step 08 else goto step 09
Step 08: x1:=-1, y1:=-1; x2:=-1, y2:=-1
Step 09: i:=0
Step 10: repeat step 11 until i=4
Step 11: c[i]:=ca[i]
Department of Computer Applications

Page 32

Computer Graphics
Step 12: if ((c[0]=0) and (c[1]=0) and (c[2]=0) and (c[3]=0)) then goto step 13
Step 13: i:=0
Step 14: repeat step 15 until i=4
Step 15: c[i]=cb[i]
Step 16: if (c[0] & 1)=1 then goto step 17 else goto step 19
Step 17: x:=xwmin
Step 18: y:=y1+(((y2-y1)/(x2-x1))*(xwmin-x1))
Step 19: if (c[1] & 1)=1 then goto step 20
Step 20: x:=xwmax
Step 21: y:=y1+(((y2-y1)/(x2-x1))*(xwmax-x1))
Step 22: if (c[2] & 1)=1 then goto step 23 else goto step 25
Step 23: x:=x1+(((x2-x1)/(y2-y1))*(ywmin-y1))
Step 24: y:=ywmin
Step 25: if (c[3] & 1)=1 then goto step 26
Step 26: x:=x1+(((x2-x1)/(y2-y1))*(ywmax-y1))
Step 27: y:=ywmax
Step 28: if((C[0]=ca[0]) and (C[1]=ca[1])
(C[2]=ca[2]) and (C[3]=ca[3]))then goto step 29 else goto step 32
Step 29: x1:=x
Step 30: y1:=y
Step 31: call function code with parameters (x1, y1, ca)
Step 32: x2:=x
Step 33: y2:=y
Step 34: call function code with parameters (x2, y2, cb)
Step 35: if(((ca[0]&cb[0])=0) and ((ca[1]&cb[1])=0)&&
((ca[2]&cb[2])=0) and ((ca[3]&cb[3])=0)) then goto step 36
Step 36: call function clipline with parameters (x1, y1, x2, y2)
Step 37: if cnt=1 then goto step 38
Step 38: display x1, y1, x2, y2
Step 39: cnt:=2
Step 40: stop
Function code
Step 01: start
Step 02: i:=0
Step 03: repeat steps 04 to 05 until i=4
Step 04: c[i]:=0
Step 05: increment i
Step 06: if x<xwmin then goto step 08 else goto step 08
Step 07: c[0]:=1
Step 08: if x>xwmax then goto step 09
Step 09: c[1]:=1
Step 10: if y<ywmin then goto step 11 else goto step 12
Step 11: c[2]:=1
Step 12: c[3]:=1
Step 13: stop

Department of Computer Applications

Page 33

Computer Graphics
PROGRAM
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
int xwmin,xwmax,ywmin,ywmax,i,x,y,cnt=1;
void clipline(int ,int, int, int);
void code(int, int, int[]);
void main()
{
int gd=DETECT,gm,n,x[10][2],y[10][2],i;
initgraph(&gd,&gm,"c:\\tc\\bgi");
printf("\nEnter the number of lines:\t");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter the co-ordinates of line %d:\t",i+1);
scanf("%d%d%d%d",&x[i][0],&y[i][0],&x[i][1],&y[i][1]);
}
printf("\nWindow Co-ordinates\n");
printf("\nXwmin:\t");
scanf("%d",&xwmin);
printf("\nYwmin:\t");
scanf("%d",&ywmin);
printf("\nXwmax:\t");
scanf("%d",&xwmax);
printf("\nYwmax:\t");
scanf("%d",&ywmax);
clearviewport();
outtextxy(200,10,"Before_Clipping");
setcolor(RED);
for(i=0;i<n;i++)
{
line(x[i][0],y[i][0],x[i][1],y[i][1]);
}
setcolor(15);
rectangle(xwmin,ywmin,xwmax,ywmax);
getch();
clearviewport();
outtextxy(200,10,"After_Clipping");
rectangle(xwmin,ywmin,xwmax,ywmax);
setcolor(RED);
for(i=0;i<n;i++)
Department of Computer Applications

Page 34

Computer Graphics
{
clipline(x[i][0],y[i][0],x[i][1],y[i][1]);
}
getch();
}
void clipline(int x1,int y1, int x2, int y2)
{
int cA[4],cB[4],C[4];
code(x1,y1,cA);
code(x2,y2,cB);
if(((cA[0]|cB[0])==0)&&((cA[1]|cB[1])==0)&&
((cA[2]|cB[2])==0)&&((cA[3]|cB[3])==0))
{
setcolor(4);
line(x1,y1,x2,y2);
return;
}
if(((cA[0]&cB[0])!=0)&&((cA[1]&cB[1])!=0)&&
((cA[2]&cB[2])!=0)&&((cA[3]&cB[3])!=0))
{
x1=-1;
y1=-1;
x2=-1;
y2=-1;
}
else
{
for(i=0;i<4;i++)
C[i]=cA[i];
if((C[0]==0)&&(C[1]==0)&&(C[2]==0)&&(C[3]==0))
{
for(i=0;i<4;i++)
C[i]=cB[i];
}
if((C[0]&1)==1)
{
x=xwmin;
y=y1+(((float)(y2-y1)/(float)(x2-x1))*(xwmin-x1));
}
else if((C[1]&1)==1)
{
x=xwmax;
y=y1+(((float)(y2-y1)/(float)(x2-x1))*(xwmax-x1));
}
if((C[2]&1)==1)
{
x=x1+(((float)(x2-x1)/(float)(y2-y1))*(ywmin-y1));
y=ywmin;
Department of Computer Applications

Page 35

Computer Graphics
}
else if((C[3]&1)==1)
{
x=x1+(((float)(x2-x1)/(float)(y2-y1))*(ywmax-y1));
y=ywmax;
}
if((C[0]==cA[0])&&(C[1]==cA[1])&&(C[2]==cA[2])&&(C[3]==cA[3]))
{
x1=x;
y1=y;
code(x1,y1,cA);
}
else
{
x2=x;
y2=y;
code(x2,y2,cB);
}
if(((cA[0]&cB[0])==0)&&((cA[1]&cB[1])==0)&&
((cA[2]&cB[2])==0)&&((cA[3]&cB[3])==0))
{
clipline(x1,y1,x2,y2);
}
}
if(cnt==1)
{
printf("Coordinates of the clipped line");
printf("(%d,%d),(%d,%d)",x1,y1,x2,y2);
cnt=2;
}
}
void code(int x, int y, int C[4])
{
int i;
for(i=0;i<4;i++)
C[i] = 0;
if( x < xwmin)
C[0] = 1;
else if(x > xwmax)
C[1] = 1;
if(y < ywmin)
C[2] = 1;
else if(y > ywmax)
C[3] = 1;
}

Department of Computer Applications

Page 36

Computer Graphics
OUTPUT
Enter the number of lines: 1
Enter the co-ordinates of line 1:
100
50
200
250
Window co-ordinates
Xwmin
:50
Ywmin
:100
Xwmax :250
Ywmax
:200

Department of Computer Applications

Page 37

Computer Graphics

Department of Computer Applications

Page 38

Computer Graphics
BOUNDARY FILL AND FLOOD FILL
AIM
Write a program to implement flood fill and boundary fill algorithms

ALGORITHM
Algorithm for main
Step 01: start
Step 02: declare integer variables s, x1,y1,x2,y2,d,z,ch,o
Step 03: repeat steps while s=1
Step 04: initialize graphics mode and graphics driver
Step 05: read start coordinates of the rectangle, x1, y1
Step 06: read end coordinates of the rectangle, x2, y2
Step 07: read the color to fill, z
Step 08: read choice, ch
Step 09: draw rectangle with parameters (x1,y1,x2,y2)
Step 10: get the pixel positions of x1 and y1 and then save it in variable d
Step 11: if choice is 1 then goto step 12
Step 12: display text Boundary fill
Step 13: call function boundfill with parameters (x1+1, y1+1, z, d)
Step 14: if choice is 2 then goto step 15
Step 15: display text Flood fill
Step 16: call function floodfill with parameters (x1+1, y1+1, z,0)
Step 17: if choice!=0 or 1 or 2 then display messageInvalid option
Step 18: if choice=0 then goto step 19
Step 19: stop
Function boundaryfill
Step 01: start
Step 02: declare integer variable c
Step 03: c:=getpixel(x,y)
Step 04: if c!=bcolor and c!=fcolor then goto step 05
Step 05: change the current color to fcolor
Step 06: put the pixel (x, y, fcolor)
Step 07: make a delay(10)
Step 08: recursively call boundfill with parameters (x,y+1,fcolor,bcolor)
Step 09: recursively call boundfill with parameters (x,y-1,fcolor,bcolor)
Step 10: recursively call boundfill with parameters (x+1,y-1,fcolor,bcolor)
Step 11: recursively call boundfill with parameters (x+1,y+1,fcolor,bcolor)
Step 12: recursively call boundfill with parameters (x-1,y-1,fcolor,bcolor)
Step 13: recursively call boundfill with parameters (x-1,y+1,fcolor,bcolor)
Step 14: recursively call boundfill with parameters (x+1,y,fcolor,bcolor)
Step 15: recursively call boundfill with parameters (x-1,y,fcolor,bcolor)
Step 16: stop
Department of Computer Applications

Page 39

Computer Graphics
Function floodfill
Step 01: start
Step 02: declare integer variable c
Step 03: c:=getpixel(x,y)
Step 04: if c=0 then goto step 05
Step 05: change the current color to fcolor
Step 06: put the pixel (x, y, fcolor)
Step 07: make a delay(10)
Step 08: recursively call flodfill with parameters (x+1,y,fcolor,o)
Step 09: recursively call flodfill with parameters (x,y+1,fcolor,o)
Step 10: recursively call flodfill with parameters (x+1,y+1,fcolor,o)
Step 11: recursively call flodfill with parameters (x-1,y-1,fcolor,o)
Step 12: recursively call flodfill with parameters (x-1,y,fcolor,o)
Step 13: recursively call flodfill with parameters flodfill(x,y-1,fcolor,o)
Step 14: recursively call flodfill with parameters (x-1,y+1,fcolor,o)
Step 15: recursively call flodfill with parameters (x+1,y-1,fcolor,o)
Step 16: stop

PROGRAM
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<stdlib.h>
void boundfill(int x,int y,int fcolor,int bcolor)
{
int c;
c=getpixel(x,y);
if((c!=bcolor)&&(c!=fcolor))
{
setcolor(fcolor);
putpixel (x,y,fcolor);
delay(10);
boundfill(x,y+1,fcolor,bcolor);
boundfill(x,y-1,fcolor,bcolor);
boundfill(x+1,y-1,fcolor,bcolor);
boundfill(x+1,y+1,fcolor,bcolor);
boundfill(x-1,y-1,fcolor,bcolor);
boundfill(x-1,y+1,fcolor,bcolor);
boundfill(x+1,y,fcolor,bcolor);
boundfill(x-1,y,fcolor,bcolor);
}
}
void flodfill(int x,int y,int fcolor,int o)
{
Department of Computer Applications

Page 40

Computer Graphics
int c;
c=getpixel(x,y);
if(c==o)
{
setcolor(fcolor);
putpixel (x,y,fcolor);
delay(10);
flodfill(x+1,y,fcolor,o);
flodfill(x,y+1,fcolor,o);
flodfill(x+1,y+1,fcolor,o);
flodfill(x-1,y-1,fcolor,o);
flodfill(x-1,y,fcolor,o);
flodfill(x,y-1,fcolor,o);
flodfill(x-1,y+1,fcolor,o);
flodfill(x+1,y-1,fcolor,o);
}
}
void main()
{
int gd=DETECT,gm,x1,y1,x2,y2,d,z,ch,o;
int s;
do
{
initgraph(&gd,&gm,"C:\\TC\\BGI");
printf("\nEnter start coordinates of the rectangle :\n");
scanf("%d%d",&x1,&y1);
printf("\nEnter end coordinates of the rectangle :\n");
scanf("%d%d",&x2,&y2);
printf("\nEnter fill color :");
scanf("%d",&z);
printf("\nEnter fill method :\n");
printf("1. Bound Fill\n");
printf("2. Flood Fill\n");
scanf("%d",&ch);
initgraph(&gd,&gm,"D:\\TC\\BGI");
rectangle(x1,y1,x2,y2);
d=getpixel(x1,y1);
switch(ch)
{
case 1: outtextxy(150,10,"BOUNDARY FILL");
boundfill(x1+1,y1+1,z,d);
break;
case 2: outtextxy(150,10,"FLOOD FILL");
flodfill(x1+1,y1+1,z,0);
break;
default: printf("\n Invalid Selection...!");
}
Department of Computer Applications

Page 41

Computer Graphics
getch();
initgraph(&gd,&gm,"D:\\TC\\BGI");
printf("\nPress 1 to continue and 0 to Exit...\n");
scanf("%d",&s);
}while(s==1);
}
OUTPUT

Department of Computer Applications

Page 42

Computer Graphics

Department of Computer Applications

Page 43

Computer Graphics

Department of Computer Applications

Page 44

Computer Graphics

Department of Computer Applications

Page 45

Computer Graphics

Department of Computer Applications

Page 46

Computer Graphics
TABLE FAN
AIM
Write a program to animate the motion of a table fan with speed control.

ALGORITHM
Step 01: Start
Step 02 : initialize xc,yc,start1,start2,start3,end1,end2,end3
Step 03 : initialize graphics modules and drivers.
Step 04 : call initleaf()
Step 05 : Stop
Function initleaf()
Step 01: Start
Step 02 : call drawcircle()
Step 03 : plot pieslice(xc,yc,start1,end1,50)
Step 04 :plot pieslice(xc,yc,start2,end2,50)
Step 05 :plot pieslice(xc,yc,start3,end3,50)
Step 06 : read ch
Step 07 : call button() with parameter ch.
Step 08 : stop
Function button(ch)
Step 01: Start
Step 02 : call initleaf() if ch=0
Step 03 : call rotate(1) if ch=1
Step 04 :call rotate(2) if ch=2
Step 05 :call rotate(3) if ch=3
Step 06 : otherwise exit
Step 07 : repeat step 02 through 06 while true.
Step 08 : stop
Function drawcircle()
Step 01: Start
Step 02 : plot bar(97,100,103,320)
Step 03 : plot bar(90,190,110,250)
Step 04 : plotellipse(103,320,0,360,40,10)
Step 05 :plotcircle(xc,yc,55)
Step 06 : plot switches,0,1,2,3.
Step 07: stop
Function rotate(x)
Step 01: Start
Step 02 : repeat through step 12 until a key is pressed
Step 03 : clear screen
Step 04 :call drawcircle()
Step 05 :plot pieslice(xc,yc,start1,end1,50)
Step 06 : plot pieslice(xc,yc,start2,end2,50)
Step 07 : plot pieslice(xc,yc,start3,end3,50)
Step 08 : increment start1,start2,start3,end1,end2, end3 each by 60.
Department of Computer Applications

Page 47

Computer Graphics
Step 09 : if start1>=360 set start1=0 and end1=30
Step 10 : if start2>=360 set start2=0 and end2=30
Step 11 : if start3>=360 set start3=0 and end3=30
Step 12 : make delay(150/x)
Step 13 : read ch
Step 14 : call button( )with parameter ch
Step 15 : stop

PROGRAM
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<process.h>
int xc=100,yc=100;
int start1=0,end1=30,start2=120,end2=150,start3=240,end3=270;
void rotate(int);
char ch;
void button(char);
void drawcircle();
void initleaf()
{
drawcircle();
pieslice(xc,yc,start1,end1,50);
pieslice(xc,yc,start2,end2,50);
pieslice(xc,yc,start3,end3,50);
ch=getch();
button(ch);
}
void drawcircle()
{
bar(97,100,103,320);
bar(90,190,110,250);
ellipse(103,320,0,360,40,10);
fillellipse(103,320,40,10);
circle(xc,yc,55);
setcolor(4);
outtextxy(95,200,"0");
outtextxy(95,210,"1");
outtextxy(95,220,"2");
outtextxy(95,230,"3");
}
void main()
{
int gd=DETECT,gm;
int ch;
initgraph(&gd,&gm,"C:\\TC\\BGI");
initleaf();
}
Department of Computer Applications

Page 48

Computer Graphics
void rotate(int x)
{
while(!kbhit())
{
cleardevice();
setcolor(15);
drawcircle();
pieslice(xc,yc,start1,end1,50);
pieslice(xc,yc,start2,end2,50);
pieslice(xc,yc,start3,end3,50);
start1=start1+60;
end1=end1+60;
start2=start2+60;
end2=end2+60;
start3=start3+60;
end3=end3+60;
if(start1>=360)
{
start1=0;
end1=30;
}
if(start2>=360)
{
start2=0;
end2=30;
}
if(start3>=360)
{
start3=0;
end3=30;
}
delay(150/x);
cleardevice();
}
ch=getch();
button(ch);
}
void button(char ch)
{
do
{ switch(ch)
{
case '0': initleaf();break;
case '1': rotate(1);break;
case '2': rotate(2);break;
case '3': rotate(3);break;
default:exit(0);
}

Department of Computer Applications

Page 49

Computer Graphics
} while(ch!='9');}

OUTPUT

Department of Computer Applications

Page 50

Computer Graphics

Department of Computer Applications

Page 51

Computer Graphics
BOUNCING BALL
AIM
Write a graphics program to show the movement of a bouncing ball

ALGORITHM
Algorithm for main
Step 01: start
Step 02: initialize graphics mode and graphics driver
Step 03: declare integer variables i, x, y, t,c
Step 04: initialize y=0,t=400,c=1
Step 05: set the background color
Step 06: set the color to lightred
Step 07: set fillstyle with parameters(sOLID_FILL, LIGHTRED)
Step 08: initialize x=40
Step 09: repeat steps 9 to until x<600
Step 10: clear the device
Step 11: draw circle with parameters (x,y,30)
Step 12:call floodfill function with paramaters (x,y,RED)
Step 13: call delay function with parameter 20
Step 14: if y>=400 then goto step 15 else goto step 17
Step 15: c:=0
Step 16: t:=t-20
Step 17: if y<=(400-t) then goto step 18 else goto step 19
Step 18: c=1;
Step 19: y=y+(c?15:-15);
Step 20: increment x
Step 21: close graph
Step 22: stop

PROGRAM
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
void main()
{
int gd=DETECT,gm=DETECT;
int x,y=0,j,t=400,c=1;
initgraph(&gd,&gm,"d:\\tc\\bgi");
setcolor(RED);
setfillstyle(SOLID_FILL,RED);
for(x=40;x<600;x++)
{
Department of Computer Applications

Page 52

Computer Graphics
cleardevice();
circle(x,y,30);
floodfill(x,y,RED);
delay(20);
if(y>=400)
{
c=0;
t-=20;
}
if(y<=(400-t))
c=1;
y=y+(c?15:-15);
}
getch();
}

OUTPUT

Department of Computer Applications

Page 53

Computer Graphics

Department of Computer Applications

Page 54

Computer Graphics

Department of Computer Applications

Page 55

Computer Graphics

Department of Computer Applications

Page 56

Computer Graphics
NATIONAL FLAG
AIM
Write a program to draw a waving national flag

ALGORITHM
Step 01: start
Step 02: initialize global variables x=100, y=100
Step 03: initialize graphics module and graphics driver
Step 04: draw bar with parameters 98,100,100,300
Step 05: while a keyboard hit happens repeat steps 06 to 55
Step 06: initialize p=x,q=y
Step 07: clear device
Step 08: set i=0
Step 09: repeat steps 10 to 16 while i<4
Step 10: set color with parameter 12 and fill style as SOLID_FILL
Step 11: draw bar with parameters p, q-i, p+10, q+10-i
Step 12: set color with parameter 15 and fill style as SOLID_FILL
Step 13: draw bar with parameters p, q+10-i, p+10, q+20-i
Step 14: set color with parameter 10 and fill style as SOLID_FILL
Step 15: draw bar with parameters p, q+20-i, p+10, q+30-i
Step 16: increment p=p+5 and i=i+1
Step 17: set color with parameter 1
Step 18: draw circle with parameters p+5, q+5, 3
Step 19: set p=p+5, q=q-2
Step 20: initialize p=x,q=y
Step 21: set i=0
Step 22: repeat steps 23 to 29 while i<4
Step 23 set color with parameter 12 and fill style as SOLID_FILL
Step 24: draw bar with parameters p, q+i, p+10, q+10+i
Step 25: set color with parameter 15 and fill style as SOLID_FILL
Step 26: draw bar with parameters p, q+10+i, p+10, q+20+i
Step 27: set color with parameter 10 and fill style as SOLID_FILL
Step 28: draw bar with parameters p, q+20+i, p+10, q+30+i
Step 29: increment p=p+5 and i=i+1
Step 30: make delay of 700
Step 31: clear device
Step 32: draw bar with parameters 98,100,100,300
Step 33: initialize p=x,q=y
Step 34: set i=0
Step 35: repeat steps 23 to 29 while i<4
Step 36 set color with parameter 12 and fill style as SOLID_FILL
Step 37: draw bar with parameters p, q+i, p+10, q+10+i
Step 38: set color with parameter 15 and fill style as SOLID_FILL
Step 39: draw bar with parameters p, q+10+i, p+10, q+20+i
Step 40: set color with parameter 10 and fill style as SOLID_FILL
Step 41: draw bar with parameters p, q+20+i, p+10, q+30+i
Department of Computer Applications

Page 57

Computer Graphics
Step 42: increment p=p+5 and i=i+1
Step 43: set color with parameter 1
Step 44: draw circle with parameters p+5, q+15, 3
Step 45: set p=p+5, q=q-2
Step 46: set i=0
Step 47: repeat steps 48 to 54 while i<4
Step 48: set color with parameter 12 and fill style as SOLID_FILL
Step 49: draw bar with parameters p, q-i, p+10, q+10-i
Step 50: set color with parameter 15 and fill style as SOLID_FILL
Step 51: draw bar with parameters p, q+10-i, p+10, q+20-i
Step 52: set color with parameter 10 and fill style as SOLID_FILL
Step 53: draw bar with parameters p, q+20-i, p+10, q+30-i
Step 54: increment p=p+5 and i=i+1
Step 55: make delay of 500
Step 56: stop

PROGRAM
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
int x=100,y=100;
void main()
{
int gd=0,gm;
int i=0,p,q;
initgraph(&gd,&gm,"c:\\tc\\bgi");
setcolor(YELLOW);
setfillstyle(SOLID_FILL,YELLOW);
bar(98,100,100,300);
while(!kbhit())
{
p=x;q=y;
cleardevice();
for(i=0;i<4;i++)
{
setcolor(YELLOW);
setfillstyle(SOLID_FILL,YELLOW);
bar(98,100,100,300);
setcolor(12);
setfillstyle(SOLID_FILL,12);
bar(p,q-i,p+10,q+10-i);
setcolor(15);
setfillstyle(SOLID_FILL,15);
bar(p,q+10-i,p+10,q+20-i);
setcolor(10);
setfillstyle(SOLID_FILL,10);
bar(p,q+20-i,p+10,q+30-i);
p+=5;
}
Department of Computer Applications

Page 58

Computer Graphics
setcolor(1);
circle(p,q+10,3);
p=p+5;
q=q-2;
for(i=0;i<4;i++)
{
setcolor(12);
setfillstyle(SOLID_FILL,12);
bar(p,q+i,p+10,q+10+i);
setcolor(15);
setfillstyle(SOLID_FILL,15);
bar(p,q+10+i,p+10,q+20+i);
setcolor(10);
setfillstyle(SOLID_FILL,10);
bar(p,q+20+i,p+10,q+30+i);
p+=5;
}
delay(700);
cleardevice();
setcolor(YELLOW);
setfillstyle(SOLID_FILL,YELLOW);
bar(98,100,100,300);
p=x;q=y ;
for(i=0;i<4;i++)
{
setcolor(12);
setfillstyle(SOLID_FILL,12);
bar(p,q+i,p+10,q+10+i);
setcolor(15);
setfillstyle(SOLID_FILL,15);
bar(p,q+10+i,p+10,q+20+i);
setcolor(10);
setfillstyle(SOLID_FILL,10);
bar(p,q+20+i,p+10,q+30+i);
p+=5;
}
setcolor(1);
circle(p+5,q+15,3);
p=p+5;
q=q-2;
for(i=0;i<4;i++)
{
setcolor(12);
setfillstyle(SOLID_FILL,12);
bar(p,q-i,p+10,q+10-i);
setcolor(15);
setfillstyle(SOLID_FILL,15);
bar(p,q+10-i,p+10,q+20-i);
setcolor(10);
Department of Computer Applications

Page 59

Computer Graphics
setfillstyle(SOLID_FILL,10);
bar(p,q+20-i,p+10,q+30-i);
p+=5;
}
delay(500);
}
getch();
}

OUTPUT

Department of Computer Applications

Page 60

Computer Graphics

Department of Computer Applications

Page 61

Computer Graphics
LANDING OF AEROPLANE ON A SHIP
AIM
Write a program to animate the landing of airplane on a ship.

ALGORITHM
Step 01: Start
Step 02: declare integer variables x, y, I, p, q, j, c
Step 03: initialize variables i=0, p=80, q=100, j=0
Step 04: initialize graphics drivers and modules
Step 05: set x=maximum x value-50
Step 06: set y=maximum y value-50
Step 07: while a keyboard hit repeat steps 08 to 21
Step 08: set j=i*2
Step 09: draw airplane by adjusting i and j values
Step 10: draw ship by adjusting x, y, I, j values
Step 11: set i= i+1
Step 12: if the y values of airplane is greater than or equal to y values ship then go to step 13
else go to step 20
Step 13: set c=0
Step 14: while c<50 repeat steps 15 to 19
Step 15: clear device
Step 16: draw airplane
Step 17: draw ship
Step 18: make delay of 200
Step 19: set c=c+1
Step 20: make delay of 200
Step 21: clear device
Step 22: stop

PROGRAM
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=0,gm;
int x,y,i=0,p=80,q=100,j=0,c;
initgraph(&gd,&gm,"c:\\tc\\bgi");
x=getmaxx()-50;
y=getmaxy()-50;
while(!kbhit())
{
j=i*2;
Department of Computer Applications

Page 62

Computer Graphics
rectangle(20+i,90+j,p+i,q+j);
line(p+i,q+j,p+10+i,q+j);
line(p+i,q-10+j,p+10+i,q+j);
line(20+i,90+j,20+i,70+j);
line(10+i,q+j,20+i,q+j);
line(10+i,q+j,20+i,70+j);
//ship
line(x-i,y,x-300-i,y);
line(x-i,y,x+50-i,y-100);
line(x-300-i,y,x-350-i,y-100);
line(x-350-i,y-100,x+50-i,y-100);
rectangle(x-100-i,y-150,x-i,y-100);
rectangle(x-75-i,y-200,x-25-i,y-150);
rectangle(x-60-i,y-300,x-40-i,y-200);
i++;
if((q+j)>=(y-100))
{
for(c=0;c<50;c++)
{
cleardevice();
//plane
rectangle(20+i+c,90+j,p+i+c,q+j);
line(p+i+c,q+j,p+10+i+c,q+j);
line(p+i+c,q-10+j,p+10+i+c,q+j);
line(20+i+c,90+j,20+i+c,70+j);
line(10+i+c,q+j,20+i+c,q+j);
line(10+i+c,q+j,20+i+c,70+j);
//ship
line(x-i,y,x-300-i,y);
line(x-i,y,x+50-i,y-100);
line(x-300-i,y,x-350-i,y-100);
line(x-350-i,y-100,x+50-i,y-100);
rectangle(x-100-i,y-150,x-i,y-100);
rectangle(x-75-i,y-200,x-25-i,y-150);
rectangle(x-60-i,y-300,x-40-i,y-200);
delay(200);
}
break;
}
delay(200);
cleardevice();
}
getch();
}

Department of Computer Applications

Page 63

Computer Graphics
OUTPUT

Department of Computer Applications

Page 64

Computer Graphics

Department of Computer Applications

Page 65

Computer Graphics
BUCKET FILLING
AIM
Write a program to fill the bucket with water.

ALGORITHM
Step 01: Start
Step 02: initialize graphics drivers and modules
Step 05: draw tap ,bucket and water using ellipse and line.
Step 06: set color blue for water.
Step 07: for (i = 0; i < 7; i++)
Step 08: line(297 + i, 103, 297 + i, 300);
Step 09: for (i = 1; i < 100; i++) {
Step 10:
setcolor(LIGHTBLUE);
Step 11: ellipse(300, 300 - i, 180, 360, 4, 2);
Step 13: delay(30);
Step 14:
fillellipse(300, 300 - i, 49, 25);
Step 15:
setcolor(1);
Step 16:
line(297, 275 - i, 303, 275 - i);
Step 17: setcolor(15);
Step 18: ellipse(300, 200, 180, 360, 50, 25);
Step 19: delay(50); }
Step 20: ellipse(300, 200, 0, 360, 50, 25);
Step 21: setcolor(0);
Step 22: for (i = 0; i < 7; i++)
Step 23: line(297 + i, 103, 297 + i, 174);
Step 23: stop

PROGRAM
#include<conio.h>
#include<dos.h>
#include<graphics.h>
void main()
{
int gd = DETECT, gm = DETECT, i, j;
initgraph(&gd, &gm, "");
ellipse(300, 200, 0, 360, 50, 25);
ellipse(300, 300, 0, 360, 50, 25);
line(250, 200, 250, 300);
line(350, 200, 350, 300);
ellipse(300, 100, 180, 360, 5, 2);
line(295, 100, 295, 80);
line(305, 100, 305, 86);
arc(300, 80, 90, 180, 5);
putpixel(306, 85, 15);
Department of Computer Applications

Page 66

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

OUTPUT

Department of Computer Applications

Page 67

Computer Graphics

Department of Computer Applications

Page 68

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy