You are not logged in.
Pages: 1
So I finally got around to figuring out how fractals are made (very simple), and I made a little test program.
Dependencies:
python
extra/python-pygame
python-numpy
Any improvements are appreciated. I'm sure I did some stupid things in there since I didn't actually check how many decimals of precision I get (I just picked "small" errors/thresholds).
If you want to see a different function, modify the return value for the function f(x) and its derivative df(x).
#!/usr/bin/python
import sys, pygame
pygame.init()
import random
random.seed()
def f(x):
return x ** 7 + 1
#return x ** 2
def df(x):
return 7 * x ** 6
#return 2 * x
def newton(x, maxiterations = 40, error = 1e-12):
for i in range(maxiterations):
res = f(x)
x = x - f(x)/df(x)
if(abs(f(x)) < error):
break
return x
size = w, h= 600, 600
screen = pygame.display.set_mode(size)
sc = pygame.surfarray.pixels3d(screen)
wi = 2.0/w
hi = 2.0/h
while 1:
colors = {}
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
for i in range(w):
for j in range(h):
x = complex(-1 + (j * hi) + (hi/2), -1 + (i * wi) + (wi/2))
result = newton(x)
result = complex(round(result.real, 6), round(result.imag, 6))
if result not in colors:
colors[result] = (random.randrange(0, 255), random.randrange(0, 255), random.randrange(0, 255))
sc[i][j] = colors[result]
pygame.surfarray.blit_array(screen, sc)
pygame.display.flip()
Edits:
- Peasantoid: thanks for reminding me of the numpy depends.
- added more screenshots
Last edited by tomd123 (2010-03-15 03:39:24)
Offline
Cool! Where do you plan to go from here?
Just a heads-up: your indent is screwy at line ~18.
Oh and you should add python-numpy to deps.
Last edited by Peasantoid (2010-03-12 02:30:08)
Offline
Cool! Where do you plan to go from here?
Just a heads-up: your indent is screwy at line ~18.
Oh and you should add python-numpy to deps.
I might take a look at some more functions, and I want to find out how to speed the process up.
I also should return None or something to notify that the point doesn't converge, which I'm suspecting is why I'm not getting a pattern in the center of the first one.
Offline
Just out of curiosity how did you decide on those particular functions? I'm no good with python but the issue with the first fractal not producing anything but seeminging random output might be because of the function and not the program. Nice program!
"The beautiful thing about learning is nobody can take it away from you."
B.B. King
Offline
Just out of curiosity how did you decide on those particular functions? I'm no good with python but the issue with the first fractal not producing anything but seeminging random output might be because of the function and not the program. Nice program!
That's true, I might need to add more precision/more iterations. As I stated earlier, I picked arbitrary "small" numbers and haven't actually done any serious calculations on specific needs
Anyways, I just picked random polynomials, and just picked the ones that looked nice.
Last edited by tomd123 (2010-03-14 07:14:40)
Offline
I like the color scheme, from the thumb it almost looks 3d with the way the colors blend in the first one. Have you tried any of the functions for mandelbrot sets or anything like that?
"The beautiful thing about learning is nobody can take it away from you."
B.B. King
Offline
I like the color scheme, from the thumb it almost looks 3d with the way the colors blend in the first one. Have you tried any of the functions for mandelbrot sets or anything like that?
here is another one
It's with the julia set z^3 -1
the equation for this is: x ** 7 - x**5 + x ** 2 - 5 * x ** 3 + 1
(I just picked a long polynomial )
if you're interested in the julia set try http://en.wikipedia.org/wiki/Julia_set
Offline
nice! once again cool program
"The beautiful thing about learning is nobody can take it away from you."
B.B. King
Offline
Pages: 1