Fractol 1.0
A fractal visualization project implemented in C.
Loading...
Searching...
No Matches
fractol.h File Reference
#include "MLX42/MLX42.h"
#include <limits.h>
#include <stdlib.h>
Include dependency graph for fractol.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  s_color
 Represents an RGB color. More...
 
struct  s_data
 Data structure for fractal rendering. More...
 
struct  s_render_vars
 Variables used for fractal rendering calculations. More...
 
struct  s_atod_data
 Helper structure for string-to-double conversion. More...
 

Macros

#define WIDTH   800
 
#define HEIGHT   WIDTH
 
#define ZOOM_IN   1.1
 
#define ZOOM_OUT   0.9
 
#define FRACTAL_LIMIT   4.0
 
#define MAX_ITERATIONS   75
 
#define RESET   "\033[0m"
 
#define LIGHT_RED   "\033[38;5;203m"
 
#define LIGHT_GOLD   "\033[38;5;220m"
 
#define LIGHT_GREEN   "\033[38;5;120m"
 

Typedefs

typedef enum e_fractal_type t_fractal_type
 
typedef enum e_offset_index t_offset_index
 Indices for offsets in the x and y directions.
 
typedef enum e_scale_index t_scale_index
 Indices for scaling factors in the x and y directions.
 
typedef enum e_complex_index t_complex_index
 Indices for complex numbers (real and imaginary)
 
typedef enum e_number_flags t_number_flags
 Flags for validating numbers.
 
typedef struct s_color t_color
 Represents an RGB color.
 
typedef struct s_data t_data
 Data structure for fractal rendering.
 
typedef struct s_render_vars t_render_vars
 Variables used for fractal rendering calculations.
 
typedef struct s_atod_data t_atod_data
 Helper structure for string-to-double conversion.
 

Enumerations

enum  e_fractal_type { MANDELBROT , JULIA }
 
enum  e_offset_index { OFFSET_X , OFFSET_Y }
 Indices for offsets in the x and y directions. More...
 
enum  e_scale_index { SCALE_X , SCALE_Y }
 Indices for scaling factors in the x and y directions. More...
 
enum  e_complex_index { COMPLEX_RE , COMPLEX_IM }
 Indices for complex numbers (real and imaginary) More...
 
enum  e_number_flags { HAS_DIGIT , HAS_POINT }
 Flags for validating numbers. More...
 
enum  e_err_msg {
  NO_TYPE , INVALID_TYPE , JULIA_ARGS , JULIA_NUM ,
  MANDELBROT_ARGS
}
 Error messages used for input validation. More...
 

Functions

void calculate_scales_and_limits (t_render_vars *vars, t_data *data)
 Calculates scales and limits for rendering fractals.
 
int precompute_coords (double **c_re, double **c_im, t_render_vars *vars)
 Precomputes coordinates for fractal rendering.
 
int calculate_color (int iterations)
 Calculates the color based on the number of iterations.
 
double ft_atod (const char *str)
 Converts a string to a double.
 
int ft_strcmp (const char *s1, const char *s2)
 Compares two strings.
 
int fr_putstr_fd (char *s, int fd)
 Writes a string to a file descriptor.
 
void render_fractal (t_data *data)
 Render the specified fractal.
 
void render_mandelbrot (t_data *data)
 Render the Mandelbrot fractal.
 
void render_julia (t_data *data)
 Renders the Julia fractal.
 
void handle_key (mlx_key_data_t keydata, void *param)
 Handles key inputs.
 
void handle_scroll (double xdelta, double ydelta, void *param)
 Handles scroll inputs.
 

Macro Definition Documentation

◆ FRACTAL_LIMIT

#define FRACTAL_LIMIT   4.0

Definition at line 192 of file fractol.h.

◆ HEIGHT

#define HEIGHT   WIDTH

Definition at line 189 of file fractol.h.

◆ LIGHT_GOLD

#define LIGHT_GOLD   "\033[38;5;220m"

Definition at line 196 of file fractol.h.

◆ LIGHT_GREEN

#define LIGHT_GREEN   "\033[38;5;120m"

Definition at line 197 of file fractol.h.

◆ LIGHT_RED

#define LIGHT_RED   "\033[38;5;203m"

Definition at line 195 of file fractol.h.

◆ MAX_ITERATIONS

#define MAX_ITERATIONS   75

Definition at line 193 of file fractol.h.

◆ RESET

#define RESET   "\033[0m"

Definition at line 194 of file fractol.h.

◆ WIDTH

#define WIDTH   800

Definition at line 188 of file fractol.h.

◆ ZOOM_IN

#define ZOOM_IN   1.1

Definition at line 190 of file fractol.h.

◆ ZOOM_OUT

#define ZOOM_OUT   0.9

Definition at line 191 of file fractol.h.

Typedef Documentation

◆ t_atod_data

typedef struct s_atod_data t_atod_data

Helper structure for string-to-double conversion.

◆ t_color

typedef struct s_color t_color

Represents an RGB color.

◆ t_complex_index

Indices for complex numbers (real and imaginary)

◆ t_data

typedef struct s_data t_data

Data structure for fractal rendering.

◆ t_fractal_type

◆ t_number_flags

Flags for validating numbers.

◆ t_offset_index

Indices for offsets in the x and y directions.

◆ t_render_vars

typedef struct s_render_vars t_render_vars

Variables used for fractal rendering calculations.

◆ t_scale_index

Indices for scaling factors in the x and y directions.

Enumeration Type Documentation

◆ e_complex_index

Indices for complex numbers (real and imaginary)

Enumerator
COMPLEX_RE 
COMPLEX_IM 

Definition at line 50 of file fractol.h.

51{
@ COMPLEX_IM
Definition fractol.h:53
@ COMPLEX_RE
Definition fractol.h:52
enum e_complex_index t_complex_index
Indices for complex numbers (real and imaginary)

◆ e_err_msg

enum e_err_msg

Error messages used for input validation.

Enumerator
NO_TYPE 
INVALID_TYPE 
JULIA_ARGS 
JULIA_NUM 
MANDELBROT_ARGS 

Definition at line 68 of file fractol.h.

69{
70 NO_TYPE,
75};
@ JULIA_NUM
Definition fractol.h:73
@ INVALID_TYPE
Definition fractol.h:71
@ NO_TYPE
Definition fractol.h:70
@ JULIA_ARGS
Definition fractol.h:72
@ MANDELBROT_ARGS
Definition fractol.h:74

◆ e_fractal_type

Enumerator
MANDELBROT 
JULIA 

Definition at line 23 of file fractol.h.

24{
26 JULIA,
@ MANDELBROT
Definition fractol.h:25
@ JULIA
Definition fractol.h:26
enum e_fractal_type t_fractal_type

◆ e_number_flags

Flags for validating numbers.

Enumerator
HAS_DIGIT 
HAS_POINT 

Definition at line 59 of file fractol.h.

60{
@ HAS_POINT
Definition fractol.h:62
@ HAS_DIGIT
Definition fractol.h:61
enum e_number_flags t_number_flags
Flags for validating numbers.

◆ e_offset_index

Indices for offsets in the x and y directions.

Enumerator
OFFSET_X 
OFFSET_Y 

Definition at line 32 of file fractol.h.

33{
enum e_offset_index t_offset_index
Indices for offsets in the x and y directions.
@ OFFSET_Y
Definition fractol.h:35
@ OFFSET_X
Definition fractol.h:34

◆ e_scale_index

Indices for scaling factors in the x and y directions.

Enumerator
SCALE_X 
SCALE_Y 

Definition at line 41 of file fractol.h.

42{
43 SCALE_X,
44 SCALE_Y,
@ SCALE_Y
Definition fractol.h:44
@ SCALE_X
Definition fractol.h:43
enum e_scale_index t_scale_index
Indices for scaling factors in the x and y directions.

Function Documentation

◆ calculate_color()

int calculate_color ( int  iterations)

Calculates the color based on the number of iterations.

Calculates the color based on the number of iterations.

This function determines the color of each pixel in the fractal by mapping the iteration count (how many steps we are needed to reach a threshold) to a color gradient.

For non-technical users:

  • The function decides the color of each pooint in the fractal, creating the beautiful patterns you see based on how "complex" the point is.
Parameters
iterationsNumber of iterations required for the opint to "escape".
Returns
Returns the calculated color as an integer.

Definition at line 131 of file calcs.c.

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}
#define MAX_ITERATIONS
Definition fractol.h:193
Represents an RGB color.
Definition fractol.h:82
int r
Definition fractol.h:83
Here is the caller graph for this function:

◆ calculate_scales_and_limits()

void calculate_scales_and_limits ( t_render_vars vars,
t_data data 
)

Calculates scales and limits for rendering fractals.

Calculates scales and limits for rendering fractals.

This function determines how the fractal is scaled and positioned on the screen. It computes the scale factors for each pixel and establishes the starting points ffor the real and imaginary part of the fractal based on the zoom level and offsets.

For non-technical users:

  • Think of it as setting the "zoom" and "pam" for the fractal to display it correctly on the screen.
Parameters
varsPointer to the rendering variables structure.
dataPointer to the main data structure containing fractal information.

Definition at line 52 of file calcs.c.

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}
#define WIDTH
Definition fractol.h:188
#define HEIGHT
Definition fractol.h:189
double offset[2]
Definition fractol.h:95
mlx_image_t * img
Definition fractol.h:97
double zoom
Definition fractol.h:93
double scale[2]
Definition fractol.h:107
double start[2]
Definition fractol.h:108
uint32_t * pixels
Definition fractol.h:114
Here is the caller graph for this function:

◆ fr_putstr_fd()

int fr_putstr_fd ( char *  s,
int  fd 
)

Writes a string to a file descriptor.

Writes a string to a file descriptor.

Definition at line 109 of file utils.c.

110{
111 int i;
112 int count;
113
114 if (!s)
115 return (0);
116 i = 0;
117 count = 0;
118 while (s[i])
119 count += write(fd, &s[i++], 1);
120 return (count);
121}
Here is the caller graph for this function:

◆ ft_atod()

double ft_atod ( const char *  str)

Converts a string to a double.

Converts a string to a double.

Definition at line 19 of file utils.c.

20{
22
23 d.result = 0.0;
24 d.fraction = 0.0;
25 d.divisor = 10.0;
26 d.sign = 1;
27 d.i = 0;
28 while (str[d.i] == 32 || (str[d.i] >= 9 && str[d.i] <= 13))
29 d.i++;
30 if (str[d.i] == '-' || str[d.i] == '+')
31 if (str[d.i++] == '-')
32 d.sign = -1;
33 while (str[d.i] >= '0' && str[d.i] <= '9')
34 d.result = d.result * 10.0 + (str[d.i++] - '0');
35 if (str[d.i] == '.')
36 {
37 d.i++;
38 while (str[d.i] >= '0' && str[d.i] <= '9')
39 {
40 d.fraction += (str[d.i] - '0') / d.divisor;
41 d.divisor *= 10.0;
42 d.i++;
43 }
44 }
45 return (d.sign * (d.result + d.fraction));
46}
Helper structure for string-to-double conversion.
Definition fractol.h:122
double divisor
Definition fractol.h:126
double fraction
Definition fractol.h:127
double result
Definition fractol.h:125
Here is the caller graph for this function:

◆ ft_strcmp()

int ft_strcmp ( const char *  s1,
const char *  s2 
)

Compares two strings.

Definition at line 51 of file utils.c.

52{
53 int i;
54
55 i = 0;
56 while (s1[i] == s2[i] && s1[i])
57 i++;
58 return (s1[i] - s2[i]);
59}
Here is the caller graph for this function:

◆ handle_key()

void handle_key ( mlx_key_data_t  keydata,
void *  param 
)

Handles key inputs.

Definition at line 64 of file utils.c.

65{
66 t_data *data;
67
68 data = (t_data *)param;
69 if (keydata.key == MLX_KEY_ESCAPE)
70 mlx_close_window(data->mlx);
71 else if (keydata.key == MLX_KEY_W)
72 data->offset[OFFSET_Y] -= 0.1 / data->zoom;
73 else if (keydata.key == MLX_KEY_S)
74 data->offset[OFFSET_Y] += 0.1 / data->zoom;
75 else if (keydata.key == MLX_KEY_A)
76 data->offset[OFFSET_X] -= 0.1 / data->zoom;
77 else if (keydata.key == MLX_KEY_D)
78 data->offset[OFFSET_X] += 0.1 / data->zoom;
79 else if (keydata.key == MLX_KEY_UP)
80 data->zoom *= ZOOM_IN;
81 else if (keydata.key == MLX_KEY_DOWN)
82 data->zoom *= ZOOM_OUT;
83 if (data->zoom < 1e-5)
84 data->zoom = 1e-5;
85 render_fractal(data);
86}
void render_fractal(t_data *data)
Renders the chosen fractal on the screen.
Definition calcs.c:108
#define ZOOM_OUT
Definition fractol.h:191
#define ZOOM_IN
Definition fractol.h:190
Data structure for fractal rendering.
Definition fractol.h:92
mlx_t * mlx
Definition fractol.h:96
Here is the call graph for this function:
Here is the caller graph for this function:

◆ handle_scroll()

void handle_scroll ( double  xdelta,
double  ydelta,
void *  param 
)

Handles scroll inputs.

Handles scroll inputs.

Definition at line 91 of file utils.c.

92{
93 t_data *data;
94
95 (void)xdelta;
96 data = (t_data *)param;
97 if (ydelta > 0)
98 data->zoom *= ZOOM_IN;
99 else
100 data->zoom *= ZOOM_OUT;
101 if (data->zoom < 1e-5)
102 data->zoom = 1e-5;
103 render_fractal(data);
104}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ precompute_coords()

int precompute_coords ( double **  c_re,
double **  c_im,
t_render_vars vars 
)

Precomputes coordinates for fractal rendering.

Precomputes coordinates for fractal rendering.

This function calculates the real and imaginary parts of the fractal for each pixel on the screen, storing them in arrays for quick access during rendering. It ensures that every point is prepared for the fractal calculations.

For non-technical users:

  • Imagine this as creating a "grid" of points where the fractal will be drawn. This points tell the program where to caalculate the colors.
Parameters
c_rePointer to the array storing real coordinates.
c_imPointer to the array storing imaginary coordinates.
varsPointer to the rendering variables structure.
Returns
Returns 1 on success, or 0 if memory allocation fails.

Definition at line 78 of file calcs.c.

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}
Here is the caller graph for this function:

◆ render_fractal()

void render_fractal ( t_data data)

Render the specified fractal.

Render the specified fractal.

This function selects the appropiate rendering algorithm based on the fractal type chosen by the user (Julia or Mandelbrot) and renders it on the screen.

For non-technical users:

  • Depending on the type of fractal (Julia or Mandelbrot), this function draws the chosen fractal pattern for you to see.
Parameters
dataPointer to the main data structure containing fractal and rendering details.

Definition at line 108 of file calcs.c.

109{
110 if (data->fractal_type == JULIA)
111 render_julia(data);
112 else if (data->fractal_type == MANDELBROT)
113 render_mandelbrot(data);
114}
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
t_fractal_type fractal_type
Definition fractol.h:98
Here is the call graph for this function:
Here is the caller graph for this function:

◆ render_julia()

void render_julia ( t_data data)

Renders the Julia fractal.

Renders the Julia fractal.

This function handles the full rendering process for the Julia set by iterating through all rows of the screen, rendering each row sequentially.

  • Setup:
    • Calculates the scaling and starting coordinates based on the zoom level and positional offsets provided by the user.
  • Row-by-Row Rendering:
    • For each row on the screen (y coordinate), the corresponding imaginary coordinate is computed.
    • The row is passed to render_julia_row, which handles the pixel-by-pixel rendering.
  • Full Image Construction:
    • The ffunction processes every row until the entire fractal is rendered.
Parameters
dataPointer to the main fractal data structure, including rendering details such as zoom, offsets, and fractal paarameters.

Definition at line 165 of file julia.c.

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
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
Variables used for fractal rendering calculations.
Definition fractol.h:105
double c_im
Definition fractol.h:113
Here is the call graph for this function:
Here is the caller graph for this function:

◆ render_mandelbrot()

void render_mandelbrot ( t_data data)

Render the Mandelbrot fractal.

Render the Mandelbrot fractal.

Definition at line 151 of file mandelbrot.c.

152{
153 t_render_vars vars;
154 double *precomputed_c_re;
155 double *precomputed_c_im;
156 int y;
157
158 calculate_scales_and_limits(&vars, data);
159 if (!precompute_coords(&precomputed_c_re, &precomputed_c_im, &vars))
160 return ;
161 y = 0;
162 while (y < HEIGHT)
163 {
164 vars.c_im = precomputed_c_im[y];
165 render_mandelbrot_row(&vars, data, y++);
166 }
167 free(precomputed_c_re);
168 free(precomputed_c_im);
169}
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
static void render_mandelbrot_row(t_render_vars *vars, t_data *data, int y)
Renders a single row of the Mandelbrot set.
Definition mandelbrot.c:121
Here is the call graph for this function:
Here is the caller graph for this function: