Fractol 1.0
A fractal visualization project implemented in C.
Loading...
Searching...
No Matches
utils.c File Reference
#include "fractol.h"
#include <unistd.h>
Include dependency graph for utils.c:

Go to the source code of this file.

Functions

double ft_atod (const char *str)
 Converts a string to a floating-point number.
 
int ft_strcmp (const char *s1, const char *s2)
 Compares two strings.
 
void handle_key (mlx_key_data_t keydata, void *param)
 Handles key inputs.
 
void handle_scroll (double xdelta, double ydelta, void *param)
 Handles mouse scroll input for zooming.
 
int fr_putstr_fd (char *s, int fd)
 Writes a string to a file descriptor and returns.
 

Function Documentation

◆ fr_putstr_fd()

int fr_putstr_fd ( char *  s,
int  fd 
)

Writes a string to a file descriptor and returns.

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 floating-point number.

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
@ OFFSET_Y
Definition fractol.h:35
@ OFFSET_X
Definition fractol.h:34
#define ZOOM_OUT
Definition fractol.h:191
#define ZOOM_IN
Definition fractol.h:190
Data structure for fractal rendering.
Definition fractol.h:92
double offset[2]
Definition fractol.h:95
mlx_t * mlx
Definition fractol.h:96
double zoom
Definition fractol.h:93
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 mouse scroll input for zooming.

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: