Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Julia set fractals

I wrote a program to generate Julia set fractals. The program also allows the user to enter their value of c or let the program generate a random value. Here is the code :-

import pygame, sys, math, cmath, random
from pygame.locals import *
print("Julia set fractal generator")
custom = int(input("Do you want a custom set? Yes(1); No(-1): "))
if custom == -1:
    c = complex((random.randint(-999,1000))/1000.0,(random.randint(-999,1000))/1000.0)
else:
    a = float(input("Real?: "))
    b = float(input("Imaginary?: "))
    c = complex(a,b)
lim = 4
limn = -4
mul = 0
iteration_detail = 100
screen = pygame.display.set_mode((512,512),0,32)
pygame.display.set_caption("Julia set fractal generator")
def iterate (px_i,py_i,iters):
    itnum = 1
    z = complex(((px_i-256)/512.0)*4,((py_i-256)/512.0)*4)
    while itnum <= iters:
        if z.real >= lim or z.imag >= lim or z.real <= limn or z.imag <= limn:
                break
        z = z**2 + c
        itnum += 1
return(z.real, z.imag, itnum)
def pixel_color_set (iterx, itery, iterations):
    pixel_color = (0,0,0)
    if iterx >= lim or itery >= lim or iterx <= limn or itery <= limn:
        if iterations < 2:
                pixel_color = (204,0,102)
        elif iterations == 2:
                pixel_color = (204,0,204)
        elif iterations == 3:
                pixel_color = (102,0,204)
        elif iterations ==4:
                pixel_color = (0,0,204)
        elif iterations ==5:
                pixel_color = (0,102,204)
        elif iterations ==6:
                pixel_color = (0,204,204)
        elif iterations ==7:
                pixel_color = (0,204,102)
        elif iterations ==8:
                pixel_color = (0,204,0)
        elif iterations ==9:
                pixel_color = (102,204,0)
return(pixel_color)
def draw_pixel (px, py, color):
return(screen.fill(color, ((px, py),(1, 1))))
while 1:
for event in pygame.event.get():
        if event.type == QUIT:
                pygame.quit()
                sys.exit()
        if event.type == KEYDOWN:
                if event.key == K_UP:
                        mul = 0.1
                elif event.key == K_DOWN:
                        mul = -0.1
                if event.key == K_SPACE:
                        pygame.image.save(screen, "fractal.jpg")
        if event.type == KEYUP:
                 if event.key == K_UP:
                        mul = 0
                 elif event.key == K_DOWN:
                        mul = 0
        c += mul
ypxl = 0
while ypxl < 512:
        xpxl = 0
        while xpxl < 512:
                ipxl = iterate(xpxl,ypxl,iteration_detail)
                cpxl = pixel_color_set(ipxl[0], ipxl[1], ipxl[2])
                draw_pixel(xpxl, ypxl, cpxl)
                xpxl += 1
        ypxl += 1
pygame.display.update()

The code does work but it doesn't generate fractals as expected. For example this fractal :-
z = z**2 + c Where c is equal to c = complex(-0.1, 0.651) It is supposed to look like this From Wikipedia

But it looks like this
Generated by the program

What is wrong in my code? Also I am unable to make a zooming mechanism... Some help there would be appreciated.

like image 504
AvZ Avatar asked Jan 15 '14 15:01

AvZ


People also ask

What is the difference between Julia set and Mandelbrot set?

The Mandelbrot set is the set of all c for which the iteration z → z2 + c, starting from z = 0, does not diverge to infinity. Julia sets are either connected (one piece) or a dust of infinitely many points. The Mandelbrot set is those c for which the Julia set is connected.

Are Julia sets infinite?

Julia showed that, except in the simplest cases, the Julia set is infinite, and he described how it is related to the periodic points of the iteration (those that return to themselves after a certain number of iterations). In some…

What is the Julia set named after?

These sets are named after the French mathematicians Gaston Julia and Pierre Fatou whose work began the study of complex dynamics during the early 20th century.


1 Answers

Changing the pixel_color_set like this gave good results.
The iterations going from 0 to iteration_detail is scaled into the range of 0 to 255.

def pixel_color_set (iterx, itery, iterations):
    pixel_color = (0,0,0)
    if iterx >= lim or itery >= lim or iterx <= limn or itery <= limn:
        RGB =int(math.sqrt(iterations) * int(255.0 / math.sqrt(iteration_detail)))
        # RGB = iterations * int(255.0/ iteration_detail)
        pixel_color = (RGB, RGB, RGB)
    return(pixel_color)

I think the one with math.sqrt looked a bit better, but try both and pick what you want.

like image 83
M4rtini Avatar answered Oct 08 '22 07:10

M4rtini