Advertisement
Guest User

Untitled

a guest
May 25th, 2015
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.78 KB | None | 0 0
  1. from PySide import QtCore, QtGui
  2. from math import *
  3. import sys
  4.  
  5.  
  6. class Point:
  7. def __init__(self, x, y):
  8. self.x = x
  9. self.y = y
  10.  
  11. def __add__(self, other):
  12. return Point(self.x + other.x, self.y + other.y)
  13.  
  14.  
  15. class Vector:
  16. def __init__(self, a, b):
  17. if type(a) == type(b) == Point:
  18. self.x = b.x - a.x
  19. self.y = b.y - a.y
  20. else:
  21. self.x = a
  22. self.y = b
  23.  
  24. def __add__(self, other):
  25. return Vector(self.x + other.x, self.y + other.y)
  26.  
  27. def __sub__(self, other):
  28. return Vector(other.x - self.x, other.y - self.y)
  29.  
  30. def __rmul__(self, other):
  31. if type(other) == Vector:
  32. return self.x * other.y - self.y * other.x
  33. return Vector(self.x * (other), self.y *(other))
  34.  
  35. def __pow__(self, other):
  36. return self.x * other.x + self.y * other.y
  37.  
  38. def rotation(self, sin, cos):
  39. return Vector(self.x * cos - self.y * sin, self.x * sin + self.y * cos)
  40.  
  41.  
  42. class MyWindow(QtGui.QMainWindow):
  43. def __init__(self, parent = None):
  44. QtGui.QMainWindow.__init__(self)
  45. self.resize(700, 600)
  46. self.setWindowTitle("Fractal")
  47. self.setMinimumSize(220, 150)
  48.  
  49. self.Widget = MyWidget(self)
  50. self.Widget.setGeometry(0, 0, self.width(), self.height())
  51.  
  52. def resizeEvent(self, event):
  53. w = self.width()
  54. h = self.height()
  55. self.Widget.setGeometry(0, 0, self.width(), self.height())
  56.  
  57.  
  58. class MyWidget(QtGui.QWidget):
  59. def __init__(self, parent = None, N = 5):
  60. QtGui.QWidget.__init__(self, parent)
  61. self.type_ = 0
  62. self.N = N
  63. self.resize(parent.width(), parent.height())
  64.  
  65. def paintEvent(self, event):
  66. X = self.width()
  67. Y = self.height()
  68. if self.type_ == 0:
  69. if Y * 2 * sqrt(3) >= X:
  70. P1 = Point(0, Y / 2 + X / 4 / sqrt(3))
  71. P2 = Point(X, Y / 2 + X / 4 / sqrt(3))
  72. self.Koch(P1, P2, self.N)
  73. else:
  74. P1 = Point(X / 2 - Y * sqrt(3), Y)
  75. P2 = Point(X / 2 + Y * sqrt(3), Y)
  76. self.Koch(P1, P2, self.N)
  77.  
  78. def Koch(self, P0, P4, n):
  79. if n == 0:
  80. painter = QtGui.QPainter()
  81. painter.begin(self)
  82. painter.drawLine(P0.x, P0.y, P4.x, P4.y)
  83. painter.end()
  84. return
  85. P1 = P0 + Vector(P0, P4) * (1 / 3)
  86. P3 = P0 + Vector(P0, P4) * (2 / 3)
  87. P2 = P1 + Vector(P1, P0).rotation(sqrt(3) / 2, -0.5)
  88. self.Koch(P0, P1, n - 1)
  89. self.Koch(P1, P2, n - 1)
  90. self.Koch(P2, P3, n - 1)
  91. self.Koch(P3, P4, n - 1)
  92.  
  93.  
  94. app = QtGui.QApplication(sys.argv)
  95. Window = MyWindow()
  96. Window.show()
  97. app.exec_()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement