Friday, November 9, 2018

Source code for Reflection in Computer graphics using C.

                                         CODE

#include<iostream.h>
#include<graphics.h>
#include<conio.h>
void main()
{
int gd=DETECT;
int gm;
initgraph(&gd,&gm,"");
char ch='y';
while(ch=='y'||ch=='Y')
{
float mul[10][10];
float ln1[10][10];
int x[3][3]={{1,0,0},{0,-1,0},{0,0,1}};
int y[3][3]={{-1,0,0},{0,1,0},{0,0,1}};
int xny[3][3]={{-1,0,0},{0,-1,0},{0,0,1}};
int xy[3][3]={{0,1,0},{1,0,0},{0,0,1}};
int nxy[3][3]={{0,-1,0},{-1,0,0},{0,0,1}};
int a=getmaxx();
int b=getmaxy();

int n;
cout<<"\nEnter the no. of edges in a polygon:";
cin>>n;
int ln[10][2];
int choice;

if(n>2)
{
 cout<<"Enter the co-ordinates of polygon:";
 cout<<"\n";
 for(int i=0;i<n;i++)
 {
 cout<<"x"<<i<<" y"<<i<<":-";
 cin>>ln[i][0]>>ln[i][1];
 }
cout<<"\n1. Along x-axis!!";
cout<<"\n2. Along y-axis!!";
cout<<"\n3. Along,when mirror is placed diagonally!!";
cout<<"\n4. Along y=x!!";
cout<<"\n5. Along y=-x!!";
cout<<"\nEnter your choice:";
cin>>choice;
cleardevice();
line(0,b/2,a,b/2);
line(a/2,0,a/2,b);

ln[n][0]=ln[0][0];
ln[n][1]=ln[0][1];

for(i=0;i<n;i++)
{
  line(ln[i][0],ln[i][1],ln[i+1][0],ln[i+1][1]);
}

float mat[3][10];
for(i=0;i<2;i++)
   {
    for(int j=0;j<n;j++)
    {
    mat[i][j] =ln[j][i];
    }
   }
   for(int p=0;p<n;p++)
   {
   mat[2][p]=1;
   }

   switch(choice)
   {
case 1:
   for(i=0;i<3;i++)
    {
    for(int j=0;j<n;j++)
      {
      mul[i][j]=0;
     for(int k=0;k<3;k++)
      {
      mul[i][j]=mul[i][j]+x[i][k]*mat[k][j];
      }}}
       for(i=0;i<2;i++)
   {
     for(int j=0;j<n;j++)
      {
       ln1[j][i]=mul[i][j];
}
       }
  for(p=0;p<n;p++)
  {
  ln1[p][1]+=b;
  }
ln1[n][0]=ln1[0][0];
ln1[n][1]=ln1[0][1];
    break;

 case 2:    for(i=0;i<3;i++)
    {
    for(int j=0;j<n;j++)
      {
      mul[i][j]=0;
     for(int k=0;k<3;k++)
      {
      mul[i][j]=mul[i][j]+y[i][k]*mat[k][j];
      }}}
       for(i=0;i<2;i++)
   {
     for(int j=0;j<n;j++)
      {
       ln1[j][i]=mul[i][j];
}
       }
  for(p=0;p<n;p++)
  {
  ln1[p][0]+=a;
  }
ln1[n][0]=ln1[0][0];
ln1[n][1]=ln1[0][1];
    break;

case 3:   for(i=0;i<3;i++)
    {
    for(int j=0;j<n;j++)
      {
      mul[i][j]=0;
     for(int k=0;k<3;k++)
      {
      mul[i][j]=mul[i][j]+xny[i][k]*mat[k][j];
      }}}
       for(i=0;i<2;i++)
   {
     for(int j=0;j<n;j++)
      {
       ln1[j][i]=mul[i][j];
}
       }
  for(p=0;p<n;p++)
  {
  ln1[p][0]+=a;
  ln1[p][1]+=b;
  }
ln1[n][0]=ln1[0][0];
ln1[n][1]=ln1[0][1];
    break;

 case 4:   line(319,239,639,0);
    for(i=0;i<3;i++)
    {
    for(int j=0;j<n;j++)
      {
      mul[i][j]=0;
     for(int k=0;k<3;k++)
      {
      mul[i][j]=mul[i][j]+xy[i][k]*mat[k][j];
      }}}
       for(i=0;i<2;i++)
   {
     for(int j=0;j<n;j++)
      {
       ln1[j][i]=mul[i][j];
}
       }
  for(p=0;p<n;p++)
  {
  ln1[p][1]-=b;
  ln1[p][0]+=a/3;
  }
ln1[n][0]=ln1[0][0];
ln1[n][1]=ln1[0][1];

    break;

 case 5: line(319,239,0,0);
    for(i=0;i<3;i++)
    {
    for(int j=0;j<n;j++)
      {
      mul[i][j]=0;
     for(int k=0;k<3;k++)
      {
      mul[i][j]=mul[i][j]+nxy[i][k]*mat[k][j];
      }}}
       for(i=0;i<2;i++)
   {
     for(int j=0;j<n;j++)
      {
       ln1[j][i]=mul[i][j];
}
       }
  for(p=0;p<n;p++)
  {
  ln1[p][1]+=b/2;
  ln1[p][0]+=a-a/3;
  }
ln1[n][0]=ln1[0][0];
ln1[n][1]=ln1[0][1];

    break;

default:cout<<"\Wrong choice!!";
break;
}

for(i=0;i<n;i++)
  {
   line(ln1[i][0],ln1[i][1],ln1[i+1][0],ln1[i+1][1]);
  }
}
else
{
  cout<<"Polygon is not valid!!";
}
getch();
cleardevice();
cout<<"\nDo you wanna continue(y/n)?";
cin>>ch  ;
}
closegraph();
}

                                      OUTPUT







                     

Source code for Rotation about fixed point in Computer graphics using C.

                                           CODE

#include<iostream.h>
#include<graphics.h>
#include<conio.h>
#include<math.h>
void main()
{
int gd=DETECT;
int gm;
initgraph(&gd,&gm,"");
char ch='y';
while(ch=='y'||ch=='Y')
{
int n;
cout<<"\nEnter the no. of edges in a polygon:";
cin>>n;
int ln[10][2];
if(n>2)
{
 cout<<"Enter the co-ordinates of polygon:";
 cout<<"\n";
 for(int i=0;i<n;i++)
 {
 cout<<"x"<<i<<" y"<<i<<":-";
 cin>>ln[i][0]>>ln[i][1];
 }

ln[n][0]=ln[0][0];
ln[n][1]=ln[0][1];

for(i=0;i<n;i++)
{
  line(ln[i][0],ln[i][1],ln[i+1][0],ln[i+1][1]);
}
double ag;
double mul[10][10];
double rad;

double mat[3][10];
for(i=0;i<2;i++)
   {
    for(int j=0;j<n;j++)
    {
    mat[i][j] =ln[j][i];
    }
   }
   for(int p=0;p<n;p++)
mat[2][p]=1;                 //homogeneous co-ordinate

double rot[3][3];
double t1[3][3];
double t2[3][3];
double ln1[10][2];
double mul1[10][10];
double mul2[10][10];
double mul3[10][10];
double tx,ty;
int ch;

cout<<"\n1.To Rotate Anti-clockwise!!";
cout<<"\n2.To Rotate clockwise!!";
cout<<"\nEnter your choice:";
cin>>ch;

   cout<<"Enter Center (Xc,Yc):";
cin>>tx>>ty;

t1[0][0]=1;
t1[0][1]=0;
t1[0][2]=-tx;
t1[1][0]=0;                      //to bring at origin
t1[1][1]=1;
t1[1][2]=-ty;
t1[2][0]=0;
t1[2][1]=0;
t1[2][2]=1;

t2[0][0]=1;
t2[0][1]=0;
t2[0][2]=tx;
t2[1][0]=0;
t2[1][1]=1;                       //to bring at original position
t2[1][2]=ty;
t2[2][0]=0;
t2[2][1]=0;
t2[2][2]=1;




  switch(ch)
  {

  case 1: cout<<"\nEnter the angle of rotation:";
       cin>>ag;
      rad=ag/180;
      rot[0][0]=cos(rad);
      rot[0][1]=-sin(rad);
      rot[0][2]=0;
      rot[1][0]=sin(rad);
      rot[1][1]=cos(rad);
      rot[1][2]=0;
      rot[2][0]=0;
      rot[2][1]=0;
      rot[2][2]=1;


  for(i=0;i<3;i++)
    {
     for(int j=0;j<3;j++)
     {
       mul1[i][j]=0;
       for(int k=0;k<3;k++)
       {
       mul1[i][j]=mul1[i][j]+t2[i][k]*rot[k][j];
       }
      }
      }
  break;

case 2:cout<<"\nEnter the angle of rotation:";
cin>>ag;
rad=ag/180;
      rot[0][0]=cos(rad);
      rot[0][1]=sin(rad);
      rot[0][2]=0;
      rot[1][0]=-sin(rad);
      rot[1][1]=cos(rad);
      rot[1][2]=0;
      rot[2][0]=0;
      rot[2][1]=0;
      rot[2][2]=1;


  for(i=0;i<3;i++)
    {
     for(int j=0;j<3;j++)
     {
       mul1[i][j]=0;
       for(int k=0;k<3;k++)
       {
       mul1[i][j]=mul1[i][j]+t2[i][k]*rot[k][j];
       }
      }
      }
  break;

default:cout<<"Wrong Choice!!" ;
break;
}


for(i=0;i<3;i++)
    {
     for(int j=0;j<3;j++)
     {
       mul2[i][j]=0;
       for(int k=0;k<3;k++)
       {
       mul2[i][j]=mul2[i][j]+mul1[i][k]*t1[k][j];
       } } }

  for(i=0;i<3;i++)
    {
     for(int j=0;j<n;j++)
     {
       mul3[i][j]=0;
       for(int k=0;k<3;k++)
       {
       mul3[i][j]=mul3[i][j]+mul2[i][k]*mat[k][j];
       } } }

for(i=0;i<2;i++)
    {
     for(int j=0;j<n;j++)
     {
      ln1[j][i]=mul3[i][j];
      }
    }

ln1[n][0]=ln1[0][0];
ln1[n][1]=ln1[0][1];
for(i=0;i<n;i++)
  {
   setcolor(CYAN);
   line(ln1[i][0],ln1[i][1],ln1[i+1][0],ln1[i+1][1]);
  }

}
else
{
  cout<<"Polygon is not valid!!";
}

cout<<"\nDo you wanna continue(y/n):";
cin>>ch;
}
closegraph();
}                     

                                     OUTPUT


                          

                                                              

Source code for scaling about fixed point in Computer graphics using C.

                                         CODE

#include<iostream.h>
#include<graphics.h>
#include<conio.h>
void main()
{
int gd=DETECT;
int gm;
initgraph(&gd,&gm,"");
char ch='y';
while(ch=='y'||ch=='Y')
{
int n;
cout<<"\nEnter the no. of edges in a polygon:";
cin>>n;
int ln[10][2];
if(n>2)
{
 cout<<"Enter the co-ordinates of polygon:";
 cout<<"\n";
 for(int i=0;i<n;i++)
 {
 cout<<"x"<<i<<" y"<<i<<":-";
 cin>>ln[i][0]>>ln[i][1];
 }

ln[n][0]=ln[0][0];
ln[n][1]=ln[0][1];

for(i=0;i<n;i++)
{
  line(ln[i][0],ln[i][1],ln[i+1][0],ln[i+1][1]);
}

float mat[10][10];
for(i=0;i<2;i++)
   {
    for(int j=0;j<n;j++)
    {
    mat[i][j] =ln[j][i];
    }
   }

   for(int p=0;p<n;p++)
mat[2][p]=1;                 //homogeneous co-ordinate


int ch;

cout<<"\n1.To increase the size!!";
cout<<"\n2.To reduce the size!!";
cout<<"\nEnter your choice:";
cin>>ch ;
float sc[3][3];
float t1[3][3];
float t2[3][3];
int ln1[10][2];
float mul1[10][10];
float mul2[10][10];
float mul3[10][10];
float tx,ty;
float inr;
float dec;
cout<<"Enter Center (Xc,Yc):";
cin>>tx>>ty;

t1[0][0]=1;
t1[0][1]=0;
t1[0][2]=-tx;
t1[1][0]=0;                      //to bring at origin
t1[1][1]=1;
t1[1][2]=-ty;
t1[2][0]=0;
t1[2][1]=0;
t1[2][2]=1;

t2[0][0]=1;
t2[0][1]=0;
t2[0][2]=tx;
t2[1][0]=0;
t2[1][1]=1;                       //to bring at original position
t2[1][2]=ty;
t2[2][0]=0;
t2[2][1]=0;
t2[2][2]=1;


  switch(ch)
  {
  case 1:cout<<"\nEnter increment factor:";
cin>>inr;
sc[0][0]=inr;
sc[0][1]=0;
sc[0][2]=0;
sc[1][0]=0;
sc[1][1]=inr;                  //for scaling
sc[1][2]=0;
sc[2][0]=0;
sc[2][1]=0;
sc[2][2]=1;

  for(i=0;i<3;i++)
    {
     for(int j=0;j<3;j++)
     {
       mul1[i][j]=0;
       for(int k=0;k<3;k++)
       {
       mul1[i][j]=mul1[i][j]+t2[i][k]*sc[k][j];
       } } }
break;


case 2:  cout<<"\nEnter Decrement factor:";
cin>>dec;
sc[0][0]=dec;
sc[0][1]=0;
sc[0][2]=0;
sc[1][0]=0;
sc[1][1]=dec;                  //for scaling
sc[1][2]=0;
sc[2][0]=0;
sc[2][1]=0;
sc[2][2]=1;

  for(i=0;i<3;i++)
    {
     for(int j=0;j<3;j++)
     {
       mul1[i][j]=0;
       for(int k=0;k<3;k++)
       {
       mul1[i][j]=mul1[i][j]+t2[i][k]*sc[k][j];
       } } }
break;

default:cout<<"Your choice is wrong!!";
 }

   for(i=0;i<3;i++)
    {
     for(int j=0;j<3;j++)
     {
       mul2[i][j]=0;
       for(int k=0;k<3;k++)
       {
       mul2[i][j]=mul2[i][j]+mul1[i][k]*t1[k][j];
       } } }

  for(i=0;i<3;i++)
    {
     for(int j=0;j<n;j++)
     {
       mul3[i][j]=0;
       for(int k=0;k<3;k++)
       {
       mul3[i][j]=mul3[i][j]+mul2[i][k]*mat[k][j];
       } } }



for(i=0;i<2;i++)
    {
     for(int j=0;j<n;j++)
     {
      ln1[j][i]=mul3[i][j];
      }
    }

ln1[n][0]=ln1[0][0];
ln1[n][1]=ln1[0][1];
for(i=0;i<n;i++)
  {
   setcolor(RED);
   line(ln1[i][0],ln1[i][1],ln1[i+1][0],ln1[i+1][1]);
  }
}
else
{
  cout<<"Polygon is not valid!!";
}

cout<<"\nDo you wanna continue(y/n):";
cin>>ch;
}
closegraph();
}

                                     OUTPUT


                             








Source code for rotation about origin in Computer graphics using C

                                        CODE

#include<iostream.h>
#include<graphics.h>
#include<conio.h>
#include<math.h>
void main()
{
int gd=DETECT;
int gm;
initgraph(&gd,&gm,"");
char ch='y';
while(ch=='y'||ch=='Y')
{
int n;
cout<<"\nEnter the no. of edges in a polygon:";
cin>>n;
int ln[10][2];
if(n>2)
{
 cout<<"Enter the co-ordinates of polygon:";
 cout<<"\n";
 for(int i=0;i<n;i++)
 {
 cout<<"x"<<i<<" y"<<i<<":-";
 cin>>ln[i][0]>>ln[i][1];
 }

ln[n][0]=ln[0][0];
ln[n][1]=ln[0][1];

for(i=0;i<n;i++)
{
  line(ln[i][0],ln[i][1],ln[i+1][0],ln[i+1][1]);
}
double ag;
double mul[10][10];
double rad;

double mat[2][10];
for(i=0;i<2;i++)
   {
    for(int j=0;j<n;j++)
    {
    mat[i][j] =ln[j][i];
    }
   }


int ch;
int ln1[10][2];
cout<<"\n1.To Rotate Anti-clockwise!!";
cout<<"\n2.To Rotate clockwise!!";
cout<<"\nEnter your choice:";
cin>>ch;
float sn;
double sc[2][2];
  switch(ch)
  {

  case 1: cout<<"\nEnter the angle of rotation:";
       cin>>ag;
      rad=ag/180;


    sc[0][0]=cos(rad);
    sc[0][1]=-sin(rad);
    sc[1][0]=sin(rad);
    sc[1][1]=cos(rad);

  for(i=0;i<2;i++)
    {
     for(int j=0;j<n;j++)
     {
       mul[i][j]=0;
       for(int k=0;k<2;k++)
       {
       mul[i][j]=mul[i][j]+sc[i][k]*mat[k][j];
       }
      }

      }
  break;

case 2:cout<<"\nEnter the angle of rotation:";
cin>>ag;
rad=ag/180;

    sc[0][0]=cos(rad);
    sc[0][1]=sin(rad);
    sc[1][0]=-sin(rad);
    sc[1][1]=cos(rad);
  for(i=0;i<2;i++)
    {
     for(int j=0;j<n;j++)
     {
       mul[i][j]=0;
       for(int k=0;k<2;k++)
       {
       mul[i][j]=mul[i][j]+sc[i][k]*mat[k][j];
       }
      }
      }
  break;

default:cout<<"Wrong Choice!!" ;
break;
}

  for(i=0;i<2;i++)
    {
     for(int j=0;j<n;j++)
     {
      ln1[j][i]=mul[i][j];
      }
    }

ln1[n][0]=ln1[0][0];
ln1[n][1]=ln1[0][1];
cout<<"\nPolygon after Rotation!!";
for(i=0;i<n;i++)
  {
   line(ln1[i][0],ln1[i][1],ln1[i+1][0],ln1[i+1][1]);
  }
}
else
{
  cout<<"Polygon is not valid!!";
}

cout<<"\nDo you wanna continue(y/n):";
cin>>ch;
}
closegraph();
}

                                     OUTPUT





                                

Source code for scaling about origin in Computer graphics using C.

                                           CODE


#include<iostream.h>
#include<graphics.h>
#include<conio.h>
void main()
{
int gd=DETECT;
int gm;
initgraph(&gd,&gm,"");
char ch='y';
while(ch=='y'||ch=='Y')
{
int n;
cout<<"\nEnter the no. of edges in a polygon:";
cin>>n;
int ln[10][2];
if(n>2)
{
 cout<<"Enter the co-ordinates of polygon:";
 cout<<"\n";
 for(int i=0;i<n;i++)
 {
 cout<<"x"<<i<<" y"<<i<<":-";
 cin>>ln[i][0]>>ln[i][1];
 }

ln[n][0]=ln[0][0];
ln[n][1]=ln[0][1];

for(i=0;i<n;i++)
{
  line(ln[i][0],ln[i][1],ln[i+1][0],ln[i+1][1]);
}

float mat[2][10];
for(i=0;i<2;i++)
   {
    for(int j=0;j<n;j++)
    {
    mat[i][j] =ln[j][i];
    }
   }
int ch;
float mul[10][10];
cout<<"\n1.To increase the size!!";
cout<<"\n2.To reduce the size!!";
cout<<"\nEnter your choice:";
cin>>ch ;
float sc[2][2];
float inr;
float dec;
  switch(ch)
  {
  case 1:cout<<"\nEnter increment factor:";
cin>>inr;
   for(i=0;i<2;i++)
       {
for(int j=0;j<2;j++)
{
   if(i==j)
   sc[i][j]=inr ;
   else
   sc[i][j]=0;
  }
}

  break;


case 2:cout<<"\nEnter decrement factor:";
cin>>dec;
   for(i=0;i<2;i++)
       {
for(int j=0;j<2;j++)
{
   if(i==j)
   sc[i][j]=dec ;
   else
   sc[i][j]=0;
  }
}
   break;

default:cout<<"Your choice is wrong!!";
 }
 for(i=0;i<2;i++)
    {
     for(int j=0;j<n;j++)
     {
       mul[i][j]=0;
       for(int k=0;k<2;k++)
       {
       mul[i][j]=mul[i][j]+sc[i][k]*mat[k][j];
     }
      }   }

int ln1[10][2];
for(i=0;i<2;i++)
    {
     for(int j=0;j<n;j++)
     {
      ln1[j][i]=mul[i][j];
      }
    }

ln1[n][0]=ln1[0][0];
ln1[n][1]=ln1[0][1];
cout<<"\nPolygon after Scaling!!";
for(i=0;i<n;i++)
  {
   line(ln1[i][0],ln1[i][1],ln1[i+1][0],ln1[i+1][1]);
  }

}
else
{
  cout<<"Polygon is not valid!!";
}

cout<<"\nDo you wanna continue(y/n):";
cin>>ch;
}
closegraph();
}

                                     OUTPUT


                                









Tuesday, October 16, 2018

Cross-Zero Game in C++.

                                     Source Code

                         //Cross-Zero Game

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>

int choice;

void display(char b[3][3])
{
clrscr();
cout<<"\n\t\tCross-Zero Game!!";
cout<<"\n\t\tPlayer_1 Symbol = X";
cout<<"\n\t\tPlayer_2 Symbol = O";

cout<<"\n\n";
cout<<"\t\t     |     |     "<<endl;
cout<<"\t\t"<<"  "<<b[0][0]<<"  "<<"|  "<<b[0][1]<<"  "<<"|  "<<b[0][2]<<"  "<<endl;
cout<<"\t\t_____|_____|_____"<<endl;
cout<<"\t\t     |     |     "<<endl;
cout<<"\t\t"<<"  "<<b[1][0]<<"  "<<"|  "<<b[1][1]<<"  "<<"|  "<<b[1][2]<<"  "<<endl;
cout<<"\t\t_____|_____|_____"<<endl;
cout<<"\t\t     |     |     "<<endl;
cout<<"\t\t"<<"  "<<b[2][0]<<"  "<<"|  "<<b[2][1]<<"  "<<"|  "<<b[2][2]<<"  "<<endl;
cout<<"\t\t     |     |     "<<endl;
}

void player_turn(char b[3][3],int &turn,int &row,int &col)
{
if((b[0][0]!='X'&& b[0][0]!='O') || (b[0][1]!='X'&& b[0][1]!='O') || (b[0][2]!='X'&& b[0][2]!='O')|| (b[1][0]!='X'&& b[1][0]!='O')|| (b[1][1]!='X'&& b[1][1]!='O')|| (b[1][2]!='X'&& b[1][2]!='O')|| (b[2][0]!='X'&& b[2][0]!='O')|| (b[2][1]!='X'&& b[2][1]!='O')|| (b[2][2]!='X'&& b[2][2]!='O'))
{
  if(turn==1)
  {
  cout<<"\n\tPlayer_1 [X] Turn:";
  cin>>choice;
  }
  else if(turn==0)
  {
  cout<<"\n\tPlayer_2 [O] Turn:";
  cin>>choice;
  }


switch(choice)
  {
  case 1:row=0;col=0; break;
  case 2:row=0;col=1; break;
  case 3:row=0;col=2; break;
  case 4:row=1;col=0; break;
  case 5:row=1;col=1; break;
  case 6:row=1;col=2; break;
  case 7:row=2;col=0; break;
  case 8:row=2;col=1; break;
  case 9:row=2;col=2; break;
  default:cout<<"Wrong choice!!";
       break;
   }
    if(turn==1 && b[row][col]!='X' && b[row][col]!='O')
       {
  b[row][col]='X';
       turn=0;
}


    else if(turn==0 && b[row][col]!='X' && b[row][col]!='O')
{
b[row][col]='O';
turn=1;
}
    else {
cout<<"\nAlready Filled, Choose Other Box!!";
player_turn(b,turn,row,col);
}
}
}


int gameover(char b[3][3],int &draw)
{

for(int i=0;i<3;i++)
{
if((b[i][0]==b[i][1] && b[i][0]==b[i][2]) || (b[0][i]==b[1][i] && b[0][i]==b[2][i]))
return 0;
}

if((b[0][0]==b[1][1] && b[0][0]==b[2][2]) || (b[0][2]==b[1][1] && b[0][2]==b[2][0]))
     return 0;

else if((b[0][0]=='X'|| b[0][0]=='O') && (b[0][1]=='X'|| b[0][1]=='O') &&(b[0][2]=='X'|| b[0][2]=='O') &&(b[1][0]=='X'|| b[1][0]=='O') &&(b[1][1]=='X'|| b[1][1]=='O') &&(b[1][2]=='X'|| b[1][2]=='O') &&(b[2][0]=='X'|| b[2][0]=='O') &&(b[2][1]=='X'|| b[2][1]=='O') && (b[2][2]=='X'|| b[2][2]=='O'))
  {
  draw=0;
  return 0;
  }
else
return 1;
}

void main()
{

char ch='y';
while(ch=='y'||ch=='Y')
{
int row=0;
int col=0;
int turn=1;
int draw=1;

char b[3][3]={{'1','2','3'},{'4','5','6'},{'7','8','9'}};
   int t=1;
   while(t==1)
   {
   display(b);
   player_turn(b,turn,row,col);
   t=gameover(b,draw);
   display(b);
   }

if(turn==0 && draw==1)
    cout<<"\n\t\tPlayer1 [X] is Winner!!" ;
else if(turn==1 && draw==1)
    cout<<"\n\t\tPlayer2 [O] is Winner!!" ;
else if(draw==0 && (turn==0 || turn==1))
    cout<<"\n\t\tGame Draw!!";
cout<<"\n\tDo You Wanna Restart The Game(y/n):" ;
cin>>ch;
}
}
                                     OUTPUT
                 Click here for output

4-bit region code or Cohen-Sutherland Line clipping program in C graphics.


                                 SOURCE CODE

#include<iostream.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"");
float w[5][2];
int i;
cout<<"Enter the co-ordinates of window:-\n"    ;
int xmin,ymin,xmax,ymax;
cout<<"xmin"<<" "<<"ymin"<<":";
cin>>xmin>>ymin;
cout<<"xmax"<<" "<<"ymax"<<":";
cin>>xmax>>ymax;
line(xmin,ymin,xmax,ymin);
line(xmax,ymin,xmax,ymax);
line(xmax,ymax,xmin,ymax);
line(xmin,ymax,xmin,ymin);
float x1,x2,y1,y2,m;
float dy,dx;
float st[4][2];
int print[2][2];
int print1[2][2];
int print2[2][2];
int flag=0;
float final[4][2];
float final1[2][2];
char ch='y';
while(ch=='Y'||ch=='y')
{
cout<<"\nEnter the line co-ordinates:-\n" ;
int ln[2][2];
for(i=0;i<2;i++)
{
  cout<<"x"<<i<<" y"<<i<<":-";
  cin>>ln[i][0]>>ln[i][1];                       //FOR LINE
}
for(i=0;i<1;i++)
{
  line(ln[i][0],ln[i][1],ln[i+1][0],ln[i+1][1]);
}
int bit1[4];
for(i=0;i<1;i++)
{
   int res1;
   res1=ln[i][0]-xmin;
   if(res1<0)
     bit1[0]=1;
   else
     bit1[0]=0;
   //Region code for 1st point
int res2;
   res2=xmax-ln[i][0];
   if(res2<0)
     bit1[1]=1;
   else
     bit1[1]=0;
int res3;
   res3=ln[i][1]-ymin;
   if(res3<0)
     bit1[2]=1;
   else
     bit1[2]=0;
int res4;
   res4=ymax-ln[i][1];
   if(res4<0)
     bit1[3]=1;
   else
     bit1[3]=0;
}

int bit2[4];
for(i=1;i<2;i++)
{
   int res1;
   res1=ln[i][0]-xmin;
   if(res1<0)
     bit2[0]=1;
   else
     bit2[0]=0;
   //Region code for 2nd point
int res2;
   res2=xmax-ln[i][0];
   if(res2<0)
     bit2[1]=1;
   else
     bit2[1]=0;
int res3;
   res3=ln[i][1]-ymin;
   if(res3<0)
     bit2[2]=1;
   else
     bit2[2]=0;
int res4;
   res4=ymax-ln[i][1];
   if(res4<0)
     bit2[3]=1;
   else
     bit2[3]=0;
}
int sum1=0,sum2=0;
cout<<"\nRegion code for 1st point:-";
for(i=3;i>=0;i--)
 {                                              //Printing region code
   sum1+=bit1[i];
   cout<<bit1[i];
 }
cout<<"\nRegion code for 2nd point:-";
for(i=3;i>=0;i--)
 { sum2+=bit2[i];                               //Printing region code
   cout<<bit2[i];
 }

int rsum=0 ;
int res[4];
for(i=0;i<4;i++)
{
  res[i]=bit1[i]&bit2[i];
}
cout<<"\n\nResult of AND operation is:-";        //AND oprn on Region code
for(i=3;i>=0;i--)
{
  rsum+=res[i];
  cout<<res[i];
}
if((sum1==0)&&(sum2==0))
{
  cout<<"\nLine is completely inside the window!!";
     for(i=0;i<1;i++)
       {
       setcolor(GREEN);
line(ln[i][0],ln[i][1],ln[i+1][0],ln[i+1][1]);
       }
}
else if(rsum>0)
{
  cout<<"\nLine is completely outside the window!!";
}
else
{
    if(sum1>=1&&sum2>=1)
    {
  cout<<"\nLine is partially inside the window!!";
   dy=ln[1][1]-ln[0][1];
   dx=ln[1][0]-ln[0][0];
   m=dy/dx;

   y1=ln[0][1]+(m*(xmin-ln[0][0])) ;   //left intercept
   y2=ln[0][1]+(m*(xmax-ln[0][0])) ;   //right intercept
   x1=ln[0][0]+((ymax-ln[0][1])/m);       //top intercept
   x2=ln[0][0]+((ymin-ln[0][1])/m);       //bottom intercept
  st[0][0]=xmin;                       //storing left-intercept co-ordinate
  st[0][1]=y1;
  st[1][0]=xmax;                       //storing right-intercept co-ordinate
  st[1][1]=y2;
  st[2][0]=x1;                       //storing top-intercept co-ordinate
  st[2][1]=ymax;
  st[3][0]=x2;                       //storing bottom-intercept co-ordinate
  st[3][1]=ymin;
cout<<"\nLeft,Right,Top & Bottom Co-ordinates resp.:-";
for(i=0;i<4;i++)
{
  cout<<"\n";
  cout<<"x"<<i<<" y"<<i<<":-"<<st[i][0]<<" "<<st[i][1];
}
cout<<"\nVisible portion of the line is:-" ;
  for(i=0;i<4;i++)
  {
    if(((st[i][0]>=xmin)&&(st[i][0]<=xmax))&&((st[i][1]>=ymin)&&(st[i][1]<=ymax)))
       {
       final[i][0]=st[i][0];
       final[i][1]=st[i][1];
   print1[0][0]=st[i][0];
   print1[0][1]=st[i][1];
   ++flag;
   if(flag==1)
   {
   print2[0][0]=st[i][0];
   print2[0][1]=st[i][1];
   }
       cout<<"("<<final[i][0]<<","<<final[i][1]<<")";
       }
   }
setcolor(GREEN);
line(print1[0][0],print1[0][1],print2[0][0],print2[0][1]);
//if((final[0][0]==st[0][0])&&(final[0][1]==st[0][1]))
 }  //if close

 else if(sum1==1&&sum2==0)
    {
       cout<<"\nLine is partially inside the window!!";
       dy=ln[1][1]-ln[0][1];
       dx=ln[1][0]-ln[0][0];
       m=dy/dx;
      //compairing region codes
       if(bit1[3]==0 && bit1[2]==0 && bit1[1]==0 && bit1[0]==1)
    {
    y1=ln[0][1]+(m*(xmin-ln[0][0])) ;   //left intercept
    st[0][0]=xmin;                       //storing left-intercept co-ordinate
    st[0][1]=y1;
    cout<<"\nVisible portion of the line is:";
    cout<<"("<<st[0][0]<<","<<st[0][1]<<") to ("<<ln[1][0]<<","<<ln[1][1]<<")";
    setcolor(GREEN);
    line(st[0][0],st[0][1],ln[1][0],ln[1][1]);
    goto Label1;
    }
      else if(bit1[3]==0 && bit1[2]==0 && bit1[1]==1 && bit1[0]==0)
    {
    y2=ln[0][1]+(m*(xmax-ln[0][0])) ;   //right intercept
    st[0][0]=xmax;                       //storing right-intercept co-ordinate
    st[0][1]=y2;
    cout<<"\nVisible portion of the line is:";
    cout<<"("<<st[0][0]<<","<<st[0][1]<<") to ("<<ln[1][0]<<","<<ln[1][1]<<")";
    setcolor(GREEN);
    line(st[0][0],st[0][1],ln[1][0],ln[1][1]);
    goto Label1;
    }

      else if(bit1[3]==1 && bit1[2]==0 && bit1[1]==0 && bit1[0]==0)
    {
    x1=ln[0][0]+((ymax-ln[0][1])/m);   //top intercept
     st[0][0]=x1;                       //storing top-intercept co-ordinate
     st[0][1]=ymax;
    cout<<"\nVisible portion of the line is:";
    cout<<"("<<st[0][0]<<","<<st[0][1]<<") to ("<<ln[1][0]<<","<<ln[1][1]<<")";
    setcolor(GREEN);
    line(st[0][0],st[0][1],ln[1][0],ln[1][1]);
    goto Label1;
    }
       else if(bit1[3]==0 && bit1[2]==1 && bit1[1]==0 && bit1[0]==0)
    {
    x2=ln[0][0]+((ymin-ln[0][1])/m);   //bottom intercept
     st[0][0]=x2;                       //storing bottom-intercept co-ordinate
     st[0][1]=ymin;
    cout<<"\nVisible portion of the line is:";
    cout<<"("<<st[0][0]<<","<<st[0][1]<<") to ("<<ln[1][0]<<","<<ln[1][1]<<")";
    setcolor(GREEN);
    line(st[0][0],st[0][1],ln[1][0],ln[1][1]);
    goto Label1;
    }
      }

    else if(sum1==0&&sum2==1)
    {
       cout<<"\nLine is partially inside the window!!";
       dy=ln[1][1]-ln[0][1];
       dx=ln[1][0]-ln[0][0];
       m=dy/dx;
      //compairing region codes
       if(bit2[3]==0 && bit2[2]==0 && bit2[1]==0 && bit2[0]==1)
    {
    y1=ln[0][1]+(m*(xmin-ln[0][0])) ;   //left intercept
    st[0][0]=xmin;                       //storing left-intercept co-ordinate
    st[0][1]=y1;
    cout<<"\nVisible portion of the line is:";
    cout<<"("<<st[0][0]<<","<<st[0][1]<<") to ("<<ln[0][0]<<","<<ln[0][1]<<")";
    setcolor(GREEN);
    line(st[0][0],st[0][1],ln[0][0],ln[0][1]);
    goto Label1;
    }
      else if(bit2[3]==0 && bit2[2]==0 && bit2[1]==1 && bit2[0]==0)
    {
    y2=ln[0][1]+(m*(xmax-ln[0][0])) ;   //right intercept
    st[0][0]=xmax;                       //storing right-intercept co-ordinate
    st[0][1]=y2;
    cout<<"\nVisible portion of the line is:";
    cout<<"("<<st[0][0]<<","<<st[0][1]<<") to ("<<ln[0][0]<<","<<ln[0][1]<<")";
    setcolor(GREEN);
    line(st[0][0],st[0][1],ln[0][0],ln[0][1]);
    goto Label1;
    }

      else if(bit2[3]==1 && bit2[2]==0 && bit2[1]==0 && bit2[0]==0)
    {
    x1=ln[0][0]+((ymax-ln[0][1])/m);   //top intercept
     st[0][0]=x1;                       //storing top-intercept co-ordinate
     st[0][1]=ymax;
    cout<<"\nVisible portion of the line is:";
    cout<<"("<<st[0][0]<<","<<st[0][1]<<") to ("<<ln[0][0]<<","<<ln[0][1]<<")";
    setcolor(GREEN);
    line(st[0][0],st[0][1],ln[0][0],ln[0][1]);
    goto Label1;
    }
       else if(bit2[3]==0 && bit2[2]==1 && bit2[1]==0 && bit2[0]==0)
    {
    x2=ln[0][0]+((ymin-ln[0][1])/m);   //bottom intercept
     st[0][0]=x2;                       //storing bottom-intercept co-ordinate
     st[0][1]=ymin;
    cout<<"\nVisible portion of the line is:";
    cout<<"("<<st[0][0]<<","<<st[0][1]<<") to ("<<ln[0][0]<<","<<ln[0][1]<<")";
    setcolor(GREEN);
    line(st[0][0],st[0][1],ln[0][0],ln[0][1]);
    goto Label1;
    }
      }

     else if(sum1==2&&sum2==0)
  {
  cout<<"\n\nLine is partially inside the window!!";
dy=ln[1][1]-ln[0][1];
dx=ln[1][0]-ln[0][0];
m=dy/dx;
     if(bit1[3]==0 && bit1[2]==1 && bit1[1]==0 && bit1[0]==1)
    {
    y1=ln[0][1]+(m*(xmin-ln[0][0])) ;   //left intercept
    x2=ln[0][0]+((ymin-ln[0][1])/m);      //bottom intercept
    st[0][0]=xmin;                       //storing left-intercept co-ordinate
    st[0][1]=y1;
    st[1][0]=x2;                         //storing bottom-intercept co-ordinate
    st[1][1]=ymin;
    cout<<"\nLeft intercept:";
    cout<<st[0][0]<<","<<st[0][1];
    cout<<"\nBottom intercept:";
    cout<<st[1][0]<<","<<st[1][1];
     cout<<"\n\nVisible portion of the line is:";
    for(i=0;i<2;i++)
      {
      if(((st[i][0]>=xmin)&&(st[i][0]<=xmax))&&((st[i][1]>=ymin)&&(st[i][1]<=ymax)))
{
       final1[i][0]=st[i][0];
       final1[i][1]=st[i][1];
print[0][0]=st[i][0];
print[0][1]=st[i][1];
       cout<<"("<<final1[i][0]<<","<<final1[i][1]<<")";
}
      }
    cout<<" to ("<<ln[1][0]<<","<<ln[1][1]<<")";
       setcolor(GREEN);
       line(print[0][0],print[0][1],ln[1][0],ln[1][1]);
    goto Label1;
    }

     if(bit1[3]==0 && bit1[2]==1 && bit1[1]==1 && bit1[0]==0)
    {
    y2=ln[0][1]+(m*(xmax-ln[0][0])) ;   //right intercept
    x2=ln[0][0]+((ymin-ln[0][1])/m);      //bottom intercept
    st[0][0]=xmax;                       //storing right-intercept co-ordinate
    st[0][1]=y2;
    st[1][0]=x2;                         //storing bottom-intercept co-ordinate
    st[1][1]=ymin;
    cout<<"\nRight intercept:";
    cout<<st[0][0]<<","<<st[0][1];
    cout<<"\nBottom intercept:";
    cout<<st[1][0]<<","<<st[1][1];
     cout<<"\n\nVisible portion of the line is:";
    for(i=0;i<2;i++)
      {
      if(((st[i][0]>=xmin)&&(st[i][0]<=xmax))&&((st[i][1]>=ymin)&&(st[i][1]<=ymax)))
{
       final1[i][0]=st[i][0];
       final1[i][1]=st[i][1];
  print[0][0]=st[i][0];
print[0][1]=st[i][1];
       cout<<"("<<final1[i][0]<<","<<final1[i][1]<<")";
}
      }
    cout<<" to ("<<ln[1][0]<<","<<ln[1][1]<<")";
    setcolor(GREEN);
       line(print[0][0],print[0][1],ln[1][0],ln[1][1]);
    goto Label1;
    }

    if(bit1[3]==1 && bit1[2]==0 && bit1[1]==1 && bit1[0]==0)
    {
    y2=ln[0][1]+(m*(xmax-ln[0][0])) ;   //right intercept
    x1=ln[0][0]+((ymax-ln[0][1])/m);      //top intercept
    st[0][0]=xmax;                       //storing right-intercept co-ordinate
    st[0][1]=y2;
    st[1][0]=x1;                         //storing top-intercept co-ordinate
    st[1][1]=ymax;
    cout<<"\nRight intercept:";
    cout<<st[0][0]<<","<<st[0][1];
    cout<<"\nTop intercept:";
    cout<<st[1][0]<<","<<st[1][1];
     cout<<"\n\nVisible portion of the line is:";
    for(i=0;i<2;i++)
      {
      if(((st[i][0]>=xmin)&&(st[i][0]<=xmax))&&((st[i][1]>=ymin)&&(st[i][1]<=ymax)))
{
       final1[i][0]=st[i][0];
       final1[i][1]=st[i][1];
  print[0][0]=st[i][0];
print[0][1]=st[i][1];
       cout<<"("<<final1[i][0]<<","<<final1[i][1]<<")";
}
      }
    cout<<" to ("<<ln[1][0]<<","<<ln[1][1]<<")";
    setcolor(GREEN);
       line(print[0][0],print[0][1],ln[1][0],ln[1][1]);
    goto Label1;
    }

      if(bit1[3]==1 && bit1[2]==0 && bit1[1]==0 && bit1[0]==1)
    {
    y1=ln[0][1]+(m*(xmin-ln[0][0])) ;   //left intercept
    x1=ln[0][0]+((ymax-ln[0][1])/m);      //top intercept
    st[0][0]=xmin;                       //storing left-intercept co-ordinate
    st[0][1]=y1;
    st[1][0]=x1;                         //storing top-intercept co-ordinate
    st[1][1]=ymax;
    cout<<"\nLeft intercept:";
    cout<<st[0][0]<<","<<st[0][1];
    cout<<"\nTop intercept:";
    cout<<st[1][0]<<","<<st[1][1];
     cout<<"\n\nVisible portion of the line is:";
    for(i=0;i<2;i++)
      {
      if(((st[i][0]>=xmin)&&(st[i][0]<=xmax))&&((st[i][1]>=ymin)&&(st[i][1]<=ymax)))
{
       final1[i][0]=st[i][0];
       final1[i][1]=st[i][1];
   print[0][0]=st[i][0];
print[0][1]=st[i][1];
       cout<<"("<<final1[i][0]<<","<<final1[i][1]<<")";
}
      }
    cout<<" to ("<<ln[1][0]<<","<<ln[1][1]<<")";
    setcolor(GREEN);
       line(print[0][0],print[0][1],ln[1][0],ln[1][1]);
    goto Label1;
    }
    }
    else if(sum1==0&&sum2==2)
  {
  cout<<"\n\nLine is partially inside the window!!";
dy=ln[1][1]-ln[0][1];
dx=ln[1][0]-ln[0][0];
m=dy/dx;
     if(bit2[3]==0 && bit2[2]==1 && bit2[1]==0 && bit2[0]==1)
    {
    y1=ln[0][1]+(m*(xmin-ln[0][0])) ;   //left intercept
    x2=ln[0][0]+((ymin-ln[0][1])/m);      //bottom intercept
    st[0][0]=xmin;                       //storing left-intercept co-ordinate
    st[0][1]=y1;
    st[1][0]=x2;                         //storing bottom-intercept co-ordinate
    st[1][1]=ymin;
    cout<<"\nLeft intercept:";
    cout<<st[0][0]<<","<<st[0][1];
    cout<<"\nBottom intercept:";
    cout<<st[1][0]<<","<<st[1][1];
     cout<<"\n\nVisible portion of the line is:";
    for(i=0;i<2;i++)
      {
      if(((st[i][0]>=xmin)&&(st[i][0]<=xmax))&&((st[i][1]>=ymin)&&(st[i][1]<=ymax)))
{
       final1[i][0]=st[i][0];
       final1[i][1]=st[i][1];
   print[0][0]=st[i][0];
print[0][1]=st[i][1];
       cout<<"("<<final1[i][0]<<","<<final1[i][1]<<")";
}
      }
    cout<<" to ("<<ln[0][0]<<","<<ln[0][1]<<")";
    setcolor(GREEN);
       line(print[0][0],print[0][1],ln[0][0],ln[0][1]);
    goto Label1;
    }

     if(bit2[3]==0 && bit2[2]==1 && bit2[1]==1 && bit2[0]==0)
    {
    y2=ln[0][1]+(m*(xmax-ln[0][0])) ;   //right intercept
    x2=ln[0][0]+((ymin-ln[0][1])/m);      //bottom intercept
    st[0][0]=xmax;                       //storing right-intercept co-ordinate
    st[0][1]=y2;
    st[1][0]=x2;                         //storing bottom-intercept co-ordinate
    st[1][1]=ymin;
    cout<<"\nRight intercept:";
    cout<<st[0][0]<<","<<st[0][1];
    cout<<"\nBottom intercept:";
    cout<<st[1][0]<<","<<st[1][1];
     cout<<"\n\nVisible portion of the line is:";
    for(i=0;i<2;i++)
      {
      if(((st[i][0]>=xmin)&&(st[i][0]<=xmax))&&((st[i][1]>=ymin)&&(st[i][1]<=ymax)))
{
       final1[i][0]=st[i][0];
       final1[i][1]=st[i][1];
   print[0][0]=st[i][0];
print[0][1]=st[i][1];
       cout<<"("<<final1[i][0]<<","<<final1[i][1]<<")";
}
      }
    cout<<" to ("<<ln[0][0]<<","<<ln[0][1]<<")";
    setcolor(GREEN);
       line(print[0][0],print[0][1],ln[0][0],ln[0][1]);
    goto Label1;
    }

    if(bit2[3]==1 && bit2[2]==0 && bit2[1]==1 && bit2[0]==0)
    {
    y2=ln[0][1]+(m*(xmax-ln[0][0])) ;   //right intercept
    x1=ln[0][0]+((ymax-ln[0][1])/m);      //top intercept
    st[0][0]=xmax;                       //storing right-intercept co-ordinate
    st[0][1]=y2;
    st[1][0]=x1;                         //storing top-intercept co-ordinate
    st[1][1]=ymax;
    cout<<"\nRight intercept:";
    cout<<st[0][0]<<","<<st[0][1];
    cout<<"\nTop intercept:";
    cout<<st[1][0]<<","<<st[1][1];
     cout<<"\n\nVisible portion of the line is:";
    for(i=0;i<2;i++)
      {
      if(((st[i][0]>=xmin)&&(st[i][0]<=xmax))&&((st[i][1]>=ymin)&&(st[i][1]<=ymax)))
{
       final1[i][0]=st[i][0];
       final1[i][1]=st[i][1];
   print[0][0]=st[i][0];
print[0][1]=st[i][1];
       cout<<"("<<final1[i][0]<<","<<final1[i][1]<<")";
}
      }
    cout<<" to ("<<ln[0][0]<<","<<ln[0][1]<<")";
    setcolor(GREEN);
       line(print[0][0],print[0][1],ln[0][0],ln[0][1]);
    goto Label1;
    }

      if(bit2[3]==1 && bit2[2]==0 && bit2[1]==0 && bit2[0]==1)
    {
    y1=ln[0][1]+(m*(xmin-ln[0][0])) ;   //left intercept
    x1=ln[0][0]+((ymax-ln[0][1])/m);      //top intercept
    st[0][0]=xmin;                       //storing left-intercept co-ordinate
    st[0][1]=y1;
    st[1][0]=x1;                         //storing top-intercept co-ordinate
    st[1][1]=ymax;
    cout<<"\nLeft intercept:";
    cout<<st[0][0]<<","<<st[0][1];
    cout<<"\nTop intercept:";
    cout<<st[1][0]<<","<<st[1][1];
     cout<<"\n\nVisible portion of the line is:";
    for(i=0;i<2;i++)
      {
      if(((st[i][0]>=xmin)&&(st[i][0]<=xmax))&&((st[i][1]>=ymin)&&(st[i][1]<=ymax)))
{
       final1[i][0]=st[i][0];
       final1[i][1]=st[i][1];
    print[0][0]=st[i][0];
print[0][1]=st[i][1];
       cout<<"("<<final1[i][0]<<","<<final1[i][1]<<")";
}
      }
    cout<<" to ("<<ln[0][0]<<","<<ln[0][1]<<")";
       setcolor(GREEN);
       line(print[0][0],print[0][1],ln[0][0],ln[0][1]);
    goto Label1;
    }
    }
}   //else close
Label1:
cout<<"\n\nDo You Wanna Continue(Y/N):-";
cin>>ch;
}  //while close
closegraph();
}                             

                                           OUTPUT







Source code for Happy Diwali Wishing program in C Graphics.

                                           SOURCE CODE #include<graphics.h> #include<stdio.h> #include<conio.h> ...