Advertisement
Guest User

Untitled

a guest
Jun 27th, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.35 KB | None | 0 0
  1. #include <iterator>
  2. #include <stdint.h>
  3. #include <limits>
  4. #include <vector>
  5. #include <algorithm>
  6. #include <cmath>
  7. #include <assert.h>
  8. #include <iterator>
  9. #include <sstream>
  10. #include "iostream"
  11.  
  12. using namespace std;
  13.  
  14. struct Point
  15. {
  16.     int x, y;
  17. };
  18.  
  19. void print_arrat(vector<Point> points) {
  20.     for (int i = 0; i < points.size(); ++i) {
  21.         Point current = points.at(i);
  22.         cout << current.x << " " << current.y << endl;
  23.     }
  24. }
  25.  
  26. float cotan(Point p1, Point p2) {
  27.     return (p2.x - p1.x) / (p2.y - p1.y);
  28. }
  29.  
  30. int dist(Point p1, Point p2)
  31. {
  32.     return (p1.x - p2.x)*(p1.x - p2.x) +
  33.           (p1.y - p2.y)*(p1.y - p2.y);
  34. }
  35.  
  36. int comparePoints(Point p1, Point p2, Point reference) {
  37.     if (p1.y == reference.y && p2.y == reference.y) {
  38.         return (dist(p1, reference) < dist(p2, reference)) ? -1 : 1;
  39.     } else if (p1.y == reference.y) {
  40.         return 1;
  41.     } else if (p2.y == reference.y) {
  42.         return -1;
  43.     } else {
  44.         float cotan_p1 = cotan(reference, p1);
  45.         float cotan_p2 = cotan(reference, p2);
  46.         if (cotan_p1 == cotan_p2) {
  47.             return 0;
  48.         }
  49.         return (cotan_p1 < cotan_p2) ? -1 : 1;
  50.     }
  51. }
  52.  
  53. class PointSorter{
  54.     Point point_;
  55. public:
  56.     PointSorter(Point point){ point_ = point; }
  57.     int operator()(Point p1, Point p2) const {
  58.         return comparePoints(p1 , p2 , point_);
  59.     }
  60. };
  61.  
  62. void find_convex_hull(vector<Point> points) {
  63.     Point lowest = points.at(0);
  64.     int index = 0;
  65.     // TODO refactor
  66.     for (int i = 1; i < points.size(); ++i) {
  67.         Point current = points.at(i);
  68.         if (current.y < lowest.y) {
  69.             lowest = current;
  70.             index = i;
  71.         } else if (current.y == lowest.y && current.x < lowest.x) {
  72.             index = i;
  73.             lowest = current;
  74.         }
  75.     }
  76.     // Swap lowest point to the beginning of the list
  77.     Point tmp = points.at(0);
  78.     points.at(0) = lowest;
  79.     points.at(index) = tmp;
  80.     print_arrat(points);
  81.     cout << endl;
  82.     sort(++points.begin(), points.end(), PointSorter(lowest));
  83.     print_arrat(points);
  84. }
  85.  
  86. int main(int argc, char *argv[]) {
  87.     Point points[] = {{0, 3}, {1, 1}, {2, 2}, {4, 4},
  88.                       {0, 0}, {1, 2}, {3, 1}, {3, 3}, {-2, 2}};
  89.     vector<Point> bulb(points, points + sizeof points / sizeof points[0]);
  90.     find_convex_hull(bulb);
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement