1
votes

I am drawing an ellipse using QPainterPath using three mouse clicks. The major and minor axis of the ellipse remain parallel to the principal coordinate axis.

The first click determines the center of the ellipse. The second click's position is used to compute length of major axis and that of third click is used to determine length of minor axis.

Irrespective of the position of the clicks, the ellipse has its axis parallel to the principal axis. How do I set the orientation of the ellipse according to the mouse clicks' position?

I have computed the angle of the line joining first point and second point as:

theta = atan2((p2.y()-p1.y()),(p2.x()-p1.x())) * (180/M_PI);

My bounding rectangle looks like:

QRectF MyEllipse::boundingRect() const
{
  return QRectF(p1.x()-majRadius, p1.y()-minRadius, 2 * majRadius, 2 *  minRadius).normalized();

}

Then I paint it using:

QPainterPath ellipse;
ellipse.moveTo(p2.x()*cos(theta),p2.y()*sin(theta));
ellipse.arcTo(boundingRect(), theta, theta+360);

QPen paintpen(Qt::black);
paintpen.setWidth(1);
painter->setRenderHint(QPainter::Antialiasing);
painter->setPen(paintpen);
painter->drawPath(ellipse);

How do I get the ellipse properly oriented as per the three points?

1
I have been able to achieve correct orientation using translation and rotation of the coordinate axes and then again translation. So I mark this as solved.Kamalpreet Grewal
Sure I have posted it.Kamalpreet Grewal

1 Answers

3
votes

I have solved this issue using translation and rotation operations in my paint function as:

painter->save();
painter->translate(p1.x(), p1.y());
painter->rotate(theta);
painter->translate(-p1.x(), -p1.y());
painter->drawPath(ellipse);
painter->restore();

This sets the shape of the ellipse according to the clicks.

The rotation about angle theta is done which is computed as:

theta = atan2((p2.y()-p1.y()),(p2.x()-p1.x())) * (180/M_PI);

if distance between p2 and p1 > distance between p3 and p1 else it is calculated as:

theta = atan2((p3.y()-p1.y()),(p3.x()-p1.x())) * (180/M_PI);