fork download
  1. // A recursive function to find the smallest distance. The array Px contains
  2. // all points sorted according to x coordinates and Py contains all points
  3. // sorted according to y coordinates
  4. float closestUtil(Point Px[], Point Py[], int n)
  5. {
  6. // If there are 2 or 3 points, then use brute force
  7. if (n <= 3)
  8. return bruteForce(Px, n);
  9.  
  10. // Find the middle point
  11. int mid = n/2;
  12. Point midPoint = Px[mid];
  13.  
  14.  
  15. // Divide points in y sorted array around the vertical line.
  16. // Assumption: All x coordinates are distinct.
  17. std::cout << "mid = " << mid;
  18. int size_a = mid + 1;
  19. int size_b = n - mid - 1;
  20. Point *Pyl = new Point[size_a]; // y sorted points on left of vertical line
  21. Point *Pyr = new Point[size_b]; // y sorted points on right of vertical line
  22. int li = 0, ri = 0; // indexes of left and right subarrays
  23. for (int i = 0; i < n; i++)
  24. {
  25. if (Py[i].x <= midPoint.x)
  26. Pyl[li++] = Py[i];
  27. else
  28. Pyr[ri++] = Py[i];
  29. }
  30.  
  31. // Consider the vertical line passing through the middle point
  32. // calculate the smallest distance dl on left of middle point and
  33. // dr on right side
  34. float dl = closestUtil(Px, Pyl, mid);
  35. float dr = closestUtil(Px + mid, Pyr, n-mid);
  36.  
  37. // Find the smaller of two distances
  38. float d = min(dl, dr);
  39.  
  40. // Build an array strip[] that contains points close (closer than d)
  41. // to the line passing through the middle point
  42. Point *strip = new Point[n];
  43. int j = 0;
  44. for (int i = 0; i < n; i++)
  45. if (abs(Py[i].x - midPoint.x) < d)
  46. strip[j] = Py[i], j++;
  47.  
  48. // Find the closest points in strip. Return the minimum of d and closest
  49. // distance is strip[]
  50. return min(d, stripClosest(strip, j, d) );
  51. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:4:20: error: 'Point' was not declared in this scope
  float closestUtil(Point Px[], Point Py[], int n)
                    ^
prog.cpp:4:32: error: 'Point' was not declared in this scope
  float closestUtil(Point Px[], Point Py[], int n)
                                ^
prog.cpp:4:44: error: expected primary-expression before 'int'
  float closestUtil(Point Px[], Point Py[], int n)
                                            ^
prog.cpp:4:49: error: expression list treated as compound expression in initializer [-fpermissive]
  float closestUtil(Point Px[], Point Py[], int n)
                                                 ^
prog.cpp:5:2: error: expected ',' or ';' before '{' token
  {
  ^
stdout
Standard output is empty