Mandelbrot图非常的神奇,下面是画出图片的效果。rust语言非常厉害,他的borrow-checker机制是代码安全性得到很高的提升。
下面是我的代码,全部用的都是Rust语言。
use image::{ImageBuffer, RgbImage};
use image::Rgb;
fn new_image() -> RgbImage {
ImageBuffer::new(512, 512)
}
pub fn make_mandelbrot_image() -> RgbImage {
let mut image = new_image();
let (_width, _height) = image.dimensions();
// Hint: make `image` mut and then modify it.
let mut x0:f64;
let mut y0:f64;
let mut x:f64 = 0.0;
let mut y:f64 = 0.0;
let mut iteration = 0.0;
let max_iteration = 1000.0;
let mut temp:f64;
let mut m:f64;
let mut n:f64;
let wid = _width as f64;
let hei = _height as f64;
let mut color:u8;
for i in 0.._width {
for j in 0.._height {
m = i as f64;
n = j as f64;
x0 = m/wid * (1.0 + 2.5) - 2.5;
y0 = n/hei * (1.0 + 1.0) - 1.0;
iteration = 0.0;
x = 0.0;
y = 0.0;
while x*x + y*y <= 4.0 && iteration < max_iteration {
temp = x*x - y*y + x0;
y = 2.0 *x*y + y0;
x = temp;
iteration += 1.0;
}
color = ((iteration as f64) / (max_iteration as f64) * 255.0) as u8;
image.put_pixel(i, j, Rgb([color, color, color]));
}
}
return image;
}