Skip to content

Commit 4d431fe

Browse files
committed
feat: add ability to specify depth from UI
1 parent 7d1f170 commit 4d431fe

File tree

5 files changed

+17
-11
lines changed

5 files changed

+17
-11
lines changed

src/components/Settings.svelte

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,11 @@
8888
</p>
8989
</div>
9090

91+
<div class="flex mb-2">
92+
<p>Number of iterations:</p>
93+
<InputNumber bind:value={$drawStore.depth} on:change={should_redraw} />
94+
</div>
95+
9196
<div class="flex mb-2">
9297
<p class="mr-2 pt-0.5">Color palette:</p>
9398
<select

src/stores/settings.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export const draw = sessionStore('draw-settings', {
1313
lx: -3,
1414
rx: 1,
1515
yc: 0,
16+
depth: 256,
1617
})
1718

1819
export const settings = sessionStore('settings', {

src/utils/draw.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@ export function drawMandelbrot() {
4444
}
4545

4646
const set = get(drawStore)
47-
mandelbrot.calc(set.lx, set.rx, set.yc, w, h)
47+
mandelbrot.calc(set.lx, set.rx, set.yc, w, h, set.depth)
4848

4949
const pixelColorsPtr = mandelbrot.pixel_steps()
50-
const pixel_steps = new Uint16Array(memory.buffer, pixelColorsPtr, len)
50+
const pixel_steps = new Uint32Array(memory.buffer, pixelColorsPtr, len)
5151

5252
let color, ind
5353

wasm/src/lib.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,15 @@ use wasm_bindgen::prelude::*;
33

44
#[wasm_bindgen]
55
pub struct Mandelbrot {
6-
pixel_steps: Vec<u16>,
6+
pixel_steps: Vec<u32>,
77
pixels_count: usize,
88
}
99

1010
const ESCAPE_MODULUS: f64 = 2.0;
11-
/// number of iterations
12-
const DEPTH: u16 = 256;
1311
/// complex zero
1412
const Z0: Complex<f64> = Complex::new(0.0, 0.0);
1513

16-
const IS_IN: u16 = 0;
14+
const IS_IN: u32 = 0;
1715

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

@@ -65,24 +64,24 @@ impl Mandelbrot {
6564
for i in 0..self.pixels_count {
6665
xd = (i % w as usize) as f64;
6766
yd = (i / w as usize) as f64;
68-
self.pixel_steps[i] = check_series(lx + xd / scale, ty - yd / scale);
67+
self.pixel_steps[i] = check_series(lx + xd / scale, ty - yd / scale, depth);
6968
}
7069
}
7170

72-
pub fn pixel_steps(&self) -> *const u16 {
71+
pub fn pixel_steps(&self) -> *const u32 {
7372
self.pixel_steps.as_ptr()
7473
}
7574
}
7675

77-
fn check_series(x: f64, i: f64) -> u16 {
76+
fn check_series(x: f64, i: f64, depth: u32) -> u32 {
7877
if check_cardioid(x, i) {
7978
return IS_IN;
8079
}
8180

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

85-
for step in 0..DEPTH {
84+
for step in 0..depth {
8685
if num.norm() >= ESCAPE_MODULUS {
8786
return step;
8887
}

wasm/src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ fn benchmark() {
2121
0.8637609863281248,
2222
W as u16,
2323
H as u16,
24+
256,
2425
);
2526

2627
let elapsed = now.elapsed();

0 commit comments

Comments
 (0)