Search code examples
c++rotationangleellipsedxf

Find the angle of a rotated ellipse


I am developing a DXF parser by using the dxflib library. I have a problem parsing ellipses.

When I parse an ellipse I receive the following data:

struct DL_EllipseData 
{
    /*! X Coordinate of center point. */
    double cx;
    /*! Y Coordinate of center point. */
    double cy;

    /*! X coordinate of the endpoint of the major axis. */
    double mx;
    /*! Y coordinate of the endpoint of the major axis. */
    double my;

    /*! Ratio of minor axis to major axis. */
    double ratio;
};

I am trying to compute the angle by using the following equation:

auto angle = std::atan2(ellipse.my, ellipse.mx);

But it gives me wrong outcomes (for example if the angle is 16 degrees it gives to me about 74 degrees).

How should I compute properly the rotation angle?


Solution

  • You are ignoring the translation of the ellipse, that is, that the center may not be placed at (0, 0). If that where the case, your solution would be OK.

    To undo the effect of the translation, simply substract the coordinates for the center:

    auto angle = std::atan2(ellipse.my - ellipse.cy, ellipse.mx - ellipse.cx);