Leon Hausmann
Leon Hausmann

Reputation: 1

How can I rotate an ellipse in a QPainterPath in Qt?

i know i can draw an ellipse in Qt with QPainter. But i want to rotate the Ellipse inside a QPainterpath. Does anybody know how i can do that?

The goal is to subtract two ellipses. I know that i can subtract two ellipses by subtracting two QPainterPaths. So my idea was to do that using the QPainterpath. But i dont know how i am able to rotate the ellipse inside my QPainterPath oder how i can rotate the QPainterPath.

But there is no function such as

QPainterPath path;
path.addEllipse(100,100,100,100);
path.rotate(45);

Upvotes: -1

Views: 359

Answers (1)

Nome
Nome

Reputation: 41

You can rotate each point and controlPoint of path elements:

void rotatePath(QPainterPath &path, const QPointF &centerPoint, const double &angle)
{
    double sinAngle = sin(angle);
    double cosAngle = cos(angle);
    for (int i = 0; i < path.elementCount(); i++) {
        double rX = path.elementAt(i).x - centerPoint.x();
        double rY = path.elementAt(i).y - centerPoint.y();
        path.setElementPositionAt(i, centerPoint.x() + rX * cosAngle - rY * sinAngle, centerPoint.y() + rY * cosAngle + rX * sinAngle);
    }
}

You must define a centerPoint for rotation: QPointF(125,150))

You drew a circular ellipse, you can't see if it's rotated when centerPoint is at ellipse's center.

Usage:

QPainterPath originalPath;
originalPath.addEllipse(100,100,50,100);
rotatePath(originalPath, QPointF(125,150), M_PI / 4);

Upvotes: 2

Related Questions