title: Find intersecting area of two trapezoids in OpenCV
body: <p>I'm writing a function that evaluates how many pixels overlap between two non-isosceles trapezoids. In my application, a trapezoid is always defined as :</p>
  
  <pre><code>typedef std::array&lt;cv::Point, 4&gt; Trapezoid;
  //[0] - bottom left
  //[1] - bottom right
  //[2] - top right
  //[3] - top left
  </code></pre>
  
  <p>Additionally, these trapezoids will have the properties that indexes [0] and [1] will always have the same Y as will [2] and [3] (Always parallel to Y axis).</p>
  
  <p>So, let's say I have two trapezoids:</p>
  
  <pre><code>//300 high, 100 top, 200 bottom
  Trapezoid trapezoid1{ cv::Point(100,400), cv::Point(300,400), cv::Point(250,100), cv::Point(150,100) };
  //250 high, 50 top, 250 bottom
  Trapezoid trapezoid2{ cv::Point(75,400), cv::Point(325,400), cv::Point(225,150), cv::Point(175,150) };
  </code></pre>
  
  <p>I could iterate over each row of one of the trapezoids and do the math to figure out how many pixels in that row overlap:</p>
  
  <pre><code>uint32_t TrapezoidOverlap( const Trapezoid&amp; trapezoid1,
                             const Trapezoid&amp; trapezoid2 )
  {
      //Count number of overlapping pixels
      uint32_t overlappedpixels {0};
  
      for (int i = trapezoid1[3].y; i &lt; trapezoid1[0].y; i++) {
          overlappedpixels += //Math for overlapping pixels in X;
      }
  
      return overlappedpixels;
  }
  </code></pre>
  
  <p>First and last row will be simple, but all rows in between will require trigonometry to find the start and end points of both trapezoids.  This looks like it will be computationally expensive and performance is critical in this application.  I have seen in <a href="http://answers.opencv.org/question/67091/how-to-find-if-2-rectangles-are-overlapping-each-other/" rel="nofollow">this</a> question that the Rect structures have a intersection operator, however I am not sure if there's any function that would help in this situation.</p>
  
  <p>What would be the solution for best performance in this situation?</p>