Advertisement
Guest User

Untitled

a guest
Apr 20th, 2014
1,514
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.49 KB | None | 0 0
  1. // Qt C++ version
  2.  
  3. #include <limits>
  4.  
  5. #include <QPointF>
  6. #include <QPolygonF>
  7.  
  8. bool doPolygonsIntersect(const QPolygonF& a, const QPolygonF& b)
  9. {
  10.   for(int polyi = 0; polyi < 2; ++polyi)
  11.     {
  12.       const QPolygonF& polygon = polyi == 0 ? a : b;
  13.  
  14.       for(int i1 = 0; i1 < polygon.size(); ++i1)
  15.         {
  16.           const int i2 = (i1 + 1) % polygon.size();
  17.  
  18.           const double normalx = polygon[i2].y() - polygon[i1].y();
  19.           const double normaly = polygon[i2].x() - polygon[i1].x();
  20.  
  21.           double minA = std::numeric_limits<double>::max();
  22.           double maxA = std::numeric_limits<double>::min();
  23.           for(int ai = 0; ai < a.size(); ++ai)
  24.             {
  25.               const double projected = normalx * a[ai].x() +
  26.                 normaly * a[ai].y();
  27.               if( projected < minA ) minA = projected;
  28.               if( projected > maxA ) maxA = projected;
  29.             }
  30.  
  31.           double minB = std::numeric_limits<double>::max();
  32.           double maxB = std::numeric_limits<double>::min();
  33.           for(int bi = 0; bi < b.size(); ++bi)
  34.             {
  35.               const double projected = normalx * b[bi].x() +
  36.                 normaly * b[bi].y();
  37.               if( projected < minB ) minB = projected;
  38.               if( projected > maxB ) maxB = projected;
  39.             }
  40.  
  41.           if( maxA < minB || maxB < minA )
  42.             return false;
  43.         }
  44.     }
  45.  
  46.   return true;
  47. }
  48.  
  49. #include <iostream>
  50.  
  51. int main()
  52. {
  53.   QPolygonF a, b, c;
  54.   a << QPointF(-5, 1) << QPointF(-3, 3) << QPointF(-1, 5)
  55.     << QPointF( 1, 5) << QPointF( 3, 3) << QPointF( 5, 1)
  56.     << QPointF( 5,-1) << QPointF( 3,-3) << QPointF( 1,-5)
  57.     << QPointF(-1,-5) << QPointF(-3,-3) << QPointF(-5,-1);
  58.   b << QPointF(-3, 0) << QPointF( 0, 3)
  59.     << QPointF( 3, 0) << QPointF( 0,-3);
  60.   c << QPointF( 4, 0) << QPointF( 7, 3)
  61.     << QPointF(10, 0) << QPointF( 7,-3);
  62.  
  63.   std::cout
  64.     << "Intersection(a,a) " << doPolygonsIntersect(a, a) << '\n'
  65.     << "Intersection(a,b) " << doPolygonsIntersect(a, b) << '\n'
  66.     << "Intersection(a,c) " << doPolygonsIntersect(a, c) << '\n'
  67.     << "Intersection(b,a) " << doPolygonsIntersect(b, a) << '\n'
  68.     << "Intersection(b,b) " << doPolygonsIntersect(b, b) << '\n'
  69.     << "Intersection(b,c) " << doPolygonsIntersect(b, c) << '\n'
  70.     << "Intersection(c,a) " << doPolygonsIntersect(c, a) << '\n'
  71.     << "Intersection(c,b) " << doPolygonsIntersect(c, b) << '\n'
  72.     << "Intersection(c,c) " << doPolygonsIntersect(c, c) << '\n';
  73.  
  74.   return 0;
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement