Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2014
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.32 KB | None | 0 0
  1. from PySide import QtCore, QtGui from math import sqrt 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 __mul__(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 rotate(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("Fractals") self.setMinimumSize(220, 150) self.Widget = MyWidget(self) self.Widget.setGeometry(0, 0, self.width(), self.height()) self.SpinBox = QtGui.QSpinBox(self) self.SpinBox.setGeometry(10, 10, 100, 30) self.SpinBox.setRange(0, 6) self.SpinBox.setValue(4) self.ComboBox = QtGui.QComboBox(self) self.ComboBox.setGeometry(self.width() - 110, 10, 100, 30) self.ComboBox.addItems(["Koch", "Levi", "Dragon"]) QtCore.QObject.connect(self.SpinBox, QtCore.SIGNAL("valueChanged(int)"), self.Widget.changeN) QtCore.QObject.connect(self.ComboBox, QtCore.SIGNAL("currentIndexChanged(int)"), self.Widget.changeType) QtCore.QObject.connect(self.Widget, QtCore.SIGNAL("changeRange(int, int)"), self.SpinBox.setRange) QtCore.QObject.connect(self.Widget, QtCore.SIGNAL("setValue(int)"), self.SpinBox.setValue) def resizeEvent(self, event): w = self.width() h = self.height() self.Widget.setGeometry(0, 0, self.width(), self.height()) self.SpinBox.setGeometry(10, 10, 100, 30) self.ComboBox.setGeometry(self.width() - 110, 10, 100, 30) class MyWidget(QtGui.QWidget): def __init__(self, parent = None, N = 4): 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) if self.type_ == 1: if Y * 1.6 >= X: P1 = Point(0.25 * X, 0.8 * Y) P2 = Point(0.75 * X, 0.8 * Y) self.Levi(P1, P2, self.N) else: P1 = Point(X / 2 - 0.4 * Y, 0.8 * Y) P2 = Point(X / 2 + 0.4 * Y, 0.8 * Y) self.Levi(P1, P2, self.N) if self.type_ == 2: if Y * 1.5 >= X: P1 = Point(2 / 9 * X, 2 / 3 * Y) P2 = Point(8 / 9 * X, 2 / 3 * Y) self.Dragon(P1, P2, self.N) else: P1 = Point(X / 2 - 5 / 12 * Y, 2 / 3 * Y) P2 = Point(X / 2 + 7 / 12 * Y, 2 / 3 * Y) self.Dragon(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).rotate(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) def Levi(self, P0, P2, n): if n == 0: painter = QtGui.QPainter() painter.begin(self) painter.drawLine(P0.x, P0.y, P2.x, P2.y) painter.end() return P1 = P0 + (Vector(P0, P2) * (1 / sqrt(2))).rotate(-1 / sqrt(2), 1 / sqrt(2)) self.Levi(P0, P1, n - 1) self.Levi(P1, P2, n - 1) def Dragon(self, P0, P2, n): if n == 0: painter = QtGui.QPainter() painter.begin(self) painter.drawLine(P0.x, P0.y, P2.x, P2.y) painter.end() return P1 = P0 + (Vector(P0, P2) * (1 / sqrt(2))).rotate(-1 / sqrt(2), 1 / sqrt(2)) self.Dragon(P0, P1, n - 1) self.Dragon(P2, P1, n - 1) def changeN(self, n): self.N = n self.repaint() def changeType(self, type_): self.type_ = type_ if type_ == 0: self.emit(QtCore.SIGNAL("changeRange(int, int)"), 0, 6) self.emit(QtCore.SIGNAL("setValue(int)"), 4) elif type_ == 1: self.emit(QtCore.SIGNAL("changeRange(int, int)"), 0, 14) self.emit(QtCore.SIGNAL("setValue(int)"), 7) elif type_ == 2: self.emit(QtCore.SIGNAL("changeRange(int, int)"), 0, 14) self.emit(QtCore.SIGNAL("setValue(int)"), 7) self.repaint() app = QtGui.QApplication(sys.argv) Window = MyWindow() Window.show() app.exec_()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement