You are on page 1of 31

Ex. No.

: 1
01-12-2009

BRESENHAMS

LINE DRAWING ALGORITHM

AIM : To implement Bresenhams Line Drawing Algorithm.

ALGORITHM :
Step1:Start the process.
Step:Stop the process.
PROGRAM:
#include<iostream.h>
#include<graphics.h>
#include<conio.h>
#include<math.h>
#include<dos.h>
#include<stdlib.h>
#include<stdio.h>
class lines
{
private:
int length,x1,y1,x2,y2,x,y,dx,dy,wx,wy,w,width;
public:
lines();
void showline();
int sign(int);
};
int lines::sign(int xx)
{
if(xx<0)
return-1;
if(xx==0)
return 0;
if(xx>0)
return 1;
return 0;
}
lines::lines()
{
x=0;y=0;
cout<<"Enter The Co_Ordinates(x1,y1):=";
cin>>x1>>y1;
cout<<"Enter The Co_ordinates(x2,y2):=";
cin>>x2>>y2;
cout<<"Enter The Width Of The Line:=";
cin>>width;

Page 1 of 31

}
void lines::showline()
{
char *s;
int s1,s2,ic;
float temp;
x=x1;y=1;
w=width;
if(y2-y1)
wx=((w-1)/2)*(sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1))/abs(y2-y1));
if(x2-x1)
wy=((w-1)/2)*(sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1))/abs(x2-x1));
dx=abs(x2-x1);
dy=abs(y2-y1);
s1=sign(x2-x1);
s2=sign(y2-y1);
if(dy>dx)
wy=wx;
if(dy>dx)
{
temp=dy;
dy=dx;
dx=temp;
ic=1;
}
else
ic=0;
float d=2*dy-dx;
setcolor(0);
for(int i=1;i<=dx;i++)
{
for(int j=0;j<wy;j++)
putpixel((x+320),480-(y+240+j),5);
for(j=0;j<wy;j++)
putpixel((x+320),480-(y+240-j),5);
putpixel((x+320),480-(y+240),5);
while(d>=0)
{
if(ic==1)
x+=s1;
else
y+=s2;
d-=2*dx;
}
if(ic==1)
y+=s2;
else
x+=s1;
d+=2*dy;
}
setcolor(15);
outtextxy(20,10,"The Points Are:=");
sprintf(s,"A(%d,%d)",x1,y1);
outtextxy(20,20,s);
sprintf(s,"B(%d,%d)",x2,y2);
outtextxy(20,30,s);
getch();
}
void main()
{
int gd=DETECT,gm,i,j,xx=200,xxx=470;

Page 2 of 31

clrscr();
lines a;
char
*mess[]={"B","R","E","S","E","N","H","A","M","","S","","L","I","N","E
","","A","L","G","O","R","I","T","H","M"};
initgraph(&gd,&gm,"..\bgi");
cleardevice();
rectangle(120,40,320,240);
rectangle(320,40,520,240);
rectangle(120,240,320,440);
rectangle(320,240,520,440);
for(i=0,j=25;i<14,j>=12;i++,j--)
{
xx+=10;
outtextxy(xx,10,mess[i]);
xxx-=10;
outtextxy(xxx,10,mess[j]);
delay(100);
}
for(i=130;i<=510;i+=10)
for(j=50;j<=430;j+=10)
putpixel(i,j,15);
for(i=130;i<=510;i+=10)
{
if(i==320)
continue;
outtextxy(i,237,"+");
}
for(i=50;i<=430;i+=10)
{
if(i==240)
continue;
outtextxy(317,i,"-");
}
outtextxy(310,230,"O");
outtextxy(530,240,"X");
outtextxy(320,450,"-Y");
outtextxy(100,240,"-X");
outtextxy(320,30,"Y");
a.showline();
closegraph();
}

Page 3 of 31

Output

RESULT:
The program was successfully executed and the output was verified.

Page 4 of 31

Ex. No. : 2
01-12-2009

BRESENHAMS CIRCLE DRAWING ALGORITHM


AIM : To implement Bresenhams circle drawing algorithm.
ALGORITHM :
Step1:Start the process.
Step:Stop the process.
PROGRAM :
#include<iostream.h>
#include<graphics.h>
#include<conio.h>
#include<math.h>
#include<dos.h>
#include<stdlib.h>
#include<stdio.h>
class myCircle
{
private:
int x,y,r,d,x1,y1;
public:
myCircle();
void showCircle();
};
myCircle::myCircle()
{
x=0;y=0;
cout<<"Enter The Co_Ordinate Of The Circle:=";
cin>>x>>y;
cout<<"Enter The Radius Of The Circle:=";
cin>>r;
}
void myCircle::showCircle()
{
char *s;
x1=0;y1=r;
d=3-2*r;
while(x1<=y1)
{
putpixel((x+x1+320),(y+y1+240),5);
putpixel((x-x1+320),(y+y1+240),5);
putpixel((x+x1+320),(y-y1+240),5);
putpixel((x-x1+320),(y-y1+240),5);
putpixel((x+y1+320),(y+x1+240),5);
putpixel((x-y1+320),(y+x1+240),5);
putpixel((x+y1+320),(y-x1+240),5);
putpixel((x-y1+320),(y-x1+240),5);

Page 5 of 31

if(d<0)
d+=4*x1+6;
else
{
d+=4*(x1-y1)+10;
y1--;
}
x1++;
}
setcolor(5);
outtextxy(318+x,235+y,".");
setcolor(15);
sprintf(s,"Center(%d,%d)",x,y);
outtextxy(20,10,"The Center Is At");
outtextxy(20,20,s);
sprintf(s,"Radius=%d",r);
outtextxy(20,30,s);
getch();
}
void main()
{
int gd=DETECT,gm,i,j,xx=190,xxx=480;
clrscr();
myCircle a;
char
*mess[]={"B","R","E","S","E","N","H","A","M","","S","","C","I","R","C
","L","E","","A","L","G","O","R","I","T","H","M"};
initgraph(&gd,&gm,"..\bgi");
cleardevice();
rectangle(120,40,320,240);
rectangle(320,40,520,240);
rectangle(120,240,320,440);
rectangle(320,240,520,440);
for(i=0,j=27;i<16,j>=14;i++,j--)
{
xx+=10;
outtextxy(xx,10,mess[i]);
xxx-=10;
outtextxy(xxx,10,mess[j]);
delay(100);
}
for(i=130;i<=510;i+=10)
for(j=50;j<=430;j+=10)
putpixel(i,j,15);
for(i=130;i<=510;i+=10)
{
if(i==320)
continue;
outtextxy(i,237,"+");
}
for(i=50;j<=430;i+=10)
{
if(i==240)
continue;
outtextxy(317,i,"-");
}
outtextxy(310,230,"O");
outtextxy(530,240,"X");
outtextxy(320,450,"-Y");
outtextxy(100,240,"-X");
outtextxy(320,30,"Y");

Page 6 of 31

a.showCircle();
//closegraph();
}

Page 7 of 31

Output :

Page 8 of 31

RESULT : The program was successfully executed and the output was verified.

Page 9 of 31

Ex. No. : 3
02-12-2009

DDA LINE DRAWING ALGORITHM


AIM : To implement DDA line drawing algorithm.
ALGORITHM :
Step1:Start the process.
Step:Stop the process.
PROGRAM :

#include<iostream.h>
#include<graphics.h>
#include<conio.h>
#include<math.h>
#include<dos.h>
#include<stdlib.h>
#include<stdio.h>
class lines
{
private:
int length,x1,y1,x2,y2,x,y,dx,dy,wx,wy,w,width;
public:
lines();
void showline();
int sign(int);
};
int lines::sign(int xx)
{
if(xx<0)
return-1;
if(xx==0)
return 0;
if(xx>0)
return 1;
return 0;
}
lines::lines()
{
x=0;y=0;
cout<<"Enter The Co_Ordinates(x1,y1):=";
cin>>x1>>y1;
cout<<"Enter The Co_ordinates(x2,y2):=";
cin>>x2>>y2;
cout<<"Enter The Width Of The Line:=";
cin>>width;
}

Page 10 of 31

void lines::showline()
{
char *s,*s1;
if(abs(x2-x1)>=abs(y2-y1))
length=abs(x2-x1);
else
length=abs(y2-y1);
w=width;
wx=((w-1)/2)*(sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1))/abs(y2-y1));
wy=((w-1)/2)*(sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1))/abs(x2-x1));
dx=abs(x2-x1)/length;
dy=abs(y2-y1)/length;
if(dy>dx)
wy=wx;
x=x1+0.5*sign(dx);
y=y1+0.5*sign(dy);
int i=1;
setcolor(0);
while(i<=length)
{
for(int j=0;j<wy;j++)
putpixel((x+320),480-(y+240+j),6);
for(j=0;j<wy;j++)
putpixel((x+320),480-(y+240-j),6);
putpixel((x+320),480-(y+240),6);
x+=dx;
y+=dy;
i++;
}
setcolor(15);
outtextxy(40,10,"The Points Are:=");
sprintf(s,"A(%d,%d)",x1,y1);
outtextxy(20,20,s);
sprintf(s,"B(%d,%d)",x2,y2);
outtextxy(20,30,s);
getch();
}
void main()
{
int gd=DETECT,gm,i,j,xx=200,xxx=380;
clrscr();
lines a;
char *mess[]={"D","D","A","","A","L","G","O","R","I","T","H","M"};
initgraph(&gd,&gm,"..\bgi");
cleardevice();
rectangle(120,40,320,240);
rectangle(320,40,520,240);
rectangle(120,240,320,440);
rectangle(320,240,520,440);
for(i=0,j=12;i<8,j>=6;i++,j--)
{
xx+=10;
outtextxy(xx,10,mess[i]);
xxx-=10;
outtextxy(xxx,10,mess[j]);
delay(100);
}
for(i=130;i<=510;i+=10)
for(j=50;j<=430;j+=10)
putpixel(i,j,15);
for(i=130;i<=510;i+=10)

Page 11 of 31

{
if(i==320)
continue;
outtextxy(i,237,"+");
}
for(i=50;i<=430;i+=10)
{
if(i==240)
continue;
outtextxy(317,i,"-");
}
outtextxy(310,230,"O");
outtextxy(530,240,"X");
outtextxy(320,450,"-Y");
outtextxy(100,240,"-X");
outtextxy(320,30,"Y");
a.showline();
closegraph();
}

OUTPUT:

RESULT : The program was successfully executed and the output was verified.

Page 12 of 31

Ex. No. : 4
02-12-2009

POLYGON FILLING ALGORITHM


AIM : To implement polygon filling algorithm.
ALGORITHM :
Step1:Start the process.
Step:Start the process.

PROGRAM :
#include<stdio.h>
#include<conio.h>
#include<iostream.h>
#include<graphics.h>
#include<dos.h>
#include<stdlib.h>
int gd=DETECT,gm;
class myStack
{
private:
int arr[10000][2],top;
public:
myStack()
{
top=-1;
}
void push(int x,int y)
{
if(top>10000)
{
printf("a Stack Full...:-("")");
exit(0);
}
top++;
arr[top][0]=x;
arr[top][1]=y;
}
void pop(int &x,int &y)
{
x=arr[top][0];
y=arr[top][1];
top--;
}
int empty(void)
{
if(top==-1)

Page 13 of 31

return(1);
else return(0);
}
};
void show_quadrant()
{
cleardevice();
rectangle(120,40,320,240);
rectangle(320,40,520,240);
rectangle(120,240,320,440);
rectangle(320,240,520,440);
for(int i=130;i<=510;i+=10)
{
if(i==320)
continue;
outtextxy(i,237,"+");
}
for(i=50;i<=430;i+=10)
{
if(i==240)
continue;
outtextxy(317,i,"-");
}
outtextxy(310,230,"O");
outtextxy(530,240,"X");
outtextxy(320,450,"-Y");
outtextxy(100,240,"-X");
outtextxy(320,30,"Y");
}
int get_poly(int ed[20])
{
int edg,i,j;
clearviewport();
closegraph();
cout<<"Enter No.OfEdges:=";
cin>>edg;
for(i=0,j=1;i<2*edg;i+=2,j++)
{
cout<<"Enter Vertex No."<<j<<":=";
flushall();
cin>>ed[i]>>ed[i+1];
ed[i]+=320;
ed[i+1]=240-ed[i+1];
}
ed[i]=ed[0];
ed[i+1]=ed[1];
initgraph(&gd,&gm,"..\bgi");
clearviewport();
show_quadrant();
drawpoly(edg+1,ed);
return(edg);
}
void get_y(int &min,int &max,int edge[20],int size)
{
int i;
min=480;
max=0;
for(i=1;i<2*size;i+=2)
{
if(edge[i]>max)
max=edge[i];

Page 14 of 31

if(edge[i]<min)
min=edge[i];
}
}
void get_x(int &min,int &max,int edge[20],int size)
{
int i;
min=480;
max=0;
for(i=0;i<2*size;i+=2)
{
if(edge[i]>max)
max=edge[i];
if(edge[i]<min)
min=edge[i];
}
}
void fill_polygon(int x,int y,int fg,int bg)
{
myStack stack;
int col;
putpixel(x,y,fg);
stack.push(x,y);
while(!stack.empty())
{
stack.pop(x,y);
col=getpixel(x,y);
if(col!=bg&&col!=fg)
putpixel(x,y,fg);
col=getpixel(x,y+1);
if(col!=bg&&col!=fg)
stack.push(x,y+1);
col=getpixel(x+1,y);
if(col!=bg&&col!=fg)
stack.push(x+1,y);
col=getpixel(x-1,y);
if(col!=bg&&col!=fg)
stack.push(x-1,y);
col=getpixel(x,y-1);
if(col!=bg&&col!=fg)
stack.push(x,y-1);
}
}
void flood_fill(int ed[20])
{
int i,j,x,y,num,bg,fg,col,k;
clearviewport();
closegraph();
cout<<"Enter No Of Edges:=";
cin>>num;
for(i=0,k=1;i<2*num;i+=2,k++)
{
cout<<"Enter The Vertex No"<<k<<":=";
flushall();
cin>>ed[j]>>ed[i+1];
ed[i]+=320;
ed[i+1]=240-ed[i+1];
}
ed[i]=ed[0];
ed[i+1]=ed[1];
cout<<"Enter The Seed Point(x,y):=";

Page 15 of 31

cin>>x>>y;
x+=320;
y=240-y;
initgraph(&gd,&gm,"..\bgi");
cleardevice();
show_quadrant();
setcolor(1);
drawpoly(num+1,ed);
fill_polygon(x,y,15,1);
}
int check_mid(int i,int ed[20],int k)
{
int max,min;
if(ed[k+1]>ed[k+3])
{
max=ed[k+1];
min=ed[k+3];
}
if(ed[k+1]<ed[k+3])
{
max=ed[k+3];
min=ed[k+1];
}
if(i>min&&i<max)
return(1);
else
return(0);
}
void scan_poly(int ed[20],int num)
{
int i,j,k,xmax,xmin,ymax,ymin,p;
void sort(float xi[10],int n);
float xi[10];
get_y(ymin,ymax,ed,num);
get_x(xmin,xmax,ed,num);
for(i=ymin;i<=ymax;i++)
{
p=0;
for(k=0;k<2*num;k+=2)
{
if(ed[k+1]==ed[k+3])
continue;
xi[p]=ed[k]+((double)((double)(i-ed[k+1]/(ed[k+1]-ed[k+3]))*(ed[k]ed[k+2])));
if(xi[p]>=xmin&&xi[p]<=xmax)
p++;
}
sort(xi,p);
for(j=0;j<p;j+=2)
line(xi[j],i,xi[j+1],i);
}
}
void sort(float xi[10],int n)
{
int i,j;
for(i=0;i<n-1;i++)
{
for(j=0;j<n-1;j++)
{
if(xi[j]>xi[j+1])
{

Page 16 of 31

float temp;
temp=xi[j];
xi[j]=xi[j+1];
xi[j+1]=temp;
}
}
}
}
void edge_fill(int ed[20],int num)
{
int i,j,k,xmax,xmin,ymax,ymin,col;
double xint;
get_y(ymin,ymax,ed,num);
get_x(xmin,xmax,ed,num);
for(k=0;k<2*num;k+=2)
{
for(i=ymin;i<=ymax;i++)
{
if(ed[k+1]==ed[k+3])
continue;
if(!check_mid(i,ed,k))
continue;
xint=ed[k]+(((double)(i-ed[k+1])/(ed[k+1]-ed[k+3]))*(ed[k]-ed[k+2]));
for(j=xmin;j<=xmax;j++)
{
if(j>xint)
{
col=getpixel(j,i);
if(col==15)
putpixel(j,i,0);
if(col==0)
putpixel(j,i,15);
}
}
}
}
}
void main()
{
clrscr();
char*mess[]={"-","=","[","","P","o","l","y","g","o","n","","F","i","l
","l","i","n","g","","]","=","-",};
int xx=28,xxx=51,i,j;
_setcursortype(_NOCURSOR);
for(i=0,j=22;i<13,j>=11;i++,j--)
{
gotoxy(xx,1);
cout<<mess[i];
xx++;
gotoxy(xxx,1);
cout<<mess[j];
xxx--;
delay(50);
}
xx=30;xxx=49;
int choice,ed[20],num;
_setcursortype(_NORMALCURSOR);
cout<<"1:==> Flood Fill";
cout<<"2:==> Ordered Edge List Fill";
cout<<"3:==> Edge Fill";
cout<<"4:==> Exit";

Page 17 of 31

cout<<"Enter Your Choice:=";


cin>>choice;
initgraph(&gd,&gm,"..\bgi");
clearviewport();
switch(choice)
{
case 1:
flood_fill(ed);
getch();
break;
case 2:
num=get_poly(ed);
scan_poly(ed,num);
getch();
break;
case 3:
num=get_poly(ed);
edge_fill(ed,num);
getch();
break;
case 4:
exit(0);
default:
cout<<"a Press A Valid Key...!!!";
getch();
main();
break;
}
closegraph();
}

