Fractol 1.0
A fractal visualization project implemented in C.
Loading...
Searching...
No Matches
calcs.c File Reference

Core calculations for fractal rendering. More...

#include "fractol.h"
Include dependency graph for calcs.c:

Go to the source code of this file.

Functions

void calculate_scales_and_limits (t_render_vars *vars, t_data *data)
 Calculates the scales and starting points for the fractal rendering.
 
int precompute_coords (double **c_re, double **c_im, t_render_vars *vars)
 Precomputes the coordinates for each pixel in the fractal.
 
void render_fractal (t_data *data)
 Renders the chosen fractal on the screen.
 
int calculate_color (int iterations)
 Calculates the color of a pixel based on how many iterations it takes to "escape".
 

Detailed Description

Core calculations for fractal rendering.

This file contains the functions responsible for performing the core calculations required to render fractals. It handles:

  • Scaling and positioning the fractal within the rendering window.
  • Precomputing the coordinates ffor each pixel to optimize rendering performance.
  • Rendering the fractal by delegating to specific algorithms (Julia or Mandelbrot).
  • Mapping iteration counts to colors for visually representing fractal complexity.

    For non-technical users:

    • This file performs the "math magic" that powers the fractal visuals, calculating where and howw the fractal is drawn and colored on the screen.

Definition in file calcs.c.

Function Documentation

◆ calculate_color()

int calculate_color ( int  iterations)

Calculates the color of a pixel based on how many iterations it takes to "escape".

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 the scales and starting points for the fractal rendering.

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
@ 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
#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:

◆ precompute_coords()

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

Precomputes the coordinates for each pixel in the fractal.

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)

Renders the chosen fractal on the screen.

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}
@ MANDELBROT
Definition fractol.h:25
@ JULIA
Definition fractol.h:26
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: