Rust画出Mandelbrot图

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;
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值