Output :

RESULT : The program was successfully executed and the output was verified.

Page 18 of 31

Ex. No. : 5
03-12-2009

LINE CLIPPING ALGORITHM


AIM : To implement line clipping algorithm.
ALGORITHM :
Step1:Start the process.
Step:Start the process.

PROGRAM:
#include<iostream.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
#include<dos.h>
#include<process.h>
int pixels[2][4];
float xn1,xn2,yn1,yn2,x3,y3,m;
void show_quadrant()
{
cleardevice();
rectangle(120,40,320,240);
rectangle(320,40,520,240);
rectangle(120,240,320,440);
rectangle(320,240,520,440);
for(int i=130;i<=510;i+=10)
{
if(i==320)
continue;
outtextxy(i,237,"+");
}
for(i=50;i<=430;i+=10)
{
if(i==240)
continue;
outtextxy(317,i,"-");
}
outtextxy(310,230,"O");
outtextxy(530,240,"X");
outtextxy(320,450,"-Y");
outtextxy(100,240,"-X");
outtextxy(320,30,"Y");
}
void su_co(int x1,int y1,int x2,int y2,int xmin,int ymin,int xmax,int
ymax)
{
int i,j,fl;
for(i=0;i<2;i++)

Page 19 of 31

for(j=0;j<4;j++)
pixels[i][j]=0;
if(y1>ymax)
pixels[0][0]=1;
if(y1<ymin)
pixels[0][1]=1;
if(x1>xmax)
pixels[0][2]=1;
if(x1<xmin)
pixels[0][3]=1;
if(y2>ymax)
pixels[1][0]=1;
if(y2<ymin)
pixels[1][1]=1;
if(x2>xmax)
pixels[1][2]=1;
if(x2<xmin)
pixels[1][3]=1;
for(j=0;j<4;j++)
{
if(pixels[0][j]==0&&pixels[1][j]==0)
continue;
if(pixels[0][j]==1&&pixels[1][j]==1)
{
fl=3;
break;
}
fl=2;
}
switch(fl)
{
case 1:
line(320+x1,240-y1,320+x2,240-y2);
break;
case 3:
cout<<"a Line Is Not Visible...:-(";
break;
case 2:
m=(y2-y1)/(x2-x1);
xn1=x1;
yn1=y1;
xn2=x2;
yn2=y2;
if(pixels[0][0]==1)
{
xn1=x1+(ymax-y1)/m;
yn1=ymax;
}
if(pixels[0][1]==1)
{
xn1=x1+(ymin-y1)/m;
yn1=ymin;
}
if(pixels[0][2]==1)
{
yn1=y1+(xmax-x1)*m;
xn1=xmax;
}
if(pixels[0][3]==1)
{
yn1=y1+(xmin-x1)*m;

Page 20 of 31

xn1=xmin;
}
if(pixels[1][0]==1)
{
xn2=x2+(ymax-y2)/m;
yn2=ymax;
}
if(pixels[1][1]==1)
{
xn2=x2+(ymin-y2)/m;
yn2=ymin;
}
if(pixels[1][2]==1)
{
yn2=y2+(xmax-x2)*m;
xn2=xmax;
}
if(pixels[1][3]==1)
{
yn2=y2+(xmin-x2)*m;
xn2=xmin;
}
line(320+xn1,240-yn1,320+xn2,240-yn2);
break;
}
}
void midpt(int x1,int y1,int x2,int y2,int xmin,int ymin, int
xmax,int ymax)
{
int fl=1;
int i,j;
int ox1=x1,ox2=x2,oy1=y1,oy2=y2;
for(i=0;i<2;i++)
for(j=0;j<4;j++)
pixels[i][j]=0;
if(y1>ymax)
pixels[0][0]=1;
if(y1<ymin)
pixels[0][1]=1;
if(x1>xmax)
pixels[0][2]=1;
if(x1<xmin)
pixels[0][3]=1;
if(y2>ymax)
pixels[1][0]=1;
if(y2<ymin)
pixels[1][1]=1;
if(x2>xmax)
pixels[1][2]=1;
if(x2<xmin)
pixels[1][3]=1;
for(j=0;j<4;j++)
{
if(pixels[0][j]==0&&pixels[1][j]==0)
continue;
if(pixels[0][j]==1&&pixels[1][j]==1)
{
fl=3;
break;
}
fl=2;

Page 21 of 31

}
switch(fl)
{
case 1:
line(320+x1,240-y1,320+x2,240-y2);
break;
case 3:
cout<<"a Line Is Not Visible...:-(";
break;
case 2:
xn1=x1;
yn1=y1;
xn2=x2;
yn2=y2;
fl=0;
x3=x1;
y3=y1;
while(1)
{
if(!(y1>ymax||y1<ymin||x1>xmax||x1<xmin)&&(x3||y3)!=0.1)
break;
x3=(x1+x2)/2;
y3=(y1+y2)/2;
if(!(y3>ymax||y3<ymin||x3>xmax||x3<xmin))
fl=1;
else
fl=0;
if(fl)
{
x2=x3;
y2=y3;
}
else
{
x1=x3;
y1=y3;
}
}
xn1=x3;
yn1=y3;
fl=0;
x1=ox1;
x2=ox2;
y1=oy1;
y2=oy2;
x3=x2;
y3=y2;
while(1)
{
if(!(y2>ymax||y2<ymin||x2>xmax||x2<xmin)&&(x3||y3)!=0.1)
break;
x3=(x1+x2)/2;
y3=(y1+y2)/2;
if(!(y3>ymax||y3<ymin||x3>xmax||x3<xmin))
fl=1;
else
fl=0;
if(fl)
{
x1=x3;
y1=y3;

Page 22 of 31

}
else
{
x2=x3;
y2=y3;
}
}
xn2=x3;
yn2=y3;
line(320+xn1,240-yn1,320+xn2,240-yn2);
break;
}
}
void show_message()
{
char*mess[]={"-","=","[","","L","i","n","e","","C","l","i","p","p","i
","n","g","","]","=","-",};
int xx=29,xxx=50,i,j;
_setcursortype(_NOCURSOR);
for(i=0,j=21;i<13,j>=11;i++,j--)
{
gotoxy(xx,1);
cout<<mess[i];
xx++;
gotoxy(xxx,1);
cout<<mess[j];
xxx--;
delay(50);
}
_setcursortype(_NORMALCURSOR);
}
void main()
{
clrscr();
int gd=DETECT,gm,i,j;
int xmin,ymin,xmax,ymax,x1,y1,x2,y2;
int choice,ed[20],num;
show_message();
cout<<"Enter The Co-Ordinates Of The Clipping Window.";
cout<<"Enter X(min)& Y(min):=";
cin>>xmin>>ymin;
cout<<"Enter X(max)& Y(max):=";
cin>>xmax>>ymax;
cout<<"Enter The Co-Ordinates Of The Line.";
cout<<"Enter X(1)& Y(1):=";
cin>>x1>>y1;
cout<<"Enter X(2)& Y(2):=";
cin>>x2>>y2;
clrscr();
show_message();
cout<<"1:==> Sutherland-Cohen";
cout<<"2:==> Mid-Point Method";
cout<<"3:==> Exit";
cout<<"Enter Your Choice:=";
cin>>choice;
switch(choice)
{
case 1:
initgraph(&gd,&gm,"..\bgi");
clearviewport();
show_quadrant();

Page 23 of 31

line(320+xmin,240-ymin,320+xmin,240-ymax);
line(320+xmin,240-ymax,320+xmax,240-ymax);
line(320+xmax,240-ymax,320+xmax,240-ymin);
line(320+xmax,240-ymin,320+xmin,240-ymin);
line(320+x1,240-y1,320+x2,240-y2);
getch();
cleardevice();
show_quadrant();
line(320+xmin,240-ymin,320+xmin,240-ymax);
line(320+xmin,240-ymax,320+xmax,240-ymax);
line(320+xmax,240-ymax,320+xmax,240-ymin);
line(320+xmax,240-ymin,320+xmin,240-ymin);
su_co(x1,y1,x2,y2,xmin,ymin,xmax,ymax);
getch();
break;
case 2:
initgraph(&gd,&gm,"..\bgi");
clearviewport();
show_quadrant();
line(320+xmin,240-ymin,320+xmin,240-ymax);
line(320+xmin,240-ymax,320+xmax,240-ymax);
line(320+xmax,240-ymax,320+xmax,240-ymin);
line(320+xmax,240-ymin,320+xmin,240-ymin);
line(320+x1,240-y1,320+x2,240-y2);
getch();
cleardevice();
show_quadrant();
line(320+xmin,240-ymin,320+xmin,240-ymax);
line(320+xmin,240-ymax,320+xmax,240-ymax);
line(320+xmax,240-ymax,320+xmax,240-ymin);
line(320+xmax,240-ymin,320+xmin,240-ymin);
midpt(x1,y1,x2,y2,xmin,ymin,xmax,ymax);
getch();
break;
case 3:
exit(0);
default:
cout<<"a Press A Valid Key...!!!";
getch();
main();
break;
}
closegraph();
}

Page 24 of 31

Output :

RESULT : The program was successfully executed and the output was verified.

Page 25 of 31

Ex. No. : 6
04-12-2009

3-D TRANSFORMATION
AIM : Program for 3-D Transformation.
ALGORITHM :
Step1:Start the process.
Step:Stop the process.
PROGRAM :
#include<iostream.h>
#include<dos.h>
#include<stdio.h>
#include<math.h>
#include<conio.h>
#include<graphics.h>
#include<process.h>
int gd=DETECT,gm;
double x1,x2,y1,y2;
void show_message()
{
char
*mess[]={"-","=","[","","3","D","-","T","r","a","n","s","f","o","r","
m","a","t","i","o","n","","]","=","-"};
int xx=28,xxx=52,i,j;
_setcursortype(_NOCURSOR);
for(i=0,j=24;i<15,j>=12;i++,j--)
{
gotoxy(xx,1);
cout<<mess[i];
xx++;
gotoxy(xxx,1);
cout<<mess[j];
xxx--;
delay(50);
}
_setcursortype(_NORMALCURSOR);
}
void draw_cube(double edge[20][3])
{
initgraph(&gd,&gm,"..\bgi");
int i;
clearviewport();
for(i=0;i<19;i++)
{
x1=edge[i][0]+edge[i][2]*(cos(2.3562));
y1=edge[i][1]-edge[i][2]*(sin(2.3562));

Page 26 of 31

x2=edge[i+1][0]+edge[i+1][2]*(cos(2.3562));
y2=edge[i+1][1]-edge[i+1][2]*(sin(2.3562));
line(x1+320,240-y1,x2+320,240-y2);
}
line(320,240,320,25);
line(320,240,550,240);
line(320,240,150,410);
getch();
closegraph();
}
void scale(double edge[20][3])
{
double a,b,c;
int i;
cout<<"Enter The Scaling Factors:=";
cin>>a>>b>>c;
initgraph(&gd,&gm,"..\bgi");
clearviewport();
for(i=0;i<20;i++)
{
edge[i][0]=edge[i][0]*a;
edge[i][1]=edge[i][1]*b;
edge[i][2]=edge[i][2]*c;
}
draw_cube(edge);
closegraph();
}
void translate(double edge[20][3])
{
int a,b,c;
int i;
cout<<"Enter The Translation Factors:=";
cin>>a>>b>>c;
initgraph(&gd,&gm,"..\bgi");
clearviewport();
for(i=0;i<20;i++)
{
edge[i][0]+=a;
edge[i][0]+=b;
edge[i][0]+=c;
}
draw_cube(edge);
closegraph();
}
void rotate(double edge[20][3])
{
int ch;
int i;
double temp,theta,temp1;
clrscr();
cout<<"-=[Rotation About]=-";
cout<<"1:==> X-Axis";
cout<<"2:==> Y-Axis";
cout<<"3:==> Z-Axis";
cout<<"Enter Your Choice:=";
cin>>ch;
switch(ch)
{
case 1:
cout<<"Enter The Angle:=";
cin>>theta;

Page 27 of 31

theta=(theta*3.14)/180;
for(i=0;i<20;i++)
{
edge[i][0]=edge[i][0];
temp=edge[i][1];
temp1=edge[i][2];
edge[i][1]=temp*cos(theta)-temp1*sin(theta);
edge[i][2]=temp*sin(theta)+temp1*cos(theta);
}
draw_cube(edge);
break;
case 2:
cout<<"Enter The Angle:=";
cin>>theta;
theta=(theta*3.14)/180;
for(i=0;i<20;i++)
{
edge[i][1]=edge[i][1];
temp=edge[i][0];
temp1=edge[i][2];
edge[i][0]=temp*cos(theta)+temp1*sin(theta);
edge[i][2]=-temp*sin(theta)+temp1*cos(theta);
}
draw_cube(edge);
break;
case 3:
cout<<"Enter The Angle:=";
cin>>theta;
theta=(theta*3.14)/180;
for(i=0;i<20;i++)
{
edge[i][2]=edge[i][2];
temp=edge[i][0];
temp1=edge[i][1];
edge[i][0]=temp*cos(theta)-temp1*sin(theta);
edge[i][1]=temp*sin(theta)+temp1*cos(theta);
}
draw_cube(edge);
break;
}
}
void reflect(double edge[20][3])
{
int ch;
int i;
clrscr();
cout<<"-=[Reflection About]=-";
cout<<"1:==> X-axis";
cout<<"2:==> Y-Axis";
cout<<"3:==> Z-Axis";
cout<<"Enter Your Choice:=";
cin>>ch;
switch(ch)
{
case 1:
for(i=0;i<20;i++)
{
edge[i][0]=edge[i][0];
edge[i][1]=-edge[i][1];
edge[i][2]=-edge[i][2];
}

Page 28 of 31

draw_cube(edge);
break;
case 2:
for(i=0;i<20;i++)
{
edge[i][1]=edge[i][1];
edge[i][0]=-edge[i][0];
edge[i][2]=-edge[i][2];
}
draw_cube(edge);
break;
case 3:
for(i=0;i<20;i++)
{
edge[i][2]=edge[i][2];
edge[i][0]=-edge[i][0];
edge[i][1]=-edge[i][1];
}
draw_cube(edge);
break;
}
}
void perspect(double edge[20][3])
{
int ch;
int i;
double p,q,r;
clrscr();
cout<<"-=[Perspective Projection About]=-";
cout<<"1:==> X-Axis";
cout<<"2:==> Y-Axis";
cout<<"3:==> Z-Axis";
cout<<"Enter Your Choice:=";
cin>>ch;
switch(ch)
{
case 1:
cout<<"Enter P:=";
cin>>p;
for(i=0;i<20;i++)
{
edge[i][0]=edge[i][0]/(p*edge[i][0]+1);
edge[i][1]=edge[i][1]/(p*edge[i][0]+1);
edge[i][2]=edge[i][2]/(p*edge[i][0]+1);
}
draw_cube(edge);
break;
case 2:
cout<<"Enter Q:=";
cin>>q;
for(i=0;i<20;i++)
{
edge[i][1]=edge[i][1]/(edge[i][1]*q+1);
edge[i][0]=edge[i][0]/(edge[i][1]*q+1);
edge[i][2]=edge[i][2]/(edge[i][1]*q+1);
}
draw_cube(edge);
break;
case 3:
cout<<"Enter R:=";
cin>>r;

Page 29 of 31

for(i=0;i<20;i++)
{
edge[i][2]=edge[i][2]/(edge[i][2]*r+1);
edge[i][0]=edge[i][0]/(edge[i][2]*r+1);
edge[i][1]=edge[i][1]/(edge[i][2]*r+1);
}
draw_cube(edge);
break;
}
closegraph();
}
void main()
{
int choice;
double edge[20]
[3]={100,0,0,100,100,0,0,100,0,0,100,100,0,0,100,0,0,0,100,0,0,100,0,
100,100,75,100,75,100,100,100,100,75,100,100,0,100,100,75,100,75,100,
75,100,100,0,100,100,0,100,0,0,0,0,0,0,100,100,0,100};
while(1)
{
clrscr();
show_message();
cout<<"1:==> Draw Cube";
cout<<"2:==> Scaling";
cout<<"3:==> Rotation";
cout<<"4:==> Reflection";
cout<<"5:==> Translation";
cout<<"6:==> Perpective Projection";
cout<<"7:==> Exit";
cout<<"Enter Your Choice:=";
cin>>choice;
switch(choice)
{
case 1:
draw_cube(edge);
break;
case 2:
scale(edge);
break;
case 3:
rotate(edge);
break;
case 4:
reflect(edge);
break;
case 5:
translate(edge);
break;
case 6:
perspect(edge);
break;
case 7:
exit(0);
default:
cout<<"a Press A Valid Key...!!!";
getch();
break;
}
closegraph();
}
}

Page 30 of 31

Output :

RESULT : The program was successfully executed and the output was verfied

Page 31 of 31

You might also like