How to render a julia set quickly with pygame

using PyPlot

function julia_set(width, height, c, max_iter)
    x = linspace(-2, 2, width)
    y = linspace(-2, 2, height)
    z = x .+ y' .* im
    img = zeros(UInt8, height, width, 3)
    
    for i in 1:max_iter
        z = z.^2 .+ c
        mask = abs.(z) .< 2
        img[mask] = img[mask] .+ 1
    end
    
    return img
end

width = 800
height = 800
c = -0.8 + 0.156im
max_iter = 100

img = julia_set(width, height, c, max_iter)

imshow(img)
axis("off")
show()

Option 1: Using Pygame

To render a Julia set quickly with Pygame, you can use the PyPlot library in Julia. PyPlot provides a simple interface to the powerful matplotlib library, which allows you to create high-quality plots and visualizations.

First, you need to install the PyPlot package in Julia by running the following command:

using Pkg
Pkg.add("PyPlot")

Once PyPlot is installed, you can use the following Julia code to render a Julia set quickly:

using PyPlot

function julia_set(width, height, c, max_iter)
    x = linspace(-2, 2, width)
    y = linspace(-2, 2, height)
    z = x .+ y' .* im
    img = zeros(UInt8, height, width, 3)
    
    for i in 1:max_iter
        z = z.^2 .+ c
        mask = abs.(z) .< 2
        img[mask] = img[mask] .+ 1
    end
    
    return img
end

width = 800
height = 800
c = -0.8 + 0.156im
max_iter = 100

img = julia_set(width, height, c, max_iter)

imshow(img)
axis("off")
show()

This code uses the PyPlot package to create a Julia set image. It defines a function julia_set that takes the width, height, complex constant c, and maximum number of iterations as input. It then generates a grid of complex numbers, applies the Julia set iteration formula, and assigns colors to the pixels based on the number of iterations.

The resulting image is displayed using the imshow function, and the axes are turned off using the axis("off") function. Finally, the image is shown using the show function.

Option 2: Using PyCall

If you prefer to use Pygame directly instead of PyPlot, you can use the PyCall package in Julia to call Python functions and libraries. PyCall provides a seamless interface between Julia and Python, allowing you to leverage the power of both languages.

First, you need to install the PyCall package in Julia by running the following command:

using Pkg
Pkg.add("PyCall")

Once PyCall is installed, you can use the following Julia code to render a Julia set quickly using Pygame:

using PyCall

@pyimport pygame

function julia_set(width, height, c, max_iter)
    pygame.init()
    screen = pygame.display.set_mode((width, height))
    
    for x in 1:width
        for y in 1:height
            zx = 3 * (x - width / 2) / (width / 2)
            zy = 2 * (y - height / 2) / (height / 2)
            c = zx + zy * im
            z = c
            
            for i in 1:max_iter
                if abs(z) > 2
                    break
                end
                
                z = z^2 + c
            end
            
            color = (i % 8 * 32, i % 16 * 16, i % 32 * 8)
            pygame.draw.rect(screen, color, (x, y, 1, 1))
        end
    end
    
    pygame.display.flip()
    pygame.time.wait(10000)
    pygame.quit()
end

width = 800
height = 800
c = -0.8 + 0.156im
max_iter = 100

julia_set(width, height, c, max_iter)

This code uses the PyCall package to call the Pygame library and render a Julia set image. It defines a function julia_set that takes the width, height, complex constant c, and maximum number of iterations as input. It initializes the Pygame library, creates a window with the specified dimensions, and then iterates over each pixel in the window.

For each pixel, it calculates the corresponding complex number based on the pixel coordinates, applies the Julia set iteration formula, and determines the color based on the number of iterations. It then draws a rectangle with the calculated color at the corresponding pixel position using the pygame.draw.rect function.

Finally, it updates the display using pygame.display.flip(), waits for 10 seconds using pygame.time.wait(10000), and quits the Pygame library using pygame.quit().

Option 3: Using Cairo.jl

If you prefer a more lightweight solution, you can use the Cairo.jl package in Julia to render a Julia set image. Cairo.jl provides a high-level interface to the Cairo graphics library, allowing you to create 2D vector graphics.

First, you need to install the Cairo.jl package in Julia by running the following command:

using Pkg
Pkg.add("Cairo")

Once Cairo.jl is installed, you can use the following Julia code to render a Julia set quickly:

using Cairo

function julia_set(width, height, c, max_iter)
    surface = CairoImageSurface(CAIRO_FORMAT_RGB24, width, height)
    context = CairoContext(surface)
    
    for x in 1:width
        for y in 1:height
            zx = 3 * (x - width / 2) / (width / 2)
            zy = 2 * (y - height / 2) / (height / 2)
            z = zx + zy * im
            
            for i in 1:max_iter
                if abs(z) > 2
                    break
                end
                
                z = z^2 + c
            end
            
            r = i % 8 * 32 / 255
            g = i % 16 * 16 / 255
            b = i % 32 * 8 / 255
            set_source_rgb(context, r, g, b)
            rectangle(context, x, y, 1, 1)
            fill(context)
        end
    end
    
    write_to_png(surface, "julia_set.png")
end

width = 800
height = 800
c = -0.8 + 0.156im
max_iter = 100

julia_set(width, height, c, max_iter)

This code uses the Cairo.jl package to create a Julia set image. It defines a function julia_set that takes the width, height, complex constant c, and maximum number of iterations as input. It creates a Cairo image surface and context, and then iterates over each pixel in the image.

For each pixel, it calculates the corresponding complex number based on the pixel coordinates, applies the Julia set iteration formula, and determines the color based on the number of iterations. It then sets the RGB color values using the set_source_rgb function, draws a rectangle with the specified color at the corresponding pixel position using the rectangle function, and fills the rectangle using the fill function.

Finally, it writes the image to a PNG file named "julia_set.png" using the write_to_png function.

Among the three options, the best choice depends on your specific requirements and preferences. If you are already familiar with Pygame and prefer to use it directly, Option 2 using PyCall would be a good choice. If you prefer a more lightweight solution and want to create high-quality vector graphics, Option 3 using Cairo.jl would be a good choice. Option 1 using PyPlot provides a balance between simplicity and functionality, making it a good choice for most users.

Rate this post

Leave a Reply

Your email address will not be published. Required fields are marked *

Table of Contents