Advertisement
Quebonamade

Untitled

Jan 12th, 2020
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.21 KB | None | 0 0
  1. //ALGO2 IS1 213B LAB10
  2. //MACIEJ DOMINIAK
  3. //dm44308@zut.edu.pl
  4. #include <iostream>
  5. #include <fstream>
  6. #include <ctime>
  7. #include <cstdlib>
  8. #include <stdio.h>
  9. using namespace std;
  10. class Point {
  11. public:
  12.     double x;
  13.     double y;
  14.     int index;
  15.  
  16.     void printPoint(int i) {
  17.         cout << index << " Point(" << x << "," << y << ")" << endl;
  18.     }
  19. };
  20. class Node {
  21. public:
  22.     Point* point;
  23.     Node* next;
  24. };
  25. class List {
  26. public:
  27.     Node* head;
  28.     int size;
  29.     List() {
  30.         head = NULL;
  31.         size = 0;
  32.     }
  33.     void addBack(Point* point) {
  34.         Node* temp = new Node;
  35.         temp->point = point;
  36.         temp->next = NULL;
  37.         if (head == NULL) {
  38.             head = temp;
  39.             return;
  40.         }
  41.         else {
  42.             Node* last = head;
  43.             while (last->next)last = last->next;
  44.             last->next = temp;
  45.             size++;
  46.         }
  47.  
  48.     }
  49.     void deleteSec() {
  50.         Node* current = head;
  51.         Node* curr_succ = current->next;
  52.         Node* succ_succ = curr_succ->next;
  53.         while (succ_succ->next != NULL) {
  54.             current = current->next;
  55.             curr_succ = curr_succ->next;
  56.             succ_succ = succ_succ->next;
  57.         }
  58.         current->next = succ_succ;
  59.         size--;
  60.     }
  61.     Node* getTopNodes() {
  62.         Node* current = head;
  63.         while (current->next->next->next != NULL)
  64.             current = current->next;
  65.         return current;
  66.     }
  67.     void printPoints() {
  68.         Node* current = head;
  69.         while (current != NULL) {
  70.             cout << current->point->index << " Point(" << current->point->x << "," << current->point->y << ")" << endl;
  71.             current = current->next;
  72.         }
  73.     }
  74. };
  75.  
  76. class Graham {
  77. public:
  78.  
  79.     int n;
  80.     Point* points;
  81.     Graham(Point points[], int n) {
  82.         this->points = new Point[n];
  83.         this->n = n;
  84.         for (int i = 0; i < n; i++) {
  85.             this->points[i] = points[i];
  86.         }
  87.     }
  88.  
  89.     int pointComparator(Point* point1, Point* point2, Point* point3) {
  90.         double cross_prod = (point1->y - point3->y) * (point2->x - point3->x) - (point2->y - point3->y) * (point1->x - point3->x);
  91.         if (cross_prod > 0.0)
  92.             return 1;
  93.         if (cross_prod < 0.0)
  94.             return -1;
  95.         return 0;
  96.     }
  97.  
  98.     void findSetMin() {
  99.         int temp = 0;
  100.         for (int i = 1; i < n; i++) {
  101.             if (points[temp].y >= points[i].y) {
  102.                 if (points[temp].y == points[i].y) {
  103.                     if (points[temp].x > points[i].x) {
  104.                         temp = i;
  105.                     }
  106.                 }
  107.                 else {
  108.                     temp = i;
  109.                 }
  110.             }
  111.         }
  112.         swap(points[0], points[temp]);
  113.     }
  114.     void printPoints() {
  115.         for (int i = 0; i < n; i++)
  116.             points[i].printPoint(i);
  117.     }
  118.  
  119.     void sortPoints() {
  120.         for (int j = 0; j < n - 1; j++) {
  121.             for (int i = 1; i < n - j - 1; i++)
  122.                 if (pointComparator(&points[i], &points[i + 1], &points[0]) > 0) {
  123.                     swap(points[i], points[i + 1]);
  124.                 }
  125.             cout << j << endl;
  126.         }
  127.     }
  128.     int pointComparatorGraham(Point* point1, Point* point2) {
  129.         double cross_prod = (point1->y * point2->x) - (point2->y * point1->x);
  130.         if (cross_prod > 0.0)
  131.             return 1;
  132.         if (cross_prod < 0.0)
  133.             return -1;
  134.         return 0;
  135.     }
  136.     Point* pointSubstract(Point* point1, Point* point2) {
  137.         Point* temp = new Point();
  138.         temp->x = point2->x - point1->x;
  139.         temp->y = point2->y - point1->y;
  140.         temp->index = -1;
  141.         return temp;
  142.     }
  143.     void merge(int l, int m, int r)
  144.     {
  145.         int i, j, k;
  146.         int n1 = m - l + 1;
  147.         int n2 = r - m;
  148.  
  149.         Point* tempL = new Point[n1];
  150.         Point* tempR = new Point[n2];
  151.  
  152.         for (i = 0; i < n1; i++)
  153.             tempL[i] = points[l + i];
  154.         for (j = 0; j < n2; j++)
  155.             tempR[j] = points[m + 1 + j];
  156.  
  157.         i = 0;
  158.         j = 0;
  159.         k = l;
  160.         while (i < n1 && j < n2)
  161.         {
  162.             if(pointComparator(&tempL[i],&tempR[j],&points[0])<0)
  163.             {
  164.                 points[k] = tempL[i];
  165.                 i++;
  166.             }
  167.             else
  168.             {
  169.                 points[k] = tempR[j];
  170.                 j++;
  171.             }
  172.             k++;
  173.         }
  174.  
  175.         while (i < n1)
  176.         {
  177.             points[k] = tempL[i];
  178.             i++;
  179.             k++;
  180.         }
  181.  
  182.         while (j < n2)
  183.         {
  184.             points[k] = tempR[j];
  185.             j++;
  186.             k++;
  187.         }
  188.     }
  189.  
  190.     void mergeSort(int l, int r)
  191.     {
  192.         if (l < r)
  193.         {
  194.             int m = l + (r - l) / 2;
  195.  
  196.             mergeSort(l, m);
  197.             mergeSort( m + 1, r);
  198.  
  199.             merge(l, m, r);
  200.         }
  201.     }
  202.     List* grahamScan() {
  203.         findSetMin();
  204.         //sortPoints();
  205.         mergeSort(1, n - 1);
  206.         List* convex_hull = new List();
  207.         convex_hull->addBack(&points[0]);
  208.         convex_hull->addBack(&points[1]);
  209.         convex_hull->addBack(&points[2]);
  210.         for (int i = 3; i < n; i++) {
  211.             Node* lastnodes = convex_hull->getTopNodes();
  212.             Point* result1 = pointSubstract(lastnodes->next->point, lastnodes->next->next->point);
  213.             Point* result2 = pointSubstract(lastnodes->point, lastnodes->next->point);
  214.             while (pointComparatorGraham(result1, result2) < 0) {
  215.                 convex_hull->deleteSec();
  216.                 lastnodes = convex_hull->getTopNodes();
  217.                 result1 = pointSubstract(lastnodes->next->point, lastnodes->next->next->point);
  218.                 result2 = pointSubstract(lastnodes->point, lastnodes->next->point);
  219.             }
  220.             convex_hull->addBack(&points[i]);
  221.  
  222.         }
  223.         return convex_hull;
  224.     }
  225. };
  226. int main()
  227. {
  228.     ifstream my_file;
  229.     my_file.open("points5.txt");
  230.     int n;
  231.     Point* points;
  232.     if (my_file.good()) {
  233.         my_file >> n;
  234.         points = new Point[n];
  235.         for (int i = 0; i < n; i++) {
  236.             my_file >> points[i].x;
  237.             my_file >> points[i].y;
  238.             points[i].index = i;
  239.         }
  240.     }
  241.     else {
  242.         cout << endl << "Could not open the file, ending program..." << endl;
  243.         return 0;
  244.     }
  245.     my_file.close();
  246.     Graham* graham = new Graham(points, n);
  247.     graham->findSetMin();
  248.     List* convex_hull = graham->grahamScan();
  249.     cout << endl;
  250.     cout << endl;
  251.     convex_hull->printPoints();
  252.     return 0;
  253. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement