cgs record (1) - copy
TRANSCRIPT
-
7/23/2019 Cgs Record (1) - Copy
1/68
Computer Graphics
Department of Computer Applications Page 1
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
-
7/23/2019 Cgs Record (1) - Copy
2/68
Computer Graphics
Department of Computer Applications Page 2
PATTERNS
AIM
Write a program to draw
1.
LINE
2. CIRCLE
3. RECTANGLE
4. TRIANGLE
ALGORITHM
Step 01: start
Step 02: declare integer variables x1, x2,x3,x4,sangle,eangle,xr,yr, rStep 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
#include
#include
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");
-
7/23/2019 Cgs Record (1) - Copy
3/68
Computer Graphics
Department of Computer Applications Page 3
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;
-
7/23/2019 Cgs Record (1) - Copy
4/68
Computer Graphics
Department of Computer Applications Page 4
}
}while(c
-
7/23/2019 Cgs Record (1) - Copy
5/68
Computer Graphics
Department of Computer Applications Page 5
3.RECTANGLE
4.TRIANGLE
-
7/23/2019 Cgs Record (1) - Copy
6/68
Computer Graphics
Department of Computer Applications Page 6
-
7/23/2019 Cgs Record (1) - Copy
7/68
Computer Graphics
Department of Computer Applications Page 7
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:=0Step 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 driverStep 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=12Step 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:=0Step 36: repeat steps 37 to 40 until i>100
-
7/23/2019 Cgs Record (1) - Copy
8/68
Computer Graphics
Department of Computer Applications Page 8
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:=0Step 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
#include
#include
#include
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"};
-
7/23/2019 Cgs Record (1) - Copy
9/68
Computer Graphics
Department of Computer Applications Page 9
char ptr[20];
clrscr();
for(i=0;i
-
7/23/2019 Cgs Record (1) - Copy
10/68
Computer Graphics
Department of Computer Applications Page 10
x = x+40;
}
for(i=0;i
-
7/23/2019 Cgs Record (1) - Copy
11/68
Computer Graphics
Department of Computer Applications Page 11
4689
Enter sales and expense for oct
5900
7000
Enter sales and expense for nov
80006000
Enter sales and expense for dec
9870
5555
-
7/23/2019 Cgs Record (1) - Copy
12/68
Computer Graphics
Department of Computer Applications Page 12
PIE CHART
AIM
Write a program to draw a pie chart corresponding to the sales and expenses for 12 months inan 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 monthsStep 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:=2Step 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
-
7/23/2019 Cgs Record (1) - Copy
13/68
Computer Graphics
Department of Computer Applications Page 13
Step 19: if (stangle+eangle/2)>90 and (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
#include
#include
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
-
7/23/2019 Cgs Record (1) - Copy
14/68
Computer Graphics
Department of Computer Applications Page 14
}
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
-
7/23/2019 Cgs Record (1) - Copy
15/68
Computer Graphics
Department of Computer Applications Page 15
OUTPUT
Enter sales for 12 months
Enter sales for jan : 8000Enter 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 : 4500Enter 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
-
7/23/2019 Cgs Record (1) - Copy
16/68
Computer Graphics
Department of Computer Applications Page 16
-
7/23/2019 Cgs Record (1) - Copy
17/68
Computer Graphics
Department of Computer Applications Page 17
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, y2Step 06: call function ddaline() with parameters x1, y1, x2, y2Step 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/dxStep 07: if abs(m)
-
7/23/2019 Cgs Record (1) - Copy
18/68
Computer Graphics
Department of Computer Applications Page 18
PROGRAM
#include
#include
#include#include
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)
-
7/23/2019 Cgs Record (1) - Copy
19/68
Computer Graphics
Department of Computer Applications Page 19
OUTPUT
-
7/23/2019 Cgs Record (1) - Copy
20/68
Computer Graphics
Department of Computer Applications Page 20
BRESENHAMSLINE
AIM
Write a program to implement Bresenhamsline 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, y2Step 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-y1Step 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
-
7/23/2019 Cgs Record (1) - Copy
21/68
Computer Graphics
Department of Computer Applications Page 21
PROGRAM
#include
#include
#include
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
-
7/23/2019 Cgs Record (1) - Copy
22/68
Computer Graphics
Department of Computer Applications Page 22
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
-
7/23/2019 Cgs Record (1) - Copy
23/68
Computer Graphics
Department of Computer Applications Page 23
BRESENHAMSCIRCLE
AIM
Write a program to implement Bresenhamscircle 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, yStep 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)-rStep 06: repeat steps 07 to 11 while x
-
7/23/2019 Cgs Record (1) - Copy
24/68
Computer Graphics
Department of Computer Applications Page 24
PROGRAM
#include
#include
#include
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
-
7/23/2019 Cgs Record (1) - Copy
25/68
Computer Graphics
Department of Computer Applications Page 25
OUTPUT
-
7/23/2019 Cgs Record (1) - Copy
26/68
Computer Graphics
Department of Computer Applications Page 26
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, chStep 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, y3Step 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
-
7/23/2019 Cgs Record (1) - Copy
27/68
Computer Graphics
Department of Computer Applications Page 27
Step 12: plot line with parameters x3, y3, x2, y2
Step 13: stop
Function rotation()
Step 01: startStep 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, ynStep 13: stop
PROGRAM
#include
#include
#include
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;
-
7/23/2019 Cgs Record (1) - Copy
28/68
-
7/23/2019 Cgs Record (1) - Copy
29/68
Computer Graphics
Department of Computer Applications Page 29
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
-
7/23/2019 Cgs Record (1) - Copy
30/68
Computer Graphics
Department of Computer Applications Page 30
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.Translation2.Rotation
-
7/23/2019 Cgs Record (1) - Copy
31/68
Computer Graphics
Department of Computer Applications Page 31
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
-
7/23/2019 Cgs Record (1) - Copy
32/68
Computer Graphics
Department of Computer Applications Page 32
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:=0Step 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 ywmaxStep 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]
-
7/23/2019 Cgs Record (1) - Copy
33/68
Computer Graphics
Department of Computer Applications Page 33
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:=xwminStep 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]:=0Step 05: increment i
Step 06: if xxwmax then goto step 09
Step 09: c[1]:=1
Step 10: if y
-
7/23/2019 Cgs Record (1) - Copy
34/68
Computer Graphics
Department of Computer Applications Page 34
PROGRAM
#include
#include
#include
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
-
7/23/2019 Cgs Record (1) - Copy
35/68
Computer Graphics
Department of Computer Applications Page 35
{
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
-
7/23/2019 Cgs Record (1) - Copy
36/68
Computer Graphics
Department of Computer Applications Page 36
}
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 xwmax)
C[1] = 1;
if(y < ywmin)
C[2] = 1;
else if(y > ywmax)
C[3] = 1;
}
-
7/23/2019 Cgs Record (1) - Copy
37/68
Computer Graphics
Department of Computer Applications Page 37
OUTPUT
Enter the number of lines: 1
Enter the co-ordinates of line 1:
10050
200
250
Window co-ordinates
Xwmin :50
Ywmin :100
Xwmax :250
Ywmax :200
-
7/23/2019 Cgs Record (1) - Copy
38/68
Computer Graphics
Department of Computer Applications Page 38
-
7/23/2019 Cgs Record (1) - Copy
39/68
Computer Graphics
Department of Computer Applications Page 39
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, y1Step 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 fcolorStep 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
-
7/23/2019 Cgs Record (1) - Copy
40/68
Computer Graphics
Department of Computer Applications Page 40
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
#include
#include
#include
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){
-
7/23/2019 Cgs Record (1) - Copy
41/68
Computer Graphics
Department of Computer Applications Page 41
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...!");}
-
7/23/2019 Cgs Record (1) - Copy
42/68
Computer Graphics
Department of Computer Applications Page 42
getch();
initgraph(&gd,&gm,"D:\\TC\\BGI");
printf("\nPress 1 to continue and 0 to Exit...\n");
scanf("%d",&s);
}while(s==1);
}
OUTPUT
-
7/23/2019 Cgs Record (1) - Copy
43/68
-
7/23/2019 Cgs Record (1) - Copy
44/68
Computer Graphics
Department of Computer Applications Page 44
-
7/23/2019 Cgs Record (1) - Copy
45/68
Computer Graphics
Department of Computer Applications Page 45
-
7/23/2019 Cgs Record (1) - Copy
46/68
Computer Graphics
Department of Computer Applications Page 46
-
7/23/2019 Cgs Record (1) - Copy
47/68
Computer Graphics
Department of Computer Applications Page 47
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: StartStep 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=1Step 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.
-
7/23/2019 Cgs Record (1) - Copy
48/68
Computer Graphics
Department of Computer Applications Page 48
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 chStep 14 : call button( )with parameter ch
Step 15 : stop
PROGRAM
#include
#include
#include
#include
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();}
-
7/23/2019 Cgs Record (1) - Copy
49/68
Computer Graphics
Department of Computer Applications Page 49
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);
}
-
7/23/2019 Cgs Record (1) - Copy
50/68
Computer Graphics
Department of Computer Applications Page 50
} while(ch!='9');}
OUTPUT
-
7/23/2019 Cgs Record (1) - Copy
51/68
Computer Graphics
Department of Computer Applications Page 51
-
7/23/2019 Cgs Record (1) - Copy
52/68
Computer Graphics
Department of Computer Applications Page 52
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 colorStep 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=400 then goto step 15 else goto step 17
Step 15: c:=0
Step 16: t:=t-20Step 17: if y
-
7/23/2019 Cgs Record (1) - Copy
53/68
Computer Graphics
Department of Computer Applications Page 53
cleardevice();
circle(x,y,30);
floodfill(x,y,RED);
delay(20);
if(y>=400){
c=0;
t-=20;
}
if(y
-
7/23/2019 Cgs Record (1) - Copy
54/68
Computer Graphics
Department of Computer Applications Page 54
-
7/23/2019 Cgs Record (1) - Copy
55/68
Computer Graphics
Department of Computer Applications Page 55
-
7/23/2019 Cgs Record (1) - Copy
56/68
Computer Graphics
Department of Computer Applications Page 56
-
7/23/2019 Cgs Record (1) - Copy
57/68
Computer Graphics
Department of Computer Applications Page 57
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 deviceStep 08: set i=0
Step 09: repeat steps 10 to 16 while i
-
7/23/2019 Cgs Record (1) - Copy
58/68
Computer Graphics
Department of Computer Applications Page 58
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
-
7/23/2019 Cgs Record (1) - Copy
59/68
Computer Graphics
Department of Computer Applications Page 59
setcolor(1);
circle(p,q+10,3);
p=p+5;
q=q-2;
for(i=0;i
-
7/23/2019 Cgs Record (1) - Copy
60/68
Computer Graphics
Department of Computer Applications Page 60
setfillstyle(SOLID_FILL,10);
bar(p,q+20-i,p+10,q+30-i);
p+=5;
}
delay(500);
}
getch();
}
OUTPUT
-
7/23/2019 Cgs Record (1) - Copy
61/68
Computer Graphics
Department of Computer Applications Page 61
-
7/23/2019 Cgs Record (1) - Copy
62/68
Computer Graphics
Department of Computer Applications Page 62
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-50Step 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
-
7/23/2019 Cgs Record (1) - Copy
63/68
Computer Graphics
Department of Computer Applications Page 63
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
-
7/23/2019 Cgs Record (1) - Copy
64/68
Computer Graphics
Department of Computer Applications Page 64
OUTPUT
-
7/23/2019 Cgs Record (1) - Copy
65/68
Computer Graphics
Department of Computer Applications Page 65
-
7/23/2019 Cgs Record (1) - Copy
66/68
Computer Graphics
Department of Computer Applications Page 66
BUCKET FILLING
AIM
Write a program to fill the bucket with water.
ALGORITHMStep 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
#include
#include
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);
-
7/23/2019 Cgs Record (1) - Copy
67/68
Computer Graphics
Department of Computer Applications Page 67
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
-
7/23/2019 Cgs Record (1) - Copy
68/68
Computer Graphics