josecelano / mandelbrot-orbits-rust

Plotting Mandelbrot Set periods using Rust language.
MIT License
1 stars 0 forks source link

Re-use calculation for composition #10

Open josecelano opened 2 years ago

josecelano commented 2 years ago

The escape_time function calculates some interactions (255 right now):

fn escape_time(c: Complex<f64>, limit: usize) -> Option<usize> {
    let mut z = Complex { re: 0.0, im: 0.0 };
    for i in 0..limit {
        if z.norm_sqr() > 4.0 {
            return Some(i);
        }
        z = z * z + c;
    }

    None
}

But the is_period_p starts again from z = 0.

fn is_period_p(z: Complex<f64>,c: Complex<f64>, n: usize) -> bool {
    let max_period = 40;

    let mut result = z.clone();

    for _iter in 0..max_period {

        let lambda = lambda(result, c, n);
        let lambda_abs = lambda.abs();

        if lambda_abs >= 1. {
            return false;
        }

        result = phi(result, c);
    }

    true
}

as you can see in the render function:

fn render(pixels: &mut [u8],
          bounds: (usize, usize),
          upper_left: Complex<f64>,
          lower_right: Complex<f64>)
{
    assert!(pixels.len() == bounds.0 * bounds.1);

    for row in 0..bounds.1 {
        for column in 0..bounds.0 {

            // ...

            pixels[row * bounds.0 + column] =
                match escape_time(point, iteration_limit) {
                    None => {
                        // Mandelbrot Set point
                        // Calculate period
                        let z0 = Complex { re: 0.0, im: 0.0 };
                        let period = calculate_period(z0, point);

                        // ...

                        color
                    },
                    // Not a Mandelbrot Set point. Grayscale depending on the escape time
                    Some(count) => iteration_limit as u8 - count as u8,
                };
        }
    }
}

Consider returning zn from escape_time instead of starting from z0.