You are not logged in.

#1 2010-03-12 02:22:58

tomd123
Developer
Registered: 2008-08-12
Posts: 565

Fractal fun.

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

Screenshots:
201003112016531280x800s.th.png
201003112027391280x800s.th.png
201003142213181280x800s.th.png
201003142235061280x800s.th.png

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

#2 2010-03-12 02:27:45

Peasantoid
Member
Registered: 2009-04-26
Posts: 928
Website

Re: Fractal fun.

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

#3 2010-03-12 02:36:51

tomd123
Developer
Registered: 2008-08-12
Posts: 565

Re: Fractal fun.

Peasantoid wrote:

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

#4 2010-03-13 01:03:24

Moose9999
Member
Registered: 2010-03-09
Posts: 32

Re: Fractal fun.

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

#5 2010-03-14 07:12:45

tomd123
Developer
Registered: 2008-08-12
Posts: 565

Re: Fractal fun.

Moose9999 wrote:

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 tongue

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

#6 2010-03-14 19:52:04

Moose9999
Member
Registered: 2010-03-09
Posts: 32

Re: Fractal fun.

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

#7 2010-03-15 03:37:02

tomd123
Developer
Registered: 2008-08-12
Posts: 565

Re: Fractal fun.

Moose9999 wrote:

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

201003142213181280x800s.th.png

It's with the julia set z^3 -1

another interesting one:
201003142235061280x800s.th.png

the equation for this is:  x ** 7 - x**5 + x ** 2 - 5 * x ** 3 + 1
(I just picked a long polynomial tongue)

if you're interested in the julia set try http://en.wikipedia.org/wiki/Julia_set

Offline

#8 2010-03-15 16:10:10

Moose9999
Member
Registered: 2010-03-09
Posts: 32

Re: Fractal fun.

nice!  once again cool program


"The beautiful thing about learning is nobody can take it away from you."
B.B. King

Offline

Board footer

Powered by FluxBB