PDA

View Full Version : drawArc in the rectangle issues



doris
15th February 2017, 04:12
123351233612336

my program will get the startpoints(x,y) and calculate width and height when you draw on the scene, the attached photo shows it.
the problem is i want to draw a perfect arc in the middle of rectangle as the sketch one attached.
my code as below:


void ResizableRectMarker::paintArc1(int x, int y, int width, int height, QPainter* painter)
{
double radius = sqrt(pow(0.5*width,2)+pow(0.5*height,2));
double step = sqrt(pow(radius,2)-pow(0.5*width,2));

QPointF topLeft(x,y);
QPointF bottomRight(x+width,y+height);
QRectF rect(topLeft, bottomRight);

int startAngle = atan2(0.5*width,step)* 180 / M_PI * 16;
int spanAngle = 2*startAngle;
qDebug() << "start angle:" << startAngle << "|" << "spanAngle:" << spanAngle;
painter->drawArc(rect, startAngle, spanAngle);
}

the problem is the the arc drawn is not in the center, and also if the rectangle is not a square, the arc is wired.
any solution for it.

newbie here..

anda_skoa
15th February 2017, 10:18
It is not entirely clear what your objective is.

Your drawing suggests that you want a circle arc that intersects the rectangle on its left and right side in the middle, the screenshot and code indicates an ellipse arc that fits into the rectangle.

Cheers,
_

Santosh Reddy
15th February 2017, 10:42
I think this is what you want


class ArcWidget : public QWidget
{
protected:
void paintEvent(QPaintEvent * event) override
{
QPainter painter(this);

const double radius = qSqrt(qPow(width()/2.0, 2) + qPow(height()/2.0, 2));
const int startAngle = qAtan((width()/2.0)/radius) * 180 / M_PI * 16;
const int spanAngle = (90 - startAngle) * 2 * 16;
const QRect rect = event->rect().adjusted(-(radius-width()/2.0), height()-radius, (radius-width()/2.0), radius);

painter.drawArc(rect, startAngle, spanAngle);
}
};

int main(int argc, char *argv[])
{
QApplication app(argc, argv);

ArcWidget widget;
widget.show();

return app.exec();
}