Created
January 22, 2016 12:47
-
-
Save efosong/3e7d15c260487867b208 to your computer and use it in GitHub Desktop.
Generate julia set images.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Requires PIL | |
import math | |
class Complex(object): | |
def __init__(self, real, img): | |
self.real = real | |
self.img = img | |
def __mul__(self, comp): | |
a = self.real * comp.real - self.img * comp.img | |
b = self.real * comp.img + self.img * comp.real | |
return Complex(a, b) | |
def __add__(self, comp): | |
a = self.real + comp.real | |
b = self.img + comp.img | |
return Complex(a, b) | |
def __sub__(self, comp): | |
a = self.real - comp.real | |
b = self.img - comp.img | |
return Complex(a,b) | |
def __div__(self, comp): | |
comp.img = -1*comp.img | |
a = self.real * comp.real - self.img * comp.img + 0.0 | |
b = self.real * comp.img + self.img * comp.real + 0.0 | |
denominator = comp.real ** 2 + comp.img ** 2 | |
a /= denominator | |
b /= denominator | |
return Complex(a,b) | |
def __str__(self): | |
if self.img > 0: | |
if self.real != 0: | |
return str(self.real) + "+" + str(self.img) + "i" | |
else: | |
return str(self.img) | |
elif self.img == 0: | |
return str(self.real) | |
else: | |
if self.real != 0: | |
return str(self.real) + str(self.img) + "i" | |
else: | |
return str(self.img) | |
def __pow__(self, index): | |
comp = Complex(1, 0) | |
for i in xrange(index): | |
comp *= self | |
return comp | |
def mod(self): | |
return self.real ** 2 + self.img ** 2 | |
def arg(self): | |
if self.real == 0: | |
if self.img == 0: | |
return 0 | |
else: | |
return math.pi/2 * (self.img / math.fabs(self.img)) | |
else: | |
return math.atan((1.0 * self.img) / self.real) | |
#==============================================================================# | |
import Image | |
# User definied parameters | |
res = int(raw_input("Input resolution: ")) | |
c_real = float(raw_input("Input real part of c: ")) | |
c_imag = float(raw_input("Input imaginary part of c: ")) | |
# Hard coded parameters | |
corn_x = -2 # x co-ordinate of top left corner | |
corn_y = 2 # y co-ordinate of top left corner | |
size = 4.0 # length of square | |
max_iter = 100 | |
mag_limit = 4 | |
img = Image.new("RGB", (res,res), (255,255,255)) | |
for i in xrange (1, res): | |
for j in xrange(1,res): | |
count = 0 | |
z = Complex(corn_x + i*size/res, corn_y - j*size/res) | |
while (count < max_iter and z.mod() < mag_limit): | |
count += 1 | |
z = z**2 + Complex(c_real, c_imag) | |
colour = 255 - int(math.floor(count * 255 / max_iter)) | |
img.putpixel((i,j),(colour,colour,colour)) | |
img.save("complex.png","PNG") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment