Wednesday, November 23, 2011

Implementation of DDA Line Drawing Algorithm

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

void main()
{
clrscr();
float x,y;int x1,x2,y1,y2,dx,dy,l,i=0;
cout<<"Enter the coordinates in the form of x1,y1,x2,y2";
cin>>x1>>y1>>x2>>y2;
int gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,"");
if(abs(x2-x1)>abs(y2-y1))
l=abs(x2-x1);
else
l=abs(y2-y1);
dx=(x2-x1)/l;
dy=(y2-y1)/l;
x=x+0.5;
y=y+0.5;
while(i<=l)
{
putpixel(int(x),int(y),9);
x=x+dx;
y=y+dy;
i++;
}
getch();
}

Implementation of Bresenham's Algorithm For Line Drawing

 The Algorithm
 
function line(x0, y0, x1, y1)
   dx := abs(x1-x0)
   dy := abs(y1-y0) 
   if x0 < x1 then sx := 1 else sx := -1
   if y0 < y1 then sy := 1 else sy := -1
   err := dx-dy
 
   loop
     setPixel(x0,y0)
     if x0 = x1 and y0 = y1 exit loop
     e2 := 2*err
     if e2 > -dy then 
       err := err - dy
       x0 := x0 + sx
     end if
     if e2 <  dx then 
       err := err + dx
       y0 := y0 + sy 
     end if
   end loop
 
 
 The Program 
 
 #include<iostream.h>
     #include<conio.h>
     #include<graphics.h>
     #include<math.h>
     #include<dos.h>
     void main()
     { int dx,dy,s1,s2,x1,x2,y1,y2,x,y,temp,e,i,c;
      cout<<"enter the coordinates of the line in the form x1 x2 y1 y2\n";
      cin>>x1>>y1>>x2>>y2;
      int gdriver=DETECT,gmode;
      initgraph(&gdriver,&gmode,"");
      x=x1;y=y1;
      dx=abs(x2-x1);
      dy=abs(y2-y1);
      if(x2>x1)
 s1=1;
 else s1=-1;
      if(y2>y1)
      s2=1;
      else s2=-1;
  if(dy>dx)
  { temp=dx;
  dx=dy;
  dy=temp;
  c=1;
  }
  else c=0;

  e=2*dy-dx;
  for(i=1;i<=dx;i++)
  {  delay(40);
   putpixel(x,y,10);
    while(e>0)
     { if(c==1)
 { x+=s1;}
       else y=y+s2;
 e-=2*dx;
     }
    if(c==1)
     y+=s2;
    else
     x=x+s1;
  e+=2*dy;
  }
  getch();
}

Monday, November 21, 2011

Implementation of First Come First Serve Sceduling in c++

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
 struct process
 {  float t ;
    int i;
 };
 int main()
 { process p[4];
  int n=4,temp;
  float t[4],w[4],ta=0,wa=0;
  clrscr();
  for( int j=0;j<n;j++)
  { cout<<"\n enter the the burst time of process p"<<j+1<<" : ";
  cin>>p[j].t;  }
  t[0]=p[0].t;
  for(j=1;j<4;j++)
  { t[j]=t[j-1]+p[j].t;
  }
   for (j=0;j<4;j++)
   { w[j]=t[j]-p[j].t;
   ta+=t[j];
   wa+=w[j];
   p[j].i=j;
   }
   // tubular display
   cout<<"\n\n        FCFS ALGO OF 4 PROCESSES \n    (\"All times are in milliseconds)\n\n\n -: RESULT :-\n";
   for(j=0;j<60;j++)
   cout<<"-";
   cout<<"\n\n     PROCESS     |     BURSTS    |    TAT     |    WT      |  \n\n";
   for(j=0;j<60;j++)
   cout<<"-";
   cout<<"\n";
   for(j=0;j<4;j++)
   {  cout<<"        "<<p[j].i+1<<"              "<<p[j].t<<"            "<<t[j]<<"            "<<w[j]<<endl;
     for(int m=0;m<60;m++)
     cout<<"-";
     cout<<endl;
   }
        float taa=ta/4,waa=wa/4;
   cout<<"\n\r      AVERAGE    :|                   "<<taa<<"           "<<waa;;
   cout<<endl;
   for(int m=0;m<60;m++)
   cout<<"-";
   getch();
   return 0;
   }

Implementation of Shortest Job First Scheduling in c++

#include<iostream.h>
#include<stdio.h>
#include<conio.h>
struct process
{int t;
 int i;
};
int main()
{  process p[4];
   int n=4,temp ,tempi;
   float w[4],t[4],ta=0,wa=0;
   clrscr();
   for(int j=0;j<4;j++)
   { cout<<"Enter the Burst Time of the process"<<j+1<<"  :";
     cin>>p[j].t;
     p[j].i=j;
   }
   for(j=n-1;j>0;j--)
   { for(int k=0;k<j;k++)
      { if(p[k].t>p[k+1].t)
      { temp=p[k+1].t;
        tempi=p[k+1].i;

        p[k+1].t=p[k].t;
        p[k+1].i=p[k].i;

        p[k].t=temp;
        p[k].i=tempi;
      }
       }
   }

   t[0]=p[0].t;
   for(j=1;j<4;j++)
    t[j]=t[j-1]+p[j].t;
   for(j=0;j<4;j++)
   {w[j]=t[j]-p[j].t;
    ta+=t[j];
    wa+=w[j];
   }

   /* for(j=0;j<4;j++)
   {cout<<"\n THE TAT OF THE PROCESS P"<<p[j].i<<"is  :- "<<t[j]<<"ms";
   }
   cout<<"\n\n\n\n\n\n";
   for(j=0;j<4;j++)
      {cout<<"\n THE WT OF PROCESS P"<<p[j].i<<"is  :-"<<w[j]<<"ms";
      }
   cout<<"\n\n\n\n\n\n\n";
   for(j=0;j<4;j++)
   { cout<<"The Given BURST Of the process P"<<p[j].i<<"is  :-"<<p[j].t<<"ms";
   }
   getch();

   // tabular display
   clrscr();*/

   cout<<"\n\n       SJF ALGO of 4 processes \n    (All times are in milliseconds)\n\n  -: RESULT  :-\n";
   for(j=0;j<60;j++)
   cout<<"-";
   cout<<"\n\n PROCESS    | BURSTS   |  WT  |    TAT    |  \n\n";
   for(j=0;j<60;j++)
      cout<<"-";
      cout<<"\n";

   for(j=0;j<4;j++)
   { cout<<"     "<<p[j].i+1<<"          "<<p[j].t<<"          "<<w[j]<<"        "<<t[j]<<"\n";
     for(int m=0;m<60;m++)
     cout<<"-";
     cout<<"\n";
   }float atat=ta/4,awt=wa/4;
   cout<<"\r  AVERAGE  :  |            "<<awt<<"        "<<atat<<endl;
   for(j=0;j<60;j++)
       cout<<"-";
   getch();
   return 0;
}

Implementation of Priority Sceduling in c++

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
struct process
{int pr,i;
float t;};

void main()
{ process p[4];
  int n=4,m,temp,tempi,temppr;
  float T[4],w[4],ta=0,wa=0;
  clrscr();
  for(int j=0;j<4;j++)
     {  cout<<"\n Enter the burst time of process P"<<j+1<<" :";
         cin>>p[j].t;
         p[j].i=j;
        cout<<"\nEnter the priority of process P"<<j+1<<" :";
        cin>>p[j].pr;
     }
  clrscr();
  for(j=n-1;j>0;j--)
    { for(int k=0;k<j;k++)
      { if(p[k].pr>p[k+1].pr)
      {
    temp=p[k+1].t;
    tempi=p[k+1].i;
    temppr=p[k+1].pr;
    p[k+1].t=p[k].t;
    p[k+1].i=p[k].i;
    p[k+1].pr=p[k].pr;
    p[k].t=temp;
    p[k].i=tempi;
    p[k].pr=temppr;
      }
    }
    }

    T[0]=p[0].t;
    for(j=1;j<=3;j++)
       T[j]=T[j-1]+p[j].t;
    for(j=0;j<4;j++)
      {w[j]=T[j]-p[j].t;
       ta=ta+T[j];
       wa=wa+w[j];
      }

cout<<"\n\n\n\n\n\n\n";
for(j=0;j<4;j++)
    {cout<<"\nThe BURSTS of process P"<<p[j].i+1<<"is :-"<<p[j].t<<"ms with PRIORITY="<<p[j].pr;
    }
getch();
clrscr();
cout<<"\n\n                    PRIORITY ALGO OF 4 PROCESS \n                (\"All times are in milliseconds\")\n\n\n\n\n      -:RESULT:-\n";
for(j=0;j<76;j++)
cout<<"-";
cout<<"\n";
cout<<"|  Process |  Burst Time    |   Priority    |   TAT     |     WT   |";
for(j=0;j<4;j++)
{
cout<<"\n    "<<p[j].i+1<<"          "<<p[j].t<<"               "<<p[j].pr<<"         "<<T[j]<<"            "<<w[j]<<endl;
for(m=0;m<76;m++)
cout<<"-";
cout<<endl;
}
ta=ta/4;
wa=wa/4;
cout<<"\n";
cout<<"\rAVERAGE TAT & WT ARE GIVEN AS :   |              "<<ta<<"            "<<wa;
cout<<endl;
for(m=0;m<76;m++)
cout<<"-";
getch();
}

