Sunday, November 27, 2011

Implementation of Cohen-Sutherland Line clipping Algorithm

#include<graphics.h>
#include<conio.h>
#include<iostream.h>
const int t=1, b=2, r=4, l=8 ;
float xmin,ymin,xmax,ymax;
int calcode (float x,float y)
{ int code =0;
  if(y> ymax) code |=t;
  else if(y<ymin) code |= b;
  else if(x>xmax) code |= r;
  else if(x<xmin) code |= l;
  return(code);
}

void lineclip(float x1,float y1,float x2,float y2)
{ unsigned int code1,code2,codeout;
  int accept = 0, done=0;
  code1 = calcode(x1,y1);
  code2 = calcode(x2,y2);
  do{ if(!(code1 | code2))
    { accept =1 ; done =1; }
    else if(code1 & code2) done = 1;
    else
    { float x,y;
       codeout = code1 ? code1 : code2;
       if(codeout & t)
    { x = x1 + (x2-x1)*(ymax-y1)/(y2-y1);y = ymax;}
       else if(codeout & b)
    {x=x1+(x2-x1)*(ymin-y1)/(y2-y1);y=ymin;}
       else if (codeout & r)
      {y=y1+(y2-y1)*(xmax-x1)/(x2-x1);x=xmax;}
       else
     {y=y1+(y2-y1)*(xmin-x1)/(x2-x1);x=xmin;}
       if(codeout == code1)
      {x1 = x; y1 = y;
      code1=calcode(x1,y1);}
       else
    {x2 = x; y2 = y;
     code2 = calcode(x2,y2);}
   }
  } while( done == 0);
  if(accept)
    line(x1,y1,x2,y2);
    rectangle(xmin,ymin,xmax,ymax);
}

main()
{ float x1,y1,x2,y2;
  int gd=DETECT,gm;
  clrscr();
  initgraph(&gd,&gm,"");
  cout<<"\n\n\t:::Enter the co-ordinates of Line::::\n\tx1 :";cin>>x1;
  cout<<"\n\ty1 :";cin>>y1;
  cout<<"\n\tx2 :";cin>>x2;
  cout<<"\n\ty2 :";cin>>y2;
  cout<<"\n\t:::Enter the co_ordinates of window:::\n ";
  cout<<"\n\txmin :";cin>>xmin;
  cout<<"\n\tymin :";cin>>ymin;
  cout<<"\n\txmax :";cin>>xmax;
  cout<<"\n\tymax :";cin>>ymax;
  clrscr();
  line(x1,y1,x2,y2);
  rectangle(xmin,ymin,xmax,ymax);
  getch();
  clrscr();
  lineclip(x1,y1,x2,y2);
  getch();
  closegraph();
  return 0;
}

Implementation of Sutherland–Hodgman Polygon Clipping Algorithm

#include <stdio.h>
#include <graphics.h>
#include <conio.h>
#include <math.h>
#include <process.h>
#define TRUE 1
#define FALSE 0
typedef unsigned int outcode;
outcode CompOutCode(float x,float y);
enum  {  TOP = 0x1,
BOTTOM = 0x2,
RIGHT = 0x4,
LEFT = 0x8
};
float xmin,xmax,ymin,ymax;
void clip(float x0,float y0,float x1,float y1)
{
outcode code1,code2,codeout;
int accept = FALSE,done = FALSE;
code1 = CompOutCode(x0,y0);
code2 = CompOutCode(x1,y1);
do
{
if(!(code1|code2))
{
accept = TRUE;
done = TRUE;
}
else
if(code1 & code2)
done = TRUE;
else
{
float x,y;
 
codeout = code1?code1:code2;
if(codeout & TOP)
{
x = x0+(x1-x0)*(ymax-y0)/(y1-y0);
y = ymax;
}
else
if(codeout & BOTTOM)
{
x = x0+(x1-x0)*(ymin-y0)/(y1-y0);
y = ymin;
}
else
if(codeout & RIGHT)
{
y = y0+(y1-y0)*(xmax-x0)/(x1-x0);
x = xmax;
}
else
{
y = y0+(y1-y0)*(xmin-x0)/(x1-x0);
x = xmin;
}
if(codeout==code1)
{
x0 = x;
y0 = y;
code1 = CompOutCode(x0,y0);
}
else
{
x1 = x;
y1 = y;
code2 = CompOutCode(x1,y1);
}
}
}while(done==FALSE);
if(accept)
line(x0,y0,x1,y1);
outtextxy(150,20,"POLYGON AFTER CLIPPING");
 
rectangle(xmin,ymin,xmax,ymax);
}
outcode CompOutCode(float x,float y)
{
outcode code = 0;
if(y>ymax)
code|=TOP;
else
if(y<ymin)
code|=BOTTOM;
if(x>xmax)
code|=RIGHT;
else
if(x<xmin)
code|=LEFT;
return code;
}
void main( )
{float x1,y1,x2,y2;
int gdriver = DETECT, gmode, n,poly[14],i;
clrscr( );
printf("Enter the no of sides of polygon:");
scanf("%d",&n);
printf("\nEnter the coordinates of polygon\n");
for(i=0;i<2*n;i++)
{scanf("%d",&poly[i]);}
poly[2*n]=poly[0];
poly[2*n+1]=poly[1];
printf("Enter the rectangular coordinates of clipping window\n");
scanf("%f%f%f%f",&xmin,&ymin,&xmax,&ymax);
initgraph(&gdriver, &gmode, "");
 
outtextxy(150,20,"POLYGON BEFORE CLIPPING");
drawpoly(n+1,poly);
rectangle(xmin,ymin,xmax,ymax);
getch( );
cleardevice( );
for(i=0;i<n;i++)
clip(poly[2*i],poly[(2*i)+1],poly[(2*i)+2],poly[(2*i)+3]);
getch( );
restorecrtmode( );
}

