【发布时间】:2014-10-04 14:58:17
【问题描述】:
我正在使用QPainterPath 绘制一个椭圆,并点击三下鼠标。椭圆的长轴和短轴与主坐标轴保持平行。
第一次单击确定椭圆的中心。第二次点击的位置用于计算长轴的长度,第三次点击的位置用于确定短轴的长度。
无论点击的位置如何,椭圆的轴都平行于主轴。如何根据鼠标点击的位置设置椭圆的方向?
我计算了连接第一个点和第二个点的线的角度:
theta = atan2((p2.y()-p1.y()),(p2.x()-p1.x())) * (180/M_PI);
我的边界矩形看起来像:
QRectF MyEllipse::boundingRect() const
{
return QRectF(p1.x()-majRadius, p1.y()-minRadius, 2 * majRadius, 2 * minRadius).normalized();
}
然后我使用:
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);
如何根据三个点正确定位椭圆?
【问题讨论】:
-
我已经能够使用坐标轴的平移和旋转然后再次平移来实现正确的方向。所以我将此标记为已解决。
-
我已经发布了。