Initialization Functions
fractol_init()
Initializes the MLX graphics system, creates the window and image buffer, and sets up event handlers.fractol.h:86
Pointer to the fractol structure to initialize. Must have
name field set, and for Julia fractals, julia_x and julia_y must be set.Implementation Details
Fromfractol_init.c:40-66:
Behavior
- MLX Connection: Calls
mlx_init()to establish connection to X server - Window Creation: Creates 2000x1500 window with fractal name as title
- Image Buffer: Allocates image buffer matching window dimensions
- Pixel Data: Retrieves direct memory address for pixel manipulation
- Event Registration: Calls
events_init()to hook keyboard/mouse handlers - Data Initialization: Calls
data_init()to set default parameters
malloc_error().
data_init()
Initializes fractal rendering parameters to default values.fractol.h:87
Pointer to the fractol structure to populate with default values.
Implementation
Fromfractol_init.c:21-28:
Default Values
Set to
4.0 - the squared magnitude threshold for escape condition (2² = 4)Set to
30 - maximum iterations before considering a point in the setSet to
0.0 - no horizontal pan offsetSet to
0.0 - no vertical pan offsetSet to
1.0 - default zoom level (no magnification)Rendering Functions
fractol_render()
Renders the fractal by calculating and coloring every pixel, then displays the result.fractol.h:88
Pointer to fully initialized fractol structure containing rendering parameters and MLX resources.
Implementation
Fromfractol_render.c:63-81:
Behavior
- Iterates through all 2000×1500 = 3,000,000 pixels
- Calls
handle_pixel()for each coordinate - After all pixels are calculated, displays image buffer to window via
mlx_put_image_to_window()
This function is called:
- Once after initialization in
main() - After every zoom event (mouse scroll)
- After every pan event (arrow keys)
- After iteration count changes (+/- keys)
handle_pixel()
Calculates the color for a single pixel by iterating the fractal formula.fractol.h:89
Screen x-coordinate (0 to WIDTH-1)
Screen y-coordinate (0 to HEIGHT-1)
Pointer to fractol structure containing rendering parameters
Implementation
Fromfractol_render.c:37-61:
Algorithm Steps
-
Coordinate Mapping: Convert screen pixel to complex plane coordinates
- X maps from [0, WIDTH] to [-2, +2]
- Y maps from [0, HEIGHT] to [+2, -2] (inverted for screen coordinates)
- Apply zoom and shift transformations
-
Fractal Selection:
mandel_vs_julia()sets initial conditions- Julia:
c= constant (julia_x, julia_y),z= pixel coordinate - Mandelbrot:
c= pixel coordinate,z= (0, 0) implicitly
- Julia:
-
Iteration Loop: Apply formula
z = z² + cup toiterations_definition -
Escape Check: If
|z|² > escape_value, point escapes- Color mapped from iteration count to gradient (LIME → MINT)
- Early return after pixel write
- In-Set Points: If max iterations reached without escape, color WHITE
Event Handlers
These functions are registered as callbacks inevents_init() (fractol_init.c:30-38):
key_handler()
Fromevents.c:26-44:
fractol.h:80
ESC: Exit program viaclose_handler()Left/A: Pan left (decrease shift_x)Right/D: Pan right (increase shift_x)Up/W: Pan up (increase shift_y)Down/S: Pan down (decrease shift_y)+: Increase iterations by 5-: Decrease iterations by 5
mouse_handler()
Fromevents.c:46-60:
fractol.h:79
Button4(scroll up): Zoom in (multiply zoom by 0.95)Button5(scroll down): Zoom out (multiply zoom by 1.05)
close_handler()
Fromevents.c:15-24:
fractol.h:81
- Destroy image buffer
- Destroy window
- Destroy display connection
- Free MLX connection
- Exit with
EXIT_SUCCESS