stryga42
28th December 2019, 17:18
I try to draw a "stadium-shape" (two half circles connected by straight lines) using QPainter on a QPainterPath. This basically works but the half circles are always slightly asymmetric. A casual viewer won't notice but for the trained eye this is really annoying.
The details - I removed the straight lines so that it becomes clear what belongs to the arc.
r is the radius of the (half)circles, d is the length of the straight line, a1 and a2 are the arc starting angles and a is always 180 deg for a semicircle.
QPainterPath p;
painter.setPen(colFont);
painter.setBrush(colBack);
QRectF lb(0, 0, 2*r, 2*r), rb(d, 0, 2*r, 2*r);
qreal a1(90), a2(270), a(180);
qDebug()<<"RBOX"<<r<<d<<lb<<rb<<a1<<a2<<a;
p.moveTo(r, r);
p.arcTo(lb, a1, a);
p.moveTo(r+d, r);
p.arcTo(rb, a2, a);
painter.drawPath(p);
This yields
RBOX 14 34 QRectF(0,0 28x28) QRectF(34,0 28x28) 90 270 180
13308
Yellow and orange stuff belongs to other widgets. The gimp selection marker you see in the screenshot is 14x28 pixel - the bounding box I would expect for the half circle. As you may notice, the upper half and the lower half are not mirror images of each other - which they should be to my understanding.
I also tried to make the width and height of the bounding box uneven integers, so that the circle has true "center pixels". Doesn't make a big difference, the asymmetry remains:
QPainterPath p;
painter.setPen(colFont);
painter.setBrush(colBack);
QRectF lb(0, 0, 2*r+1, 2*r+1), rb(d, 0, 2*r+1, 2*r+1);
qreal a1(90), a2(270), a(180);
qDebug()<<"RBOX"<<r<<d<<lb<<rb<<a1<<a2<<a;
p.moveTo(r, r);
p.arcTo(lb, a1, a);
p.moveTo(r+d, r);
p.arcTo(rb, a2, a);
painter.drawPath(p);
RBOX 14 34 QRectF(0,0 29x29) QRectF(34,0 29x29) 90 270 180
gimp box is now 15x29
13309
Has anyone an idea why the upper half of the semicircle is not a mirror image of the lower half?
Qt 5.7.0 open source on Ubuntu 18.04.3
The details - I removed the straight lines so that it becomes clear what belongs to the arc.
r is the radius of the (half)circles, d is the length of the straight line, a1 and a2 are the arc starting angles and a is always 180 deg for a semicircle.
QPainterPath p;
painter.setPen(colFont);
painter.setBrush(colBack);
QRectF lb(0, 0, 2*r, 2*r), rb(d, 0, 2*r, 2*r);
qreal a1(90), a2(270), a(180);
qDebug()<<"RBOX"<<r<<d<<lb<<rb<<a1<<a2<<a;
p.moveTo(r, r);
p.arcTo(lb, a1, a);
p.moveTo(r+d, r);
p.arcTo(rb, a2, a);
painter.drawPath(p);
This yields
RBOX 14 34 QRectF(0,0 28x28) QRectF(34,0 28x28) 90 270 180
13308
Yellow and orange stuff belongs to other widgets. The gimp selection marker you see in the screenshot is 14x28 pixel - the bounding box I would expect for the half circle. As you may notice, the upper half and the lower half are not mirror images of each other - which they should be to my understanding.
I also tried to make the width and height of the bounding box uneven integers, so that the circle has true "center pixels". Doesn't make a big difference, the asymmetry remains:
QPainterPath p;
painter.setPen(colFont);
painter.setBrush(colBack);
QRectF lb(0, 0, 2*r+1, 2*r+1), rb(d, 0, 2*r+1, 2*r+1);
qreal a1(90), a2(270), a(180);
qDebug()<<"RBOX"<<r<<d<<lb<<rb<<a1<<a2<<a;
p.moveTo(r, r);
p.arcTo(lb, a1, a);
p.moveTo(r+d, r);
p.arcTo(rb, a2, a);
painter.drawPath(p);
RBOX 14 34 QRectF(0,0 29x29) QRectF(34,0 29x29) 90 270 180
gimp box is now 15x29
13309
Has anyone an idea why the upper half of the semicircle is not a mirror image of the lower half?
Qt 5.7.0 open source on Ubuntu 18.04.3