-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweek7.py
More file actions
109 lines (93 loc) · 3.78 KB
/
Copy pathweek7.py
File metadata and controls
109 lines (93 loc) · 3.78 KB
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
import colorsys
from PIL import Image
from Geometry import *
size = 255
image = Image.new("RGB", (size, size), color="white")
draw = ImageDraw.Draw(image)
middle = size // 2
def mandelbrot(zoom=((-2, 1), (-1.5, 1))):
for i in range(size):
for j in range(size):
zoomX = zoom[0][1] - zoom[0][0]
zoomY = zoom[1][1] - zoom[1][0]
realPart = zoom[0][0] + i * max(zoomX, zoomY) / size
imaginaryPart = zoom[1][0] + j * max(zoomX, zoomY) / size
c = complex(realPart, imaginaryPart)
z = complex(0, 0)
steps = 0
while abs(z) <= 2 and steps < 30:
zPrevious = z
# z = z*z + c
z = z * z + c * c * c * c
steps += 1
if abs(z) <= 2:
image.putpixel((i, j), (int(255 * abs(z) + 1), 0, 0))
# image.putpixel((i, j), (int(255 * abs(c) + 1), 0, 0))
else:
# smooth = steps + 1 - (math.log(abs(zPrevious) + 1))/math.log(2)
# smooth = steps + 1 - math.log(abs(z) - abs(zPrevious) + 1)/math.log(2)
smooth = steps + 1 - math.log(math.log(abs(z) + 1)) / math.log(2)
image.putpixel((i, j), hsv2rgb(smooth / 30, 1, 1))
# image.putpixel((i, j), ((255 // 30 * steps), (255 // 30 * steps), (255 // 30 * steps)))
image.save("images7\\mandelbrotColouredZZCCCC.png")
def hsv2rgb(h, s, v):
return tuple(int(i * 255) for i in colorsys.hsv_to_rgb(h, s, v))
def julia(a, b, zoom=((-2, 1), (-1.5, 1))):
for i in range(size):
for j in range(size):
zoomX = zoom[0][1] - zoom[0][0]
zoomY = zoom[1][1] - zoom[1][0]
realPart = zoom[0][0] + i * max(zoomX, zoomY) / size
imaginaryPart = zoom[1][0] + j * max(zoomX, zoomY) / size
z = complex(realPart, imaginaryPart)
c = complex(a, b)
steps = 1
while abs(z) <= 2 and steps < 30:
z = z * z + c
steps += 1
if abs(z) <= 2:
image.putpixel((i, j), (int(255 * abs(z) + 1), 0, 0))
else:
smooth = steps + 1 - math.log(math.log(abs(z) + 1)) / math.log(2)
image.putpixel((i, j), hsv2rgb(smooth / 30, 1, 1))
image.save("images7\\julia" + str(a) + "b=" + str(b) + ".png")
def newton(zoom=((-2, 2), (-2, 2))):
roots = []
colors = [(0, 0, 255), (0, 255, 0), (255, 0, 0), (255, 255, 0), (255, 0, 255), (0, 255, 255)]
for i in range(size):
for j in range(size):
zoomX = zoom[0][1] - zoom[0][0]
zoomY = zoom[1][1] - zoom[1][0]
realPart = zoom[0][0] + i * max(zoomX, zoomY) / size
imaginaryPart = zoom[1][0] + j * max(zoomX, zoomY) / size
z = complex(realPart, imaginaryPart)
z, steps = newtonMethod(z)
if z:
correspondingRootFound = False
for root in roots:
if abs(root - z) < 0.1:
z = root
correspondingRootFound = True
break
if not correspondingRootFound:
roots.append(z)
image.putpixel((i, j), (steps, steps, steps))
# image.putpixel((i, j), colors[roots.index(z)])
image.save("images7\\newtonStepsPower3.png")
def newtonMethod(z):
steps = 1
while steps < 60:
if z == 0: # mozne delenie nulou
return None, None
zNew = z - (z ** 3 - 1) / (3 * z ** 2)
if abs(zNew - z) < 0.01:
return z, steps
z = zNew
steps += 1
return None, None
# mandelbrot()
# mandelbrot(((-1.9, -1.6),(-0.3, 0.3)))
# julia(0.3, 0.01)
newton()
# newton(((0.3, 0.6), (0.3, 0.6)))
image.show()