2012-04-24 63 views
10

我有一個點列表。例如,points = [[160, 75], [115, 567]]如何在PyQt4中用動畫畫一條線

如何繪製PyQt4中一條線,所以這將是這樣的:Line

在此先感謝。

編輯:爲了記錄在案,我想實現Bezier曲線,所以它看起來像這樣: Quartic Bézier curve

這裏是我此刻的代碼:

from PyQt4.QtGui import QWidget, QPolygonF, QPainter, QPen, QBrush, QColor, \ 
    QApplication, QIcon, QVBoxLayout, QSlider, QHBoxLayout, QPushButton, QLCDNumber 
from PyQt4.QtCore import QObject, SIGNAL, SLOT, QPointF, Qt, QRectF, QPointF 
import time, sys 

#================================================================ 
# For the next block I used this post 
# http://stackoverflow.com/a/3220819/736306, 
# but I wish to replace it with self.liniar_bez(), 
# biliniar_bez(), self.cubic_bez() and self.fourG_bez() 
# because I want to control 't' with self.slider 

def avg(a, b): 
    xa, ya = a 
    xb, yb = b 
    return (xa + xb) * 0.5, (ya + yb) * 0.5 

def bez4split(p0, p1, p2, p3, p4): 
    p01 = avg(p0, p1) 
    p12 = avg(p1, p2) 
    p23 = avg(p2, p3) 
    p34 = avg(p3, p4) 
    p012 = avg(p01, p12) 
    p123 = avg(p12, p23) 
    p234 = avg(p23, p34) 
    p= avg(p012, p123) 
    p1234 = avg(p123, p234) 
    p= avg(p0123, p1234) 
    return [(p0, p01, p012, p0123, p), 
     (p, p1234, p234, p34, p4)] 

def bez4(p0, p1, p2, p3, p4, levels=5): 
    if levels <= 0: 
     return [p0, p4] 
    else: 
     (a0, a1, a2, a3, a4), (b0, b1, b2, b3, b4) = bez4split(p0, p1, p2, p3, p4) 
     return (bez4(a0, a1, a2, a3, a4, levels - 1) + 
       bez4(b0, b1, b2, b3, b4, levels - 1)[1:]) 
#================================================================ 

points = [[160, 75], [115, 567], [292, 58], [685, 194], [734, 517]] 
coords = bez4(*points) 

class Bezier(QWidget): 
    def __init__(self, parent=None): 
     QWidget.__init__(self, parent) 

     self.lcd = QLCDNumber(self) 
     self.slider = QSlider(Qt.Horizontal, self) 
     self.slider.setRange(0, 100) 
     self.closeButton = QPushButton('Close') 

     bottomLayout = QHBoxLayout() 
     bottomLayout.addWidget(self.lcd) 
     bottomLayout.addWidget(self.slider) 
     bottomLayout.addWidget(self.closeButton) 

     mainLayout = QVBoxLayout() 
     mainLayout.addStretch(1) 
     mainLayout.addLayout(bottomLayout) 

     self.setLayout(mainLayout) 

     self.time = 0 
     self.time_step = 0.025 
     self.timer_id = self.startTimer(1) 

     self.tracking = None 

     QObject.connect(self.closeButton, SIGNAL('clicked(bool)'), app.exit) 
     QObject.connect(self.slider, SIGNAL('valueChanged(int)'), self.lcd, SLOT('display(float)')) 
     #self.slider.valueChanged.connect(self.lcd.display) 

     self.setWindowTitle('Bonus Example') 

    def poly(self, pts): 
     return QPolygonF(map(lambda p: QPointF(*p), pts)) 

    def bezAnimation(self): 
     painter = QPainter(self) 
     painter.setRenderHints(QPainter.Antialiasing) 

     pts = points[:] 
     crds = coords[:] 

     painter.setPen(QPen(QColor(Qt.lightGray), 3, Qt.DashDotDotLine)) 
     painter.drawPolyline(self.poly(pts)) 

     painter.setBrush(QBrush(QColor(255, 025, 0))) 
     painter.setPen(QPen(QColor(Qt.lightGray), 1)) 
     for x, y in pts: 
      painter.drawEllipse(QRectF(x - 4, y - 4, 8, 8)) 

    def paintEvent(self, event): 
     self.bezAnimation() 

    # As you know, 0.00 > 't' > 1.00 
    # and QSlider does not provide support for float numbers, 
    # so we simply divide t by 100 
    def liniar_bez(self, p, t): 
     t = t/100.0 
     new_p[0] = (1 - t) * p[0][0] + t * p[1][0] 
     new_p[1] = (1 - t) * p[0][1] + t * p[1][1] 

    def biliniar_bez(self, p, t): 
     t = t/100.0 
     new_p[0][0] = ((1 - t) ** 2) * p[0][0] + 2 * (1 - t) * t * p[1][0] + (t ** 2) * p[2][0] 
     new_p[0][1] = ((1 - t) ** 2) * p[0][1] + 2 * (1 - t) * t * p[1][1] + (t ** 2) * p[2][1] 

    def cubic_bez(self, p, t): 
     t = t/100.0 
     new_p[0][0] = ((1 - t) ** 3) * p[0][0] + 3 * ((1 - t) ** 2) * t * p[1][0] + (t ** 2) * p[2][0] + (t ** 3) * p[3][0] 
     new_p[0][1] = ((1 - t) ** 3) * p[0][1] + 3 * ((1 - t) ** 2) * t * p[1][1] + (t ** 2) * p[2][1] + (t ** 3) * p[3][1] 

    def fourG_bez(self, p, t): 
     t = t/100.0 
     new_p[0][0] = ((1 - t) ** 4) * p[0][0] + 4 * ((1 - t) ** 3) * t * p[1][0] + 4 * (1 - t) * (t ** 2) * p[2][0] + 4 * (1 - t) * (t ** 3) * p[3][0] + (t ** 4) * p[4][0] 
     new_p[0][1] = ((1 - t) ** 4) * p[0][1] + 4 * ((1 - t) ** 3) * t * p[1][1] + 4 * (1 - t) * (t ** 2) * p[2][1] + 4 * (1 - t) * (t ** 3) * p[3][1] + (t ** 4) * p[4][1] 

    def timerEvent(self, event): 
     if self.timer_id == event.timerId(): 
      #self.bezAnimation() 
      self.time += self.time_step 
      self.update() 

    def closeEvent(self, event): 
     self.deleteLater() 

    #================================================================ 
    # And this one too 
    # http://stackoverflow.com/a/3220819/736306 
    def mousePressEvent(self, event): 
     i = min(range(len(points)), 
      key=lambda i: (event.x() - points[i][0]) ** 2 + 
         (event.y() - points[i][1]) ** 2) 

     self.tracking = lambda p: points.__setitem__(i, p) 

    def mouseMoveEvent(self, event): 
     if self.tracking: 
      self.tracking((event.x(), event.y())) 
      self.update() 

    def mouseReleaseEvent(self, event): 
     self.tracking = None 
    #================================================================ 

if __name__ == '__main__': 
    app = QApplication(sys.argv) 
    form = Bezier() 
    form.setGeometry(10, 30, 871, 727) 
    form.show() 
    sys.exit(app.exec_()) 

所以,我如何解決這個問題的想法: 1.打造專業化所有self.biliniar_bez曲線(),...,self.fourG_bez() 2.它們共享相同的點,所以他們會真正匹配這個點。 3.移動點(我認爲,他們的立場必須牛逼匹配,因此,如果T = 0.25然後每個點必須通過在他們的臺詞此時季度)

的問題是在路上移動點。我有每一行的第一和第二點的座標。需要中間的。

+0

你嘗試過什麼了嗎?你堅持什麼部分?我記得在動畫波上回答你的其他問題,所以我認爲這會讓你開始正確的? http://stackoverflow.com/questions/9465047/make-an-animated-wave-with-drawpolyline-in-pyside-pyqt。它明顯不是數學,你卡住了......所以你更具體地遇到了什麼困難? – jdi 2012-04-25 23:45:34

回答

2
  1. 如果使用正常的小工具,那麼你會在QPainter.drawPath()
  2. 如果您正在使用QGraphics尋找,那麼你會在看QGraphicsPathItem

這兩個的這些都會有類似的做法,其中在你的抽籤期間,你將不得不逐步建立這條線。回顧一下自己以前的問題,瞭解如何繪製線條的具體示例:Make an animated wave with drawPolyline in PySide/PyQt

作爲示例,如果您使用的是QGraphics,那麼您的場景中將有一個QGraphicsPathItem實例。然後你會被循環和增量設置的完整路徑更新QPainterPath製作動畫:http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/qpainterpath.html#cubicTo

+0

我已經添加了一些代碼併發布瞭解決方案,但在我看來,這還不夠好。 – SaulTigh 2012-04-26 07:00:16

+0

@Saul_Tigh:有什麼辦法可以將代碼示例分解爲一個較小的特定和可運行的問題?有了這些代碼,我不確定在哪裏解決您的問題。而且,它無法運行。沒有'self.updatePoints(畫家)'。你需要問一個具體的問題。這太模糊了。 – jdi 2012-04-26 18:04:21

+0

我的問題是關於如何在兩個點之間移動一個點(如第一個圖像)來畫一條線。我有2分(他們之間沒有分數),並導致我的問題。 – SaulTigh 2012-04-28 12:03:37

2

請參閱您的油漆事件下面的代碼繪製在Qt的一條線。

def paintEvent(self, event): 
    painter = Qt.QPainter() 
    painter.begin(self) 
    painter.pen().setWidth(int width) 
painter.setPen(Qt.QColor(100,100,100)) 
    painter.drawLine(x1,y1,x2,y2) 
    painter.end() 
    Qt.QFrame.paintEvent(self, event)