Thursday, January 7, 2016

program to check if a cordinates fall in black or white box of an infinite chess board

Consider the image as an infinite chessboard.With each block of 4 units.The boxes are colored alternatively black and white.

Now given the x and y co-ordinates of any point on this board we have to decide whether it lies inside a white box or back square.The points on border will be considered as  present in black.

All blue dots are inside black square while the whites are in white square.




#include <cstdlib>
#include <iostream>
#define steps 4
using namespace std;


void insideblack(int x,int y);

int main(int argc, char *argv[])
{
     insideblack(5,4);
   
    system("PAUSE");
    return EXIT_SUCCESS;
}
void insideblack(int x,int y)
{      
     bool inx,iny,xyin;
     //for x cordinates
     if(x<=steps)
     {
       //cout<<x<<" is on black x-axis"<<endl;
       inx=true;
     }
     else
     {        
          if(  ((x/steps)%2 !=0 ) && ( x%4 != 0))
          {
              //cout<<x<<" is on white x-axis"<<endl;
              inx=false;
          }
           else
           {
              //cout<<x<<" is on black x-axis"<<endl;
              inx=true;
           }
       }
   
     //for y cordinates
     if(y<=steps)
     {
       //cout<<y<<" is on black x-axis"<<endl;
       iny=true;
     }
     else
     {        
          if(  ((y/steps)%2 ==0 ) && ( y%4 != 0))
          {
              //cout<<y<<" is on black y-axis"<<endl;
              iny=true;
          }
          else
          {
               //cout<<y<<" is on white y-axsis"<<endl;
               iny=false;
          }
     }
     if(inx==true && iny==true)
     {
        xyin=true;
     }
     if(inx==true && iny==false)
     {
        if(x>0 && x%steps ==0) //border cases
        {xyin=true;}
        else
        {xyin=false;}
     }
     if(inx==false && iny==true)
     {
        if(y>0 && y%steps==0)//border condition
        {
           xyin=true;
        }
        else
        {
           xyin=false;
        }
     
     }
     if(inx==false && iny==false)
     {
        xyin=true;
     }
     
       if(xyin==true)
       cout<<"cordinates in black region"<<endl;
       else
       cout<<"cordinates in white region"<<endl;
           
}