** The drawing part uses the code of @ noc06140728. ** ** http://qiita.com/noc06140728/items/8b8f06cfc312b8492df4
I wanted to see the drawing process, so I made it. Please use it as a reference for creating applications with PySide.
** * Drawing is started with mousePressEvent ** ** * Not assumed if drawing is started again during drawing **
fern_gui.py
import sys
import random
from PySide.QtGui import QApplication
from PySide.QtGui import QMainWindow
from PySide.QtGui import QLabel
from PySide.QtGui import QPixmap
from PySide.QtGui import QPainter
from PySide.QtGui import QColor
# Quot Begin
# From http://qiita.com/noc06140728/items/8b8f06cfc312b8492df4
N = 20
xm = 0
ym = 0.5
h = 0.6
width = 500
height = 500
W1x = lambda x, y: 0.836 * x + 0.044 * y
W1y = lambda x, y: -0.044 * x + 0.836 * y + 0.169
W2x = lambda x, y: -0.141 * x + 0.302 * y
W2y = lambda x, y: 0.302 * x + 0.141 * y + 0.127
W3x = lambda x, y: 0.141 * x - 0.302 * y
W3y = lambda x, y: 0.302 * x + 0.141 * y + 0.169
W4x = lambda x, y: 0
W4y = lambda x, y: 0.175337 * y
def f(k, x, y):
    if 0 < k:
        for p in f(k - 1, W1x(x, y), W1y(x, y)):
            yield p
        if random.random() < 0.3:
            for p in f(k - 1, W2x(x, y), W2y(x, y)):
                yield p
        if random.random() < 0.3:
            for p in f(k - 1, W3x(x, y), W3y(x, y)):
                yield p
        if random.random() < 0.3:
            for p in f(k - 1, W4x(x, y), W4y(x, y)):
                yield p
    else:
        s = 490
        yield x * s + width * 0.5, height - y * s
# Quot End
class MainWindow(QMainWindow):
    def __init__(self, parent = None):
        super().__init__(parent)
        self.setFixedSize(width, height)
        self.refresh_rate = 200
        self.pixmap = QPixmap(width, height)
        self.pixmap.fill()
    def paintEvent(self, event):
        painter = QPainter(self)
        painter.drawPixmap(0, 0, self.pixmap)
    def mousePressEvent(self, event):
        painter = QPainter(self.pixmap)
        painter.setPen(QColor(0, 128, 0))
        for i, p in enumerate(f(N, 0, 0)):
            painter.drawPoint(p[0], p[1])
            if i % self.refresh_rate == 0:
                self.repaint()
        self.repaint()
        painter.end()
        print("finished")
def main():
    app = QApplication(sys.argv)
    window = MainWindow()
    app.setActiveWindow(window)
    window.show()
    sys.exit(app.exec_())
if __name__ == '__main__':
    main()
fern_cui.py
import sys
import random
from PySide.QtCore import QDir
from PySide.QtGui import QDesktopServices
from PySide.QtGui import QApplication
from PySide.QtGui import QPixmap
from PySide.QtGui import QPainter
from PySide.QtGui import QColor
# Quot Begin
# From http://qiita.com/noc06140728/items/8b8f06cfc312b8492df4
N = 20
xm = 0
ym = 0.5
h = 0.6
width = 500
height = 500
W1x = lambda x, y: 0.836 * x + 0.044 * y
W1y = lambda x, y: -0.044 * x + 0.836 * y + 0.169
W2x = lambda x, y: -0.141 * x + 0.302 * y
W2y = lambda x, y: 0.302 * x + 0.141 * y + 0.127
W3x = lambda x, y: 0.141 * x - 0.302 * y
W3y = lambda x, y: 0.302 * x + 0.141 * y + 0.169
W4x = lambda x, y: 0
W4y = lambda x, y: 0.175337 * y
def f(k, x, y):
    if 0 < k:
        for p in f(k - 1, W1x(x, y), W1y(x, y)):
            yield p
        if random.random() < 0.3:
            for p in f(k - 1, W2x(x, y), W2y(x, y)):
                yield p
        if random.random() < 0.3:
            for p in f(k - 1, W3x(x, y), W3y(x, y)):
                yield p
        if random.random() < 0.3:
            for p in f(k - 1, W4x(x, y), W4y(x, y)):
                yield p
    else:
        s = 490
        yield x * s + width * 0.5, height - y * s
# Quot End
def main():
    app = QApplication(sys.argv)
    # Output Directory
    # <Desktop>/ferns/
    directory = QDir(QDesktopServices.storageLocation(QDesktopServices.DesktopLocation))
    directory.mkdir("ferns")
    if directory.cd("ferns") == False:
        print("directory error")
        app.quit()
        return
    # Output
    # <Desktop>/ferns/fern_*****.png
    output_rate = 2000
    pixmap = QPixmap(width, height)
    pixmap.fill()
    painter = QPainter(pixmap)
    painter.setPen(QColor(0, 128, 0))
    for i, p in enumerate(f(N, 0, 0)):
        painter.drawPoint(p[0], p[1])
        if i % output_rate == 0:
            pixmap.save("%s/fern_%05d.png " % (directory.path(), i / output_rate))
    pixmap.save("%s/fern_%05d.png " % (directory.path(), i / output_rate + 1))
    painter.end()
    print("finished")
    app.quit()
if __name__ == '__main__':
    main()

Recommended Posts