5

I'm using c++ in Qt and I would like to draw an ellipse. From the Qt documentation I found that I can use drawEllipse to draw an ellipse from a boundary rectangle:

void QPainter::drawEllipse(const QRectF &rectangle)

and the rectangle is given by:

QRectF(qreal x, qreal y, qreal width, qreal height)

However, this only provides an ellipse with horizontal/vertical major and minor axis.

My ellipse is given by two coordinate sets to denote the major axis and a ratio between the length of the major and minor axes. Therefore the axes can have a slope that is not horizontal or vertical. (I need to use this method anyway since I also export it to a dxf file, which has this notation)

My question is:

Is there any other way to draw an ellipse than using a boundary rectangle and then rotating it?

It seems a little silly to put it in a horizontal/vertical rectangle and then computing the rotation when I have the axes coordinates from the beginning.

Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313
Little geek
  • 592
  • 8
  • 22
  • 1
    i quickly searched the docs and from what i understood the QPainter uses some coordinate system to draw stuff and you can apply transformations to that to draw e.g. an ellipse whose major axes are not horizontal/vertical. see here: http://doc.qt.io/qt-4.8/coordsys.html – 463035818_is_not_an_ai Apr 27 '16 at 19:46
  • 1
    btw whats wrong with using a boundary rectangle and rotating it? you just have to calculate the corresponding numbers – 463035818_is_not_an_ai Apr 27 '16 at 19:47
  • Additionally, with the draw/rotate method, you can paint one ellipse, and create the rest by rotating and scaling, which in Qt is a lot less resource intensive then painting them all individually. – Nicolas Holthaus Apr 27 '16 at 19:49
  • imho it would be silly by Qt to provide another method of drawing ellipses, when it is already possible to draw arbitrary ellipses by rotating the bounding rectangle – 463035818_is_not_an_ai Apr 27 '16 at 19:51
  • visit this link: http://stackoverflow.com/questions/29196610/qt-drawing-a-filled-rounded-rectangle-with-border – Farhad Apr 27 '16 at 19:53

1 Answers1

1

Is there any other way to draw an ellipse than using a boundary rectangle and then rotating it?

No. There isn't. Of course you'll have to rotate the coordinate system first, before you draw the ellipse.

The computation of the rotation angles is not necessary. Since you're given the major axis, you can easily normalize it and derive an orthogonal vector. These vectors form an orthonormal basis and their components are the directional cosines in a transformation matrix you'll set on the painter.

As the major axis direction is already expressed as a vector, you don't need to compute any transcendental functions to arrive at the transformation matrix for the painter. Very few additions and multiplications is all it takes, even if you had to invert the matrix (and you don't).

Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313