-
Notifications
You must be signed in to change notification settings - Fork 0
/
mandelbrot.py
executable file
·144 lines (113 loc) · 3.26 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
#!/usr/bin/env python2.6
#
# Calculate the mandelbrot set using python and/or native C.
# Copyright (C) 2009 Alejandro Segovia
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
import pygame
import sys
import math
from ctypes import *
class Color(Structure):
_fields_ = ("r", c_int), ("g", c_int), ("b", c_int)
def __getitem__(self, i):
if i == 0:
return int(self.r)
elif i == 1:
return int(self.g)
elif i == 2:
return int(self.b)
class Canvas:
def __init__(self, w, h):
self.w = w
self.h = h
self.surface = pygame.display.set_mode((w,h), pygame.DOUBLEBUF)
def clear(self):
self.surface.fill([0,0,0])
def update(self):
pygame.display.flip()
def putpixel(self, x, y, color):
x1 = x + self.w/2
y1 = -y + self.h/2
self.surface.set_at((x1,y1), color)
class Complex:
def __init__(self, real, imag):
self.real = real
self.imag = imag
def square(self):
real = self.real*self.real - self.imag*self.imag
imag = 2.0 * self.real * self.imag
return Complex(real, imag)
def __add__(self, other):
return Complex(self.real + other.real, self.imag + other.imag)
def modulo(self):
return math.sqrt(self.real * self.real + self.imag * self.imag)
def __str__(self):
return "%f + %fi" % (self.real, self.imag)
def __repr__(self):
return str(self)
def calc_set(w, h, num_iters):
l = [(0.0, 0.0, 0.0)] * (w * h)
for i in range(0, w):
for j in range(h):
ii = i - w/2.0
jj = j - h/2.0
c = Complex(ii * 4.0 / w, jj * 4.0 / h)
z = Complex(0.0, 0.0)
count = 0
while count < num_iters and z.modulo() <= 2:
z = z.square() + c
count += 1
if z.modulo() <= 2:
l[j * w + i] = (int(255*z.modulo()/2.0)*0, 0, int(255*z.modulo()/2.0))
else:
l[j * w + i] = (0, int(255*count/num_iters), 0)
return l
def main():
w, h = 512, 512
pygame.init()
canvas = Canvas(w, h)
canvas.clear()
canvas.update()
z = Complex(0.0, 0.0)
num_iters = 20
native_comp = True
t0 = pygame.time.get_ticks()
if not native_comp:
colors = calc_set(w, h, num_iters)
else:
colors = (Color * (w * h))()
lib = CDLL("libmandelbrot.so")
lib.calc_set(w, h, num_iters, colors)
tf = pygame.time.get_ticks()
time_str = "Time: %f secs" % ((tf - t0)/1000.0)
print time_str
pygame.display.set_caption(time_str)
pygame.display.set_caption("Mandelbrot Set")
for i in xrange(w):
for j in range(h):
ii = i - w/2
jj = j - h/2
color = colors[j * w + i]
canvas.putpixel(ii, jj, (color[0], color[1], color[2]))
print "Done"
canvas.update()
while True:
evt = pygame.event.wait()
if evt.type == pygame.QUIT:
sys.exit(0)
if evt.type == pygame.KEYDOWN and evt.key == pygame.K_ESCAPE:
sys.exit(0)
if __name__ == "__main__":
main()