Saturday, August 25, 2012

C++ CODE FOR POLYGON CLIPPING

            C++ CODE FOR POLYGON CLIPPING

So here's my C++ code for Polygon Clipping.This program can clip ant polygon except rectangle and square.
Source code:
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
void main(){ int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\TC\\BGI");
int p[20],xl,xh,yl,yh,i,n,t[20];
float m[10];
cout<<"Enter the lower window co-ordinates:";
cin>>xl>>yl;
cout<<"Enter the higher window co-ordinates:";
cin>>xh>>yh;
setcolor(BLUE);
rectangle(xl,yl,xh,yh);
cout<<"Enter no: of vertices for polygon:";
cin>>n;
int j=0;
do{
for(i=0;i<n;i++){
cout<<"\n x"<<i+1<<"& y"<<i+1<<":";
cin>>p[j];
j=j+1;
cin>>p[j];
j=j+1;}
} while(j<2*n);
p[2*n]=p[0];
p[2*n+1]=p[1];
cout<<"Before clipping:";
drawpoly(n+1,p);
getch();
int k=0;
for(i=0;i<2*n;i=i+2)
{m[i]=(float)(p[i+3]-p[i+1])/(p[i+2]-p[i]);
if(p[i]<xl || p[i]>xh || p[i+1]<yl || p[i+1]>yh)
{ if(p[i]<xl){
t[k+1]=p[i+1] +m[i]*(xl-p[i]);
t[k]=xl;}
else if(p[i]>xh){
t[k+1]=p[i+1]+m[i]*(xh-p[i]);
t[k]=xh;}
if(p[i+1]<yl)
{t[k]=p[i]+(yl-p[i+1])/m[i];
t[k+1]=yl;}
else if(p[i+1]>yh)
{t[k]=p[i]+(yh-p[i+1])/m[i];
t[k+1]=yh;} k=k+2;}
if(p[i]>=xl && p[i]<=xh && p[i+1]>=yl && p[i+1]<=yh)
{t[k]=p[i];
t[k+1]=p[i+1];
k=k+2;}
if(p[i+2]<xl || p[i+2]>xh || p[i+3]<yl || p[i+3]>yh)
{ if(p[i+2]<xl){
t[k+1]=p[i+3] +m[i]*(xl-p[i+2]);
t[k]=xl;}
else if(p[i+2]>xh){
t[k+1]=p[i+3]+m[i]*(xh-p[i+2]);
t[k]=xh;}
if(p[i+3]<yl)
{t[k]=p[i+2]+(yl-p[i+3])/m[i];
t[k+1]=yl;}
else if(p[i+3]>yh)
{t[k]=p[i+2]+(yh-p[i+3])/m[i];
t[k+1]=yh;}k=k+2;
}}
t[k]=t[0];
t[k+1]=t[1];
cout<<"\n After clipping:";
setcolor(WHITE);
drawpoly((k+2)/2,t);
getch();
}
OUTPUT:

Saturday, August 18, 2012

Liang-Barsky line clipping program in c++

 Liang-Barsky line clipping program in c++

This is my own code in c++ for Liang-Barsky line clipping algorithm :
Source code:
#include<iostream.h>
# include<conio.h>
# include<graphics.h>
void main() {
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\TC\\BGI");
int x1,y1,x2,y2,xmax,xmin,ymax,ymin,xx1,yy1,xx2,yy2,dx,dy,i;
int p[4],q[4];
float t1,t2,t[4];
cout<<"Enter the lower co-ordinates of window";
cin>>xmin>>ymin;
cout<<"Enter the upper co-ordinates of window";
cin>>xmax>>ymax;
setcolor(RED);
rectangle(xmin,ymin,xmax,ymax);
cout<<"Enter x1:";
cin>>x1;
cout<<"Enter y1:";
cin>>y1;
cout<<"Enter x2:";
cin>>x2;
cout<<"Enter y2:";
cin>>y2;
line(x1,y1,x2,y2);
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)
{t[i]=(float)q[i]/p[i];}
else
if(p[i]==0 && q[i]<0)
cout<<"line completely outside the window";
else
if(p[i]==0 && q[i]>=0)
cout<<"line completely inside the window";
}
if (t[0]>t[2])
{t1=t[0];}
else
{t1=t[2];}
if (t[1]<t[3])
{t2=t[1];}
else
{t2=t[3];}
if (t1<t2)
{xx1=x1+t1*dx;
xx2=x1+t2*dx;
yy1=y1+t1*dy;
yy2=y1+t2*dy;
cout<<"line after clipping:";
setcolor(WHITE);
line(xx1,yy1,xx2,yy2);}
else
{cout<<"line lies out of the window";}
getch();
}

OUTPUT:



Thursday, August 16, 2012

COHEN SUTHERLAND line clipping code in C++

COHEN SUTHERLAND line clipping code in C++

Here is my own generated code for Cohen Sutherland line clipping algorithm in C++.Hope its useful for you.
Any suggestions to improve the code are welcomed.
Note:Here the xl,yl,xh and yh are assumed as shown in figure below:

This is different to the dimensioning done on the screen.
Source Code:

#include<iostream.h>
#include<conio.h>
# include<graphics.h>
void main(){
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\TC\\BGI");
int xl,xh,yl,yh,bit1,bit2,i,x1,y1,x2,y2,b3,b4,b1,b2,d,e;
float m;
cout<<"Enter the lower window co-ordinates";
cin>>xl>>yl;
cout<<"Enter higher window co-ordinates";
cin>>xh>>yh;
rectangle(xl,yl,xh,yh);
cout<<"\n Enter the two end-points of line:";
cout<<"x1:";
cin>>x1;
cout<<"y1:";
cin>>y1;
cout<<"x2:";
cin>>x2;
cout<<"y2:";
cin>>y2;
line(x1,y1,x2,y2);
//for 1st end-point
if(x1<xl && y1>yl)
bit1=5;
else
if(x1<xl && y1>yh && y1<yl)
bit1=1;
else
if(x1<xl && y1<yh)
bit1=9;
else
if(x1>xl && x1<xh && y1<yh)
bit1=8;
else
if(x1>xh && y1<yh)
bit1=10;
else
if(x1>xh && y1>yh && y1<yl)
bit1=2;
else
if(x1>xh && y1>yl)
bit1=6;
else
if(x1<xh && x1>xl && y1>yl)
bit1=4;
else
{bit1=0;}
//for 2nd end point
if(x2<xl && y2>yl)
bit2=5;
else
if(x2<xl && y2>yh && y2<yl)
bit2=1;
else
if(x2<xl && y2<yh)
bit2=9;
else
if(x2>xl && x2<xh && y2<yh)
bit2=8;
else
if(x2>xh && y2<yh)
bit2=10;
else
if(x2>xh && y2>yh && y2<yl)
bit2=2;
else
if(x2>xh && y2>yl)
bit2=6;
else
if(x2<xh && x2>xl && y2>yl)
bit2=4;
else
{bit2=0;}

m=(float)(y2-y1)/(x2-x1);
 b1=m*(xl-x1)+y1;
b2=m*(xh-x1)+y1;
 d=yh-y1;
 e=yl-y1;
 b3=x1+(d/m);
 b4=x1+(e/m);
if(bit1!=0 || bit2!=0)
{setcolor(46);
 if(b1<=yl && b1>=yh && b2<=yl && b2 >=yh)
  line(xl,b1,xh,b2);
else
 if(b1<=yl && b1>=yh && b3<=xh && b3 >=xl)
  line(xl,b1,b3,yh);
else
 if(b1<=yl && b1>=yh && b4<=xh && b4 >=xl)
  line(xl,b1,b4,yl);
else
 if(b2<=yl && b2>=yh && b3<=xh && b3>=xl)
  line(xh,b2,b3,yh);
else
 if(b2<=yl && b2>=yh && b4<=xh && b4 >=xl)
  line(xh,b2,b4,yl);
else
 if( b3<=xh && b3>=xl && b4<=xh && b4>=xl)
  line(b3,yh,b4,yl);
}
else
{cout<<"line lies inside the window";  }
getch();
}

OUTPUT:

Friday, August 10, 2012

REVEL-programming the sense of touch!

                               Programming the sense of touch!!


Everything from mobile phones to PCs can now be accessed with one touch.You may think this is it but would love to correct you by introducing REVEL which simply programs the sense of touch.Using this we can add artificial tactile sensation to any real-world object.This technology is based on Reverse Electrovibration wherein an oscillating  magnetic field is created around the user's skin by introducing a weak electrical signal.While you explore an object through your hands,you perceive highly distinctive tactile textures that augment the physical object.

Watch the video for more information and its future use!

"MESHWORM!!"

A new species in Robots clan-The Meshworm!


 Born at  MIT, Harvard University and Seoul National University, this creature...nah..a bot..hmm...better a earthworm like bot has revolutionized the world of robotics!!Its been named as "MESHWORM". Like an earthworm,it gets through hard-to reach space and traverse bumpy train.Unlike the previous robots,it doesn't have a bulky or breakable structure.

The movement of this miniature follows the simple science of contraction and relaxation of muscles generally  used by the worms to crawl.It has a mesh-like tube and artificial muscles developed from wires made of nickel and titanium.The nickel-titanium wire is wounded on the tube creating segments and a small current is passed through it.The alloy changes phase on heating up which is the reason for its stretching and contraction.As evident from the video,its not affected by the hammer or even if you throw it away!

Seems to be helpful for developing the next-generation endoscopes but exciting would be to see its usage to mobile phones or portable PCs!!

Sunday, August 5, 2012

Just go for it!!: Round Robin Process Scheduling Algorithm in C++

Just go for it!!: Round Robin Process Scheduling Algorithm in C++:            Round Robin Process Scheduling Algorithm in C++ Here is my own written code in C++ for Round Robin process scheduling algorit...

Round Robin Process Scheduling Algorithm in C++

           Round Robin Process Scheduling Algorithm in C++

Here is my own written code in C++ for Round Robin process scheduling algorithm.Hope its useful for you.Any improvement to this code is welcomed.So this is your code:
Source code:
# include<iostream.h>
# include<conio.h>
# include<stdlib.h>
void main(){
clrscr();
int slice,i,j,m,wt[10],st[10],et[10],n,tat[10],pt[10],a[10],twt=0,ttat=0;
float avgwt,avgtat;
char p[10][10];
cout<<"enter no of process";
cin>>n;
for(i=0;i<n;i++)
{
cout<<"Enter the name of process:";
cin>> p[i];
cout<<"\n Enter the burst time:";
cin>>pt[i];
}
cout<<"Enter the time-slice:";
cin>>slice;
st[0]=0;
i=0;
do{
if(pt[i]<=slice)
{et[i]=st[i]+pt[i];
pt[i]=0;
tat[i]=et[i];}
else
{et[i]=st[i]+slice;
pt[i]=pt[i]-slice;}
wt[i]=st[i];
st[i+1]=et[i];
i=i+1;
}while(i<n);
 m=n-1;
j=0;
a[m]=1234;
do{ for(i=0;i<n;i++){
if(pt[i]==0)
{tat[i]=tat[i];}
else
{st[m+1]=et[m];
 a[m+1]=i;
if(pt[i]<=slice)
{et[m+1]=st[m+1]+pt[i];
pt[i]=0;
tat[i]=et[m+1];}
else
{et[m+1]=st[m+1]+slice;
pt[i]=pt[i]-slice;}
if(a[m]==a[m+1])
{wt[i]=wt[i]+0;}
else
{wt[i]+=st[m+1]-et[i];}
m=m+1;} }
if(pt[j]!=0)continue;
j=j+1;
}while(j<n);
cout<<"\n name"<<"\t waiting time"<<"\t turn aroun time";
for(i=0;i<n;i++)
{cout<<"\n"<<p[i]<<"\t\t"<<wt[i]<<"\t\t\t"<<tat[i]<<"\n";
twt+=wt[i];
ttat+=tat[i];
}
  avgwt=(float) (twt)/n;
 avgtat= (float)(ttat)/n;
cout<<"\n Average waiting time:"<<avgwt;
cout<<"\n Average turn around time:"<<avgtat;
getch();
}

Gantt chart:



OUTPUT: