From 29d8297d40d63ffd5f39964f907164ef88f8d693 Mon Sep 17 00:00:00 2001 From: Ilia Date: Sat, 12 Feb 2022 09:43:58 +0300 Subject: [PATCH] feat: speed up by checking for point in cardioid --- src/wasm/mandelbrot.cpp | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/wasm/mandelbrot.cpp b/src/wasm/mandelbrot.cpp index 5b767bd..b0c3e25 100644 --- a/src/wasm/mandelbrot.cpp +++ b/src/wasm/mandelbrot.cpp @@ -25,6 +25,29 @@ extern "C" { } #endif +#ifdef __EMSCRIPTEN__ +EMSCRIPTEN_KEEPALIVE +#endif +bool checkCardioid(double x, double i) { + double a4 = x - 0.25, + b2 = i * i; + double q = a4 * a4 + b2; + + // cardioid + if (q * (q + a4) < b2 * 0.25) { + return true; + } + + double x1 = x + 1; + + // circle to the left of cardioid + if (x1 * x1 + b2 < 1/16) { + return true; + } + + return false; +} + const int R = 2, N = 100; const complex z0 = 0; @@ -32,6 +55,10 @@ const complex z0 = 0; EMSCRIPTEN_KEEPALIVE #endif unsigned short checkSeries(double x, double i) { + if (checkCardioid(x, i)) { + return 1; + } + complex point = complex(x, i); complex num = z0 + point;