Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

It is not a bug, the reason is that it is measured in arc-angles from an ellipse, not from a circle. I ran in the same troubles some weeks ago. The phenomen is visualized in paragraph (59) of http://mathworld.wolfram.com/Ellipse.html . It follows that you cannot compute the angle via atan2(y-center.y,x-center.x) since the points are not lying on a circle.

I had the problem that I wanted to draw an elliptical arc given the extremal points and all parameters of the ellipse except start and end angle via OpenCV, my solution was to convert the ellipse to a polygon and cut out the area where my extremal-points were located, so after cv::ellipse2poly():

void cutOut(const std::vector<cv::Point> & pts,
            std::vector<cv::Point> & poly, 
            const cv::Point2d & ext1, const cv::Point2d & ext2) const
{
    // whole ellipse
    if ( ext1 == cv::Point2d(0.0,0.0)
         && ext2 == cv::Point2d(0.0,0.0) )
    {
        return;
    }

    // get closest points on ellipse to given extremal points
    double min1 = std::numeric_limits<double>::max();
    double min2 = std::numeric_limits<double>::max();
    int min_ind1, min_ind2;
    for( size_t i = 0; i < pts.size(); i++ ) {
        double n1 = cv::norm(cv::Point2d(pts[i].x,pts[i].y) - ext1);
        if ( n1 < min1 ) {
            min_ind1 = i;
            min1 = n1;
        }
        double n2 = cv::norm(cv::Point2d(pts[i].x,pts[i].y) - ext2);
        if ( n2 < min2 ) {
            min_ind2 = i;
            min2 = n2;
        }
    }

    // copy points to poly
    if ( min_ind2 == min_ind1 ) {
        poly = pts;
    }
    if ( min_ind2 < min_ind1 ) {
        poly.insert(poly.begin(), pts.begin()+min_ind1, pts.end());
        poly.insert(poly.end(),pts.begin(), pts.begin()+min_ind2+1);
    } else {
        poly.insert(poly.begin(), pts.begin()+min_ind1, pts.begin()+min_ind2+1);
    }
}

I guess from this post: http://stackoverflow.com/questions/197649/how-to-calculate-center-of-an-ellipse-by-two-points-and-radius-sizes you can derive a direct solution?!

Please let me know, if you know a better (= mathematical) solution than my quick & dirty hack ;) .