Advertisement
Guest User

Untitled

a guest
Feb 25th, 2018
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.40 KB | None | 0 0
  1. #include <iostream>
  2. #include <conio.h>
  3. #include <vector>
  4. #include <queue>
  5. #include "band.h"
  6. #include "bandSummit.h"
  7.  
  8.  
  9. bool cmpByX(Band p1, Band p2);
  10. bool cmpByY(Band p1, Band p2);
  11.  
  12. int main()
  13. {
  14.     //the example given band list as vector
  15.     /* - - - example input - - - - - - */
  16.     Band b1(9,7,9), b2(9,5,7), b3(22,14,2), b4(32,8,5), b5(34,6,7);
  17.     std::vector<Band> bandList;
  18.     bandList.push_back(b4);
  19.     bandList.push_back(b2);
  20.     bandList.push_back(b5);
  21.     bandList.push_back(b1);
  22.     bandList.push_back(b3);
  23.     bandList.push_back(Band(51, 3, -3));
  24.     bandList.push_back(Band(55, 3, -5));
  25.     /* - - - example input END - - -  */
  26.  
  27.  
  28.  
  29.     //sort by nearest X position
  30.     std::sort(bandList.begin(), bandList.end(), cmpByX);
  31.     //sort by heigh
  32.     std::sort(bandList.begin(), bandList.end(), cmpByY);
  33.  
  34.     std::vector<Band> acceptedBands;
  35.  
  36.     //for every exisiting band
  37.     for (int i = 0; i < bandList.size(); i++)
  38.     {
  39.         //initialize band list if empty
  40.         if (acceptedBands.size() == 0)
  41.         {
  42.             acceptedBands.push_back(bandList[i]);
  43.             continue;
  44.         }
  45.  
  46.  
  47.         for (int j = 0; j < acceptedBands.size(); j++)
  48.         {
  49.             if (bandList[i].getX1() < acceptedBands[j].getX1() || bandList[i].getX1() > acceptedBands[j].getX2())//check if X1 is placed outside
  50.             {
  51.                 if (bandList[i].getX2() < acceptedBands[j].getX1() || bandList[i].getX2() > acceptedBands[j].getX2())//check if X2 is placed outside
  52.                 {
  53.                     //nothin
  54.                 }
  55.                 else
  56.                 {
  57.                     bandList[i].setX2(acceptedBands[j].getX1()); // 'cut' the line
  58.                 }
  59.  
  60.                 //break only if that was last band to compare
  61.                 if (j == acceptedBands.size() - 1)
  62.                 {
  63.                     acceptedBands.push_back(bandList[i]);
  64.                     break;
  65.                 }
  66.             }
  67.             else
  68.             {
  69.                 if (bandList[i].getX2() > acceptedBands[j].getX2())
  70.                 {
  71.                     bandList[i].setX1(acceptedBands[j].getX2());//modify
  72.  
  73.                     //break only if that was last band to compare
  74.                     if (j == acceptedBands.size() - 1)
  75.                     {
  76.                         acceptedBands.push_back(bandList[i]);
  77.                         break;
  78.                     }
  79.                 }
  80.                 else
  81.                 {//cant be added
  82.                     break;
  83.                 }
  84.             }
  85.  
  86.         }
  87.  
  88.     }
  89.  
  90.  
  91.     for (int j = 0; j < acceptedBands.size(); j++)
  92.     {
  93.         printf("X:%i, Y:%i \n", acceptedBands[j].getX1(), acceptedBands[j].getY());
  94.         printf("X:%i, Y:%i \n", acceptedBands[j].getX2(), acceptedBands[j].getY());
  95.     }
  96.  
  97.     _getch();
  98.     return 0;
  99. }
  100.  
  101.  
  102. bool cmpByX(Band b1, Band b2)
  103. {
  104.     return b1.getX1() < b2.getX1();
  105. }
  106.  
  107. bool cmpByY(Band b1, Band b2)
  108. {
  109.     return b1.getY() > b2.getY();
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement