Fractol 1.0
A fractal visualization project implemented in C.
Loading...
Searching...
No Matches
julia.c
Go to the documentation of this file.
1/* ************************************************************************** */
2/* */
3/* ::: :::::::: */
4/* julia.c :+: :+: :+: */
5/* +:+ +:+ +:+ */
6/* By: meghribe <meghribe@student.42barcelona.co +#+ +:+ +#+ */
7/* +#+#+#+#+#+ +#+ */
8/* Created: 2024/12/24 19:09:26 by meghribe #+# #+# */
9/* Updated: 2025/01/05 14:00:18 by meghribe ### ########.fr */
10/* */
11/* ************************************************************************** */
12
13#include "fractol.h"
14
15/**
16 * @file julia
17 * @brief Implementation of the Julia set rendering.
18 *
19 * This file handles all calculations and rendering logic for he Julia set
20 * fractal.
21 * It contains functions that:
22 * - Perrform the iterative mathematical calculations for each point in the
23 * Julia set.
24 * - Render invidiaul rows of the fractal to optimize performance.
25 * - Manage the rendering off the entire fractal based on user-defined
26 * parameters.
27 *
28 * ### For Technical Users:
29 * - **Core Alrogithm:** The Julia set is calculated iteratively using the
30 * formula: `z = z^2 + c`, where `z` is a complex number and `c` is
31 * a constant parameter.
32 * The function computes whether each point "escapes" or remains bounded
33 * within a set threshold.
34 * - **Optimization:** The fractal is rendered row by row to efficiently
35 * utilize memory and computational resources. Intermediate results like
36 * squared values are stored to avoid redundant calculations.
37 * - **Color Mapping:** Each point's "escape time" determines its color,
38 * creating the fractal's visually striking patterns.
39 *
40 * ### For Non-Technical Users:
41 * - **What is the Julia Set?**
42 * The Julia set is a mathematical pattern generated by repeatedly
43 * applying a formula to points on a grid. Some points "escape" quickly,
44 * while others stay trapped. The resulting image is mesmerizing, intricate
45 * design.
46 * - **How does this file work?**
47 * Think of this like as the "artist" responsible for drawing the Julia set.
48 * It decides:
49 * - Where each point in the fractal goes on the screen.
50 * - How many iterations it takes for each point to escape the threshold.
51 * - What color each point should have based on its behavior.
52 * - **Why is it row by row?**
53 * Instead of drawing the entire image at once, this program works row by row
54 * for better performance, like filling in coloring book on line at a time
55 * Instead of drawing the entire image at once, this program works row by row
56 * for better performance, like filling in coloring book on line at a time.
57 *
58 * The Julia set is unique because its shape depends on the user-provided
59 * parameters. You can explore endless variations by tweaking the input values!
60 */
61
62/**
63 * @brief Performs the iterations for a signle point in the Julia set.
64 *
65 * This function applies the Julia set formula iteratively to determine
66 * whether a given point escapes the threshold. The number of iterations taken
67 * before escape determines the point's color.
68 *
69 * - **Core Algorithm:** The formula `z = z^2 + c` is applied, where:
70 * - `z` is a complex number, represented by real (`z_re`) and
71 * imaginary (`z_im`) parts.
72 * - `c` is a constant defined by the user (real and imaginary parts).
73 * - **Bounding Condition:** The iteerations stop when:
74 * - The sum of the squares of the real and imaginary parts exceeds 4.0.
75 * - The maximum number of iterations (`MAX_ITERATIONS`) is reached.
76 * - **Optimization:** Squared values (`z_re^2` and `z_im^2`) are cached
77 * to avoid redundant calculations.
78 *
79 * @param vars Pointer to the structure holding rendering variables
80 * (e.g., `z` and `iterations`).
81 * @params data Pointer to the main fractal data, including the Julia
82 * constant (`c`).
83 */
84static void julia_iterations(t_render_vars *vars, t_data *data)
85{
86 double temp_z_re;
87
88 vars->iterations = 0;
89 while (vars->z_squared[COMPLEX_RE] + vars->z_squared[COMPLEX_IM] <= 4.0 && \
91 {
92 temp_z_re = vars->z_squared[COMPLEX_RE];
93 temp_z_re -= vars->z_squared[COMPLEX_IM];
94 temp_z_re += data->c[COMPLEX_RE];
95 vars->z[COMPLEX_IM] = 2.0 * vars->z[COMPLEX_RE] * \
96 vars->z[COMPLEX_IM] + data->c[COMPLEX_IM];
97 vars->z[COMPLEX_RE] = temp_z_re;
98 vars->z_squared[COMPLEX_RE] = vars->z[COMPLEX_RE] * vars->z[COMPLEX_RE];
99 vars->z_squared[COMPLEX_IM] = vars->z[COMPLEX_IM] * vars->z[COMPLEX_IM];
100 vars->iterations++;
101 }
102}
103
104/**
105 * @brief Renders a single row of the Julia set.
106 *
107 * This function processes one row of the pixel in the Juliaa set by iterating
108 * through each pixel, applying the Juliaa set formula, and determining its
109 * color.
110 *
111 * - **Coordinate Mapping:**
112 * - Each pixel is mapped to a complex number (`z`) based on its position
113 * and the scaling factors calculated from the zoom and offsets.
114 * - **Iteration and Coloring:**
115 * - The Julia set formula is applied iteratively for each pixel.
116 * - The number of iterations is used to calculate the pixel's color.
117 * - **Row Processing:** Each pixel in the row is processed sequentially,
118 * and the result is stored in the image buffer.
119 *
120 * @param vars Pointer to the rendering variables structure (e.g., pixel and
121 * scaling).
122 * @param data Pointer to the main fractal data.
123 * @param y The current row index being processed (0 for the first row, up to
124 * `HEIGHT - 1`).
125 */
126static void render_julia_row(t_render_vars *vars, t_data *data, int y)
127{
128 int x;
129
130 x = 0;
131 while (x < WIDTH)
132 {
133 vars->c_re[0] = vars->start[COMPLEX_RE] + x * vars->scale[SCALE_X];
134 vars->z[COMPLEX_RE] = vars->c_re[0];
135 vars->z[COMPLEX_IM] = vars->c_im;
136 vars->z_squared[COMPLEX_RE] = vars->z[COMPLEX_RE] * vars->z[COMPLEX_RE];
137 vars->z_squared[COMPLEX_IM] = vars->z[COMPLEX_IM] * vars->z[COMPLEX_IM];
138 julia_iterations(vars, data);
139 vars->row = vars->pixels + y * WIDTH + x;
140 *vars->row = calculate_color(vars->iterations);
141 x++;
142 }
143}
144
145/**
146 * @brief Renders the Julia set fractal.
147 *
148 * This function handles the full rendering process for the Julia set by
149 * iterating through all rows of the screen, rendering each row sequentially.
150 *
151 * - **Setup:**
152 * - Calculates the scaling and starting coordinates based on the zoom level
153 * and positional offsets provided by the user.
154 * - **Row-by-Row Rendering:**
155 * - For each row on the screen (`y` coordinate), the corresponding imaginary
156 * coordinate is computed.
157 * - The row is passed to `render_julia_row`, which handles the pixel-by-pixel
158 * rendering.
159 * - **Full Image Construction:**
160 * - The ffunction processes every row until the entire fractal is rendered.
161 *
162 * @param data Pointer to the main fractal data structure, including rendering
163 * details such as zoom, offsets, and fractal paarameters.
164 */
166{
167 t_render_vars vars;
168 int y;
169
170 calculate_scales_and_limits(&vars, data);
171 y = 0;
172 while (y < HEIGHT)
173 {
174 vars.c_im = vars.start[COMPLEX_IM] + y * vars.scale[SCALE_Y];
175 render_julia_row(&vars, data, y++);
176 }
177}
void calculate_scales_and_limits(t_render_vars *vars, t_data *data)
Calculates the scales and starting points for the fractal rendering.
Definition calcs.c:52
int calculate_color(int iterations)
Calculates the color of a pixel based on how many iterations it takes to "escape".
Definition calcs.c:131
#define MAX_ITERATIONS
Definition fractol.h:193
#define WIDTH
Definition fractol.h:188
@ SCALE_Y
Definition fractol.h:44
@ SCALE_X
Definition fractol.h:43
@ COMPLEX_IM
Definition fractol.h:53
@ COMPLEX_RE
Definition fractol.h:52
#define HEIGHT
Definition fractol.h:189
static void julia_iterations(t_render_vars *vars, t_data *data)
Performs the iterations for a signle point in the Julia set.
Definition julia.c:84
void render_julia(t_data *data)
Renders the Julia set fractal.
Definition julia.c:165
static void render_julia_row(t_render_vars *vars, t_data *data, int y)
Renders a single row of the Julia set.
Definition julia.c:126
Data structure for fractal rendering.
Definition fractol.h:92
double c[2]
Definition fractol.h:94
Variables used for fractal rendering calculations.
Definition fractol.h:105
double z_squared[2]
Definition fractol.h:110
double scale[2]
Definition fractol.h:107
uint32_t * row
Definition fractol.h:115
double c_im
Definition fractol.h:113
double c_re[2]
Definition fractol.h:111
double start[2]
Definition fractol.h:108
uint32_t * pixels
Definition fractol.h:114
double z[2]
Definition fractol.h:109