RUST勉強中、9/19の積み上げ?

マンデルブロ集合の処理を実装していきます。

まぁ、この処理は難解なので、ここはテキストを写経していきます。

目標はテストコードをパスするところまで。

fn get_n_driverged(x0: f64, y0: f64, max_iter: usize) -> u8 {
    let mut xn = 0.0;
    let mut yn = 0.0;
    for i in 1..max_iter {
        let x_next = xn * xn - yn * yn + x0;
        let y_next = 2.0 * xn * yn + y0;
        xn = x_next;
        yn = y_next;
        if yn * yn + xn * xn > 4.0 {
            return i as u8;
        }
    }
    max_iter as u8
}

pub fn generate_mandalbrot_set(
    canvas_w: usize,
    canvas_h: usize,
    x_min: f64,
    x_max: f64,
    y_min: f64,
    y_max: f64,
    max_iter: usize,
) -> Vec<u8> {
    let canvas_w_f64 = canvas_w as f64;
    let canvas_h_f64 = canvas_h as f64;
    let mut data = vec![];
    for i in 0..canvas_h {
        let i_f64 = i as f64;
        let y = y_min + (y_max - y_min) * i_f64 / canvas_h_f64;
        for j in 0..canvas_w {
            let x = x_min + (x_max - x_min) * j as f64 / canvas_w_f64;
            let iter_index = get_n_driverged(x, y, max_iter);
            let v = iter_index % 8 * 32;
            data.push(v);
            data.push(v);
            data.push(v);
            data.push(255);
        }
    }
    data
}

#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn test_get_n_diverged() {
        let max_iter = 10;
        assert_eq!(get_n_driverged(1.0, 0.0, max_iter), 3);
        assert_eq!(get_n_driverged(0.0, 0.0, max_iter), max_iter as u8);
        assert_eq!(get_n_driverged(0.0, 1.0, max_iter), max_iter as u8);
    }
    #[test]
    fn test_generate_mandelbrot_set() {
        let canvas_w = 2;
        let canvas_h = 2;
        let x_min = -1.0;
        let x_max = 1.0;
        let y_min = -1.0;
        let y_max = 1.0;
        let max_iter = 8;
        assert_eq!(
            generate_mandalbrot_set(canvas_w, canvas_h, x_min, x_max, y_min, y_max, max_iter),
            vec![96, 96, 96, 255, 0, 0, 0, 255, 0, 0, 0, 255, 0, 0, 0, 255]
        );
    }
}
:~/rust/mandelbrot$ cargo test
   Compiling mandelbrot v0.1.0 (/home/taki/rust/mandelbrot)
warning: function `set_panic_hook` is never used
 --> src/utils.rs:1:8
  |
1 | pub fn set_panic_hook() {
  |        ^^^^^^^^^^^^^^
  |
  = note: `#[warn(dead_code)]` on by default

warning: function `get_n_driverged` is never used
 --> src/logic.rs:1:4
  |
1 | fn get_n_driverged(x0: f64, y0: f64, max_iter: usize) -> u8 {
  |    ^^^^^^^^^^^^^^^

warning: function `generate_mandalbrot_set` is never used
  --> src/logic.rs:16:8
   |
16 | pub fn generate_mandalbrot_set(
   |        ^^^^^^^^^^^^^^^^^^^^^^^

warning: `mandelbrot` (lib) generated 3 warnings
warning: `mandelbrot` (lib test) generated 1 warning (1 duplicate)
    Finished test [unoptimized + debuginfo] target(s) in 0.46s
     Running unittests src/lib.rs (target/debug/deps/mandelbrot-c2f466aeb790a116)

running 2 tests
test logic::tests::test_generate_mandelbrot_set ... ok
test logic::tests::test_get_n_diverged ... ok

test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s

     Running tests/web.rs (target/debug/deps/web-ca9c3937d615c7ed)

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s

   Doc-tests mandelbrot

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s

「RUST勉強中、9/19の積み上げ?」への1件のフィードバック

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください