Advertisement
lukasd

zadanie 1 zestaw 18

Jan 15th, 2018
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.94 KB | None | 0 0
  1. typedef struct
  2. {
  3.     float x, y;
  4. }Point;
  5.  
  6. typedef struct
  7. {
  8.     Point a;
  9.     Point b;
  10. }Section;
  11. float euclid(Section k)
  12. {
  13.     return hypot(k.a.x - k.b.x, k.a.y - k.b.y);
  14.     //return sqrt(pow(k.a.x-k.b.x,2)+pow(k.a.y-k.b.y,2));
  15. }
  16. float city(Section k)
  17. {
  18.     return fabs(k.a.x - k.b.x) + fabs(k.a.y - k.b.y);
  19. }
  20. float river(Section k)
  21. {
  22.     if (k.a.x == k.b.x) return fabs(k.a.y - k.b.y);
  23.     else return fabs(k.a.y) + fabs(k.a.x - k.b.x) + fabs(k.b.y);
  24. }
  25. Section shortest(Point arr[], int n, float(*fun)(Section))
  26. {
  27.     float dist;
  28.     float mindist=FLT_MAX;
  29.     Section mins;
  30.     for (int i = 0; i < n; i++)
  31.     {
  32.         for (int j = i + 1; j < n; j++)
  33.         {
  34.             Section s = { arr[i],arr[j] };
  35.             dist = fun(s);
  36.             if (dist < mindist)
  37.             {
  38.                 mindist = dist;
  39.                 mins = s;
  40.             }
  41.         }
  42.     }
  43.     return mins;
  44. }
  45. void kolos1()
  46. {
  47.     Point arr[4] = { {1,2},{5,8},{-4,6},{-20,3} };
  48.     Section s = shortest(arr, 4, euclid);
  49.     printf("%f %f %f %f", s.a.x, s.a.y, s.b.x, s.b.y);
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement