Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from PySide import QtCore, QtGui
- from math import *
- import sys
- class Point:
- def __init__(self, x, y):
- self.x = x
- self.y = y
- def __add__(self, other):
- return Point(self.x + other.x, self.y + other.y)
- class Vector:
- def __init__(self, a, b):
- if type(a) == type(b) == Point:
- self.x = b.x - a.x
- self.y = b.y - a.y
- else:
- self.x = a
- self.y = b
- def __add__(self, other):
- return Vector(self.x + other.x, self.y + other.y)
- def __sub__(self, other):
- return Vector(other.x - self.x, other.y - self.y)
- def __rmul__(self, other):
- if type(other) == Vector:
- return self.x * other.y - self.y * other.x
- return Vector(self.x * (other), self.y *(other))
- def __pow__(self, other):
- return self.x * other.x + self.y * other.y
- def rotation(self, sin, cos):
- return Vector(self.x * cos - self.y * sin, self.x * sin + self.y * cos)
- class MyWindow(QtGui.QMainWindow):
- def __init__(self, parent = None):
- QtGui.QMainWindow.__init__(self)
- self.resize(700, 600)
- self.setWindowTitle("Fractal")
- self.setMinimumSize(220, 150)
- self.Widget = MyWidget(self)
- self.Widget.setGeometry(0, 0, self.width(), self.height())
- def resizeEvent(self, event):
- w = self.width()
- h = self.height()
- self.Widget.setGeometry(0, 0, self.width(), self.height())
- class MyWidget(QtGui.QWidget):
- def __init__(self, parent = None, N = 5):
- QtGui.QWidget.__init__(self, parent)
- self.type_ = 0
- self.N = N
- self.resize(parent.width(), parent.height())
- def paintEvent(self, event):
- X = self.width()
- Y = self.height()
- if self.type_ == 0:
- if Y * 2 * sqrt(3) >= X:
- P1 = Point(0, Y / 2 + X / 4 / sqrt(3))
- P2 = Point(X, Y / 2 + X / 4 / sqrt(3))
- self.Koch(P1, P2, self.N)
- else:
- P1 = Point(X / 2 - Y * sqrt(3), Y)
- P2 = Point(X / 2 + Y * sqrt(3), Y)
- self.Koch(P1, P2, self.N)
- def Koch(self, P0, P4, n):
- if n == 0:
- painter = QtGui.QPainter()
- painter.begin(self)
- painter.drawLine(P0.x, P0.y, P4.x, P4.y)
- painter.end()
- return
- P1 = P0 + Vector(P0, P4) * (1 / 3)
- P3 = P0 + Vector(P0, P4) * (2 / 3)
- P2 = P1 + Vector(P1, P0).rotation(sqrt(3) / 2, -0.5)
- self.Koch(P0, P1, n - 1)
- self.Koch(P1, P2, n - 1)
- self.Koch(P2, P3, n - 1)
- self.Koch(P3, P4, n - 1)
- app = QtGui.QApplication(sys.argv)
- Window = MyWindow()
- Window.show()
- app.exec_()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement