Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I draw an ellipse in Qt c++ without using a rectangle?

Tags:

c++

qt

ellipse

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.

like image 891
Little geek Avatar asked Nov 09 '22 16:11

Little geek


1 Answers

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).

like image 73
Kuba hasn't forgotten Monica Avatar answered Nov 14 '22 23:11

Kuba hasn't forgotten Monica