Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- import sys
- from PyQt5.QtWidgets import *
- from PyQt5.QtGui import *
- import math
- class Example(QWidget):
- def __init__(self):
- super().__init__()
- self.initUI()
- self.rules = dict()
- with open('input.txt', 'r') as f:
- data = f.read().split("\n")
- self.angle = 360 / int(data[1])
- self.axioms = data[2]
- for i in data[3:]:
- self.rules[i.split()[0]] = i.split()[1]
- self.iterations = 1
- self.program = self.lsystem(self.axioms, self.rules, self.iterations)
- self.alpha = 0
- self.coords = [[0, 500, "YES"]]
- self.generator()
- def generator(self):
- for i in self.program:
- if i == "F":
- x = self.coords[-1][0] + 5000 * (math.cos(self.alpha) * math.pi / 180)
- y = self.coords[-1][1] + 5000 * (math.sin(self.alpha) * math.pi / 180)
- self.coords.append([x, y, "YES"])
- elif i == "f":
- x = self.coords[-1][0] + 5000 * (math.cos(self.alpha) * math.pi / 180)
- y = self.coords[-1][1] + 5000 * (math.sin(self.alpha) * math.pi / 180)
- self.coords.append([x, y, "NO"])
- elif i == "+":
- self.alpha = (self.alpha + self.angle) % 360
- elif i == "-":
- self.alpha = (self.alpha - self.angle) % 360
- elif i == "|":
- self.alpha = (self.alpha + 180) % 360
- def paintEvent(self, event):
- qp = QPainter()
- qp.begin(self)
- for i in range(len(self.coords) - 1):
- qp.drawLine(self.coords[i][0], self.coords[i][1], self.coords[i + 1][0], self.coords[i + 1][1])
- qp.end()
- def lsystem(self, axioms, rules, iterations):
- for _ in range(iterations):
- newAxioms = ''
- for axiom in axioms:
- if axiom in rules:
- newAxioms += rules[axiom]
- else:
- newAxioms += axiom
- axioms = newAxioms
- return axioms
- def initUI(self):
- self.setGeometry(0, 0, 1024, 720)
- self.show()
- if __name__ == '__main__':
- app = QApplication(sys.argv)
- ex = Example()
- ex.show()
- sys.exit(app.exec())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement