Skip to content

colingalbraith/Fractal-Generator

Repository files navigation

2D Fractal Generator

A 2D fractal generator made in Python that allows the user to zoom in and out of fractals that are color-coded based on iteration. The program allows exploration of the Rational, Julia, and Mandelbrot sets. It uses Pygame for rendering graphics, NumPy for computation, and Numba to optimize speed, making fractal generation efficient and responsive, even when zoomed in on detailed regions.


Features

  • Fractal Types: Generate and explore Mandelbrot, Julia, and Rational fractals.
  • Interactive Interface: Zoom and pan across the fractals for an immersive experience.
  • Efficient Computation: Uses the Numba library for speed optimization, ensuring fast fractal generation.
  • Color-Coded Iterations: Colorful rendering of fractals based on the number of iterations.
  • Switch Fractals: Easily switch between different fractal types using on-screen buttons.
  • Reset Zoom: Reset the zoom level to return to the initial view with a click of a button.

309ecdc5-0e6c-4416-90ac-5c6539e297fe.mp4

Untitled design (3)

Untitled design (3) copy

Untitled design (3) copy 4

Untitled design (3) copy 3

Untitled design (3) copy 2


Fractals Explained

Mandelbrot Set

The Mandelbrot set is defined by iterating the function:

image
  • Initial Conditions: Start with ( z = 0 ).
  • Iteration: For each pixel, corresponding to a complex number ( c ), the function is iterated by applying the formula repeatedly: ( z = z^2 + c ).
  • Escape Criterion: If ( |z| ) (the magnitude of ( z )) exceeds 2, the point is considered to have escaped. The number of iterations before escaping is used to color the pixel.
  • Coloring: Points that escape quickly are given brighter colors, while those that take longer to escape are given darker colors. Points that do not escape are colored black, indicating they belong to the Mandelbrot set.

https://en.wikipedia.org/wiki/Mandelbrot_set

Julia Set

image
  • Fixed ( c ): In the Julia set, ( c ) is fixed, such as ( c = -0.7 + 0.27015i ).
  • Iteration: For each pixel (corresponding to a complex number ( z )), the function is iterated as ( z = z^2 + c ).
  • Escape Criterion: If ( |z| ) exceeds 2, the point escapes, and the iteration count determines the pixel's color.

https://en.wikipedia.org/wiki/Julia_set

Rational Fractals

  • Iteration: For each pixel (represented by a complex number ( c )), the rational formula is applied iteratively.
  • Escape Criterion: Similar to the Mandelbrot and Julia sets, if ( |z| ) exceeds 2, the point escapes, and the number of iterations is used to determine the pixel color.
image

Technical Details

The fractal generation is performed by iterating complex functions at each pixel and determining the escape time (i.e., how many iterations it takes before the point escapes the fractal's boundary). This value is used to color each pixel accordingly.

  • Zooming/Panning: Zooming is achieved by dynamically adjusting the boundaries of the complex plane that the fractals are drawn on. Panning allows the user to shift these boundaries without changing the zoom level.
  • NumPy Arrays: All computations are handled with NumPy arrays for efficient memory usage and fast numerical operations.
  • Numba Optimization: The fractal generation loop is optimized using Numba's Just-In-Time (JIT) compilation, which accelerates Python code by compiling it to machine code at runtime.

Installation

To run the Fractal Generator on your local machine, follow these steps:

  1. Clone the Repository:
    git clone https://github.com/colingalbraith/Fractal-Generator.git
    pip install -r requirements.txt
    python main.py