Guest User

Untitled

a guest
May 20th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.61 KB | None | 0 0
  1. /*-------------------------------------------------------------------------
  2. Include files:
  3. --------------------------------------------------------------------------*/
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <math.h>
  8.  
  9.  
  10. /*=========================================================================
  11. Constants and definitions:
  12. ==========================================================================*/
  13.  
  14. /* put your #defines and typedefs here*/
  15.  
  16.  
  17.  
  18. /*-------------------------------------------------------------------------
  19. The main program. (describe what your program does here)
  20. -------------------------------------------------------------------------*/
  21. struct box
  22. {
  23. unsigned short size;
  24. unsigned int friends[5];
  25. };
  26. /*--------------------------------------------------------------------------------*/
  27. void create_boxes(struct box[10][10], float*x_p, float*y_p, unsigned int friends);
  28. int Is_Complete(float number);
  29. int Quarter(float x,float y);
  30. void the_best_friend(float *my_co, struct box boxes[10][10], float *x_p, float *y_p, float *best_friend);
  31.  
  32. /*----------------------------------------------------------------------------------*/
  33.  
  34. int main()
  35. {
  36. unsigned int number_of_friends;
  37. float *x_Coordinates,*y_Coordinates;
  38. float my_x,my_y;
  39. unsigned int i;
  40. struct box boxes[10][10]={{{0,{0,0,0,0,0}}}};
  41. float my_co_array[2];
  42. float best_friend[2];
  43.  
  44. printf(">Please enter the number of your friends:\n");
  45. printf(">Please enter your coordinates:\n");
  46. printf(">Please enter your friends' coordinates:\n");
  47. scanf("%u %f %f",&number_of_friends,&my_x,&my_y);
  48. x_Coordinates=(float *)malloc((number_of_friends) * sizeof(float));
  49. y_Coordinates=(float *)malloc((number_of_friends) * sizeof(float));
  50.  
  51. for(i=0;i<number_of_friends;i++)
  52. {
  53. scanf("%f %f",& x_Coordinates[i],& y_Coordinates[i]);
  54. }
  55. my_co_array[0]=my_x;
  56. my_co_array[1]=my_y;
  57.  
  58. create_boxes(boxes,x_Coordinates,y_Coordinates,number_of_friends);
  59.  
  60. /*for(i=0;i<10;i++)
  61. {
  62. for(j=0;j<10;j++)
  63. {
  64. printf("box [%u][%u] size is : %d\n",i,j,boxes[i][j].size);
  65. }
  66.  
  67. }
  68. */
  69. the_best_friend(my_co_array,boxes,x_Coordinates,y_Coordinates,best_friend);
  70.  
  71. printf("your best friend location is : %f %f \n",best_friend[0],best_friend[1]);
  72.  
  73. free(x_Coordinates);
  74. free(y_Coordinates);
  75. return 0;
  76. }
  77.  
  78.  
  79. void create_boxes(struct box boxes[10][10], float *x_p, float *y_p, unsigned int friends)
  80. {
  81. int co_x=0,co_y=0;
  82. int m,n;
  83. unsigned int i;
  84. for(i=0;i<friends;i++)
  85. {
  86. switch(Quarter(x_p[i],y_p[i]))
  87. {
  88. case 1:
  89. { co_x=(int)(x_p[i]);
  90. if(Is_Complete(y_p[i]))
  91. co_y=(int)(y_p[i]);
  92. else co_y=((int)(y_p[i]))+1;
  93. }break;
  94.  
  95. case 2:
  96. {
  97. if(Is_Complete(x_p[i]))
  98. co_x=(int)(x_p[i]);
  99. else
  100. co_x=(int)(x_p[i])-1;
  101. if(Is_Complete(y_p[i]))
  102. co_y=(int)(y_p[i]);
  103. else co_y=((int)(y_p[i]))+1;
  104. }break;
  105.  
  106. case 3:
  107. {
  108. if(Is_Complete(x_p[i]))
  109. co_x=(int)(x_p[i]);
  110. else
  111. co_x=(int)(x_p[i])-1;
  112. if(Is_Complete(y_p[i]))
  113. co_y=(int)(y_p[i]);
  114. else co_y=(int)(y_p[i]);
  115. }break;
  116.  
  117. case 4:
  118. {
  119. co_x=(int)x_p[i];
  120. if(Is_Complete(y_p[i]))
  121. co_y=(int)(y_p[i]);
  122. else co_y=(int)(y_p[i]);
  123.  
  124. }break;
  125.  
  126. }
  127.  
  128. if(co_y==-5)
  129. {
  130. m=4-co_y;
  131. n=5+co_x;
  132. if(co_x==5)
  133. n=4+co_x;
  134. }
  135. else if(co_x==5)
  136. {
  137. m=5-co_y;
  138. n=4+co_x;
  139. }
  140. else
  141. {
  142. m=5-co_y;
  143. n=5+co_x;
  144. }
  145.  
  146.  
  147. boxes[m][n].friends[boxes[m][n].size]=i;
  148. boxes[m][n].size++;
  149. }
  150.  
  151. return ;
  152. }
  153.  
  154. int Is_Complete(float number)
  155. {
  156. float a;
  157. a=number;
  158. return((int)(number)==a);
  159. }
  160. int Quarter(float x,float y)
  161. {
  162. if(x>0 && y>0) return 1;
  163. if(x<0 && y>0) return 2;
  164. if(x<0 && y<0) return 3;
  165. return 4;
  166. }
  167.  
  168. void the_best_friend(float *my_co, struct box boxes[10][10], float *x_p, float *y_p, float *best_friend)
  169. {
  170. unsigned int my_box_x,my_box_y,shell_size,i,j,friend_index;
  171. float min_dis=10.0,temp_dis;
  172. int temp [2];
  173. float friend_x_temp,friend_y_temp;
  174. unsigned begin_row,begin_col,temp_col;
  175. switch(Quarter(my_co[0],my_co[1]))
  176. {
  177. case 1:
  178. { temp[0]=(int)(my_co[0]);
  179. if(Is_Complete(my_co[1]))
  180. temp[1]=(int)(my_co[1]);
  181. else temp[1]=((int)(my_co[1]))+1;
  182. }break;
  183.  
  184. case 2:
  185. {
  186. if(Is_Complete(my_co[0]))
  187. temp[0]=(int)(my_co[0]);
  188. else
  189. temp[0]=(int)(my_co[0])-1;
  190. if(Is_Complete(my_co[1]))
  191. temp[1]=(int)(my_co[1]);
  192. else temp[1]=((int)(my_co[1]))+1;
  193. }break;
  194.  
  195. case 3:
  196. {
  197. if(Is_Complete(my_co[0]))
  198. temp[0]=(int)(my_co[0]);
  199. else
  200. temp[0]=(int)(my_co[0])-1;
  201. if(Is_Complete(my_co[1]))
  202. temp[1]=(int)(my_co[1]);
  203. else temp[1]=(int)(my_co[1]);
  204. }break;
  205.  
  206. case 4:
  207. {
  208. temp[0]=(int)my_co[0];
  209. if(Is_Complete(my_co[1]))
  210. temp[1]=(int)(my_co[1]);
  211. else temp[1]=(int)(my_co[1]);
  212.  
  213. }break;
  214.  
  215. }
  216. if(temp[1]==-5)
  217. {
  218. my_box_x=4-temp[1];
  219. my_box_y=5+temp[0];
  220. if(temp[0]==5)
  221. my_box_y=4+temp[0];
  222. }
  223. else if(temp[0]==5)
  224. {
  225. my_box_x=5-temp[1];
  226. my_box_y=4+temp[0];
  227. }
  228. else
  229. {
  230. my_box_x=5-temp[1];
  231. my_box_y=5+temp[0];
  232. }
  233. /*----------------------------------------------------------------------*/
  234. if(my_box_x==0)
  235. { begin_row=0;
  236. shell_size=4;
  237. if(my_box_y==0)
  238. {
  239. begin_col=0;
  240. }
  241. else if(my_box_y==9)
  242. {
  243. begin_col=8;
  244. }
  245. else
  246. {
  247. shell_size=6;
  248. begin_col=my_box_y-1;
  249. }
  250. }
  251. else if(my_box_x==9)
  252. {
  253. begin_row=8;
  254. shell_size=4;
  255. if(my_box_y==0)
  256. {
  257. begin_col=0;
  258. }
  259. else if(my_box_y==9)
  260. {
  261. begin_col=8;
  262. }
  263. else
  264. {
  265. shell_size=6;
  266. begin_col=my_box_y-1;
  267. }
  268. }
  269.  
  270. else
  271. {
  272. begin_row=my_box_x-1;
  273. shell_size=6;
  274. if(my_box_y==0) begin_col=0;
  275. else if(my_box_y==9) begin_col=8;
  276. else { shell_size=9; begin_col=my_box_y-1; }
  277. }
  278. /*matches the shell size and the begin row and col for each case of the nine*/
  279. /*-----------------------------------------------------------------------------*/
  280. temp_col=begin_col;
  281. for(i=0;i<shell_size;i++)
  282. {
  283. for(j=0;j<boxes[begin_row][begin_col].size;j++)
  284. {
  285.  
  286. friend_index=boxes[begin_row][begin_col].friends[j];
  287. friend_x_temp=x_p[friend_index];
  288. friend_y_temp=y_p[friend_index];
  289.  
  290.  
  291. temp_dis=pow(((my_co[0])-(friend_x_temp)),2)+pow(((my_co[1])-(friend_y_temp)), 2);
  292. if(temp_dis<min_dis)
  293. {
  294. min_dis=temp_dis;
  295. best_friend[0]=friend_x_temp;
  296. best_friend[1]=friend_y_temp;
  297. }
  298. }
  299. begin_col++;
  300. if(((i+1)%3)==0 && ((shell_size==6) || (shell_size==9)))
  301. {
  302. begin_row++;
  303. begin_col=temp_col;
  304. }
  305. else if(((i+1)%2)==0 && (shell_size==2))
  306. {
  307. begin_row++;
  308. begin_col=temp_col;
  309. }
  310.  
  311. }
  312.  
  313. return ;
  314.  
  315.  
  316. }
Add Comment
Please, Sign In to add comment