Monday, October 17, 2011

Implementation of 2-D transformations

C program for translations-scaling -rotation-reflection

#include<iostream.h>
#include<conio.h>
#include<graphics.h>
long double object[10][10],translate[10][10],translate2[10][10],output[10][10],theta;
long double scale[10][10], mirror[10][10],rotate[10][10],output2[10][10],output3[10][10];
int n ,i,j,k;
void init(long double a[10][10])
{ for(i=0;i<10;i++)
     for(j=0;j<10;j++)
     a[i][j]=0;
}
void input(long double a[10][10],int b)
{for( i=0;i<b;i++)
     {cout<<"\nenter row  #"<<i+1<<endl;
      cin>>a[i][0]>>a[i][1]>>a[i][2];
     }
}
void matmulti(long double a[10][10],long double b[10][10],long double c[10][10])
{for(i=0;i<n;i++)
  for(j=0;j<3;j++)
   for(k=0;k<n;k++)
       c[i][j]+=a[i][k]*b[k][j];
}

void display(long double a[10][10])
{ for(i=0;i<n-1;i++)
      line(a[i][0],a[i][1],a[i+1][0],a[i+1][1]);
       line(a[0][0],a[0][1],a[n-1][0],a[n-1][1]);
}

void matdisp(long double a[10][10],int b)
{  for(i=0;i<b;i++)
  {for(j=0;j<3;j++)
   cout<<a[i][j]<<"  ";
   cout<<endl;
  }
}

void matdisp(long double a[10][10],long double b[10][10],long double c[10][10])
{  for(i=0;i<3;i++)
  {for(j=0;j<3;j++)
   cout<<a[i][j]<<"   ";
   cout<<"           ";
   for(j=0;j<3;j++)
   cout<<b[i][j]<<"   ";
   cout<<"           ";
   for(j=0;j<3;j++)
   cout<<c[i][j]<<"   ";
   cout<<endl;
  }
}
void matdisp(long double a[10][10],long double b[10][10])
{  for(i=0;i<n;i++)
  {for(j=0;j<3;j++)
   cout<<a[i][j]<<"  ";
   cout<<"              ";
   for(j=0;j<3;j++)
   cout<<b[i][j]<<"  ";
   cout<<endl;
  }
}

int main()
{clrscr();
 int i,j,ch,gd=DETECT,gm;
long double theta;
char choice='n';
 do{
 if(choice=='n')
 {cout<<"\n enter the no of verices in the figure";
  cin>>n;
  cout<<"\ enter the object matrix";
  input(object,n);
 }
  initgraph(&gd,&gm,"");
  display(object);
 getch();
 closegraph();
 cout<<"\n choose an operation";
 cout<<"\n 0.translate ";
 cout<<"\n 1.simple scale";
 cout<<"\n 2.fix point scale";
 cout<<"\n 3.simple rotate";
 cout<<"\n 4.fix point rotate";
 cout<<"\n 5.reflection about x-axis";
  cout<<"\n 6.reflection about y- axis";
 cout<<"\n Enter CHOICE : ";

 cin>>ch;
 if(ch==0)
{ cout<<"\n enter the translation matrix";
  input(translate,3);
  matmulti(object,translate,output3);
  cout<<endl;
  cout<<"\n OBJECT MATRICX\n";
  matdisp(object,n);
  getch();
  cout<<"\n translation matrix \n";
  matdisp(translate,3);
  cout<<"\n output matrix \n";
  matdisp(output3,n);
  getch();
}
 if(ch==1)
{ cout<<"\n enter the scaling matrix";
  input(scale,3);
  matmulti(object,scale,output3);
  cout<<endl;
  cout<<"\n OBJECT MATRICX\n";
  matdisp(object,n);
  getch();
  cout<<"\n scaling matrix \n";
  matdisp(scale,3);            
  cout<<"\noutput matrix \n";
  matdisp(output3,n);
  getch();

  getch();
}
 if(ch==2)
{ cout<<"\n enter the translation matrix";
  input(translate,3);
  cout<<"\n enter the scaling matrix";
  input(scale,3);
  cout<<"\n enter the inverse translation matrix";
  input(translate2,3);
  cout<<"\n object matrix\n";
  matdisp(object,n);
  cout<<"\n transformation matrixes       \n";
  cout<<"    TRANSLATION            SCALING            DE TRANSLATION  \n";
  matdisp(translate,scale,translate2);
  getch();
  matmulti(translate,scale,output);
  matmulti(output,translate2,output2);
  matmulti(object,output2,output3);
  cout<<"\n output matrix is \n";
  matdisp(output3,n);
  cout<<endl;
  getch();
}
 if(ch==3)
{  cout<<"\n enter the rotation matrix";
   input(rotate,3);
   matmulti(object,rotate,output3);
   cout<<endl;
   cout<<"\n OBJECT MATRICX\n";
  matdisp(object,n);
  getch();
    cout<<"\n rotation matrix \n";
  matdisp(rotate,3);
  cout<<"\n output matrix \n";
  matdisp(output3,n);
   getch();
}

 if(ch==4)
{  cout<<"\n enter the translation matrix";
   input(translate,3);
   cout<<"\n enter the rotation matrix";
   input(rotate,3);
   cout<<"\n enter the inverse translation matrix";
   input(translate2,3);
   cout<<"\n object matrix\n";
   matdisp(object,n);
   cout<<"\n transformation matrixes       \n";
   cout<<"    TRANSLATION            ROTATION            DE TRANSLATION  \n";
   matdisp(translate,rotate,translate2);
   getch();
   matmulti(translate,rotate,output);
   matmulti(output,translate2,output2);
   matmulti(object,output2,output3);
   cout<<"\n output matrix is \n";
   matdisp(output3,n);
   cout<<endl;
   getch();
}
if(ch==5)
{ translate[2][1]=-240;
 translate[2][0]=0;
  translate[0][0]=1;
  translate[1][1]=1;
  translate[2][2]=1;
  cout<<"\nenter reflection matrix\n";
  input(mirror,3);
  matmulti(translate,mirror,output);
  translate[2][1]=240;
  matmulti(output,translate,output2);
  matmulti(object,output2,output3);
    cout<<"\n input matrix\n";
  matdisp(object,n);
  getch();
  cout<<"\n mirror matrix\n";
  matdisp(mirror,3);
  getch();
  cout<<"\n output matric\n";
  matdisp(output3,n);
  getch();
}
if(ch==6)
{ translate[2][0]=-320;
 translate[2][1]=0;
  translate[0][0]=1;
  translate[1][1]=1;
  translate[2][2]=1;
  cout<<"\nenter reflection matrix\n";
  input(mirror,3);
  matmulti(translate,mirror,output);
  translate[2][0]=320;
  matmulti(output,translate,output2);
  matmulti(object,output2,output3);
  cout<<"\n input matrix\n";
  matdisp(object,n);
  getch();
  cout<<"\n mirror matrix\n";
  matdisp(mirror,3);
  getch();
  cout<<"\n output matric\n";
  matdisp(output3,n);
  getch();
}
 initgraph(&gd,&gm,"");
 display(object);
 setcolor(12);
 display(output3);
 if(ch==5||ch==6)
 {setcolor(10);
  line(0,240,640,240);
  setcolor(11);
  line(320,0,320,480);
 }
 getch();
 closegraph();
 init(output);
 init(output2);
 init(output3);
 cout<<"\n want to go to main menu  (y/n)\n";
 choice=getche();
}while(choice!='n');
  return 0;
}