kartikkukreja

Closest pair of points

May 24th, 2013
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.80 KB | None | 0 0
  1. #include <cstdio>
  2. #include <algorithm>
  3. #include <cmath>
  4. #include <limits>
  5. using namespace std;
  6.  
  7. struct Point {
  8.     double x, y;
  9. };
  10.  
  11. Point result1, result2;
  12. double bestDistance;
  13.  
  14. double euclideanDistance(Point a, Point b)  {
  15.     double X = a.x - b.x, Y = a.y - b.y;
  16.     return sqrt(X*X + Y*Y);
  17. }
  18.  
  19. // comparison first done by y coordinate, then by x coordinate
  20. bool less(Point a, Point b) {
  21.     if(a.y < b.y) return true;
  22.     if(a.y > b.y) return false;
  23.     return a.x < b.x;
  24. }
  25.  
  26. void merge(Point* a, Point* aux, int lo, int mid, int hi)   {
  27.     int i, j, k;
  28.     for(k = lo; k <= hi; k++)
  29.         aux[k] = a[k];
  30.  
  31.     i = lo; j = mid + 1; k = lo;
  32.     while(i <= mid && j <= hi)
  33.         a[k++] = less(aux[i], aux[j]) ? aux[i++] : aux[j++];
  34.  
  35.     // Copy the rest of the left side of the array into the target array
  36.     while(i <= mid)
  37.         a[k++] = aux[i++];
  38. }
  39.  
  40. double closestPair(Point* pointsByX, Point* pointsByY, Point* aux, int lo, int hi)    {
  41.     if(hi <= lo)
  42.         return numeric_limits<double>::infinity();
  43.  
  44.     int mid = lo + (hi - lo)/2;
  45.     double delta = closestPair(pointsByX, pointsByY, aux, lo, mid);
  46.     double dist = closestPair(pointsByX, pointsByY, aux, mid+1, hi);
  47.     if(dist < delta)
  48.         delta = dist;
  49.  
  50.     merge(pointsByY, aux, lo, mid, hi);
  51.  
  52.     int M = 0, i, j;
  53.     for(i = lo; i <= hi; i++)
  54.         if(abs(pointsByY[i].x - pointsByX[mid].x) < delta)
  55.             aux[M++] = pointsByY[i];
  56.  
  57.     double distance, t;
  58.     for(i = 0; i < M; i++)  {
  59.         for(j = i+1; j < M && (aux[j].y - aux[i].y < delta); j++) {
  60.             distance = euclideanDistance(aux[i], aux[j]);
  61.             if(distance < delta)    {
  62.                 delta = distance;
  63.                 if(delta < bestDistance) {
  64.                     bestDistance = delta;
  65.                     result1 = aux[i];
  66.                     result2 = aux[j];
  67.                 }
  68.             }
  69.         }
  70.     }
  71.     return delta;
  72. }
  73.  
  74. bool X_ORDER(Point a, Point b)  {
  75.     return a.x < b.x;
  76. }
  77.  
  78. int main()  {
  79.     int N, i;
  80.     Point *points, *pointsByY, *aux;
  81.  
  82.     //freopen("input.txt", "r", stdin);
  83.  
  84.     scanf("%d", &N);    //Enter the number of points in the plane
  85.     points = new Point[N];
  86.     for(i=0; i<N; i++)  // Enter N points (x, y)
  87.         scanf("%lf %lf", &points[i].x, &points[i].y);
  88.  
  89.     if(N <= 1) return 0;
  90.  
  91.     sort(points, points + N, X_ORDER);
  92.     pointsByY = new Point[N];
  93.     for(i=0; i<N; i++)
  94.         pointsByY[i] = points[i];
  95.     aux = new Point[N];
  96.  
  97.     bestDistance = numeric_limits<double>::infinity();
  98.     closestPair(points, pointsByY, aux, 0, N-1);
  99.  
  100.     // print the closest pair of points and their euclidean distance
  101.     printf("%lf %lf\n", result1.x, result1.y);
  102.     printf("%lf %lf\n", result2.x, result2.y);
  103.     printf("%lf\n", bestDistance);
  104.  
  105.     return 0;
  106. }
Advertisement
Add Comment
Please, Sign In to add comment