Monday, March 30, 2009

LINE CLIPPING

#include
#include
#include
#include
#include
class clip
{
private:
int left,bottom,top,right;
int x1,y1,x2,y2,b;
int bit1,bit2;
float m;
public:
void clipping();
void lineclip();
int check(int,int);
};
int clip::check(int x,int y)
{
int b=0000;
if(xb=b|1;
if(x>right)
b=b|2;
if(y>bottom)
b=b|4;
if(yb=b|8;
return(b);
}
void clip::clipping()
{
if((bit1&bit2)!=0)
{
x1=x2=y1=y2=0;
}
if((bit1&bit2)==0)
{
m=float(y2-y1)/float(x2-x1);
if((bit1&1)==1)
{
y1=y1+int((left-x1)*(m));
x1=left;
if((x1==left)&&(y1>top)&&(y1bit1=0000;
}
if((bit1&2)==2)
{
y1=y1+int((right-x1)*(m));
x1=right;
if((x1==right)&&(y1>top))
bit1=0000;
}
if((bit1&8)==8)
{
x1=x1+int((bottom-y1)/(m));
y1=top;
}
if((bit2&1)==1)
{
y2=y1+int(left-x1)*(m);
x2=left;
if((x2==left)&&(y2>top)&&(y2bit2=0000;
}
if((bit2&2)==2)
{
y2=y1+int((right-x1)*(m));
x2=right;
if((x2==right)&&(y2>top)&&(y2bit2=0000;
}
if((bit2&4)==4)
{
x2=x1+int((bottom-y1)/(m));
y2=bottom;
}
if((bit2&8)==8)
{
x2=x1+int((top-y1)/(m));
y2=top;
}}}
void clip::lineclip()
{
int gd=DETECT,gm;
cout<<"\n enter window coordinate(l,t,r,b)";
cin>>left>>top>>right>>bottom;
cout<<"\n enter the line coordinate";
cin>>x1>>y1>>x2>>y2;
outtextxy(400,400,"before clipping");
rectangle(left,top,right,bottom);
line(x1,y1,x2,y2);
bit1=check(x1,y1);
bit2=check(x2,y2);
getch();
closegraph();
clipping();
initgraph(&gd,&gm,"c:\\Tc\\BGI");
outtextxy(400,400,"after clipping");
if((bit1&bit2)==0)
{
cout<<"\n\tpart of the line is inside\n";
rectangle(left,top,right,bottom);
line(x1,y1,x2,y2);
getch();
}
if((bit1&bit2)!=0)
{
cout<<"\n the line is completely outside\n";
rectangle(left,top,right,bottom);
cout<<"clipped line coordinates";
cout<<"(x1,y1)("<cout<<"(x2,y2)("<}
getch();
closegraph();
}
void main()
{
clrscr();
int gd=DETECT;
int gm,ch;
char op;
clip obj;
initgraph(&gd,&gm,"c:\\Tc\\BGI");
do
{
cout<<"\n line clipping\n";
obj.lineclip();
cout<<"\n do you want to continue\n";
cin>>op;
}
while(op!='n');
getch();
closegraph();
}}

TEXT COMPRESSION

#include
#include
#include
#include
void main()
{
char a[50],b[50],dl;
int i,j=0,flag=0,countindex=0,index=-1,count=0;
FILE *ip,*op;
clrscr();
printf("Enter the text for input\t");
scanf("%s",a);
ip=fopen("source","w");
fprintf(ip,"%s",a);
printf("\n input file length=\t");
printf("%d",ftell(ip));
fclose(ip);
printf("\n");
ip=fopen("compress","w");
for(i=0;i{
j=i+1;
if(a[j]==a[i])
count=count+1;
else
if(count>0)
{
b[++index]=a[i];
fprintf(ip,"%c",b[index]);
b[++index]='#';
fprintf(ip,"%c",b[index]);
b[++index]=count+1;
fprintf(ip,"%c",b[index]);
count=0;
}
else
{
b[++index]=a[i];
fprintf(ip,"%c",b[index]);
}
countindex=index;
}
fclose(ip);
op=fopen("compress","r+");
printf("\n Compressed output\n");
for(i=0;i{
fscanf(op,"%c",b[i]);
if(b[i]=='#')
{
flag=1;
printf("%c",b[i]);
continue;
}
else if(flag==1)
{
printf("%d",b[i]);
flag=0;
}
else
printf("%c",b[i]);
}
printf("\n Compressed file length\t");
printf("%d",ftell(op));
fclose(op);
flag=0;
op=fopen("comperss","r+");
ip=fopen("decompress","w");
printf("\n decompressed output\n");
for(i=0;i<=countindex;i++)
{
fscanf(op,"%c",b[i]);
if(b[i]=='#')
{
flag=1;
//printf("%c",b[i]);
continue;
}
else if(flag==1)
{
for(j=0;j{
fprintf(ip,"%c",dl);
printf("%c",dl);
}
flag=0;
}
else if(b[i]!='\0')
{
fprintf(ip,"%c",b[i]);
printf("%c",b[i]);
dl=b[i];
}
}
printf("\n Decompressed length");
printf("%d",ftell(ip));
fclose(ip);
fclose(op);
getch();
return;
}

Thursday, March 26, 2009

BRESENHAM’S ELLIPSE DRAWING ALGORITHM

#include
#include
#include
void main()
{
long d1,d2;
int i,gd,gm,x,y;
long rx,ry,rxsq,rysq,tworxsq,tworysq,dx,dy;
printf("\n enter the x radius of the ellipse:");
scanf("%ld",&rx);
printf("\n enter the y radius of the ellipse:");
scanf("%ld",&ry);
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"c:\\Tc\\BGI ");
rxsq=rx*rx;
rysq=ry*ry;
tworxsq=2*rxsq;
tworysq=2*rysq;
x=0;
y=ry;
d1=rysq-rxsq*ry+(0.25*rxsq);
dx=tworysq*x;
dy=tworxsq*y;
do
{
putpixel(200+x,200+y,15);
putpixel(200-x,200-y,15);
putpixel(200+x,200-y,15);
putpixel(200-x,200+y,15);
if(d1<0)
{
x=x+1;
y=y;
dx=dx+tworysq;
d1=d1+dx+rysq;
}
else
{
x=x+1;
y=y-1;
dx=dx+tworysq;
dy=dy-tworxsq;
d1=d1+dx-dy+rysq;
}
delay(5);
}
while(dxd2=rysq*(x+0.5)*(x+0.5)+rxsq*(y-1)*(y-1)-rxsq*rysq;
do
{
putpixel(200+x,200+y,15);
putpixel(200-x,200-y,15);
putpixel(200+x,200-y,15);
putpixel(200-x,200+y,15);
if(d2>0)
{
x=x;
y=y-1;
dy=dy-tworxsq;
d2=d2-dy+rxsq;
}
else
{
x=x+1;
y=y-1;
dy=dy-tworxsq;
dx=dx+tworysq;
d2=d2+dx-dy+rxsq;
}}
while(y>0);
getch();
closegraph();
}

3D TRANSFORMATION

#include
#include
#include
#include
#include
int s1,s2,h,w;
class trans
{
public:
void draw();
void modified();
void translate();
void shearx();
void sheary();
void rotate();
void scale();
void assign();
int op[10],ip[10],mop[10],mip[10],i,dist;
}tr;
void trans::assign()
{
op[0]=op[6]=op[8]=s1;
op[1]=op[3]=op[9]=s2;
op[2]=op[4]=s1+w;
op[5]=op[7]=s2+h;
ip[0]=ip[6]=ip[8]=s1+w/2;
ip[1]=ip[3]=ip[9]=s2-h/2;
ip[2]=ip[4]=s1+w+w/2;
ip[5]=ip[7]=s2+h-h/2;
}
void trans::draw()
{
cleardevice();
setcolor(5);
drawpoly(5,op);
drawpoly(5,ip);
line(op[0],op[1],ip[0],ip[1]);
line(op[2],op[3],ip[2],ip[3]);
line(op[4],op[5],ip[4],ip[5]);
line(op[6],op[7],ip[6],ip[7]);
}
void trans::modified()
{
setcolor(15);
drawpoly(5,mop);
drawpoly(5,mip);
line(mop[0],mop[1],mip[0],mip[1]);
line(mop[2],mop[3],mip[2],mip[3]);
line(mop[4],mop[5],mip[4],mip[5]);
line(mop[6],mop[7],mip[6],mip[7]);
}
void trans::translate()
{
cout<<"\n Transition distance";
cin>>dist;
cleardevice();
draw();
for(i=0;i<10;i++)
{
mop[i]=op[i]+dist;
mip[i]=ip[i]+dist;
}
modified();
}
void trans::shearx()
{
cout<<"\n\t shearx factor";
cin>>dist;
cleardevice();
draw();
for(i=0;i<10;i=i+2)
{
mop[i]=op[i]+(dist+op[i+1]);
mop[i+1]=op[i+1];
mip[i]=ip[i]+(dist+ip[i+1]);
mip[i+1]=ip[i+1];
}
modified();
}
void trans::sheary()
{
cout<<"\n\t sheary factor";
cin>>dist;
cleardevice();
draw();
for(i=0;i<10;i=i+2)
{
mop[i]=op[i];
mop[i+1]=dist+op[i]+op[i+1];
mip[i]=ip[i];
mip[i+1]=dist+ip[i]+ip[i+1];
}
modified();
}
void trans::rotate()
{
int h,k;
float ang,rad;
cout<<"rotation angle";
cin>>ang;
rad=(3.14*ang)/180;
cout<<"\n\t rotate x by:";
cin>>h;
cout<<"\n\t rotate y by:";
cin>>k;
cleardevice();
draw();
for(int i=0;i<10;i=i+2)
{
mop[i]=h+(op[i]-h)*cos(rad)-(op[i+1]-k)*sin(rad);
mop[i+1]=k+(op[i]-h)*sin(rad)+(op[i+1]-k)*cos(rad);
mip[i]=h+(ip[i]-h)*cos(rad)-(ip[i+1]-k)*sin(rad);
mip[i+1]=k+(ip[i]-h)*sin(rad)+(ip[i+1]-k)*cos(rad);
}
modified();
}
void trans::scale()
{
int sx,sy;
cout<<"\n\t increment x by:";
cin>>sx;
cout<<"\n\t increment y by:";
cin>>sy;
cleardevice();
draw();
for(i=0;i<10;i++)
{
if(i%2==0)
{
mop[i]=op[i]*sx;
mip[i]=ip[i]*sx;
}
else
{
mop[i]=op[i]*sy;
mip[i]=ip[i]*sy;
}
}
modified();
}
void main()
{
char op,again;
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc\\bgi");
cout<<"\n\t Give cuboid co ordinate:";
cout<<"\n\t enter top left:";
cin>>s1>>s2;
cout<<"Enter height and width:";
cin>>h>>w;
tr.assign();
do
{
cout<<"\n\t menu";
cout<<"\n\t.....";
cout<<"\n\t translate\n\t scale\n\t rotate";
cout<<"\n\t shearx\n\tsheary";
cout<<"\n\n\t I like to do transformation";
op=toupper(getch());
cout<getch();
cleardevice();
switch(op)
{
case 'T':
tr.translate();
break;
case 'S':
tr.scale();
break;
case 'X':
tr.shearx();
break;
case 'Y':
tr.sheary();
break;
case 'R':
tr.rotate();
break;
}
cout<<"do once again:";
again=getch();
cleardevice();
}
while(again!='n'&&again!='n');
getch();
closegraph();
}

Projection of 3D Image

#include
#include
#include
#include
#include
#include
sphere ()
{
int appx,appy,appx1,appy1,h,majlen,minlen;
setcolor(15);
cout<<"\n\t start point";
cin>>appx>>appy;
cout<<"\n\t height";
cin>>h;
cout<<"\n\t major axis length";
cin>>majlen;
cout<<"\n\t minor axis length";
cin>>minlen;
appx1=appx;
appy1=appy+h;
line(appx,appy,appx1,appy1);
ellipse(appx1,appy1,0,360,majlen,minlen);
int x1,y1,x2,y2;
x1=appx1-majlen;
y1=appy1;
x2=appx1+majlen;
y2=appy1;
line(x1,y1,appx,appy);
line(x2,y2,appx,appy);
line(x1+majlen-10,y1-minlen,appx,appy);
line(x2-majlen+10,y1+minlen,appx,appy);
arc(appx1,appy1,180,0,majlen);
getch();
return(0);
}
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\TC\\BGI");
int ch;
while(1)
{
clrscr();
cleardevice();
cout<<"\n 3d objects";
cout<<"\n";
cout<<"\n menu";
cout<<"\n";
cout<<"\n";
cout<<"\n 1.sphere";
cout<<"\n 2.exit";
cout<<"\n enter the choice";
cin>>ch;
switch(ch)
{
case 1:
sphere();
break;
case 2:
closegraph();
exit(0);
break;
}}}

2D- TRANSFORMATION

#include
#include
#include
#include
#include
class tdt
{
private:
int n,poly[100],tpoly[100],ypoly[100],tx,ty;
int spoly[100],rpoly[100],sx,sy,rx,ry,ang,h,k;
int shxpoly[100],shx,shy,shypoly[100];
float rad;
public:
void oldpoly();
void transpoly();
void rotatepoly();
void scalepoly();
void shearxpoly();
void shearypoly();
};
void tdt::oldpoly()
{
cout<<"\n Enter the no.of sides";
cin>>n;
cout<<"\n Enter the co-ordinates";
for(int i=0;i<(n*2);i++)
{
cin>>poly[i];
}
poly[i]=poly[0];
poly[i+1]=poly[1];
drawpoly(n+1,poly);
getch();
}
void tdt::transpoly()
{
cout<<"\n Enter thye increment co-ordinates";
cin>>tx>>ty;
for(int i=0;i<(n*2);i++)
{
if(i%2==0)


tpoly[i]=poly[i]+tx;
else
tpoly[i]=poly[i]+ty;
}
tpoly[i]=tpoly[0];
tpoly[i+1]=tpoly[1];
drawpoly(n+1,tpoly);
getch();
}
void tdt::rotatepoly()
{
cout<<"enter the angle";
cin>>ang;
rad=(3.14*ang)/180;
cout<<"enter the point at which rotate";
cin>>h>>k;
for(int i=0;i<(n*2);i=i+2)
{
rpoly[i]=h+(poly[i]-h)*cos(rad)-poly[i+1]*sin(rad);
rpoly[i+1]=k+(poly[i]-h)*sin(rad)+poly[i+1]-k*cos(rad);
}
rpoly[i]=rpoly[0];
rpoly[i+1]=rpoly[1];
drawpoly(n+1,rpoly);
getch();
}
void tdt::shearxpoly()
{
cout<<"\n Enter the point";
cin>>shx;
for(int i=0;i<(n*2);i+=2)
{
shxpoly[i]=poly[i]+(shx*poly[i]);
shxpoly[i+1]=poly[i+1];
}
shxpoly[i]=shxpoly[0];
shxpoly[i+1]=shxpoly[1];
drawpoly(n+1,shxpoly);
getch();
}
void tdt::shearypoly()
{
cout<<"\n Enter the point";
cin>>shy;
for(int i=0;i<(n*2);i+=2)
{
shypoly[i]=poly[i];
shypoly[i+1]=(shy*poly[i+1]+poly[i+1]);
}
shypoly[i]=shypoly[0];
shypoly[i+1]=shypoly[1];
drawpoly(n+1,shypoly);
getch();
}
void tdt::scalepoly()
{
cout<<"\n Enter the scaling distance";
cin>>sx>>sy;
for(int i=0;i<(n*2);i++)
{
if(i%2==0)
{
spoly[i]=poly[i]*sx;
}
else
spoly[i]=poly[i]*sy;
}
spoly[i]=spoly[0];
spoly[i+1]=spoly[1];
drawpoly(n+1,spoly);
getch();
}
void main()
{
int gd=DETECT,gm;
int ch,i;
char op;
initgraph(&gd,&gm,"c:/tc/bgi");
tdt obj;
do
{
cleardevice();
for(i=0;i<7;i++)
{
cout<<"\n menu";
cout<<"\n 1.translate";
cout<<"\n 2.rotate";
cout<<"\n 3.scale";
cout<<"\n 4.shearx";
cout<<"\n 5.sheary";
cout<<"\n 6.exit";
cout<<"\n Enter ur choice";
cin>>ch;
switch(ch)
{
case 1:
obj.oldpoly();
obj.transpoly();
break;
case 2:
obj.oldpoly();
obj.rotatepoly();
break;
case 3:
obj.oldpoly();
obj.scalepoly();
break;
case 4:
obj.oldpoly();
obj.shearxpoly();
break;
case 5:
obj.oldpoly();
obj.shearypoly();
break;
}
cleardevice();
cout<<"\n Do u want to continue";
cin>>op;
}}
while(op!='n');
closegraph();
}

Wednesday, December 24, 2008

"Implementation of Lexical Analyzer"

#include
#include
#include
#include
FILE *fp;
char delim[14]={' ','\t','\n',',',';','(',')','{','}','[',']','#','<','>'};
char oper[7]={'+','-','*','/','%','=','!'};
char key[21][12]={"int","float","char","double","bool","void","extern","unsigned","goto","static","class","struct","for","if","else","return","register","long","while","do"};
char predirect[2][12]={"include","define"};
char header[6][15]={"stdio.h","conio.h","malloc.h","process.h","string.h","ctype.h"};
void skipcomment();
void analyze();
void check(char []);
int isdelim(char);
int isop(char);
int fop=0,numflag=0,f=0;
char c,ch,sop;
void main()
{
char fname[12];
clrscr();
printf("\n enter filename:");
scanf("%s",fname);
fp=fopen(fname,"r");
if(fp==NULL)
printf("\n the file does not exist");
else
analyze();
printf("\n end of file \n");
getch();
}
void analyze()
{
char token[50];
int j=0;
while(!feof(fp))
{
c=getc(fp);
if(c=='/')
{
//skip comment();
}
else if(c==’ ‘)
while((c=getc(fp))!="");
else if(isalpha(c))
{
if(numflag==1)
{
token[j]='\0';
check(token);
numflag=0;
j=0;
f=0;
}
else
{
token[j]=c;
j++;
}
if(f==0)
f=1;
}
else if(isalnum(c))
{
if(numflag==0)
numflag=1;
token[j]=c;
j++;
}
else
{
if(isdelim(c))
{
if(numflag==1)
{
token[j]='\0';
check(token);
numflag=0;
}
if(f==1)
{
token[j]='\0';
numflag=0;
check(token);
}
j=0;
f=0;
printf("\n delimiters \t%c",c);
}
else if(isop(c))
{
if(numflag==1)
{
token[j]='\0';
check(token);
numflag=0;
j=0;
f=0;
}
if(f==1)
{
token[j]='\0';
j=0;
f=0;
numflag=0;
check(token);
}
if(fop==1)
{
fop=0;
printf("\n operator \t %c%c",c,sop);
}
else
printf("\n operator \t%c",c);
}
else if(c=='.')
{
token[j]=c;
j++;
}
}
}
}
int isdelim(char c)
{
int i;
for(i=0;i<14;i++)
{
if(c==delim[i])
return 1;
}
return 0;
}
int isop(char c)
{
int i,j;
char ch;
for(i=0;i<7;i++)
{
if(c==oper[i])
{
ch=getc(fp);
for(j=0;j<6;j++)
{
if(ch==oper[j])
{
fop=1;
sop=ch;
return 1;
}
}
ungetc(ch,fp);
return 1;
}
}
return 0;
}
void check(char t[])
{
int i;
if(numflag==1)
{
printf("\n number \t\t%s",t);
return;
}
for(i=0;i<2;i++)
{
if(strcmp(t,predirect[i])==0)
{
printf("\n preprocessor directive %s",t);
return;
}
}
for(i=0;i<6;i++)
{
if(strcmp(t,header[i])==0)
{
printf("\n header file \t%s",t);
return;
}
}
for(i=0;i<21;i++)
{
if(strcmp(key[i],t)==0)
{
printf("\n keyword \t\t%s",key[i]);
return;
}
}
printf("\n identifier \t%s",t);
}
void skipcomment()
{
ch=getc(fp);
if(ch=='/')
{
while((ch=getc(fp))!='\0');
}
else if(ch=='*')
{
while(f==0)
{
ch=getc(fp);
if(c=='/')
f=1;
}
}
f=0;
}