Fractol 1.0
A fractal visualization project implemented in C.
Loading...
Searching...
No Matches
calcs.c
Go to the documentation of this file.
1/* ************************************************************************** */
2/* */
3/* ::: :::::::: */
4/* calcs.c :+: :+: :+: */
5/* +:+ +:+ +:+ */
6/* By: meghribe <meghribe@student.42barcelona.co +#+ +:+ +#+ */
7/* +#+#+#+#+#+ +#+ */
8/* Created: 2025/01/03 02:00:53 by meghribe #+# #+# */
9/* Updated: 2025/01/05 13:19:20 by meghribe ### ########.fr */
10/* */
11/* ************************************************************************** */
12
13#include "fractol.h"
14
15/**
16 * @file calcs.c
17 * @brief Core calculations for fractal rendering.
18 *
19 * This file contains the functions responsible for performing the core
20 * calculations required to render fractals. It handles:
21 * - Scaling and positioning the fractal within the rendering window.
22 * - Precomputing the coordinates ffor each pixel to optimize rendering
23 * performance.
24 * - Rendering the fractal by delegating to specific algorithms
25 * (Julia or Mandelbrot).
26 * - Mapping iteration counts to colors for visually representing
27 * fractal complexity.
28 *
29 * For non-technical users:
30 * - This file performs the "math magic" that powers the fractal visuals,
31 * calculating where and howw the fractal is drawn and colored on
32 * the screen.
33 */
34
35/**
36 * @brief Calculates the scales and starting points for the fractal rendering.
37 *
38 * This function determines how the fractal is scaled and positioned on the
39 * screen.
40 * It computes the scale factors for each pixel and establishes the
41 * starting points ffor the real and imaginary part of the fractal based on the
42 * zoom level and offsets.
43 *
44 * For non-technical users:
45 * - Think of it as setting the "zoom" and "pam" for the fractal to display it
46 * correctly on the screen.
47 *
48 * @param vars Pointer to the rendering variables structure.
49 * @param data Pointer to the main data structure containing fractal
50 * information.
51 */
53{
54 vars->scale[SCALE_X] = 4.0 / (WIDTH * data->zoom);
55 vars->scale[SCALE_Y] = 4.0 / (HEIGHT * data->zoom);
56 vars->start[COMPLEX_RE] = -2.0 / data->zoom + data->offset[OFFSET_X];
57 vars->start[COMPLEX_IM] = -2.0 / data->zoom + data->offset[OFFSET_Y];
58 vars->pixels = (uint32_t *)data->img->pixels;
59}
60
61/**
62 * @brief Precomputes the coordinates for each pixel in the fractal.
63 *
64 * This function calculates the real and imaginary parts of the fractal for
65 * each pixel on the screen, storing them in arrays for quick access during
66 * rendering.
67 * It ensures that every point is prepared for the fractal calculations.
68 *
69 * For non-technical users:
70 * - Imagine this as creating a "grid" of points where the fractal will be
71 * drawn. This points tell the program where to caalculate the colors.
72 *
73 * @param c_re Pointer to the array storing real coordinates.
74 * @param c_im Pointer to the array storing imaginary coordinates.
75 * @param vars Pointer to the rendering variables structure.
76 * @return Returns 1 on success, or 0 if memory allocation fails.
77 */
78int precompute_coords(double **c_re, double **c_im, t_render_vars *vars)
79{
80 int i;
81
82 *c_re = (double *)malloc(WIDTH * sizeof(double));
83 *c_im = (double *)malloc(HEIGHT * sizeof(double));
84 if (!(*c_re) || !(*c_im))
85 return (free(*c_re), free(*c_im), 0);
86 i = -1;
87 while (++i < WIDTH)
88 (*c_re)[i] = vars->start[COMPLEX_RE] + i * vars->scale[SCALE_X];
89 i = -1;
90 while (++i < HEIGHT)
91 (*c_im)[i] = vars->start[COMPLEX_IM] + i * vars->scale[SCALE_Y];
92 return (1);
93}
94
95/**
96 * @brief Renders the chosen fractal on the screen.
97 *
98 * This function selects the appropiate rendering algorithm based on the fractal
99 * type chosen by the user (Julia or Mandelbrot) and renders it on the screen.
100 *
101 * For non-technical users:
102 * - Depending on the type of fractal (Julia or Mandelbrot), this function
103 * draws the chosen fractal pattern for you to see.
104 *
105 * @param data Pointer to the main data structure containing fractal and
106 * rendering details.
107 */
109{
110 if (data->fractal_type == JULIA)
111 render_julia(data);
112 else if (data->fractal_type == MANDELBROT)
113 render_mandelbrot(data);
114}
115
116/**
117 * @brief Calculates the color of a pixel based on how many iterations it takes
118 * to "escape".
119 *
120 * This function determines the color of each pixel in the fractal by mapping
121 * the iteration count (how many steps we are needed to reach a threshold)
122 * to a color gradient.
123 *
124 * For non-technical users:
125 * - The function decides the color of each pooint in the fractal, creating the
126 * beautiful patterns you see based on how "complex" the point is.
127 *
128 * @param iterations Number of iterations required for the opint to "escape".
129 * @return Returns the calculated color as an integer.
130 */
131int calculate_color(int iterations)
132{
133 double it_count;
134 int base_gray;
135 t_color color;
136
137 it_count = (double)iterations / MAX_ITERATIONS;
138 base_gray = (30 + 190 * (it_count * (1 - 0.2 * it_count)));
139 color.r = base_gray - (5 * (1 - it_count) * it_count * it_count);
140 color.g = base_gray - (10 * (1 - it_count) * (1 - it_count));
141 color.b = base_gray - (15 * (1 - it_count) * it_count);
142 return ((0xFF << 24) | (color.r << 16) | (color.g << 8) | color.b);
143}
void render_fractal(t_data *data)
Renders the chosen fractal on the screen.
Definition calcs.c:108
int precompute_coords(double **c_re, double **c_im, t_render_vars *vars)
Precomputes the coordinates for each pixel in the fractal.
Definition calcs.c:78
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
@ MANDELBROT
Definition fractol.h:25
@ JULIA
Definition fractol.h:26
#define WIDTH
Definition fractol.h:188
@ SCALE_Y
Definition fractol.h:44
@ SCALE_X
Definition fractol.h:43
@ OFFSET_Y
Definition fractol.h:35
@ OFFSET_X
Definition fractol.h:34
@ COMPLEX_IM
Definition fractol.h:53
@ COMPLEX_RE
Definition fractol.h:52
void render_mandelbrot(t_data *data)
Render the Mandelbrot fractal.
Definition mandelbrot.c:151
void render_julia(t_data *data)
Renders the Julia fractal.
Definition julia.c:165
#define HEIGHT
Definition fractol.h:189
Represents an RGB color.
Definition fractol.h:82
int b
Definition fractol.h:85
int r
Definition fractol.h:83
int g
Definition fractol.h:84
Data structure for fractal rendering.
Definition fractol.h:92
double offset[2]
Definition fractol.h:95
t_fractal_type fractal_type
Definition fractol.h:98
mlx_image_t * img
Definition fractol.h:97
double zoom
Definition fractol.h:93
Variables used for fractal rendering calculations.
Definition fractol.h:105
double scale[2]
Definition fractol.h:107
double start[2]
Definition fractol.h:108
uint32_t * pixels
Definition fractol.h:114