Advertisement
Guest User

Untitled

a guest
Jan 9th, 2020
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.30 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. import sys
  3. from PyQt5.QtWidgets import *
  4. from PyQt5.QtGui import *
  5. import math
  6.  
  7.  
  8. class Example(QWidget):
  9. def __init__(self):
  10. super().__init__()
  11. self.initUI()
  12. self.rules = dict()
  13. with open('input.txt', 'r') as f:
  14. data = f.read().split("\n")
  15. self.angle = 360 / int(data[1])
  16. self.axioms = data[2]
  17. for i in data[3:]:
  18. self.rules[i.split()[0]] = i.split()[1]
  19. self.iterations = 1
  20. self.program = self.lsystem(self.axioms, self.rules, self.iterations)
  21. self.alpha = 0
  22. self.coords = [[0, 500, "YES"]]
  23. self.generator()
  24.  
  25. def generator(self):
  26. for i in self.program:
  27. if i == "F":
  28. x = self.coords[-1][0] + 5000 * (math.cos(self.alpha) * math.pi / 180)
  29. y = self.coords[-1][1] + 5000 * (math.sin(self.alpha) * math.pi / 180)
  30. self.coords.append([x, y, "YES"])
  31. elif i == "f":
  32. x = self.coords[-1][0] + 5000 * (math.cos(self.alpha) * math.pi / 180)
  33. y = self.coords[-1][1] + 5000 * (math.sin(self.alpha) * math.pi / 180)
  34. self.coords.append([x, y, "NO"])
  35. elif i == "+":
  36. self.alpha = (self.alpha + self.angle) % 360
  37. elif i == "-":
  38. self.alpha = (self.alpha - self.angle) % 360
  39. elif i == "|":
  40. self.alpha = (self.alpha + 180) % 360
  41.  
  42. def paintEvent(self, event):
  43. qp = QPainter()
  44. qp.begin(self)
  45. for i in range(len(self.coords) - 1):
  46. qp.drawLine(self.coords[i][0], self.coords[i][1], self.coords[i + 1][0], self.coords[i + 1][1])
  47. qp.end()
  48.  
  49. def lsystem(self, axioms, rules, iterations):
  50. for _ in range(iterations):
  51. newAxioms = ''
  52. for axiom in axioms:
  53. if axiom in rules:
  54. newAxioms += rules[axiom]
  55. else:
  56. newAxioms += axiom
  57. axioms = newAxioms
  58. return axioms
  59.  
  60. def initUI(self):
  61. self.setGeometry(0, 0, 1024, 720)
  62. self.show()
  63.  
  64.  
  65. if __name__ == '__main__':
  66. app = QApplication(sys.argv)
  67. ex = Example()
  68. ex.show()
  69. sys.exit(app.exec())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement