Files
intelliphoto/Abgabe/Abgabe 4/Painting/IntelliHelper/IntelliHelper.cpp
2019-12-04 17:48:58 +01:00

40 lines
1.4 KiB
C++

#include"IntelliHelper.h"
#include<algorithm>
int IntelliHelper::orientation(QPoint& p, QPoint& q, QPoint& r){
int value = (q.y()-p.y())*(r.x()-q.x())-
(q.x()-p.x())*(r.y()-q.y());
if(value==0) return 0;
return (value>0)?1:2;
}
bool IntelliHelper::onSegment(QPoint& p1, QPoint& q, QPoint& p2){
return (q.x() >= std::min(p1.x(),p2.x()) && q.x() <= std::max(p1.x(), p2.x()) &&
q.y() >= std::min(p1.y(),p2.y()) && q.y() <= std::max(p1.y(), p2.y()));
}
bool IntelliHelper::hasIntersection(QPoint& p1, QPoint& q1, QPoint& p2, QPoint& q2){
int o1 = IntelliHelper::orientation(p1,q1,p2);
int o2 = IntelliHelper::orientation(p1,q1,q2);
int o3 = IntelliHelper::orientation(p2,q2,p1);
int o4 = IntelliHelper::orientation(p2,q2,q1);
// General case
if (o1 != o2 && o3 != o4)
return true;
// p1, q1 and p2 are colinear and p2 lies on segment p1q1
if (o1 == 0 && onSegment(p1, p2, q1)) return true;
// p1, q1 and q2 are colinear and q2 lies on segment p1q1
if (o2 == 0 && onSegment(p1, q2, q1)) return true;
// p2, q2 and p1 are colinear and p1 lies on segment p2q2
if (o3 == 0 && onSegment(p2, p1, q2)) return true;
// p2, q2 and q1 are colinear and q1 lies on segment p2q2
if (o4 == 0 && onSegment(p2, q1, q2)) return true;
return false; // Doesn't fall in any of the above cases
}