0% found this document useful (0 votes)
50 views27 pages

Computer Graphics

Uploaded by

infinitybros2003
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views27 pages

Computer Graphics

Uploaded by

infinitybros2003
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

FAIRFIELD INSITTUTE OF MANAGEMENT AND TECHNOLOGY

PRACTICAL FILE
Subject Name:- COMPUTER GRAPHICS
Subject Code:- BCA 373

Submitted to:- Submitted by:-


MR. SHASHI KANT TIWARI RAKSHIT KUMAR
SCHOOL OF INFORMATIVE 04490102021
TECHOLOGY (I.T) BCA 5th SEM

1
TABLE OF CONTENT

S.NO PRACTICAL LIST PAGE NO


1 Rotation in 2D 1

2 Scaling in 2D 3
3 Reflection in 2D 4
4 Shearing in 2D 6

5 Cohen Sutherland's Algorithm 7


6 Translation in 2D 11
7 Making an Analog Clock 12

8 Design a screensaver 14

9 Draw a moving cycle 15


10 Show Moving Car Animation 16

11 Show Bouncing Ball Animation 18


12 Circle Algorithms: - 19

13 Line Drawing Algorithms (DDA & Bradenham’s 21


Algorithm)
14 Graphics Inbuilt functions 23

15 Program to draw Flying Balloons 24

2
Practical 1: Rotation in 2D.

1
OUTPUT:

2
Practical 2: Scaling in 2D.

#include <iostream>
#include <conio.h>
#include <graphics.h>
void main()
{
int gd=DETECT,gm;
float p,q,r,s,Sx,Sy;

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

cout<<"Enter the first coordinate of a line:";


cin>>p>>" ">>q;
cout << endl;

cout<<"Enter the second coordinate of a line:";


cin>>r>>" ">>s;
cout << endl;

line(p,q,r,s);

cout<<"Enter the scaling factor:";


cin>>Sx>>" ">>Sy;
cout << endl;

p=p*Sx;
q=q*Sy;
r=r*Sx;
s=s*Sy;

line(p,q,r,s);

getch();
closegraph();
}

OUTPUT :

3
Practical 3: Reflection in 2D.

#include <conio.h>
#include <graphics.h>
#include <stdio.h>
void main()
{
int gm, gd = DETECT, ax, x1 = 100;
int x2 = 100, x3 = 200, y1 = 100;
int y2 = 200, y3 = 100;
initgraph(&gd, &gm, "");
cleardevice();
line(getmaxx() / 2, 0, getmaxx() / 2,
getmaxy());
line(0, getmaxy() / 2, getmaxx(),
getmaxy() / 2);
printf("Before Reflection Object"
" in 2nd Quadrant");
setcolor(14);
line(x1, y1, x2, y2);
line(x2, y2, x3, y3);
line(x3, y3, x1, y1);
getch();
printf("\nAfter Reflection");
setcolor(4);
line(getmaxx() - x1, getmaxy() - y1,
getmaxx() - x2, getmaxy() - y2);
line(getmaxx() - x2, getmaxy() - y2,
getmaxx() - x3, getmaxy() - y3);
line(getmaxx() - x3, getmaxy() - y3,
getmaxx() - x1, getmaxy() - y1);
setcolor(3);
line(getmaxx() - x1, y1,
getmaxx() - x2, y2);
line(getmaxx() - x2, y2,
getmaxx() - x3, y3);
line(getmaxx() - x3, y3,
getmaxx() - x1, y1);
setcolor(2);
line(x1, getmaxy() - y1, x2,
getmaxy() - y2);
line(x2, getmaxy() - y2, x3,
getmaxy() - y3);
line(x3, getmaxy() - y3, x1,
getmaxy() - y1);
getch();
closegraph();
}

4
OUTPUT:

5
Practical 4: Shearing in 2D.
#include<stdio.h>
#include<graphics.h>
#include<conio.h>
void main()
{
int gd=DETECT,gm;
int x,y,x1,y1,x2,y2,shear_f;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
printf("\n please enter first coordinate = ");
scanf("%d %d",&x,&y);
printf("\n please enter second coordinate = ");
scanf("%d %d",&x1,&y1);
printf("\n please enter third coordinate = ");
scanf("%d %d",&x2,&y2);
printf("\n please enter shearing factor x = ");
scanf("%d",&shear_f);
cleardevice();
line(x,y,x1,y1);
line(x1,y1,x2,y2);
line(x2,y2,x,y);

setcolor(RED);
x=x+ y*shear_f;
x1=x1+ y1*shear_f;
x2=x2+ y2*shear_f;

line(x,y,x1,y1);
line(x1,y1,x2,y2);
line(x2,y2,x,y);
getch();
closegraph();
}

OUTPUT:-

6
Practical 5: Cohen Sutherland's Algorithm.
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<graphics.h>
#include<dos.h>
typedef struct coordinate
{
int x,y;
char code[4];
}PT;

void drawwindow();
void drawline(PT p1,PT p2);
PT setcode(PT p);
int visibility(PT p1,PT p2);
PT resetendpt(PT p1,PT p2);

void main()
{
int gd=DETECT,v,gm;
PT p1,p2,p3,p4,ptemp;
printf("\nEnter x1 and y1\n");
scanf("%d %d",&p1.x,&p1.y);
printf("\nEnter x2 and y2\n");
scanf("%d %d",&p2.x,&p2.y);
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
drawwindow();
delay(500);
drawline(p1,p2);
delay(500);
cleardevice();
delay(500);
p1=setcode(p1);
p2=setcode(p2);
v=visibility(p1,p2);
delay(500);
switch(v)
{
case 0: drawwindow();
delay(500);
drawline(p1,p2);
break;
case 1: drawwindow();
delay(500);
break;
case 2: p3=resetendpt(p1,p2);
p4=resetendpt(p2,p1);

7
drawwindow();
delay(500);
drawline(p3,p4);
break;
}
delay(5000);
closegraph();
}

void drawwindow()
{
line(150,100,450,100);
line(450,100,450,350);
line(450,350,150,350);
line(150,350,150,100);
}

void drawline(PT p1,PT p2)


{
line(p1.x,p1.y,p2.x,p2.y);
}

PT setcode(PT p) //for setting the 4 bit code


{
PT ptemp;
if(p.y<100)
ptemp.code[0]='1'; //Top
else
ptemp.code[0]='0';
if(p.y>350)
ptemp.code[1]='1'; //Bottom
else
ptemp.code[1]='0';
if(p.x>450)
ptemp.code[2]='1'; //Right
else
ptemp.code[2]='0';
if(p.x<150)
ptemp.code[3]='1'; //Left
else
ptemp.code[3]='0';
ptemp.x=p.x;
ptemp.y=p.y;
return(ptemp);
}

int visibility(PT p1,PT p2)


{

8
int i,flag=0;
for(i=0;i<4;i++)
{
if((p1.code[i]!='0') || (p2.code[i]!='0'))
flag=1;
}
if(flag==0)
return(0);
for(i=0;i<4;i++)
{
if((p1.code[i]==p2.code[i]) && (p1.code[i]=='1'))
flag='0';
}
if(flag==0)
return(1);
return(2);
}

PT resetendpt(PT p1,PT p2)


{
PT temp;
int x,y,i;
float m,k;
if(p1.code[3]=='1')
x=150;
if(p1.code[2]=='1')
x=450;
if((p1.code[3]=='1') || (p1.code[2]=='1'))
{
m=(float)(p2.y-p1.y)/(p2.x-p1.x);
k=(p1.y+(m*(x-p1.x)));
temp.y=k;
temp.x=x;
for(i=0;i<4;i++)
temp.code[i]=p1.code[i];
if(temp.y<=350 && temp.y>=100)
return (temp);
}
if(p1.code[0]=='1')
y=100;
if(p1.code[1]=='1')
y=350;
if((p1.code[0]=='1') || (p1.code[1]=='1'))
{
m=(float)(p2.y-p1.y)/(p2.x-p1.x);
k=(float)p1.x+(float)(y-p1.y)/m;
temp.x=k;
temp.y=y;

9
for(i=0;i<4;i++)
temp.code[i]=p1.code[i];
return(temp);
}
else
return(p1);
}

OUTPUT:-

BEFORE CLIPPING

AFTER CLIPPING

10
Practical 6: Translation in 2D.
#include<conio.h>
#include<graphics.h>
#include<stdio.h>
void main()
{
int gd=DETECT,gm;
int l[2][2],v[2]={10,15},i=0,j;
clrscr();
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
printf("Enter the initial and final coordinates of a line ");
while(i<2)
{
printf("x%d and y%d = ",i,i);
j=0;
scanf("%d",&l[i][j]);
scanf("%d",&l[i][j+1]);
i++;
}
line(l[0][0],l[0][1],l[1][0],l[1][1]);
setcolor(BLUE);
line(l[0][0]+v[0],l[0][1]+v[1],l[1][0]+v[0],l[1][1]+v[1]);
getch();
closegraph();
}

OUTPUT:-

11
Practical 7: Making an Analog Clock.
#include<conio.h>
#include<graphics.h>
#include<dos.h>
int calculatehrs(int h)
{
int x;
switch(h)
{
case 0: x=90;
break;
case 1:
case 13: x=60;
break;
case 2:
case 14: x=30
break;
}
int calculatemin(int m )
{
int x;
if(m%5==0){
switch(m){
case 0: x= 90;
break;
case 5: x= 60;
break;
case 10: x= 30;
break;
case 15: x= 360;
break;
case 20: x= 330;
break;
case 25: x= 300;
break;
initgraph(&gd,&gm,"C:\Users\jravi\OneDrive\Desktop");
get time (&t);
h=t.t i_hour;
m=t.t i_min;
s=t.t i_sec;
a = calculatemin(h);
b= calculatemin(m);
c= calculatemin(s);
a =changehrs(m,a)
for(i=a; i>0; i-=6)
for(j=b; j>0; j-=6)
for(k=c; k>0; k-=6)
{

12
outtextxy(190,20, "Analog Clock");
circle(300, 200,102);
}
}

}
}
}

OUTPUT:-

13
Practical 8: Design a screensaver.
#include <stdio.h>
#include <stdlib.h>
#include <graphics.h>
#include <conio.h>

void main(){
int gdriver=DETECT,gmode,col=480,row=640,font=4,direction=2,size=8,color=15;
initgraph(&gdriver,&gmode,"C:\\TurboC3\\BGI");
cleardevice();
while(!kbhit()){
settextstyle(random(font),random(direction),random(size));
setcolor(random(color));
outtextxy(random(col),random(row),"ShadowHack");
delay(250);
}
closegraph();
}

OUTPUT:-

14
Practical 9: Draw a moving cycle.
#include<stdio.h>
#include<graphics.h>
int main()
{
int gd = DETECT, gm ;
int i;
initgraph(&gd,&gm,"");
for(i = 0; i < 500; i++){
circle(200 + i,300,30);
circle(350 + i,300,30);
arc(200 + i, 300, 0, 180, 34);
arc(350 + i, 300, 30, 195, 34);
line(200 + i,300,270 + i,300);
line(200 + i,240,200 + i,300);
line(200 + i,250,270 + i,300);
line(270 + i,300,350 + i,250);
line(200 + i,250,350 + i,250);
line(350 + i,300,350 + i,220);
line(340 + i,210,360 + i,230);
line(265 + i,280,275 + i, 320);
line(190 + i,240,220 + i,240);

line(0,330,650,330);
delay(30);
cleardevice();
}
getch();
return 0;
}

OUTPUT:-

15
Practical 10: Show Moving Car Animation.

#include <graphics.h>
#include <stdio.h>
void draw_moving_car(void) {
int i, gd = DETECT, gm;
initgraph(&gd, &gm, "");
for (i = 0; i <= 420; i = i + 10) {
setcolor(RED);
line(0 + i, 300, 210 + i, 300);
line(50 + i, 300, 75 + i, 270);
line(75 + i, 270, 150 + i, 270);
line(150 + i, 270, 165 + i, 300);
line(0 + i, 300, 0 + i, 330);
line(210 + i, 300, 210 + i, 330);
circle(65 + i, 330, 15);
circle(65 + i, 330, 2);
circle(145 + i, 330, 15);
circle(145 + i, 330, 2);
line(0 + i, 330, 50 + i, 330);
line(80 + i, 330, 130 + i, 330);
line(210 + i, 330, 160 + i, 330);
delay(100);
setcolor(BLACK);
line(0 + i, 300, 210 + i, 300);
line(50 + i, 300, 75 + i, 270);
line(75 + i, 270, 150 + i, 270);
line(150 + i, 270, 165 + i, 300);
line(0 + i, 300, 0 + i, 330);
line(210 + i, 300, 210 + i, 330);
circle(65 + i, 330, 15);
circle(65 + i, 330, 2);
circle(145 + i, 330, 15);
circle(145 + i, 330, 2);
line(0 + i, 330, 50 + i, 330);
line(80 + i, 330, 130 + i, 330);
line(210 + i, 330, 160 + i, 330);
}
getch();
closegraph();
}
int main()
{
draw_moving_car();
return 0;
}

16
OUTPUT:-

17
Practical 11: Show Bouncing Ball Animation.
#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, "");
setcolor(RED);
setfillstyle(SOLID_FILL, RED);
for (x = 40; x < 602; x++) {
cleardevice();
circle(x, y, 30);
floodfill(x, y, RED);
delay(40);
if (y >= 400) {
c = 0;
t -= 20;
}
if (y <= (400 - t))
c = 1;
y = y + (c ? 15 : -15);
}
getch();
}

OUTPUT:-

18
Practical 12: Circle Algorithms.
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <math.h>
void EightWaySymmetricPlot(int xc,int yc,int x,int y)
{
putpixel(x+xc,y+yc,RED);
putpixel(x+xc,-y+yc,YELLOW);
putpixel(-x+xc,-y+yc,GREEN);
putpixel(-x+xc,y+yc,YELLOW);
putpixel(y+xc,x+yc,12);
putpixel(y+xc,-x+yc,14);
putpixel(-y+xc,-x+yc,15);
putpixel(-y+xc,x+yc,6);
}
void BresenhamCircle(int xc,int yc,int r)
{
int x=0,y=r,d=3-(2*r);
EightWaySymmetricPlot(xc,yc,x,y);
while(x<=y)
{
if(d<=0)
{
d=d+(4*x)+6;
}
else {
d=d+(4*x)-(4*y)+10;
y=y-1;
}
x=x+1;
EightWaySymmetricPlot(xc,yc,x,y);
}
}
int main(void)
{
int xc,yc,r,gdriver = DETECT, gmode, errorcode;
initgraph(&gdriver, &gmode, "C:\\TURBOC3\\BGI");
errorcode = graphresult();
if (errorcode != grOk)
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1);
}
printf("Enter the values of xc and yc :");

19
scanf("%d%d",&xc,&yc);
printf("Enter the value of radius :");
scanf("%d",&r);
BresenhamCircle(xc,yc,r);

getch();
closegraph();
return 0;
}

OUTPUT:

20
Practical 13: Line Drawing Algorithms (DDA & Bradenham’s Algorithm).
#include<stdio.h>
#include<graphics.h>
void drawline(int x0, int y0, int x1, int y1)
{
int dx, dy, p, x, y;
dx=x1-x0;
dy=y1-y0;
x=x0;
y=y0;
p=2*dy-dx;
while(x<x1)
{
if(p>=0)
{
putpixel(x,y,7);
y=y+1;
p=p+2*dy-2*dx;
}
else
{
putpixel(x,y,7);
p=p+2*dy;}
x=x+1;
}
}
int main()
{
int gdriver=DETECT, gmode, error, x0, y0, x1, y1;
initgraph(&gdriver, &gmode, "c:\\turboc3\\bgi");
printf("Enter co-ordinates of first point: ");
scanf("%d%d", &x0, &y0);
printf("Enter co-ordinates of second point: ");
scanf("%d%d", &x1, &y1);
drawline(x0, y0, x1, y1);
return 0;
}

21
OUTPUT:-

22
Practical 14: Graphics Inbuilt functions
I. Line Function

OUTPUT:

ii. The “bar” Function

OUTPUT:

23
Practical 15:
#include <conio.h>
#include <graphics.h>
#include <stdio.h>
void main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\""turboc3\\bgi");

setfillstyle(SOLID_FILL, BLUE);
floodfill(100, 100, 15);

setfillstyle(SOLID_FILL, RED);

circle(550, 200, 100);


floodfill(552, 202, 15);

setfillstyle(SOLID_FILL, WHITE);

line(650, 200, 630, 400);

line(650, 200, 620, 400);


line(620, 400, 630, 400);

floodfill(625, 398, 15);


line(450, 200, 470, 400);

line(450, 200, 480, 400);

24
line(470, 400, 480, 400);
floodfill(475, 398, 15);

setfillstyle(SOLID_FILL, BROWN);
rectangle(450, 400, 650, 500);
floodfill(452, 402, 15);

setfillstyle(XHATCH_FILL, YELLOW);

line(450, 430, 650, 430);


floodfill(452, 498, 15);
getch();
closegraph();
}

OUTPUT:

25

You might also like