Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #include <iostream>
- #include <queue>
- #include <set>
- #include "Compare.h"
- #include "AVL.h"
- #include "Geom.h"
- using namespace std;
- int main()
- {
- BOLine* line1 = new BOLine(Point(0, 0), Point(50, 50), NULL);
- BOLine* line2 = new BOLine(Point(0, 50), Point(50, 0), NULL);
- BOLine* line3 = new BOLine(Point(35, 10), Point(35, 30), NULL); // pionowa
- BOLine* line4 = new BOLine(Point(35, 10), Point(35, 30), NULL); // taka sama pionowa
- BOLine* line5 = new BOLine(Point(30, 0), Point(30, 40), NULL); // inna pionowa
- priority_queue<Event, vector<Event>, Compare> eventQueue;
- AVL* avl = new AVL();
- eventQueue.push(Event(line1->start, line1, line1, EventType::START));
- eventQueue.push(Event(line1->end, line1, line1, EventType::END));
- eventQueue.push(Event(line2->start, line2, line2, EventType::START));
- eventQueue.push(Event(line2->end, line2, line2, EventType::END));
- eventQueue.push(Event(line3->start, line3, line3, EventType::START));
- eventQueue.push(Event(line3->end, line3, line3, EventType::END));
- eventQueue.push(Event(line4->start, line4, line4, EventType::START));
- eventQueue.push(Event(line4->end, line4, line4, EventType::END));
- eventQueue.push(Event(line5->start, line5, line5, EventType::START));
- eventQueue.push(Event(line5->end, line5, line5, EventType::END));
- int cnt = 0;
- set<BOLine*> linesSet;
- vector<Point> intersectionVector;
- while (!eventQueue.empty())
- {
- Event e = eventQueue.top();
- eventQueue.pop();
- BOLine::sweepX = e.point.x;
- cout << "sweepX = " << BOLine::sweepX << endl;
- Point intersectionPoint;
- if (e.eventType == EventType::START)
- {
- cout << "START: " << *e.line << endl;
- if (e.line->isVertical)
- {
- cout << "VERTICAL" << endl;
- for (set<BOLine*>::iterator it = linesSet.begin(); it != linesSet.end(); it++)
- {
- BOLine* currLine = *it;
- if (e.line->Intersect(*currLine, intersectionPoint) == INTERESECTING)
- {
- intersectionVector.push_back(intersectionPoint);
- }
- }
- continue;
- }
- Node* insertedNode = avl->Insert(e.line);
- e.line->node = insertedNode;
- Node* prevNode = avl->pred(insertedNode);
- Node* nextNode = avl->succ(insertedNode);
- linesSet.insert(e.line);
- if (prevNode)
- {
- BOLine* prevLine = prevNode->pValue;
- if (e.line->Intersect(*prevLine, intersectionPoint) == INTERESECTING)
- {
- if (intersectionPoint.x > BOLine::sweepX)
- {
- eventQueue.push(Event(intersectionPoint, e.line, prevLine, EventType::INTERSECTION));
- intersectionVector.push_back(intersectionPoint);
- }
- }
- }
- if (nextNode)
- {
- BOLine* nextLine = nextNode->pValue;
- if (e.line->Intersect(*nextLine, intersectionPoint) == INTERESECTING)
- {
- if (intersectionPoint.x > BOLine::sweepX)
- {
- eventQueue.push(Event(intersectionPoint, e.line, nextLine, EventType::INTERSECTION));
- intersectionVector.push_back(intersectionPoint);
- }
- }
- }
- }
- if (e.eventType == EventType::END)
- {
- cout << "END: " << *e.line << endl;
- if (e.line->isVertical)
- {
- continue;
- }
- Node* nodeToRemove = e.line->node;
- Node* prevNode = avl->pred(nodeToRemove);
- Node* nextNode = avl->succ(nodeToRemove);
- if (prevNode && nextNode)
- {
- BOLine* prevLine = prevNode->pValue;
- BOLine* nextLine = nextNode->pValue;
- if (prevLine->Intersect(*nextLine, intersectionPoint) == INTERESECTING)
- {
- if (intersectionPoint.x > BOLine::sweepX)
- {
- eventQueue.push(Event(intersectionPoint, prevLine, nextLine, EventType::INTERSECTION));
- intersectionVector.push_back(intersectionPoint);
- }
- }
- }
- avl->Remove(e.line);
- linesSet.insert(e.line);
- }
- if (e.eventType == EventType::INTERSECTION)
- {
- cout << "INTERSECTION:\n " << *e.line << "\n " << *e.line2 << endl;
- Node* node1 = e.line->node;
- Node* node2 = e.line2->node;
- node1->pValue = e.line2; // u góry
- node2->pValue = e.line; // u dołu
- e.line2->node = node1;
- e.line->node = node2;
- Node* prevNode = avl->pred(node2);
- Node* nextNode = avl->succ(node1);
- if (prevNode)
- {
- BOLine* prevLine = prevNode->pValue;
- if (e.line->Intersect(*prevLine, intersectionPoint) == INTERESECTING)
- {
- if (intersectionPoint.x > BOLine::sweepX)
- {
- eventQueue.push(Event(intersectionPoint, e.line, prevLine, EventType::INTERSECTION));
- intersectionVector.push_back(intersectionPoint);
- }
- }
- }
- if (nextNode)
- {
- BOLine* nextLine = nextNode->pValue;
- if (e.line2->Intersect(*nextLine, intersectionPoint) == INTERESECTING)
- {
- if (intersectionPoint.x > BOLine::sweepX)
- {
- eventQueue.push(Event(intersectionPoint, e.line2, nextLine, EventType::INTERSECTION));
- intersectionVector.push_back(intersectionPoint);
- }
- }
- }
- }
- //std::cout << *avl << endl;
- }
- cout << "Punkty przeciecia" << endl;
- for (vector<Point>::iterator it = intersectionVector.begin(); it != intersectionVector.end(); it++)
- {
- cout << *it << endl;
- }
- getchar();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment