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







Monday, October 8, 2018

Program to fill any shape using Half-filling method in C/C++ Graphics.

                                          

                                        SOURCE CODE

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

void rboundaryfill(int x,int y,int fcolor,int bcolor)

{
int current=getpixel(x,y);
if((current!=fcolor)&&(current!=bcolor))
   {
   putpixel(x,y,fcolor);
   rboundaryfill(x+1,y,fcolor,bcolor);
   rboundaryfill(x,y+1,fcolor,bcolor);
   rboundaryfill(x,y-1,fcolor,bcolor);
   }
}

void lboundaryfill(int x,int y,int fcolor,int bcolor)

{
int current=getpixel(x,y);
if((current!=fcolor)&&(current!=bcolor))
   {
   putpixel(x,y,fcolor);
   lboundaryfill(x-1,y,fcolor,bcolor);
   lboundaryfill(x,y+1,fcolor,bcolor);
   lboundaryfill(x,y-1,fcolor,bcolor);
   }
}
void main()
{
int gd=DETECT;
int gm;
initgraph(&gd,&gm,"");
int x,y,r;
int a[10][2];
int n;
printf("Enter the no. of edges in a polygon");
scanf("%d",&n);
printf("Enter the co-ordinates of shape:-")  ;
for(int i=0;i<n;i++)
{
scanf("%d%d",&a[i][0],&a[i][1]) ;          //to take co-ordinates
}

a[n][0]=a[0][0];

a[n][1]=a[0][1];
for(int p=0;p<n;p++)
{
line(a[p][0],a[p][1],a[p+1][0],a[p+1][1]);           // to draw polygon
}

int ylargest,ysmallest,ymid;

ylargest=a[0][1];
for(i=0;i<n;i++)
{
  if(a[i+1][1]>=ylargest)
  ylargest=a[i+1][1];
}
    //to calculate ymid
ysmallest=a[0][1];
for(i=0;i<n;i++)
{
  if(a[i+1][1]<=ysmallest)
  ysmallest=a[i+1][1];
}

//printf("ylargst:-%d",ylargest);

//printf("ysmallest:-%d",ysmallest);
ymid=(ysmallest)+((ylargest-ysmallest)/2);



int xlargest,xsmallest,xmid;

xlargest=a[0][0];
for(i=0;i<n;i++)
{
  if(a[i+1][0]>=xlargest)
  xlargest=a[i+1][0];
}
     //to calculate xmid
xsmallest=a[0][0];
for(i=0;i<n;i++)
{
  if(a[i+1][0]<=xsmallest)
  xsmallest=a[i+1][0];
}
//printf("xlargst:-%d",xlargest);
//printf("xsmallest:-%d",xsmallest);
xmid=(xsmallest)+((xlargest-xsmallest)/2);


rboundaryfill(xmid+1,ymid,5,15);

lboundaryfill(xmid-1,ymid,3,15);
getch();
closegraph();
}

                                                                 OUTPUT




Sunday, October 7, 2018

Source code for Generalized Animation program using Translation concept in C/C++ Graphics



                                      SOURCE CODE

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

pl[n][0]=pl[0][0];
pl[n][1]=pl[0][1];

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

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

cout<<"1.Tanslation along x-axis!!";
cout<<"\n2.Tanslation along y-axis!!";
cout<<"\n3.Tanslation along both axis!!";
cout<<"\nEnter your choice:";
cin>>ch;
int xt,yt;
int ax[2][2],ay[2][2],axy[2][2];
int add[2][10];
int x,y;
int ln1[10][2];
int t,xn,yn,t1;
switch(ch)
{
  case 1:cout<<"Enter translation factor along x-axis:";
cin>>xt;
    if(xt>0)
    {
 for(x=0;x<=xt;x+=5)
  {
ax[0][0]=x;
ax[1][0]=0;
for(i=0;i<2;i++)
{
  for(int j=0;j<n;j++)
  {
  add[i][j] = mat[i][j]+ax[i][0];
  }
}

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

ln1[n][0]=ln1[0][0];
ln1[n][1]=ln1[0][1];
//cout<<"\nPolygon after translation along x-axis!!";

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

    if(x>=xt)
      getch();
    else
      cleardevice();
    if(x>=xt)
       goto label1;
   }
}

else
{
t=abs(xt);
for(x=0;x<=abs(xt);x+=5)
  {

xn=xt+(t-=5);
ax[0][0]=xn;
ax[1][0]=0;
for(i=0;i<2;i++)
{
  for(int j=0;j<n;j++)
  {
  add[i][j] = mat[i][j]+ax[i][0];
  }
}

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

ln1[n][0]=ln1[0][0];
ln1[n][1]=ln1[0][1];
//cout<<"\nPolygon after translation along x-axis!!";

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

    if(x>=abs(xt))
      getch();
    else
      cleardevice();
    if(x>=abs(xt))
       goto label1;
}
}
break;

case 2:  cout<<"Enter translation factor along y-axis:";
cin>>yt;
      if(yt>0)
      {
for(y=0;y<=yt;y+=5)
  {
ay[0][0]=0;
ay[1][0]=y;
for(i=0;i<2;i++)
{
  for(int j=0;j<n;j++)
  {
  add[i][j] = mat[i][j]+ay[i][0];
  }
  }

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

ln1[n][0]=ln1[0][0];
ln1[n][1]=ln1[0][1];
//cout<<"\nPolygon after translation along y-axis!!";

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

  delay(60);

    if(y>=yt)
      getch();
    else
      cleardevice();
    if(y>=yt)
      goto label1;
  }
}

else
{
t=abs(yt);
for(y=0;y<=abs(yt);y+=5)
  {
yn=yt+(t-=5);
ay[0][0]=0;
ay[1][0]=yn;
for(i=0;i<2;i++)
{
  for(int j=0;j<n;j++)
  {
  add[i][j] = mat[i][j]+ay[i][0];
  }
  }

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

ln1[n][0]=ln1[0][0];
ln1[n][1]=ln1[0][1];
//cout<<"\nPolygon after translation along y-axis!!";

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

  delay(60);

    if(y>=abs(yt))
      getch();
    else
      cleardevice();
    if(y>=abs(yt))
      goto label1;
  }
}



break;

case 3:cout<<"Enter translation factor along:\n";
cout<<"x-axis:";
cin>>xt;
cout<<"y-axis:";
cin>>yt;

if(xt>0&&yt>0)
{
for(x=0,y=0;x<=xt||y<=yt;)               //for animation
    {
axy[0][0]=x;
axy[1][0]=y;

for(i=0;i<2;i++)
{
  for(int j=0;j<n;j++)
  {
  add[i][j] = mat[i][j]+axy[i][0];
  }
  }

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

ln1[n][0]=ln1[0][0];
ln1[n][1]=ln1[0][1];
//cout<<"\nPolygon after translation along x and y-axes!!";

for(i=0;i<n;i++)
  {

   line(ln1[i][0],ln1[i][1],ln1[i+1][0],ln1[i+1][1]);
  }

  delay(60);

    if(x>=xt&&y>=yt)
       getch();
    else
       cleardevice();
    if(x>=xt&&y>=yt)
       goto label1;

      if(x<xt)
      x+=5;
      if(y<yt)
      y+=5;
   }
}

       else if(xt<0&&yt<0)
{
t=abs(xt);
t1=abs(yt);
for(x=0,y=0;x<=abs(xt)||y<=abs(yt);)               //for animation
    {
if(x<abs(xt))
xn=xt+(t-=5);
if(y<abs(yt))
yn=yt+(t1-=5);

axy[0][0]=xn;
axy[1][0]=yn;

for(i=0;i<2;i++)
{
  for(int j=0;j<n;j++)
  {
  add[i][j] = mat[i][j]+axy[i][0];
  }
  }

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

ln1[n][0]=ln1[0][0];
ln1[n][1]=ln1[0][1];
//cout<<"\nPolygon after translation along x and y-axes!!";

for(i=0;i<n;i++)
  {

   line(ln1[i][0],ln1[i][1],ln1[i+1][0],ln1[i+1][1]);
  }

  delay(60);

    if(x>=abs(xt)&&y>=abs(yt))
       getch();
    else
       cleardevice();
    if(x>=abs(xt)&&y>=abs(yt))
       goto label1;

      if(x<abs(xt))
      x+=5;
      if(y<abs(yt))
      y+=5;
   }
}

else if(xt<0&&yt>0)
{
t=abs(xt);

for(x=0,y=0;x<=abs(xt)||y<=yt;)               //for animation
    {
if(x<abs(xt))
{
xn=xt+(t-=5);
}
axy[0][0]=xn;
axy[1][0]=y;

for(i=0;i<2;i++)
{
  for(int j=0;j<n;j++)
  {
  add[i][j] = mat[i][j]+axy[i][0];
  }
  }

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

ln1[n][0]=ln1[0][0];
ln1[n][1]=ln1[0][1];
//cout<<"\nPolygon after translation along x and y-axes!!";

for(i=0;i<n;i++)
  {

   line(ln1[i][0],ln1[i][1],ln1[i+1][0],ln1[i+1][1]);
  }

  delay(60);

    if(x>=abs(xt)&&y>=yt)
       getch();
    else
       cleardevice();
    if(x>=abs(xt)&&y>=yt)
       goto label1;

      if(x<abs(xt))
      x+=5;
      if(y<yt)
      y+=5;
   }
}

else if(xt>0&&yt<0)
{

t1=abs(yt);
for(x=0,y=0;x<=xt||y<=abs(yt);)               //for animation
    {

if(y<abs(yt))
{
yn=yt+(t1-=5);
}
axy[0][0]=x;
axy[1][0]=yn;

for(i=0;i<2;i++)
{
  for(int j=0;j<n;j++)
  {
  add[i][j] = mat[i][j]+axy[i][0];
  }
  }

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

ln1[n][0]=ln1[0][0];
ln1[n][1]=ln1[0][1];
//cout<<"\nPolygon after translation along x and y-axes!!";

for(i=0;i<n;i++)
  {

   line(ln1[i][0],ln1[i][1],ln1[i+1][0],ln1[i+1][1]);
  }

  delay(60);

    if(x>=xt&&y>=abs(yt))
       getch();
    else
       cleardevice();
    if(x>=xt&&y>=abs(yt))
       goto label1;

      if(x<xt)
      x+=5;
      if(y<abs(yt))
      y+=5;
   }
}


break;

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

 else
 {
  cout<<"\nPlease enter edges greater than 2!!";
 }
label1:
cout<<"\nDo you wanna continue(y/n)??:";
cin>>ch1;
}

closegraph();
}
                               OUTPUT

                       Click here for output

Source code for Happy Diwali Wishing program in C Graphics.

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