Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

CGA Practicals Final2

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

Computer Graphics

Aim: -Study and enlist the basic functions used for graphics in C / C++ / Python language. Give an
example for each of them

CIRCLE FUNCTION

#include<graphics.h>
#include<conio.h>
void main()
{
intgd=DETECT,gm;
initgraph(&gd,&gm,"C:\\TC\\bgi");
circle(150,100,100);
getch();
closegraph();
}

Output:-

LINE FUNCTION

PROGRAM:
#include<graphics.h>
#include<conio.h>
void main()
{
intgd=DETECT,gm;
initgraph(&gd,&gm,"c:\\TC\\bgi");
line(100,100,200,200);
getch();
closegraph();
}

1
Computer Graphics

RECTANGULAR FUNCTION:

PROGRAM:
#include<graphics.h>
#include<conio.h>
void main()
{
intgd=DETECT,gm;
initgraph(&gd,&gm,"c:\\TC\\bgi");
rectangle(50,150,200,200);
getch();
closegraph();
}
Output: -

Aim : -Study and Enlist the basic Function used for moving the
graphics position.

#include<iostream.h>
#include<graphics.h>
#include<conio.h>
#include<dos.h>
{
intgd=DETECT,gm;
initgraph(&gd,&gm,"C:\\TC\\BGI");
setcolor(RED);
setbkcolor(WHITE);
for(inti=0;i<50;i++)
{
circle(100+i,100,50);
delay(200);
}
getch();
closegraph();
}

2
Computer Graphics

B. Aim:-Draw co-ordinates axis at the center of screen.

PROGRAM:

#include<dos.h>
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
void main()
{
intgd=DETECT,gm,a,b,c,d;
initgraph(&gd,&gm,"c:\\TC\\bgi");
a=getmaxx();
b=getmaxy();
c=a/2;
d=b/2;
line(c,0,c,b);
line(0,d,a,d);
getch();
closegraph();
}

3
Computer Graphics
2a)Study and enlist the basic functions used for graphics in C / C++ / Python language.
Give an example for each of them

#include<graphics.h>
#include<conio.h>
void main()
{
intgd=DETECT, gm;
initgraph(&gd,&gm,"c:\\TC\\bgi");
putpixel(50,50,100);
inta,b,c,d;
a=getmaxx();
c=a/2;
b=getmaxy();
d=b/2;
line(0,d,a,d);
line(c,0,c,b);
circle(450,350,20);
ellipse(450,150,0,360,25,40);
rectangle(100,100,200,200);
ellipse(150,350,1,180,50,40);
outtextxy(400,400,"Circle");
outtextxy(450,200,"Ellipse");
outtextxy(150,220,"Rectangle");
outtextxy(120,360,"Half Ellipse");
getch();
closegraph();
}

4
Computer Graphics

2B)Draw a simple hut on the screen


#include<graphics.h>
#include<conio.h>
intmain()
{
intgd=DETECT,gm;
initgraph(&gd, &gm, "c:\\TC\\bgi");
setcolor(WHITE);
rectangle(150,180,250,300);
rectangle(250,180,420,300);
rectangle(180,250,220,300);
line(200,100,150,180);
line(200,100,250,180);
line(200,100,370,100);
line(370,100,420,180);
setfillstyle(SOLID_FILL,GREEN);
floodfill(152,182,WHITE);
floodfill(252,182,WHITE);
setfillstyle(SLASH_FILL,BLUE);
floodfill(182,252,WHITE);
setfillstyle(HATCH_FILL,YELLOW);
floodfill(200,105,WHITE);
floodfill(210,105,WHITE);
getch();
closegraph();
return 0;
}

5
Computer Graphics
Aim: - Draw the following basic shapes in the center of the screen

Circle: -
#include<iostream.h>
#include<graphics.h>
#include<conio.h>
void main()
{
intgd=DETECT,gm;
intx,y,radius=80;
initgraph(&gd,&gm,"c:\\TC\\BGI");
x=getmaxx()/2;
y=getmaxy()/2;
circle(x,y,radius);
getch();
closegraph();
}

6
Computer Graphics
Ellipse: -

#include<iostream.h>
#include<graphics.h>
#include<conio.h>

voidmain(){
intgd = DETECT,gm;
intx ,y;
initgraph(&gd, &gm, "C:\\TC\\BGI");
x = getmaxx()/2;
y = getmaxy()/2;

ellipse(x, y, 0, 360, 120, 60);

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

7
Computer Graphics
Concentric Circle: -

#include<iostream.h>
#include<graphics.h>
#include<conio.h>
void main()
{
intgd = DETECT,gm;
intx ,y;
initgraph(&gd, &gm, "C:\\TC\\BGI");
x = getmaxx()/2;
y = getmaxy()/2;
setcolor(RED);
circle(x, y, 30);
setcolor(GREEN);
circle(x, y, 50);
setcolor(YELLOW);
circle(x, y, 70);
setcolor(BLUE);
circle(x, y, 90);

getch();
closegraph();
}

8
Computer Graphics
Line: -

#include<iostream.h>
#include<graphics.h>
#include<conio.h>

void main()
{
intgd = DETECT, gm;
int x1 = 320, y1 =300;
int x2 = 320, y2 =150;
initgraph(&gd, &gm, "c:\\tc\\bgi");
line(x1, y1, x2, y2);
getch();
closegraph();
}

9
Computer Graphics
Rectangle: -

#include<iostream.h>
#include<graphics.h>
#include<conio.h>
void main()
{
intgd = DETECT, gm;

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

rectangle(180,200,450,300);

getch();
closegraph();
}

10
Computer Graphics
Square: -
#include<iostream.h>
#include<graphics.h>
#include<conio.h>
void main()
{
intgd = DETECT, gm;

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

rectangle(250,180,370,300);

getch();
closegraph();
}

11
Computer Graphics
Aim: -Develop the program for DDA Line drawing algorithm

#include<dos.h>
#include<iostream.h>
#include<graphics.h>
#include<math.h>
#include<conio.h>
void main()
{
float x,y,x1,y1,x2,y2,dx,dy,length;
inti,gd=DETECT,gm;
cout<<"Enter the value of x1 :\t";
cin>>x1;
cout<<"Enter the value of y1 :\t";
cin>>y1;
cout<<"Enter the value of x2 :\t";
cin>>x2;
cout<<"Enter the value of y2 :\t";
cin>>y2;
initgraph(&gd,&gm,"c:\\TC\\BGI");
dx=abs(x2-x1);
dy=dbs(y2-y1);
if(dx >= dy)
{
length=dx;
}
else
{
length=dy;
}
dx=(x2-x1)/length;
dy=(y2-y1)/length;
x=x1+0.5;
y=y1+0.5;
i=1;
while(i<= length)
{
putpixel(x,y,15);
x=x+dx;
y=y+dy;
i=i+1;
delay(100);
}
getch();
closegraph();
}

12
Computer Graphics
Output: -

13
Computer Graphics

Aim: -Develop the program for Bresenham’s Line drawing algorithm.

#include<dos.h>
#include<iostream.h>
#include<graphics.h>
#include<math.h>
#include<conio.h>
void main()
{
float x,y,x1,y1,x2,y2,dx,dy,length;
inte,i,gd=DETECT,gm;
cout<<"Enter the value of x1 :\t";
cin>>x1;
cout<<"Enter the value of y1 :\t";
cin>>y1;
cout<<"Enter the value of x2 :\t";
cin>>x2;
cout<<"Enter the value of y2 :\t";
cin>>y2;
initgraph(&gd,&gm,"c:\\TC\\BGI");
dx=abs(x2-x1);
dy=abs(y2-y1);
x=x1;
y=y1;
e=2*dy-dx;
i=1;
do
{
putpixel(x,y,WHITE);
while(e >= 0)
{
y++;
e=e-2*dx;
putpixel(x,y,WHITE);
}
x++;
e=e+2*dy;
i++;
}
while(i<= dx);
getch();
closegraph();
}

14
Computer Graphics
Output: -

15
Computer Graphics
AIM: - Develop the program for the mid-point circle drawing algorithm

Program: -

#include<dos.h>
#include<iostream.h>
#include<graphics.h>
#include<conio.h>
#include<math.h>
void main()
{
float d;
intgd=DETECT,gm,x,y,r;
cout<<"Enter the radius of a circle :";
cin>>r;
initgraph(&gd,&gm,"c:\\TC\\BGI");
x=0;
y=r;
d=(5/4)-r;
do
{
putpixel(200+x,200+y,15);
putpixel(200+y,200+x,13);
putpixel(200+y,200-x,11);
putpixel(200+x,200-y,9);
putpixel(200-x,200-y,7);
putpixel(200-y,200-x,5);
putpixel(200-y,200+x,3);
putpixel(200-x,200+y,1);
if (d<0)
{
d=d+2*x+3;
}
else
{
d=d+2*(x-y)+5;
y=y-1;
}
x=x+1;
delay(10);
}
while(x<y);
getch();
closegraph();
}

Output: -

16
Computer Graphics

AIM: - Develop the program for the mid-point ellipse drawing algorithm

17
Computer Graphics
Program: -

#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
#include<dos.h>
void ellipse_midpoint(int,int,int,int);
void drawellipse(int,int,int,int);
intmain()
{
intxc,yc,rx,ry;
intgd=DETECT,gm;
initgraph(&gd,&gm,"c:\\TC\\BGI");
cout<<"Enter center coordinate of ellipse:";
cin>>xc>>yc;
cout<<"Enter radius of Ellipse(rx):";
cin>>rx;
cout<<"Enter radius of Ellipse(ry):";
cin>>ry;
ellipse_midpoint(xc,yc,rx,ry);
getch();
closegraph();
return 0;
}
void ellipse_midpoint(intxc,intyc,intrx,intry)
{
int x=0,y=ry,x2,y2;
float p1=(ry*ry)-(rx*rx*ry)+(rx*rx)/4;
int a=2*ry*ry*x;
int b=2*rx*rx*y;
while(a<=b)
{
drawellipse(xc,yc,x,y);
x++;
if(p1<0)
{
a=2*ry*ry*x;
p1=p1+a+(ry*ry);}
else
{
y--;
a=2*ry*ry*x;
b=2*rx*rx*y;
p1=p1+a-b+(ry*ry);
}
drawellipse(xc,yc,x,y);
delay(10);

18
Computer Graphics
}
float p2=((ry*ry)*(x+0.5)*(x+0.5))+((rx*rx)*(y-1)*(y-1))-((rx*rx)*(ry*ry));
a=0;
b=0;
while(y>=0)
{
drawellipse(xc,yc,x,y);
y--;
if(p2<0)
{
x++;
a=2*ry*ry*x;
b=2*rx*rx*y;
p2=p2+a-b+(rx*rx);
}
else
{
b=2*rx*rx*y;
p2=p2-b+(rx*rx);
}
drawellipse(xc,yc,x,y);
}
}
void drawellipse(intxc,intyc,intx,int y)
{
putpixel(xc+x,yc+y,15);
putpixel(xc-x,yc+y,WHITE);
putpixel(xc+x,yc-y,WHITE);
putpixel(xc-x,yc-y,15);
getch();
closegraph();
}

19
Computer Graphics
Write a program to perform 2D translation

Aim: -Translate a Line

Program:
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
#include<math.h>
void main()
{
intgd=DETECT,gm,x1,y1,x2,y2,x3,y3,x4,y4,tx,ty;
initgraph(&gd,&gm,"C:\\TC\\BGI");
cout<<"Enter x1 & y1";
cin>>x1>>y1;
cout<<"Enter x2 & y2";
cin>>x2>>y2;
setcolor(10);
line(x1,y1,x2,y2);
cout<<"Enter the translation in x & y direction";
cin>>tx>>ty;
x3=x1+tx;
y3=y1+ty;
x4=x2+tx;
y4=y2+ty;
line(x3,y3,x4,y4);
getch();
}

Output:

20
Computer Graphics
Aim: - Translate a Point

Program:-
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
#include<math.h>
void main()
{
intgd=DETECT,gm,x,y,x1,y1,tx,ty;
initgraph(&gd,&gm,"C:\\TC\\BGI");
cout<<"Enter x & y:";
cin>>x>>y;
putpixel(x,y,15);
cout<<"Enter the translation in x & y direction:";
cin>>tx>>ty;
x1=x+tx;
y1=y+ty;
putpixel(x1,y1,15);
getch();
}

Output:-

21
Computer Graphics
Write a program to implement 2D scaling.

Aim: - Scale a Polygon

Program: -
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
#include<math.h>
void main()
{
clrscr();
intgd=DETECT,gm,x1,y1,x2,y2,sx,sy,x3,y3,x4,y4,x5,y5,x6,y6;
initgraph(&gd,&gm,"C:\\TC\\BGI");
cout<<"Enter x1 & y1 values";
cin>>x1>>y1;
cout<<"Enter x2 & y2 values";
cin>>x2>>y2;
cout<<"Enter x3 & y3 values";
cin>>x3>>y3;
clrscr();
cout<<"Enter scale in x & y";
cin>>sx>>sy;
x4=x1*sx;
y4=y1*sy;
x5=x2*sx;
y5=y2*sy;
x6=x3*sx;
y6=y3*sy;
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
delay(100);
line(x4,y4,x5,y5);
line(x5,y5,x6,y6);
line(x6,y6,x4,y4);
getch();
}

Output: -

22
Computer Graphics
Aim: -Scale a Point

Program: -
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
#include<math.h>
void main()
{
intgd=DETECT,gm,x,y,x1,y1,sx,sy;
initgraph(&gd,&gm,"C:\\TC\\BGI");
cout<<"Enter x & y values";
cin>>x>>y;
putpixel(x,y,RED);
cout<<"Enter scale in x & y";
cin>>sx>>sy;
x1=x*sx;
y1=y*sy;
delay(100);
putpixel(x1,y1,RED);
getch();
}

23
Computer Graphics

Aim:-Write a program to implement Liang - Barsky Line Clipping Algorithm

Program:-
#include<stdio.h>
#include<graphics.h>
#include<math.h>
#include<dos.h>
void main()
{
inti,gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\TC\\BGI");
int x1,y1,x2,y2,xmin,xmax,ymin,ymax,xx1,xx2,yy1,yy2,dx,dy;
float t1,t2,p[4],q[4],temp;
x1=120;
y1=120;
x2=300;
y2=300;
xmin=100;
ymin=100;
xmax=250;
ymax=250;
rectangle(xmin,ymin,xmax,ymax);
dx=x2-x1;
dy=y2-y1;
p[0]=-dx;
p[1]=dx;
p[2]=-dy;
p[3]=dy;
q[0]=x1-xmin;
q[1]=xmax-x1;
q[2]=y1-ymin;
q[3]=ymax-y1;
for(i=0;i<4;i++)
{
if(p[i]==0)
{
print("line is parallel to one of the clipping boundary");
if(q[i]>=0)
{
if(i<2)
{
if(y1<ymin)
{
y1=ymin;
}
if(y2>ymax)
{

24
Computer Graphics
y2=ymax;
}
line(x1,y1,x2,y2);
}
if(i>1)
{
if(x1<xmin)
{
x1=xmin;
}
if(x2<xmax)
{
x2=xmax;
}
line(x1,y1,x2,y2);
}
}
}
}
t1=0;
t2=1;
for(i=0;i<4;i++)
{
temp=q[i]/p[i];
if(p[i]<0)
{
if(t1<=temp)
t1=temp;
}
else
{
if(t2>temp)
t2=temp;
}
}
if(t1<t2)
{
xx1=x1 + t1 * p[1];
xx2=x1 + t2 * p[1];
yy1=y1 + t1 * p[3];
yy2=y1 + t2 * p[3];
line(xx1,yy1,xx2,yy2);
}
delay(5000);
closegraph();
}

25
Computer Graphics

Output:

26
Computer Graphics
Aim:- Perform 2D Rotation on a given object

Program:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>

void rotate();

void main()
{
intch;
intgd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc\\bgi");

setcolor(6);
outtextxy (100,88,"Object.");
rectangle(100,150,150,100);

{
float theta;
int x1,x2,x3,x4;
int y1,y2,y3,y4;
int ax1,ax2,ax3,ax4,ay1,ay2,ay3,ay4;
intrefx,refy;
printf("\nEnter the angle for rotation: ");
scanf("%f",&theta);
theta=theta*(3.14/180);
cleardevice();
setcolor(2);
outtextxy(240,10,"ROTATE");
outtextxy(238,20,"-------");
refx=100;
refy=100;

x1=100;
y1=100;
x2=150;
y2=100;
x3=150;
y3=150;
x4=100;
y4=150;

ax1=refy+(x1-refx)*cos(theta)-(y1-refy)*sin(theta);
ay1=refy+(x1-refx)*sin(theta)+(y1-refy)*cos(theta);

27
Computer Graphics

ax2=refy+(x2-refx)*cos(theta)-(y2-refy)*sin(theta);
ay2=refy+(x2-refx)*sin(theta)+(y2-refy)*cos(theta);

ax3=refy+(x3-refx)*cos(theta)-(y3-refy)*sin(theta);
ay3=refy+(x3-refx)*sin(theta)+(y3-refy)*cos(theta);

ax4=refy+(x4-refx)*cos(theta)-(y4-refy)*sin(theta);
ay4=refy+(x4-refx)*sin(theta)+(y4-refy)*cos(theta);
rectangle(100,150,150,100);
line(ax1,ay1,ax2,ay2);
line(ax2,ay2,ax3,ay3);
line(ax3,ay3,ax4,ay4);
line(ax4,ay4,ax1,ay1);

getch();
}
}

Output:-

28
Computer Graphics

Aim:Program to create a house like figure and perform the following operations.
i. Scaling about the origin followed by translation.
ii. Scaling with reference to an arbitrary point.
iii. Reflect about the line y = mx + c.

Program:
#include <stdio.h>
#include <graphics.h>
#include <stdlib.h>
#include <math.h>
#include <conio.h>

void reset (inth[][2])


{
intval[9][2] = {
{ 50, 50 },{ 75, 50 },{ 75, 75 },{ 100, 75 },
{ 100, 50 },{ 125, 50 },{ 125, 100 },{ 87, 125 },{ 50, 100 }
};
inti;
for (i=0; i<9; i++)
{
h[i][0] = val[i][0]-50;
h[i][1] = val[i][1]-50;
}
}
void draw (inth[][2])
{
inti;
setlinestyle (DOTTED_LINE, 0, 1);
line (320, 0, 320, 480);
line (0, 240, 640, 240);
setlinestyle (SOLID_LINE, 0, 1);
for (i=0; i<8; i++)
line (320+h[i][0], 240-h[i][1], 320+h[i+1][0], 240-h[i+1][1]);
line (320+h[0][0], 240-h[0][1], 320+h[8][0], 240-h[8][1]);

29
Computer Graphics
}
void rotate (inth[][2], float angle)
{
inti;
for (i=0; i<9; i++)
{
intxnew, ynew;
xnew = h[i][0] * cos (angle) - h[i][1] * sin (angle);
ynew = h[i][0] * sin (angle) + h[i][1] * cos (angle);
h[i][0] = xnew; h[i][1] = ynew;
}
}
void scale (inth[][2], intsx, intsy)
{
inti;
for (i=0; i<9; i++)
{
h[i][0] *= sx;
h[i][1] *= sy;
}
}
void translate (inth[][2], int dx, intdy)
{
inti;
for (i=0; i<9; i++)
{
h[i][0] += dx;
h[i][1] += dy;
}
}
void reflect (inth[][2], int m, int c)
{
inti;
float angle;
for (i=0; i<9; i++)
h[i][1] -= c;
angle = M_PI/2 - atan (m);
rotate (h, angle);
for (i=0; i<9; i++)
h[i][0] = -h[i][0];
angle = -angle;
rotate (h, angle);
for (i=0; i<9; i++)
h[i][1] += c;
}

void ini()
{

30
Computer Graphics
intgd=DETECT,gm;
initgraph(&gd,&gm,"..\\bgi");
}
void dini()
{
getch();
closegraph();
}
void main()
{

int h[9][2],sx,sy,x,y,m,c,choice;
do
{
clrscr();
printf("1. Scaling about the origin.\n");
printf("2. Scaling about an arbitrary point.\n");
printf("3. Reflection about the line y = mx + c.\n");
printf("4. Exit\n");
printf("Enter the choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1: printf ("Enter the x- and y-scaling factors: ");
scanf ("%d%d", &sx, &sy);
ini();
reset (h);
draw (h);getch();
scale (h, sx, sy);
cleardevice();
draw (h);
dini();
break;

case 2: printf ("Enter the x- and y-scaling factors: ");


scanf ("%d%d", &sx, &sy);
printf ("Enter the x- and y-coordinates of the point: ");
scanf ("%d%d", &x, &y);
ini();
reset (h);
translate (h, x, y);// Go to arbitrary point
draw(h); getch();//Show its arbitrary position
cleardevice();
translate(h,-x,-y);//Take it back to origin
draw(h);
getch();
cleardevice();
scale (h, sx, sy);//Now Scale it

31
Computer Graphics
draw(h);
getch();
translate (h, x, y);//Back to Arbitrary point
cleardevice();
draw (h);
putpixel (320+x, 240-y, WHITE);
dini();
break;

case 3: printf ("Enter the values of m and c: ");


scanf ("%d%d", &m, &c);
ini();
reset (h);
draw (h); getch();
reflect (h, m, c);
cleardevice();
draw (h);
dini();
break;

case 4: exit(0);
}
}while(choice!=4);
}

Output:
Scaling about the origin followed by translation

Scaling with reference to an arbitrary point

32
Computer Graphics

Reflect about the line y = mx + c.

33
Computer Graphics
Reflection

34
Computer Graphics
Aim:- Write a program to implement Cohen-Sutherland clipping

Program:-
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<dos.h>
#include<math.h>
#include<graphics.h>
typedef struct coordinate
{
intx,y;
char code[4];
}PT;
void drawwindow();
void drawline(PT p1,PT p2,int c1);
PT setcode(PT p);
intvisibility(PT p1,PT p2);
PT resetendpt(PT p1,PT p2);
main()
{
intgd=DETECT,gm,v;
initgraph(&gd,&gm,"C:\\TC\\BGI");
PT p1,p2,ptemp;
cout<<"\n\n\t\tENTER END-POINT 1(x,y):";
cin>>p1.x>>p1.y;
cout<<"\n\n\t\tENTER END-POINT 2(x,y):";
cin>>p2.x>>p2.y;
cleardevice();
drawwindow();
getch();
drawline(p1,p2,15);
getch();
p1=setcode(p1);
p2=setcode(p2);
v=visibility(p1,p2);
switch(v)
{
case 0:
drawwindow();
drawline(p1,p2,15);
break;
case 1:
drawwindow();
break;
case 2:
p1=resetendpt(p1,p2);
p2=resetendpt(p2,p1);

35
Computer Graphics
drawwindow();
drawline(p1,p2,15);
break;
}
getch();
closegraph();
return(0);
}
void drawwindow()
{
setcolor(RED);
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,int c1)
{
setcolor(c1);
line(p1.x,p1.y,p2.x,p2.y);
}
PT setcode(PT p)
{
PT ptemp;
if(p.y<100)
ptemp.code[0]='1';
else
ptemp.code[0]='0';
if(p.y>350)
ptemp.code[1]='1';
else
ptemp.code[1]='0';
if(p.x>450)
ptemp.code[2]='1';
else
ptemp.code[2]='0';
if(p.x<150)
ptemp.code[3]='1';
else
ptemp.code[3]='0';
ptemp.x=p.x;
ptemp.y=p.y;
return(ptemp);
}
intvisibility(PT p1,PT p2)
{
inti,flag=0;
for(i=0;i<4;i++)

36
Computer Graphics
{
if((p1.code[i]!='0')||(p2.code[i]!='0'))
flag=1;
}
if(flag==0)
return(0);
for(i-0;i<4;i++)
{
if((pi.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;
intx,y,i;
float m,k;
if(p1.code[3]=='1')
x=150;
if(p1.code[2]=='1')
x=450;
if((pi.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;
for(i=0;i<4;i++)
temp.code[i]=p1.code[i];
return(temp);

37
Computer Graphics
}
else
return(p1);
}

Output:-

38
Computer Graphics

Aim:Write a program to fill a circle using Flood Fill Algorithm.

Program:
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
void flood_f(intx,inty,intoc, intnc)
{
if(getpixel(x,y)==oc)
{
putpixel(x,y,nc);
flood_f(x,y+1,oc,nc);
flood_f(x-1,y,oc,nc);
flood_f(x,y-1,oc,nc);
flood_f(x+1,y,oc,nc);
flood_f(x+1,y+1,oc,nc);
flood_f(x-1,y+1,oc,nc);
flood_f(x+1,y-1,oc,nc);
flood_f(x-1,y-1,oc,nc);
}
};
void main()
{
intgd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc\\bgi");
setcolor(9);
rectangle(50,50,100,100);
flood_f(60,60,00,14);
getch();
closegraph();
}

Output:

39
Computer Graphics

Aim:Write a program to fill a circle using Boundary Fill Algorithm.


Program:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<iostream.h>
#include<dos.h>
void boundryFill(int, int, int, int);
intmidx=319, midy=239;
void main()
{
intgd=DETECT,gm,x,y,r;
initgraph(&gdriver,&gmode,"c:\\tc\bgi");
cleardevice();
cout<<"Enter the Center of Circle(X,Y):";
cin>>x>>y;
cout<<"Enter the Radius of Circle R:";
cin>>r;
circle(midx+x,midy-y,r);
getch();
boundryFill(midx+x,midy-y,13,15);
getch();
closegraph();
}
void boundryFill(intx,inty,intfill,intboundry)
{
if((getpixel(x,y) != fill)&& (getpixel(x,y) !=boundry))
{
putpixel(x,y,fill);
delay(5);
boundryFill(x+1,y,fill,boundry);
boundryFill(x-1,y,fill,boundry);
boundryFill(x,y+1,fill,boundry);
boundryFill(x,y-1,fill,boundry);
}
}

OUTPUT:

40
Computer Graphics

Aim: Character Generation


Program:
#include<iostream.h>
#include<conio.h>
#include<graphics.h.
void main()
{
intgd,gm,i,j;
inta[13][9]={
{0,0,0,0,1,0,0,0,0},
{0,0,0,1,0,1,0,0,0},
{0,0,1,0,0,0,1,0,0},
{0,1,0,0,0,0,0,1,0}
{0,1,0,0,0,0,0,1,0}
{0,1,0,0,0,0,0,1,0}
{0,1,1,1,1,1,1,1,0}
{0,1,0,0,0,0,0,1,0}
{0,1,0,0,0,0,0,1,0}
{0,1,0,0,0,0,0,1,0}
{0,1,0,0,0,0,0,1,0}
{0,1,0,0,0,0,0,1,0}
{0,1,0,0,0,0,0,1,0}
};
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"c:\\tc\bgi");
for(i=0;i<13;i++)
{
for(j=0;j<9;j++)
{
putpixel(200+j,200+i,15*a[i][j]);
}
}
getch();
closegraph();
}

Output:-

41
Computer Graphics

Aim:- Develop a simple text screen saver using graphics functions

Program: #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),"Saish");
delay(250);
}
closegraph();
}

OutPut:

42
Computer Graphics

Aim: Perform smiling face animation using graphic functions.

Program: #include<graphics.h>
#include<conio.h>
#include<stdlib.h>
#include<dos.h>
#include<iostream.h>

main()
{
intgd = DETECT, gm, area, temp1, temp2, left = 25, top = 75;
void *p;

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

setcolor(YELLOW);
circle(50,100,25);
setfillstyle(SOLID_FILL,YELLOW);
floodfill(50,100,YELLOW);

setcolor(BLACK);
setfillstyle(SOLID_FILL,BLACK);
fillellipse(44,85,2,6);

43
Computer Graphics
fillellipse(56,85,2,6);

ellipse(50,100,205,335,20,9);
ellipse(50,100,205,335,20,10);
ellipse(50,100,205,335,20,11);

area = imagesize(left, top, left + 50, top + 50);


p = malloc(area);

setcolor(WHITE);
settextstyle(SANS_SERIF_FONT,HORIZ_DIR,2);
outtextxy(155,451,"Smiling Face Animation");

setcolor(BLUE);
rectangle(0,0,639,449);

while(!kbhit())
{
temp1 = 1 + random ( 588 );
temp2 = 1 + random ( 380 );

getimage(left, top, left + 50, top + 50, p);


putimage(left, top, p, XOR_PUT);
putimage(temp1 , temp2, p, XOR_PUT);
delay(500);
left = temp1;
top = temp2;
}

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

Output:

Aim: Draw the moving car on the screen

Program: #include<iostream.h>

44
Computer Graphics
#include<conio.h>
#include<graphics.h>
#include<dos.h>

void main()
{
clrscr();
intgd=DETECT,gm;
initgraph(&gd,&gm,"C:\\tc\\bgi");

for (inti=0;i<500;i++)
{
/***CAR BODY ******/
setcolor(YELLOW);
line(50+i,370,90+i,370);
arc(110+i,370,0,180,20);
line(130+i,370,220+i,370);
arc(240+i,370,0,180,20);
line(260+i,370,300+i,370);
line(300+i,370,300+i,350);
line(300+i,350,240+i,330);
line(240+i,330,200+i,300);
line(200+i,300,110+i,300);
line(110+i,300,80+i,330);
line(80+i,330,50+i,340);
line(50+i,340,50+i,370);

/***CAR Windows***/
setcolor(RED);
line(165+i,305,165+i,330);
line(165+i,330,230+i,330);
line(230+i,330,195+i,305);
line(195+i,305,165+i,305);

line(160+i,305,160+i,330);
line(160+i,330,95+i,330);
line(95+i,330,120+i,305);
line(120+i,305,160+i,305);

/**Wheels**/
setcolor(GREEN);
circle(110+i,370,17);
circle(240+i,370,17);

delay(10);
cleardevice();

45
Computer Graphics
setcolor(RED);
line(0,390,639,390); //ROAD
}
getch();
}

OutPut:

46
Computer Graphics

Aim : Firework Animation For Shiv Jayanti

Program:
#include<conio.h>
#include<graphics.h>
#include<stdio.h>
#include<math.h>
#include<dos.h>
void main()
{
intgd,gm;
intx,y;
inti,j,kk;

detectgraph(&gd,&gm);
initgraph(&gd,&gm,"c:\\tc\\bgi");

setcolor(WHITE);
line(0,400,640,400);
rectangle(300,330,340,400);
rectangle(310,320,330,330);
setcolor(4);
line(319,280,319,398);
line(320,280,320,398);
rectangle(320,280,330,300);
outtextxy(340,280,"PRESS ANY KEY TO LAUNCH THE ROCKET");
getch();
for(j=400;j<640;j++)
{
cleardevice();
setcolor(WHITE);
line(0,j,640,j);
rectangle(300,j-70,340,j);
rectangle(310,j-80,330,j-70);

setcolor(RED);
line(319,280,319,400);
line(320,280,320,400);
rectangle(320,280,330,300);

setcolor(YELLOW);
circle(325,300,2);

delay(5);
}

47
Computer Graphics
for(i=400;i>340;i--)
{
cleardevice();

setcolor(RED);
line(319,i,319,i-120);
line(320,i,320,i-120);
rectangle(320,i-120,330,i-100);

setcolor(YELLOW);
circle(325,i-100,2);
delay(25);
}

cleardevice();
kk=0;
for(j=100;j<350;j++)
{
if(j%20==0)
{
setcolor(kk);
kk=kk+3;
delay(50);
}
ellipse(320,30,0,360,j+100,j+0);
}
for(j=100;j<350;j++)
{
if(j%20==0)
{
setcolor(BLACK);
delay(2);
}
ellipse(320,30,0,360,j+100,j+0);
}
cleardevice();
for(i=0;i<70;i++)
{
setcolor(i);
settextstyle(EUROPEAN_FONT,HORIZ_DIR,3);
outtextxy(50,150,"HAPPY SHIV JAYANTI");
outtextxy(90,200,"To All Of You");
delay(90);
}
getch();
}

48
Computer Graphics

Output:

49

You might also like