Skip to content

Commit dab4cd2

Browse files
author
PythonGermany
committed
Better colors
1 parent 9350fc0 commit dab4cd2

File tree

5 files changed

+63
-45
lines changed

5 files changed

+63
-45
lines changed

colors.c

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,24 +25,29 @@ uint32_t get_color(int r, int g, int b, int a)
2525
uint32_t tcr(int iter, int curr, int cs)
2626
{
2727
int c;
28+
int max = 255 * 6 + 1;
29+
int fa;
2830

29-
c = range(255 * 6 + 1, iter, curr);
30-
if (c == 255 * 6 + 1)
31+
fa = 0;
32+
c = range(max, iter, curr);
33+
if (c == max)
3134
return (get_color(0, 0, 0, 255));
3235
c += cs;
33-
if (c <= 255 && !cs)
36+
if (c > max)
37+
c -= max;
38+
if (cs <= 255 && c <= 255 * ++fa)
3439
return (get_color(c, 0, 0, c));
35-
if (c <= 255 && cs)
40+
if (cs > 255 && c <= 255 * ++fa)
3641
return (get_color(255, 0, 255 - c, 255));
37-
if (c <= 255 * 2)
42+
if (c <= 255 * ++fa)
3843
return (get_color(255, c - 255, 0, 255));
39-
if (c <= 255 * 3)
40-
return (get_color(255 * 3 - c, 255 , 0, 255));
41-
if (c <= 255 * 4)
42-
return (get_color(0, 255, c - 255 * 3, 255));
43-
if (c <= 255 * 5)
44-
return (get_color(0, 255 * 5 - c, 255, 255));
45-
return (get_color(c - 255 * 5, 0, 255, 255));
44+
if (c <= 255 * ++fa)
45+
return (get_color(255 * fa - c, 255 , 0, 255));
46+
if (c <= 255 * ++fa)
47+
return (get_color(0, 255, c - 255 * (fa - 1), 255));
48+
if (c <= 255 * ++fa)
49+
return (get_color(0, 255 * fa - c, 255, 255));
50+
return (get_color(c - 255 * (fa - 1), 0, 255, 255));
4651
}
4752

4853
// uint32_t lin_inter(uint32_t c1, uint32_t c2, double f)

fractals.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ int tricorn(t_dta *dta)
6060
y_im = 0;
6161
while (x_re * x_re + y_im * y_im <= 4 && n < dta->iter)
6262
{
63-
x_buf = x_re * x_re - y_im * y_im + dta->re;
64-
y_im = -2 * x_re * y_im + dta->im;
63+
x_buf = x_re * x_re - y_im * y_im + dta->x;
64+
y_im = -2 * x_re * y_im + dta->y;
6565
x_re = x_buf;
6666
n++;
6767
}

fractol.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515

1616
# include "MLX42.h"
1717

18-
# define WIDTH 500
19-
# define HEIGHT 500
18+
# define WIDTH 400
19+
# define HEIGHT 400
2020

2121
typedef struct s_dta
2222
{

hooks.c

Lines changed: 37 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,37 +17,50 @@
1717
int i = 0;
1818
int ms_g = 0;
1919

20+
void key_act_d(mlx_t *mlx, keys_t key, double *n, double op)
21+
{
22+
if (mlx_is_key_down(mlx, key))
23+
*n += op;
24+
}
25+
26+
void key_act_i(mlx_t *mlx, keys_t key, int *n, int op)
27+
{
28+
if (mlx_is_key_down(mlx, key))
29+
*n = op;
30+
}
31+
2032
void key_hook(void *dta)
2133
{
2234
t_dta *tmp;
35+
int32_t x;
36+
int32_t y;
2337

2438
tmp = (t_dta *)dta;
2539
if (mlx_is_key_down(tmp->mlx, MLX_KEY_ESCAPE))
2640
mlx_close_window(tmp->mlx);
27-
if (mlx_is_key_down(tmp->mlx, MLX_KEY_MINUS))
28-
tmp->iter = tmp->iter / 1.1 + 1;
29-
if (mlx_is_key_down(tmp->mlx, MLX_KEY_EQUAL))
30-
tmp->iter = tmp->iter * 1.1 + 1;
31-
if (mlx_is_key_down(tmp->mlx, MLX_KEY_UP))
32-
tmp->ys += 0.02 * tmp->scale;
33-
if (mlx_is_key_down(tmp->mlx, MLX_KEY_DOWN))
34-
tmp->ys -= 0.02 * tmp->scale;
35-
if (mlx_is_key_down(tmp->mlx, MLX_KEY_LEFT))
36-
tmp->xs -= 0.02 * tmp->scale;
37-
if (mlx_is_key_down(tmp->mlx, MLX_KEY_RIGHT))
38-
tmp->xs += 0.02 * tmp->scale;
3941
if (mlx_is_key_down(tmp->mlx, MLX_KEY_R))
4042
populate_dta(tmp);
43+
key_act_i(tmp->mlx, MLX_KEY_MINUS, &tmp->iter, tmp->iter / 1.1 + 1);
44+
key_act_i(tmp->mlx, MLX_KEY_EQUAL, &tmp->iter, tmp->iter * 1.1);
45+
key_act_i(tmp->mlx, MLX_KEY_C, &tmp->cs, tmp->cs % (254 * 6) + 6);
46+
key_act_d(tmp->mlx, MLX_KEY_UP, &tmp->ys, 0.02 * tmp->scale);
47+
key_act_d(tmp->mlx, MLX_KEY_DOWN, &tmp->ys, -0.02 * tmp->scale);
48+
key_act_d(tmp->mlx, MLX_KEY_LEFT, &tmp->xs, -0.02 * tmp->scale);
49+
key_act_d(tmp->mlx, MLX_KEY_RIGHT, &tmp->xs, 0.02 * tmp->scale);
4150
if (mlx_is_mouse_down(tmp->mlx, MLX_MOUSE_BUTTON_RIGHT))
42-
tmp->cs = (tmp->cs % (254 * 6)) + 6;
43-
// clock_t diff;
44-
// clock_t start = clock();
51+
{
52+
mlx_get_mouse_pos(tmp->mlx, &x, &y);
53+
tmp->re = (x - WIDTH / 2) * tmp->scale / WIDTH;
54+
tmp->im = (HEIGHT / 2 - y) * tmp->scale / HEIGHT;
55+
}
56+
clock_t diff;
57+
clock_t start = clock();
4558
calculate_window(tmp);
46-
// diff = clock() - start;
47-
// int msec = diff * 1000 / CLOCKS_PER_SEC;
48-
// ms_g += msec%1000;
49-
// i++;
50-
// printf("Time taken %d seconds %d milliseconds av: %d\n", msec/1000, msec%1000, ms_g / i);
59+
diff = clock() - start;
60+
int msec = diff * 1000 / CLOCKS_PER_SEC;
61+
ms_g += msec%1000;
62+
i++;
63+
printf("%d milliseconds, av: %d\nScale: %f, %d iter\nXparam %f, Yparam %f\n\n", msec%1000, ms_g / i, tmp->scale, tmp->iter, tmp->re, tmp->im);
5164

5265
}
5366

@@ -63,14 +76,14 @@ void scroll_hook(double xdelta, double ydelta, void *param)
6376
if (ydelta > 0)
6477
{
6578
tmp->scale /= 1.2;
66-
tmp->xs += ((x - WIDTH / 2) * tmp->scale) / WIDTH * 0.2;
67-
tmp->ys += ((HEIGHT / 2 - y) * tmp->scale) / HEIGHT * 0.2;
79+
tmp->xs += (x - WIDTH / 2) * tmp->scale / WIDTH * 0.2;
80+
tmp->ys += (HEIGHT / 2 - y) * tmp->scale / HEIGHT * 0.2;
6881
}
6982
if (ydelta < 0)
7083
{
7184
tmp->scale *= 1.2;
72-
tmp->xs += ((x - WIDTH / 2) * tmp->scale) / WIDTH * -(1 - 1 / 1.2);
73-
tmp->ys += ((HEIGHT / 2 - y) * tmp->scale) / HEIGHT * -(1 - 1 / 1.2);
85+
tmp->xs += (x - WIDTH / 2) * tmp->scale / WIDTH * -(1 - 1 / 1.2);
86+
tmp->ys += (HEIGHT / 2 - y) * tmp->scale / HEIGHT * -(1 - 1 / 1.2);
7487
}
7588
}
7689

main.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: rburgsta <[email protected]. +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2022/11/27 16:31:38 by rburgsta #+# #+# */
9-
/* Updated: 2022/11/30 02:27:57 by rburgsta ### ########.fr */
9+
/* Updated: 2022/11/30 14:08:26 by rburgsta ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -83,17 +83,17 @@ void populate_dta(t_dta *dta)
8383

8484
void calculate_window(t_dta *dta)
8585
{
86-
size_t i;
87-
size_t i2;
86+
int i;
87+
int i2;
8888

8989
i = 0;
9090
while (i < HEIGHT)
9191
{
9292
i2 = 0;
9393
while (i2 < WIDTH)
9494
{
95-
dta->x = ((i2 - WIDTH / 2.0) * dta->scale) / WIDTH + dta->xs;
96-
dta->y = ((HEIGHT / 2.0 - i) * dta->scale) / HEIGHT + dta->ys;
95+
dta->x = (i2 - WIDTH / 2.0) * dta->scale / WIDTH + dta->xs;
96+
dta->y = (HEIGHT / 2.0 - i) * dta->scale / HEIGHT + dta->ys;
9797
mlx_put_pixel(dta->img, i2++, i, \
9898
tcr(dta->iter, (*dta->f)(dta), dta->cs));
9999
}

0 commit comments

Comments
 (0)