Skip to content

Commit

Permalink
feat: add ability to specify depth from UI
Browse files Browse the repository at this point in the history
  • Loading branch information
istudyatuni committed Jun 21, 2022
1 parent 7d1f170 commit 4d431fe
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 11 deletions.
5 changes: 5 additions & 0 deletions src/components/Settings.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@
</p>
</div>

<div class="flex mb-2">
<p>Number of iterations:</p>
<InputNumber bind:value={$drawStore.depth} on:change={should_redraw} />
</div>

<div class="flex mb-2">
<p class="mr-2 pt-0.5">Color palette:</p>
<select
Expand Down
1 change: 1 addition & 0 deletions src/stores/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export const draw = sessionStore('draw-settings', {
lx: -3,
rx: 1,
yc: 0,
depth: 256,
})

export const settings = sessionStore('settings', {
Expand Down
4 changes: 2 additions & 2 deletions src/utils/draw.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ export function drawMandelbrot() {
}

const set = get(drawStore)
mandelbrot.calc(set.lx, set.rx, set.yc, w, h)
mandelbrot.calc(set.lx, set.rx, set.yc, w, h, set.depth)

const pixelColorsPtr = mandelbrot.pixel_steps()
const pixel_steps = new Uint16Array(memory.buffer, pixelColorsPtr, len)
const pixel_steps = new Uint32Array(memory.buffer, pixelColorsPtr, len)

let color, ind

Expand Down
17 changes: 8 additions & 9 deletions wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,15 @@ use wasm_bindgen::prelude::*;

#[wasm_bindgen]
pub struct Mandelbrot {
pixel_steps: Vec<u16>,
pixel_steps: Vec<u32>,
pixels_count: usize,
}

const ESCAPE_MODULUS: f64 = 2.0;
/// number of iterations
const DEPTH: u16 = 256;
/// complex zero
const Z0: Complex<f64> = Complex::new(0.0, 0.0);

const IS_IN: u16 = 0;
const IS_IN: u32 = 0;

#[wasm_bindgen]
impl Mandelbrot {
Expand Down Expand Up @@ -46,7 +44,8 @@ impl Mandelbrot {
/// * `yc` - Center of y axis (complex plane)
/// * `w` - Width of display (canvas plane) - how many points to calculate
/// * `h` - Height of display (canvas plane)
pub fn calc(&mut self, lx: f64, rx: f64, yc: f64, w: u16, h: u16) {
/// * `depth` - Number of iterations
pub fn calc(&mut self, lx: f64, rx: f64, yc: f64, w: u16, h: u16, depth: u32) {
// total width of x axis (complex plane)
let xwidth = rx - lx;

Expand All @@ -65,24 +64,24 @@ impl Mandelbrot {
for i in 0..self.pixels_count {
xd = (i % w as usize) as f64;
yd = (i / w as usize) as f64;
self.pixel_steps[i] = check_series(lx + xd / scale, ty - yd / scale);
self.pixel_steps[i] = check_series(lx + xd / scale, ty - yd / scale, depth);
}
}

pub fn pixel_steps(&self) -> *const u16 {
pub fn pixel_steps(&self) -> *const u32 {
self.pixel_steps.as_ptr()
}
}

fn check_series(x: f64, i: f64) -> u16 {
fn check_series(x: f64, i: f64, depth: u32) -> u32 {
if check_cardioid(x, i) {
return IS_IN;
}

let point = Complex::new(x, i);
let mut num = Z0 + point;

for step in 0..DEPTH {
for step in 0..depth {
if num.norm() >= ESCAPE_MODULUS {
return step;
}
Expand Down
1 change: 1 addition & 0 deletions wasm/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ fn benchmark() {
0.8637609863281248,
W as u16,
H as u16,
256,
);

let elapsed = now.elapsed();
Expand Down

0 comments on commit 4d431fe

Please sign in to comment.