Fractol 1.0
A fractal visualization project implemented in C.
Loading...
Searching...
No Matches
utils.c
Go to the documentation of this file.
1/* ************************************************************************** */
2/* */
3/* ::: :::::::: */
4/* utils.c :+: :+: :+: */
5/* +:+ +:+ +:+ */
6/* By: meghribe <meghribe@student.42barcelon +#+ +:+ +#+ */
7/* +#+#+#+#+#+ +#+ */
8/* Created: 2024/12/07 17:32:45 by meghribe #+# #+# */
9/* Updated: 2025/01/04 16:33:38 by meghribe ### ########.fr */
10/* */
11/* ************************************************************************** */
12
13#include "fractol.h"
14#include <unistd.h>
15
16/**
17 * @brief Converts a string to a floating-point number.
18 */
19double ft_atod(const char *str)
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}
47
48/**
49 * @brief Compares two strings.
50 */
51int ft_strcmp(const char *s1, const char *s2)
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}
60
61/*
62 * @brief Handles keyboard input.
63 */
64void handle_key(mlx_key_data_t keydata, void *param)
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}
87
88/**
89 * @brief Handles mouse scroll input for zooming.
90 */
91void handle_scroll(double xdelta, double ydelta, void *param)
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}
105
106/**
107 * @brief Writes a string to a file descriptor and returns
108 */
109int fr_putstr_fd(char *s, int fd)
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}
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
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
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
int ft_strcmp(const char *s1, const char *s2)
Compares two strings.
Definition utils.c:51
int fr_putstr_fd(char *s, int fd)
Writes a string to a file descriptor and returns.
Definition utils.c:109
void handle_key(mlx_key_data_t keydata, void *param)
Handles key inputs.
Definition utils.c:64
double ft_atod(const char *str)
Converts a string to a floating-point number.
Definition utils.c:19
void handle_scroll(double xdelta, double ydelta, void *param)
Handles mouse scroll input for zooming.
Definition utils.c:91