program to show movement of circle along a line

#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
#include<dos.h>
// function for drawing circle of radius r having centre at m,n
void draw(int m,int n,int r)
{int x,y,p;
x=0;
y=r;
putpixel(m+y,n+x,1);
putpixel(m-y,n+x,1);
putpixel(m+x,n+y,1);
putpixel(m+x,n-y,1);
p=3-(2*r);
while(y>x)
{  if (p<0)
   p=(p+(4*x)+6);
   else
   { y=y-1;
     p=p+((4*(x-y)+10));
   }
   x++;
   putpixel(m+x,n+y,1);
   putpixel(m+x,n-y,1);
   putpixel(m-x,n-y,1);
   putpixel(m-x,n+y,1);
   putpixel(m-y,n-x,1);
   putpixel(m-y,n+x,1);
   putpixel(m+y,n+x,1);
   putpixel(m+y,n-x,1);
}
}

  void main()
     { int dx,dy,s1,s2,x1,x2,y1,y2,x,y,temp,e,i,c,r;
       cout<<"Enter the radius ";
       cin>>r;
      cout<<"enter the end points of line to move the circle along ( 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);
      clrscr();
      // calling function draw the circle
      draw(x,y,r);
      line(x1,y1,x,y);
    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();
}


implementation of concept of rotation in computer graphics


 #include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
double object[3][3],translate[3][3],output[3][3], rotate[3][3],output2[3][3],output3[3][3];
int n;
void input(int n)
{for(int i=0;i<n;i++)
     {cout<<"\nenter x and y for cordinate #"<<i+1<<endl;
      cin>>object[i][0]>>object[i][1];
     }
}
void matmulti(double a[10][3],double b[3][3],double c[10][3])
{for(int i=0;i<3;i++)
  for(int j=0;j<3;j++)
   for(int k=0;k<3;k++)
       c[i][j]+=a[i][k]*b[k][j];
}

void display(double a[10][3],int n)
{ for(int 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 main()
{int i,j,ch,gd=DETECT,gm;
double theta;
translate[0][0]=1;
translate[1][1]=1;
translate[2][2]=1;
object[0][2]=1;
object[1][2]=1;
object[2][2]=1;


 cout<<"\n 1.rotate a line";
 cout<<"\n 2.rotate a triangle";
 cout<<"\n enter your choice : ";
 cin>>ch;
 if(ch==1)
   n=2;
 if(ch==2)
   n=3;
 input(n);
 initgraph(&gd,&gm,"");
 display(object,n);
 getch();
 closegraph();
 cout<<"\nenter the fix points tx and ty\n";
 cin>>translate[2][0]>>translate[2][1];
 cout<<"\nenter the angle to rotate";
 cin>>theta;theta=-theta;
 theta=(3.14)*theta/180;
 rotate[0][0]=cos(theta);
 rotate[0][1]=sin(theta);
 rotate[1][0]=-sin(theta);
 rotate[1][1]=cos(theta);
 rotate[2][2]=1;

 translate[2][0]=(-translate[2][0]);
 translate[2][1]=(-translate[2][1]);
 matmulti(translate,rotate,output);
 translate[2][0]=(-translate[2][0]);
 translate[2][1]=(-translate[2][1]);
 matmulti(output,translate,output2);
 matmulti(object,output2,output3);

  initgraph(&gd,&gm,"");
 display(object,n);
 setcolor(12);
 display(output3,n);
 getch();

}

implementation of concept of translation in computer graphics

Program in c for 2-D Translation of line and Triangle

#include<iostream.h>
#include<conio.h>
#include<graphics.h>
int object[3][3],translate[3][3],output[3][3],n;
void input(int n)
{for(int i=0;i<n;i++)
     {cout<<"\nenter x and y for cordinate #"<<i+1<<endl;
      cin>>object[i][0]>>object[i][1];
     }
}    
void matmulti(int a[10][3],int b[3][3],int c[10][3])
{for(int i=0;i<3;i++)
  for(int j=0;j<3;j++)
   for(int k=0;k<3;k++)
       c[i][j]+=a[i][k]*b[k][j];
}

void display(int a[10][3],int n)
{ for(int 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 main()
{int i,j,ch,gd=DETECT,gm;
translate[0][0]=1;
translate[1][1]=1;
translate[2][2]=1;
object[0][2]=1;
object[1][2]=1;
object[2][2]=1;


 cout<<"\n 1.translate a line";
 cout<<"\n 2.translate a triangle";
 cout<<"\n enter your choice : ";
 cin>>ch;
 if(ch==1)
   n=2;
 if(ch==2)
   n=3;
 input(n);
 initgraph(&gd,&gm,"");
 display(object,n);
 getch();
 closegraph();
 cout<<"\nenter the translation factors tx and ty\n";
 cin>>translate[2][0]>>translate[2][1];
 matmulti(object,translate,output);
  initgraph(&gd,&gm,"");
 display(object,n);
 setcolor(12);
 display(output,n);
 getch();

}

Implementation of Midpoint Circle Algorithm

 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();
}

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();
}