From 7cc971f6b0011efea0f459948cf98ce2afed0cef Mon Sep 17 00:00:00 2001 From: Mitya Selivanov Date: Tue, 13 Aug 2024 17:11:14 +0200 Subject: Add Julia set example --- examples/julia_set.c | 66 +++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 63 insertions(+), 3 deletions(-) (limited to 'examples/julia_set.c') diff --git a/examples/julia_set.c b/examples/julia_set.c index 85a4674..9d12bb8 100755 --- a/examples/julia_set.c +++ b/examples/julia_set.c @@ -17,7 +17,7 @@ gcc \ -Wno-unused-but-set-variable \ -Wno-unused-parameter \ -Wno-overlength-strings \ - -O3 \ + -g -O3 \ -fsanitize=undefined,address,leak -mshstk \ -lX11 -lm \ -o $BIN $SRC && \ @@ -39,10 +39,70 @@ i32 main(i32 argc, c8 **argv) { p_init(); + i64 p = 4; + + f64 view_x = 0.; + f64 view_y = 0.; + f64 view_s = 1.; + + f64 cx = -.8; + f64 cy = .1; + f64 radius = 2.; + i64 limit = 1024; + while (!platform.done) { - p_handle_events(); + p_wait_events(); + + if (platform.key_pressed['\n']) + p = (p == 1 ? 4 : 1); + + if (platform.key_pressed[KEY_ESCAPE]) { + view_x = 0.; + view_y = 0.; + view_s = 1.; + } + + f64 d = platform.key_down[MOD_CTRL] ? .0005 : .01; + + if (platform.key_pressed['q']) cx -= d; + if (platform.key_pressed['w']) cx += d; + if (platform.key_pressed['a']) cy -= d; + if (platform.key_pressed['s']) cy += d; + + + if (platform.key_down[BUTTON_LEFT]) { + view_x += platform.cursor_dx * view_s; + view_y += platform.cursor_dy * view_s; + } + + view_s += .1 * platform.wheel_dy * view_s; + + for (i32 j = 0; j + p <= platform.frame_height; j += p) + for (i32 i = 0; i + p <= platform.frame_width; i += p) { + f64 x = .003 * ((i - platform.frame_width * .5) * view_s - view_x); + f64 y = .003 * ((j - platform.frame_height * .5) * view_s - view_y); + + i64 n = 0; + + for (; x * x + y * y < radius * radius && n < limit; ++n) { + f64 z = x * x - y * y; + y = 2. * x * y + cy; + x = z + cx; + } + + u32 c; + + if (n == limit) + c = 0; + else + c = 0xffffff - n * 8 - n * 256 * 4; + + for (i32 jj = 0; jj < p; ++jj) + for (i32 ii = 0; ii < p; ++ii) + platform.pixels[(j + jj) * platform.frame_width + (i + ii)] = c; + } + p_render_frame(); - p_sleep_for(0); } p_cleanup(); -- cgit v1.2.3