【问题标题】:Set the orientation of an ellipse in Qt在 Qt 中设置椭圆的方向
【发布时间】: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);

如何根据三个点正确定位椭圆?

【问题讨论】:

  • 我已经能够使用坐标轴的平移和旋转然后再次平移来实现正确的方向。所以我将此标记为已解决。
  • 我已经发布了。

标签: qt ellipse qpainter


【解决方案1】:

我在paint 函数中使用平移和旋转操作解决了这个问题:

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

这会根据点击设置椭圆的形状。

完成关于角度 theta 的旋转,计算如下:

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

如果 p2 和 p1 之间的距离 > p3 和 p1 之间的距离,则计算为:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-06-29
    • 2014-05-13
    • 2016-02-25
    • 1970-01-01
    • 1970-01-01
    • 2014-03-29
    • 1970-01-01
    相关资源
    最近更新 更多