Create a gist now

Instantly share code, notes, and snippets.

@algebroid /island_generator.rs Secret
Last active Jul 27, 2017

What would you like to do?
extern crate rand;
extern crate noise;
extern crate image;
use noise::{NoiseModule, Perlin, Seedable};
use rand::Rng;
use image::{ImageBuffer};
use std::fs::File;
use std::path::Path;
fn main() {
let mut rng = rand::thread_rng();
let width = 800;
let height = 640;
let perlin = Perlin::new();
let seed = rng.gen::<usize>();
perlin.set_seed(0);
let white_noise = |x: u32, y: u32| {
let altitude = perlin.get([x as f32 / 160.0, y as f32 / 108.0]) + 1.0
+ 0.5 * (perlin.get([x as f32 / 40.0, y as f32 / 32.0]) + 1.0)
+ 0.25 * (perlin.get([x as f32 / 20.0, y as f32 / 16.0]) + 1.0);
let altitude = (altitude * 75.0) as u8;
image::Luma([altitude])
};
let island = |x: u32, y: u32| {
let altitude = 1.0 * (perlin.get([x as f32 / 160.0, y as f32 / 108.0]) + 1.0)
+ 0.5 * (perlin.get([x as f32 / 40.0, y as f32 / 32.0]) + 1.0)
+ 0.25 * (perlin.get([x as f32 / 20.0, y as f32 / 16.0]) + 1.0);
let altitude = (altitude * 75.0) as u8;
match altitude {
0 ...63 => image::Rgba([ 0, 0, 228, 255]),
64 ...127 => image::Rgba([ 0, 0, 255, 255]),
128...143 => image::Rgba([128, 128, 255, 255]),
144...151 => image::Rgba([239, 221, 111, 255]),
152...167 => image::Rgba([180, 255, 170, 255]),
168...184 => image::Rgba([100, 255, 100, 255]),
185...207 => image::Rgba([189, 200, 100, 255]),
208...255 => image::Rgba([255, 255, 255, 255]),
_ => image::Rgba([ 0, 0, 0, 0]),
}
};
// let image_buf = ImageBuffer::from_fn(width, height, white_noise);
let image_buf = ImageBuffer::from_fn(width, height, island);
let ref mut fout = File::create(&Path::new("island.png")).unwrap();
// let _ = image::ImageLuma8(image_buf).save(fout, image::PNG).unwrap();
let _ = image::ImageRgba8(image_buf).save(fout, image::PNG).unwrap();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment