Borneq

Zamiatanie-fragment.cpp

Nov 21st, 2014
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.12 KB | None | 0 0
  1. #pragma once
  2. #include <iostream>
  3. #include <queue>
  4. #include <set>
  5. #include "Compare.h"
  6. #include "AVL.h"
  7. #include "Geom.h"
  8.  
  9. using namespace std;
  10.  
  11. int main()
  12. {
  13.  
  14.     BOLine* line1 = new BOLine(Point(0, 0), Point(50, 50), NULL);
  15.     BOLine* line2 = new BOLine(Point(0, 50), Point(50, 0), NULL);
  16.     BOLine* line3 = new BOLine(Point(35, 10), Point(35, 30), NULL); // pionowa
  17.     BOLine* line4 = new BOLine(Point(35, 10), Point(35, 30), NULL); // taka sama pionowa
  18.     BOLine* line5 = new BOLine(Point(30, 0), Point(30, 40), NULL); // inna pionowa
  19.  
  20.     priority_queue<Event, vector<Event>, Compare> eventQueue;
  21.  
  22.     AVL* avl = new AVL();
  23.  
  24.     eventQueue.push(Event(line1->start, line1, line1, EventType::START));
  25.     eventQueue.push(Event(line1->end, line1, line1, EventType::END));
  26.  
  27.     eventQueue.push(Event(line2->start, line2, line2,  EventType::START));
  28.     eventQueue.push(Event(line2->end, line2, line2, EventType::END));
  29.  
  30.     eventQueue.push(Event(line3->start, line3, line3,  EventType::START));
  31.     eventQueue.push(Event(line3->end, line3, line3, EventType::END));
  32.  
  33.     eventQueue.push(Event(line4->start, line4, line4,  EventType::START));
  34.     eventQueue.push(Event(line4->end, line4, line4, EventType::END));
  35.  
  36.     eventQueue.push(Event(line5->start, line5, line5,  EventType::START));
  37.     eventQueue.push(Event(line5->end, line5, line5, EventType::END));
  38.  
  39.     int cnt = 0;
  40.  
  41.     set<BOLine*> linesSet;
  42.     vector<Point> intersectionVector;
  43.  
  44.     while (!eventQueue.empty())
  45.     {
  46.         Event e = eventQueue.top();
  47.         eventQueue.pop();
  48.  
  49.         BOLine::sweepX = e.point.x;
  50.  
  51.         cout << "sweepX = " << BOLine::sweepX << endl;
  52.  
  53.         Point intersectionPoint;
  54.         if (e.eventType == EventType::START)
  55.         {
  56.             cout << "START: " << *e.line << endl;
  57.             if (e.line->isVertical)
  58.             {
  59.                 cout << "VERTICAL" << endl;
  60.                 for (set<BOLine*>::iterator it = linesSet.begin(); it != linesSet.end(); it++)
  61.                 {
  62.                     BOLine* currLine = *it;
  63.                     if (e.line->Intersect(*currLine, intersectionPoint) == INTERESECTING)
  64.                     {
  65.                         intersectionVector.push_back(intersectionPoint);
  66.  
  67.                     }
  68.                 }
  69.                 continue;
  70.  
  71.             }
  72.  
  73.             Node* insertedNode = avl->Insert(e.line);
  74.             e.line->node = insertedNode;
  75.  
  76.             Node* prevNode = avl->pred(insertedNode);
  77.             Node* nextNode = avl->succ(insertedNode);
  78.  
  79.             linesSet.insert(e.line);
  80.  
  81.  
  82.             if (prevNode)
  83.             {
  84.                 BOLine* prevLine = prevNode->pValue;
  85.  
  86.                 if (e.line->Intersect(*prevLine, intersectionPoint) == INTERESECTING)
  87.                 {
  88.                     if (intersectionPoint.x > BOLine::sweepX)
  89.                     {
  90.                         eventQueue.push(Event(intersectionPoint, e.line, prevLine, EventType::INTERSECTION));
  91.                         intersectionVector.push_back(intersectionPoint);
  92.                     }
  93.                 }
  94.             }
  95.  
  96.             if (nextNode)
  97.             {
  98.                 BOLine* nextLine = nextNode->pValue;
  99.  
  100.                 if (e.line->Intersect(*nextLine, intersectionPoint) == INTERESECTING)
  101.                 {
  102.                     if (intersectionPoint.x > BOLine::sweepX)
  103.                     {
  104.                         eventQueue.push(Event(intersectionPoint, e.line, nextLine, EventType::INTERSECTION));
  105.                         intersectionVector.push_back(intersectionPoint);
  106.                     }
  107.                 }
  108.             }
  109.  
  110.         }
  111.         if (e.eventType == EventType::END)
  112.         {
  113.             cout << "END: " << *e.line << endl;
  114.             if (e.line->isVertical)
  115.             {
  116.                 continue;
  117.             }
  118.             Node* nodeToRemove = e.line->node;
  119.  
  120.             Node* prevNode = avl->pred(nodeToRemove);
  121.             Node* nextNode = avl->succ(nodeToRemove);
  122.  
  123.             if (prevNode && nextNode)
  124.             {
  125.                 BOLine* prevLine = prevNode->pValue;
  126.                 BOLine* nextLine = nextNode->pValue;
  127.  
  128.                 if (prevLine->Intersect(*nextLine, intersectionPoint) == INTERESECTING)
  129.                 {
  130.                     if (intersectionPoint.x > BOLine::sweepX)
  131.                     {
  132.                         eventQueue.push(Event(intersectionPoint, prevLine, nextLine, EventType::INTERSECTION));
  133.                         intersectionVector.push_back(intersectionPoint);
  134.                     }
  135.                 }
  136.             }
  137.  
  138.  
  139.             avl->Remove(e.line);
  140.             linesSet.insert(e.line);
  141.         }
  142.         if (e.eventType == EventType::INTERSECTION)
  143.         {
  144.             cout << "INTERSECTION:\n " << *e.line << "\n " << *e.line2 << endl;
  145.  
  146.             Node* node1 = e.line->node;
  147.             Node* node2 = e.line2->node;
  148.  
  149.             node1->pValue = e.line2; // u góry
  150.             node2->pValue = e.line; // u dołu
  151.  
  152.             e.line2->node = node1;
  153.             e.line->node = node2;
  154.  
  155.             Node* prevNode = avl->pred(node2);
  156.             Node* nextNode = avl->succ(node1);
  157.  
  158.  
  159.             if (prevNode)
  160.             {
  161.                 BOLine* prevLine = prevNode->pValue;
  162.  
  163.                 if (e.line->Intersect(*prevLine, intersectionPoint) == INTERESECTING)
  164.                 {
  165.                     if (intersectionPoint.x > BOLine::sweepX)
  166.                     {
  167.                         eventQueue.push(Event(intersectionPoint, e.line, prevLine, EventType::INTERSECTION));
  168.                         intersectionVector.push_back(intersectionPoint);
  169.                     }
  170.                 }
  171.             }
  172.  
  173.  
  174.             if (nextNode)
  175.             {
  176.                 BOLine* nextLine = nextNode->pValue;
  177.  
  178.                 if (e.line2->Intersect(*nextLine, intersectionPoint) == INTERESECTING)
  179.                 {
  180.                     if (intersectionPoint.x > BOLine::sweepX)
  181.                     {
  182.                         eventQueue.push(Event(intersectionPoint, e.line2, nextLine, EventType::INTERSECTION));
  183.                         intersectionVector.push_back(intersectionPoint);
  184.                     }
  185.  
  186.                 }
  187.             }
  188.         }
  189.  
  190.         //std::cout << *avl << endl;
  191.     }
  192.  
  193.     cout << "Punkty przeciecia" << endl;
  194.     for (vector<Point>::iterator it = intersectionVector.begin(); it != intersectionVector.end(); it++)
  195.     {
  196.         cout << *it << endl;
  197.     }
  198.  
  199.  
  200.     getchar();
  201.     return 0;
  202. }
Advertisement
Add Comment
Please, Sign In to add comment