#include#include   //Header files are files with extension of .h which contains C function declarations and macro definitions//#include                           //these are shared between several source files.//#define MinLength 1                                          //Definitions of the .c file.//#define MaxLength 64   //#define is the preprocessor directive in c. It is used to define the preprocessor macros for the texts.//#define MinWidth 1            //The #define is used to make substitutions throughout the file in which it is located.//#define MaxWidth 64               //The program will have a defined range with a minimum of 1 to a maximum of 64.//#define Black 0#define White 1#define Grey 2typedef struct node                                           //Structure node.//{                    //The node containing the declarations of length, width, coordinates (X,Y) and color (black, white or grey).// int PositionX; int PositionY; int Value; int Length; int Width; int** MainArray; struct node* Sub4;}Node;void ExecuteFile (char *File_Name, Node *node);   //Declaring functions used in code.//void LayNode(Node* node,int PositionX, int PositionY, int Width, int Value, int** MainArray);int Validity (int x);void Quadtree(Node *node);int CheckColor (Node *node, int Top, int Bot, int Left, int Right);int** ProduceArray (Node *node, int Top, int Bot, int Left, int Right, int Center);int main (int ArgA, char *ArgB)   //The main function is specified to start the program, resulting in reading all the functions.//{ Node SourceNode; if (ArgA!= 2) {  puts (“Error executing input!”);   //Error message given if the program fails to launch or read.//  exit(-1); } ExecuteFile(ArgB1, &SourceNode); Quadtree(&SourceNode);}//Main functions used in running the program.//void ExecuteFile (char *File_Name, Node* node)   //The program starts by reading the file, where it allocates memory and load time for the file.//{                      //Terminals color, width and coordinate is then defined and stored in memory with allocated space.// int CountedBlackTerminals=1, Counter=0, i, j, a, b, TerminalWidth, XValue, YValue, V, ValidSize, BlackTerminalsNumber; long String, ReadMag; int** PrimaryArray; char *loading=NULL; FILE *fp;   //Command to open file (supposedly .txt file).// fp=fopen(File_Name,”r”); if (fp==NULL) {  puts(“Invalid input file!”);   //Message displayed if invalid file is read.//  exit (-1); } if (fp) {  fseek(fp,0,SEEK_END);  String=ftell(fp);  rewind(fp);  loading=(char*) malloc (sizeof(char) * (String + 1));   //File is being buffered.//  ReadMag=fread(loading,sizeof(char),String,fp);  loadingString=’’;  if(loadingString-1==’
‘)  {   loadingString-1=’’;  }  if (String!=ReadMag)  {   free(loading);   loading=NULL;   puts(“Error storing file!”);   exit(-1);  } }   //All terminals are read as white pixels. When the coordinates are read along with coordinates, some of them change to black.// char *str=loading, *p=str; while (*p) {  if (*p==’
‘)  {   CountedBlackTerminals++;   //Counted number of black terminals after they have been converted (as mentioned in the previous comment.//   p++;  }  else if (isdigit(*p))  {   V=strtol(p, &p, 10);   if (Counter==0)   {    TerminalWidth=V;    ValidSize=Validity(TerminalWidth);    if (ValidSize!=1)    {     puts(“Image width does not match!”);   //If width and data given do not match.//     exit (-1);    }    if (TerminalWidthMaxWidth)    {     puts(“Image is bigger than maximum image width!”);   //Error given if the image exceeds the maximum range.//     exit (-1);    }    Counter++;    PrimaryArray=(int**) malloc(TerminalWidth*sizeof(int*));    for(i=0; i(TerminalWidth*TerminalWidth))    {     puts(“Number of black no terminals exceed limit!”);   //Error when black pixels exceed limit.//     exit (-1);    }    if (BlackTerminalsNumber==(TerminalWidth*TerminalWidth))    {     puts(“Image contains only black terminals.”);   //Message given when all pixels are black.//     exit (-1);    }    if (BlackTerminalsNumber==0)    {     puts(“Image contains only white terminals.”);   //Message given when all pixels are white.//     exit (-1);    }    if (BlackTerminalsNumber<0)    {     puts("There is less than 0 black terminals.");   //Message given if termination gives and invalid value of black terminals.//     exit (-1);    }    Counter++;   }                               //Function is executed multiple times to ensure that the information is within the range.//   else if (Counter%2==0)   {    XValue=V;    Counter++;   }   else   {    YValue=V;    if (XValue<0 || XValue>=TerminalWidth || YValue<0 || YValue>=TerminalWidth)    {     printf(“Coordinate (%i,%i) is out of bounds.
“, XValue, YValue);   //Displaying coordinates that are out of bounds.//     exit (-1);    }    PrimaryArrayXValueYValue=Black;    Counter++;   }  }  else  {   p++;  } } CountedBlackTerminals=CountedBlackTerminals-2;   //Program checks and reads the number of black terminals theoretically placed.// if (BlackTerminalsNumber!= CountedBlackTerminals) {  puts(“Number of black terminals does not equal the number counted!”);   //When theoretical number of terminals counted and displayed does not match//  exit (0);                                                                                  //this error message is displayed.// } b=TerminalWidth-1; LayNode(node, 0, b, TerminalWidth, 2, PrimaryArray);   //LayNode is used to place the black terminal after it has been configured.// free(loading); for (a=0; a1)   //This is used to check if the value of x given is true.// x/=2; return x;}void LayNode(Node* newNode, int PositionX, int PositionY, int Width, int Value, int** Nodes)   //This bit is used to place information calculated and//{                                                                                                          //defined on the terminals.// int i,j,k; newNode->PositionX=PositionX;   //The X position is defined.// newNode->PositionY=PositionY;   //The Y position is defined.// newNode->Width=Width;   //The width of the terminal is defined.// newNode->Value=Value;   //The value of the terminal is defined.// for (k=0; k<4; k++) newNode->Subk=(Node*) malloc(sizeof(Node)); newNode->MainArray=(int**) malloc(Width*sizeof(int*)); for(i=0; iMainArrayi=(int*) malloc(Width*sizeof(int));  for (j=0; jMainArrayij=Nodesij;   //Data is then passed down to terminals.//  } }}void Quadtree(Node *node){ int x, Value, Center, upperrange, edge, a, PreviousPositionX,PreviousPositionY, PositionX, PositionY;   //Defining terminal variables (data).// int** DataArray; PreviousPositionX=node->PositionX; PreviousPositionY=node->PositionY; upperrange=((node->Width)/2);   //Calculating terminals data.// Center=upperrange-1; edge=node->Width-1; for (x=0; x<4; x++)   //Four (4) main quadrants for terminal calculation and placement.// {  switch(x)  {   case 0: //North-West (NW).//   Value=CheckColor(node, 0, Center, 0, Center);   DataArray=ProduceArray(node, 0, Center, 0, Center, upperrange);   PositionX=PreviousPositionX;   PositionY=PreviousPositionY-upperrange;   LayNode(node->Sub0, PositionX, PositionY, upperrange, Value, DataArray);   for (a=0; aSub0);   break;   case 1: //North-East (NE).//   Value=CheckColor(node, 0, Center, upperrange, edge);   DataArray=ProduceArray(node, 0, Center, upperrange, edge, upperrange);   PositionX=PreviousPositionX+upperrange;   PositionY=PreviousPositionY-upperrange;   LayNode(node->Sub1, PositionX , PositionY , upperrange, Value, DataArray);   for (a=0; aSub1);   break;   case 2: //South-West (SW).//   Value=CheckColor(node, upperrange, edge, 0, Center);   DataArray=ProduceArray(node, upperrange, edge, 0, Center, upperrange);   PositionX=PreviousPositionX;   PositionY=PreviousPositionY;   LayNode(node->Sub2, PositionX, PositionY, upperrange, Value, DataArray);   for (a=0; aSub2);   break;   case 3: //South-East (SE).//   Value=CheckColor(node, upperrange, edge, upperrange, edge);   DataArray=ProduceArray(node, upperrange, edge, upperrange, edge, upperrange);   PositionX=PreviousPositionX+upperrange;   PositionY=PreviousPositionY;   LayNode(node->Sub3, PositionX, PositionY, upperrange, Value, DataArray);   for (a=0; aSub3);   //Terminal printed with all data required and then code breaks.//   break;  } }}int CheckColor (Node *node, int Top, int Bot, int Left, int Right)   //Terminal directions are defined.//{ int i,j,BlackCount=0, WhiteCount=0;   //Counting of terminals.// for (j=Top; j<=Bot; j++)   //Vertical directions.// {  for (i=Left; i<=Right; i++)   //Horizontal directions.//  {   if (node->MainArrayij==White)   //Counting white terminals.//   {    WhiteCount++;   }   else if (node->MainArrayij==Black)   //Counting black terminals.//   {    BlackCount ++;   }  } } if (WhiteCount==0)   //If there is no white terminals the function returns.// {  return Black; } if (BlackCount==0)   //If there is no black terminals the function returns.// {  return White; } return Grey;}int** ProduceArray (Node *node, int Top, int Bot, int Left, int Right, int Center)   //Defining terminal data.//{ int i=0,j=0,x,y,z; int** DataArray=(int**) malloc(Center*sizeof(int*));    //This part creates a temporary array where it allocates memory for it.// for(z=0; z < Center; z++) {  DataArrayz=(int*) malloc(Center*sizeof(int)); } for (y=Top; y<=Bot; y++) {  for (x=Left; x<=Right; x++)  {   DataArrayij=node->MainArrayxy;   //New terminal data is combined with old data, creating an overlapped version.//   i=i+1;   if (i==Center)   {    j=j+1;LayNode;    //Terminal is saved.//    i=0;   }  } } return DataArray;   //Function returns.//}


I'm Cody!

Would you like to get a custom essay? How about receiving a customized one?

Check it out