Overview
Fract’ol uses three main structures defined infractol.h:
- t_complex: Represents complex numbers
- t_img: Manages MLX image buffer data
- t_fractol: Central state container for the entire application
t_complex
Represents a complex number in Cartesian form (real + imaginary components).fractol.h:46-50
Fields
Real component of the complex number. In standard mathematical notation, this represents the real part of .
- For screen coordinates: horizontal position mapped to complex plane (typically -2 to +2)
- For fractal calculations: the in
Imaginary component of the complex number. Represents the coefficient of in .
- For screen coordinates: vertical position mapped to complex plane (typically -2 to +2)
- For fractal calculations: the in
Usage
Design Notes
The structure uses
x and y instead of real and imag to emphasize the geometric interpretation:x= horizontal axis (real axis)y= vertical axis (imaginary axis)
t_img
Contains MiniLibX image buffer data for direct pixel manipulation.fractol.h:52-59
Fields
MLX image pointer returned by
mlx_new_image().- Opaque handle to the image buffer
- Used for operations like
mlx_put_image_to_window()andmlx_destroy_image() - Must not be freed directly (use
mlx_destroy_image())
Direct memory address of the pixel data array, obtained from
mlx_get_data_addr().- Points to raw pixel buffer in memory
- Allows direct byte-level pixel manipulation
- Used to calculate offset:
(y * line_length) + (x * (bpp / 8))
Bits per pixel - the number of bits used to store each pixel’s color.
- Typical value: 32 (4 bytes per pixel for ARGB)
- Divide by 8 to get bytes per pixel
- Populated by
mlx_get_data_addr()
Line length in bytes - the number of bytes per row in the image buffer.
- May include padding for memory alignment
- Not necessarily equal to
width * (bpp / 8) - Critical for calculating pixel offset:
y * line_length
Endianness of pixel data - byte order for multi-byte color values.
- 0 = little-endian (least significant byte first)
- 1 = big-endian (most significant byte first)
- Populated by
mlx_get_data_addr()
Initialization
Fromfractol_init.c:53-63:
Pixel Manipulation
Fromfractol_render.c:15-21:
t_fractol
The central data structure containing all application state.fractol.h:61-74
Fields
Fractal Configuration
Fractal type identifier - points to the command-line argument string.
- Value:
"Mandelbrot"or"Julia" - Set in
main.c:22fromargv[1] - Used to determine rendering algorithm in
mandel_vs_julia() - Not allocated - points to argv, do not free
Real component of the Julia set constant .
- Only used when
name == "Julia" - Set from command-line:
./fractol Julia 0.285 0.01 - Parsed in
main.c:25viaatoi_plus(argv[2]) - Common values: -0.8 to 0.8
Imaginary component of the Julia set constant .
- Only used when
name == "Julia" - Parsed in
main.c:26viaatoi_plus(argv[3]) - Common values: -0.5 to 0.5
- Together with
julia_x, defines which Julia set to render
MLX Resources
MLX connection handle to the X Window System.
- Initialized by
mlx_init()infractol_init.c:42 - Required for all MLX function calls
- Must be freed with
free()aftermlx_destroy_display() - NULL indicates initialization failure
MLX window pointer for the display window.
- Created by
mlx_new_window()with dimensions 2000×1500 - Window title set to
fractol->name - Destroyed with
mlx_destroy_window()on exit
Image buffer structure containing pixel data.
- See t_img documentation above
- Embedded structure (not a pointer)
- Initialized in
fractol_init()after window creation
Rendering Parameters
Escape threshold for fractal iteration - squared magnitude limit.
- Default:
4.0(set indata_init()) - Represents where is the escape radius
- Point escapes when escape_value
- Using squared value avoids expensive
sqrt()calls
Maximum iteration count before considering a point in the set.
- Default:
30(set indata_init()) - Increased by 5 when
+key pressed - Decreased by 5 when
-key pressed - Higher values = more detail but slower rendering
- Also used as range for color gradient mapping
View Transformation
Horizontal pan offset in complex plane coordinates.
- Default:
0.0(centered) - Decreased by
0.5 * zoomwhen Left/A pressed - Increased by
0.5 * zoomwhen Right/D pressed - Added to mapped x-coordinate:
z.x = (map(...) * zoom) + shift_x
Vertical pan offset in complex plane coordinates.
- Default:
0.0(centered) - Increased by
0.5 * zoomwhen Up/W pressed (note: inverted) - Decreased by
0.5 * zoomwhen Down/S pressed - Added to mapped y-coordinate:
z.y = (map(...) * zoom) + shift_y
Zoom level multiplier - scales the view of the complex plane.
- Default:
1.0(no magnification) - Multiplied by
0.95on scroll up (zoom in) - Multiplied by
1.05on scroll down (zoom out) - Lower values = more zoomed in (smaller zoom multiplier)
- Applied to coordinates:
z.x = map(...) * zoom
Initialization Sequence
Frommain.c:17-30 and fractol_init.c:40-66:
main.c
fractol_init.c
Usage Patterns
Memory Layout
Size estimate (64-bit system):The structure is stack-allocated in
main() and passed by pointer to all functions. This avoids heap allocation overhead while maintaining single-source-of-truth for application state.Utility Functions
These helper functions fromstring_utils.c support argument parsing and string comparison.
atoi_plus()
Enhanced string-to-double converter that handles floating-point numbers with optional sign.fractol.h:77
String to parse - must contain valid numeric characters, optional sign (+/-), and optional decimal point
Returns
Parsed floating-point value with sign applied
Implementation
Fromstring_utils.c:15-41:
Behavior
- Whitespace skipping: Handles spaces and control characters (ASCII 9-13, 32)
- Sign parsing: Processes multiple +/- characters (each
-toggles sign) - Integer part: Builds integer by multiplying by 10 and adding each digit
- Decimal point: Detects and skips ’.’
- Fractional part: Accumulates decimal places with decreasing powers of 10
- Sign application: Multiplies result by final sign
Usage in main()
Frommain.c:25-26:
Examples
ft_strncmp()
Simplified string comparison function that compares two strings until they differ or one ends.fractol.h:76
First string to compare (can be NULL)
Second string to compare (can be NULL)
Returns
0if strings are identical or both NULL- Positive if s1 > s2 at first differing character
- Negative if s1 < s2 at first differing character
Implementation
Fromstring_utils.c:43-53:
Behavior
- NULL safety: Returns 0 if either string is NULL
- Character-by-character: Compares until mismatch or null terminator
- ASCII difference: Returns difference of character values at mismatch point
Unlike standard
strncmp(), this compares entire strings without a length limit. The name is a 42 School convention rather than standard library compatibility.Usage in Fractal Type Detection
Frommain.c:19-20:
fractol_render.c:25: