-
Notifications
You must be signed in to change notification settings - Fork 0
/
Mandelbrot.py
170 lines (136 loc) · 5.37 KB
/
Mandelbrot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import sys, random, math
from PyQt5.QtWidgets import *
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QPainter, QColor
from multiprocessing import Process
xMin = -3
xMax = 3
yMin = -3
yMax = 3
#widthScale = 3
#heightScale = 3
zoomLevel = 4
class Mandelbrot(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setGeometry(300, 300, 700, 700) #300, 190
self.setWindowTitle('Mandelbrot')
self.show()
#Custom version of the range function that works with float numbers
def frange(self, start, stop, step):
i = start
while i < stop:
yield i
i += step
def linearMap(self, value, low, high, newLow, newHigh):
return newLow + ((value - low) / (high - low)) * (newHigh - newLow)
#Called whenever the window is resized or brought into focus
def paintEvent(self, event):
qp = QPainter()
qp.begin(self)
#Run the drawMandelbrot program
#self.runMultiprocessing(qp)
self.drawMandelbrot(qp, xMin, xMax, yMin, yMax)
qp.end()
def runMultiprocessing(self, qp):
numberOfThreads = 4
totalLength = abs(xMin) + abs(xMax)
pieceLength = totalLength / numberOfThreads
for i in range(numberOfThreads):
xMinNew = xMin + (pieceLength * i)
xMaxNew = xMin + (pieceLength * (i + 1))
print("Process ", i, " started.")
p = Process(target=self.drawMandelbrot, args=(qp, xMinNew, xMaxNew, yMin, yMax))
p.start()
p.join()
def mousePressEvent(self, event):
global xMin
global xMax
global yMin
global yMax
size = self.size()
windowWidth = size.width()
windowHeight = size.height()
xMouse = event.x()
yMouse = event.y()
#print("xMouse: ", xMouse)
#print("yMouse: ", yMouse)
#print("Before Map - xMin: ", xMin)
#print("Before Map - yMin: ", yMin)
#print("Before Map - xMax: ", xMax)
#print("Before Map - yMax: ", yMax)
xMouse = self.linearMap(xMouse, 0, windowWidth, xMin, xMax)
yMouse = self.linearMap(yMouse, 0, windowHeight, yMax, yMin)
#print("xMouse: ", xMouse)
#print("yMouse: ", yMouse)
#Make temporary variables to store the new x/y min/max so they aren't changed while the algorithms are still working
xMinTemp = xMouse - ((xMax - xMin) / (zoomLevel * zoomLevel))
xMaxTemp = xMouse + ((xMax - xMin) / (zoomLevel * zoomLevel))
yMinTemp = yMouse - ((yMax - yMin) / (zoomLevel * zoomLevel))
yMaxTemp = yMouse + ((yMax - yMin) / (zoomLevel * zoomLevel))
xMin = xMinTemp
xMax = xMaxTemp
yMin = yMinTemp
yMax = yMaxTemp
#Update scale for the new zoomed in view
#widthScale = widthScale / ((zoomLevel * zoomLevel) / 1.5)
#heightScale = heightScale / ((zoomLevel * zoomLevel) / 1.5)
widthScale = (xMax - xMin) / size.width()
heightScale = (yMax - yMin) / size.height()
self.repaint()
#print("Done zooming in.")
#print("New xMin: ", xMin)
#print("New xMax: ", xMax)
#print("New yMin: ", yMin)
#print("New yMax: ", yMax)
#print("New widthScale: ", widthScale)
#print("New heightScale: ", heightScale)
#print(x)
#print(y)
def drawMandelbrot(self, qp, xMin, xMax, yMin, yMax):
#Variables
size = self.size()
maxIteration = 255
widthScale = (xMax - xMin) / size.width()
heightScale = (yMax - yMin) / size.height()
# widthScale = 6 / size.width()
# heightScale = 6 / size.height()
for w in self.frange(xMin, xMax, widthScale):
for h in self.frange(yMin, yMax, heightScale):
x = 0
y = 0
iteration = 0
while (x*x + y*y <= 4) and (iteration < maxIteration):
xtemp = (x*x - y*y) + w
y = ((2*x) * y) + h
x = xtemp
iteration += 1
'''
if iteration != maxIteration:
if iteration <= 33:
qp.setPen(QColor(self.linearMap(iteration, 0, 33, 0, 255), 255, 255)) #Red is based on iteration
#qp.setPen(Qt.red)
elif iteration > 33 and iteration <= 66:
qp.setPen(QColor(255, self.linearMap(iteration, 33, 66, 0, 255), 255)) #Green is based on iteration
#qp.setPen(Qt.green)
else:
qp.setPen(QColor(255, 255, self.linearMap(iteration, 67, maxIteration, 0, 255))) #Blue is based on iteration
#qp.setPen(Qt.blue)
else:
qp.setPen(Qt.black)
'''
if iteration != maxIteration:
qp.setPen(QColor.fromHsv(iteration, 255, 255))
else:
qp.setPen(Qt.black)
newW = self.linearMap(w, xMin, xMax, 0, size.width() - 1)
newH = self.linearMap(h, yMin, yMax, size.height() - 1, 0)
qp.drawPoint(newW, newH)
def main():
app = QApplication([])
ex = Mandelbrot()
app.exec_()
if __name__ == '__main__':
main()