【问题标题】:draw ellipse with specific points in scene用场景中的特定点绘制椭圆
【发布时间】:2013-12-10 11:25:28
【问题描述】:

我想知道如何用 qgraphicsscene 中标记的三个点绘制椭圆。假设用户使用 mousePress 在场景中选择三个点,我将用标记突出显示按下的点。现在使用三个标记,我必须在三个标记周围绘制一个椭圆,或者将这些点作为我绘制的椭圆的边界。

对于实现painterpath drawEllipse 将工作..?

【问题讨论】:

    标签: qt qgraphicsitem


    【解决方案1】:

    我曾用 PySide 解决过类似的问题。您可以轻松地在 C++ 中修改此代码:

    def paintEvent( self, ev ):
        painter = QPainter( self )
        painter.setPen( QPen( Qt.red, 4 ) )
        painter.drawPoint( self.a )
        painter.drawPoint( self.b )
        painter.drawPoint( self.c )
    
        # 1. Find the long axis.
        maxL = max( QLineF( self.a, self.b ), QLineF( self.a, self.c ), QLineF( self.b, self.c ), key=lambda x: x.length() )
        center = maxL.pointAt( 0.5 )
    
        # 2. Find the small axis
        if maxL == QLineF( self.a, self.b ):
            c = self.c
        elif maxL == QLineF( self.a, self.c ):
            c = self.b
        else:
            c = self.a
        w = maxL.length()
        h = QLineF( c, center ).length() * 2.0
    
        # 3. Define the bounding rect for our ellipse
        rect = QRect( -w/2.0, -h/2.0, w, h )
    
        # 4. Rotate the painter and draw the ellipse
        painter.translate( center.x(), center.y() )
        painter.rotate( -maxL.angle() )
        painter.setPen( Qt.black )
        painter.drawEllipse( rect )
    

    考虑到三个点是两个轴的端点,目标是计算椭圆的边界矩形。

    所以,第一步找到更长的轴。第二步找到小轴。
    矩形由轴长度定义,我们只需要旋转画笔即可绘制椭圆。

    应该存在更好的解决方案,但目前看来我的解决方案可以工作......

    编辑:它又快又脏。因此,它不适用于很多情况。 更好的解决方案是使用以下属性:对于椭圆上的每个点,该点与两个静态点之间的距离之和是恒定的。 因此,您可以计算这个距离并确定另外两个点并确定两个轴。

    【讨论】:

    • 效果很好。我面临着找到最大中线的问题,当我定义自己修复它时,它起作用了。但我仍然不知道如何使用普通 qt 找到最大中线。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多