Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*-------------------------------------------------------------------------
- Include files:
- --------------------------------------------------------------------------*/
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- /*=========================================================================
- Constants and definitions:
- ==========================================================================*/
- /* put your #defines and typedefs here*/
- /*-------------------------------------------------------------------------
- The main program. (describe what your program does here)
- -------------------------------------------------------------------------*/
- struct box
- {
- unsigned short size;
- unsigned int friends[5];
- };
- /*--------------------------------------------------------------------------------*/
- void create_boxes(struct box[10][10], float*x_p, float*y_p, unsigned int friends);
- int Is_Complete(float number);
- int Quarter(float x,float y);
- void the_best_friend(float *my_co, struct box boxes[10][10], float *x_p, float *y_p, float *best_friend);
- /*----------------------------------------------------------------------------------*/
- int main()
- {
- unsigned int number_of_friends;
- float *x_Coordinates,*y_Coordinates;
- float my_x,my_y;
- unsigned int i;
- struct box boxes[10][10]={{{0,{0,0,0,0,0}}}};
- float my_co_array[2];
- float best_friend[2];
- printf(">Please enter the number of your friends:\n");
- printf(">Please enter your coordinates:\n");
- printf(">Please enter your friends' coordinates:\n");
- scanf("%u %f %f",&number_of_friends,&my_x,&my_y);
- x_Coordinates=(float *)malloc((number_of_friends) * sizeof(float));
- y_Coordinates=(float *)malloc((number_of_friends) * sizeof(float));
- for(i=0;i<number_of_friends;i++)
- {
- scanf("%f %f",& x_Coordinates[i],& y_Coordinates[i]);
- }
- my_co_array[0]=my_x;
- my_co_array[1]=my_y;
- create_boxes(boxes,x_Coordinates,y_Coordinates,number_of_friends);
- /*for(i=0;i<10;i++)
- {
- for(j=0;j<10;j++)
- {
- printf("box [%u][%u] size is : %d\n",i,j,boxes[i][j].size);
- }
- }
- */
- the_best_friend(my_co_array,boxes,x_Coordinates,y_Coordinates,best_friend);
- printf("your best friend location is : %f %f \n",best_friend[0],best_friend[1]);
- free(x_Coordinates);
- free(y_Coordinates);
- return 0;
- }
- void create_boxes(struct box boxes[10][10], float *x_p, float *y_p, unsigned int friends)
- {
- int co_x=0,co_y=0;
- int m,n;
- unsigned int i;
- for(i=0;i<friends;i++)
- {
- switch(Quarter(x_p[i],y_p[i]))
- {
- case 1:
- { co_x=(int)(x_p[i]);
- if(Is_Complete(y_p[i]))
- co_y=(int)(y_p[i]);
- else co_y=((int)(y_p[i]))+1;
- }break;
- case 2:
- {
- if(Is_Complete(x_p[i]))
- co_x=(int)(x_p[i]);
- else
- co_x=(int)(x_p[i])-1;
- if(Is_Complete(y_p[i]))
- co_y=(int)(y_p[i]);
- else co_y=((int)(y_p[i]))+1;
- }break;
- case 3:
- {
- if(Is_Complete(x_p[i]))
- co_x=(int)(x_p[i]);
- else
- co_x=(int)(x_p[i])-1;
- if(Is_Complete(y_p[i]))
- co_y=(int)(y_p[i]);
- else co_y=(int)(y_p[i]);
- }break;
- case 4:
- {
- co_x=(int)x_p[i];
- if(Is_Complete(y_p[i]))
- co_y=(int)(y_p[i]);
- else co_y=(int)(y_p[i]);
- }break;
- }
- if(co_y==-5)
- {
- m=4-co_y;
- n=5+co_x;
- if(co_x==5)
- n=4+co_x;
- }
- else if(co_x==5)
- {
- m=5-co_y;
- n=4+co_x;
- }
- else
- {
- m=5-co_y;
- n=5+co_x;
- }
- boxes[m][n].friends[boxes[m][n].size]=i;
- boxes[m][n].size++;
- }
- return ;
- }
- int Is_Complete(float number)
- {
- float a;
- a=number;
- return((int)(number)==a);
- }
- int Quarter(float x,float y)
- {
- if(x>0 && y>0) return 1;
- if(x<0 && y>0) return 2;
- if(x<0 && y<0) return 3;
- return 4;
- }
- void the_best_friend(float *my_co, struct box boxes[10][10], float *x_p, float *y_p, float *best_friend)
- {
- unsigned int my_box_x,my_box_y,shell_size,i,j,friend_index;
- float min_dis=10.0,temp_dis;
- int temp [2];
- float friend_x_temp,friend_y_temp;
- unsigned begin_row,begin_col,temp_col;
- switch(Quarter(my_co[0],my_co[1]))
- {
- case 1:
- { temp[0]=(int)(my_co[0]);
- if(Is_Complete(my_co[1]))
- temp[1]=(int)(my_co[1]);
- else temp[1]=((int)(my_co[1]))+1;
- }break;
- case 2:
- {
- if(Is_Complete(my_co[0]))
- temp[0]=(int)(my_co[0]);
- else
- temp[0]=(int)(my_co[0])-1;
- if(Is_Complete(my_co[1]))
- temp[1]=(int)(my_co[1]);
- else temp[1]=((int)(my_co[1]))+1;
- }break;
- case 3:
- {
- if(Is_Complete(my_co[0]))
- temp[0]=(int)(my_co[0]);
- else
- temp[0]=(int)(my_co[0])-1;
- if(Is_Complete(my_co[1]))
- temp[1]=(int)(my_co[1]);
- else temp[1]=(int)(my_co[1]);
- }break;
- case 4:
- {
- temp[0]=(int)my_co[0];
- if(Is_Complete(my_co[1]))
- temp[1]=(int)(my_co[1]);
- else temp[1]=(int)(my_co[1]);
- }break;
- }
- if(temp[1]==-5)
- {
- my_box_x=4-temp[1];
- my_box_y=5+temp[0];
- if(temp[0]==5)
- my_box_y=4+temp[0];
- }
- else if(temp[0]==5)
- {
- my_box_x=5-temp[1];
- my_box_y=4+temp[0];
- }
- else
- {
- my_box_x=5-temp[1];
- my_box_y=5+temp[0];
- }
- /*----------------------------------------------------------------------*/
- if(my_box_x==0)
- { begin_row=0;
- shell_size=4;
- if(my_box_y==0)
- {
- begin_col=0;
- }
- else if(my_box_y==9)
- {
- begin_col=8;
- }
- else
- {
- shell_size=6;
- begin_col=my_box_y-1;
- }
- }
- else if(my_box_x==9)
- {
- begin_row=8;
- shell_size=4;
- if(my_box_y==0)
- {
- begin_col=0;
- }
- else if(my_box_y==9)
- {
- begin_col=8;
- }
- else
- {
- shell_size=6;
- begin_col=my_box_y-1;
- }
- }
- else
- {
- begin_row=my_box_x-1;
- shell_size=6;
- if(my_box_y==0) begin_col=0;
- else if(my_box_y==9) begin_col=8;
- else { shell_size=9; begin_col=my_box_y-1; }
- }
- /*matches the shell size and the begin row and col for each case of the nine*/
- /*-----------------------------------------------------------------------------*/
- temp_col=begin_col;
- for(i=0;i<shell_size;i++)
- {
- for(j=0;j<boxes[begin_row][begin_col].size;j++)
- {
- friend_index=boxes[begin_row][begin_col].friends[j];
- friend_x_temp=x_p[friend_index];
- friend_y_temp=y_p[friend_index];
- temp_dis=pow(((my_co[0])-(friend_x_temp)),2)+pow(((my_co[1])-(friend_y_temp)), 2);
- if(temp_dis<min_dis)
- {
- min_dis=temp_dis;
- best_friend[0]=friend_x_temp;
- best_friend[1]=friend_y_temp;
- }
- }
- begin_col++;
- if(((i+1)%3)==0 && ((shell_size==6) || (shell_size==9)))
- {
- begin_row++;
- begin_col=temp_col;
- }
- else if(((i+1)%2)==0 && (shell_size==2))
- {
- begin_row++;
- begin_col=temp_col;
- }
- }
- return ;
- }
Add Comment
Please, Sign In to add comment