This commit is contained in:
Crispy 2023-07-13 17:21:26 +02:00
commit f02250a507
5 changed files with 234 additions and 0 deletions

2
.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
/target
*.png

153
Cargo.lock generated Normal file
View file

@ -0,0 +1,153 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "adler"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe"
[[package]]
name = "autocfg"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa"
[[package]]
name = "bitflags"
version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
[[package]]
name = "bytemuck"
version = "1.13.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "17febce684fd15d89027105661fec94afb475cb995fbc59d2865198446ba2eea"
[[package]]
name = "byteorder"
version = "1.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610"
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "color_quant"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3d7b894f5411737b7867f4827955924d7c254fc9f4d91a6aad6b097804b1018b"
[[package]]
name = "crc32fast"
version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b540bd8bc810d3885c6ea91e2018302f68baba2129ab3e88f32389ee9370880d"
dependencies = [
"cfg-if",
]
[[package]]
name = "fdeflate"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d329bdeac514ee06249dabc27877490f17f5d371ec693360768b838e19f3ae10"
dependencies = [
"simd-adler32",
]
[[package]]
name = "flate2"
version = "1.0.26"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3b9429470923de8e8cbd4d2dc513535400b4b3fef0319fb5c4e1f520a7bef743"
dependencies = [
"crc32fast",
"miniz_oxide",
]
[[package]]
name = "image"
version = "0.24.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "527909aa81e20ac3a44803521443a765550f09b5130c2c2fa1ea59c2f8f50a3a"
dependencies = [
"bytemuck",
"byteorder",
"color_quant",
"num-rational",
"num-traits",
"png",
]
[[package]]
name = "julia-set"
version = "0.1.0"
dependencies = [
"image",
]
[[package]]
name = "miniz_oxide"
version = "0.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e7810e0be55b428ada41041c41f32c9f1a42817901b4ccf45fa3d4b6561e74c7"
dependencies = [
"adler",
"simd-adler32",
]
[[package]]
name = "num-integer"
version = "0.1.45"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "225d3389fb3509a24c93f5c29eb6bde2586b98d9f016636dff58d7c6f7569cd9"
dependencies = [
"autocfg",
"num-traits",
]
[[package]]
name = "num-rational"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0638a1c9d0a3c0914158145bc76cff373a75a627e6ecbfb71cbe6f453a5a19b0"
dependencies = [
"autocfg",
"num-integer",
"num-traits",
]
[[package]]
name = "num-traits"
version = "0.2.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "578ede34cf02f8924ab9447f50c28075b4d3e5b269972345e7e0372b38c6cdcd"
dependencies = [
"autocfg",
]
[[package]]
name = "png"
version = "0.17.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "59871cc5b6cce7eaccca5a802b4173377a1c2ba90654246789a8fa2334426d11"
dependencies = [
"bitflags",
"crc32fast",
"fdeflate",
"flate2",
"miniz_oxide",
]
[[package]]
name = "simd-adler32"
version = "0.3.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "238abfbb77c1915110ad968465608b68e869e0772622c9656714e73e5a1a522f"

13
Cargo.toml Normal file
View file

@ -0,0 +1,13 @@
[package]
name = "julia-set"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
image = { version = "0.24.6", default_features = false, features = ["png"] }
# Enable high optimizations for dependencies
[profile.dev.package."*"]
opt-level = 3

1
rustfmt.toml Normal file
View file

@ -0,0 +1 @@
hard_tabs = true

65
src/main.rs Normal file
View file

@ -0,0 +1,65 @@
use std::time::SystemTime;
use image::{Rgb, RgbImage};
const WIDTH: u32 = 4096;
const HEIGHT: u32 = WIDTH / 2;
const TOTAL_UNITS_WIDE: f64 = 4.0;
const MAX_ITER: u32 = 512;
const CX: f64 = -0.98;
const CY: f64 = -0.277;
const WIDTH_F: f64 = WIDTH as f64;
const HEIGHT_F: f64 = HEIGHT as f64;
const PIXELS_PER_UNIT: f64 = WIDTH_F / TOTAL_UNITS_WIDE;
fn main() {
let start_time = SystemTime::now();
let mut img = RgbImage::new(WIDTH, HEIGHT);
for y in 0..HEIGHT {
for x in 0..WIDTH {
let pixel = fractal(x as f64, y as f64);
img.put_pixel(x, y, pixel);
}
}
println!(
"Generating took {} ms",
start_time.elapsed().unwrap().as_millis()
);
let start_time = SystemTime::now();
let filename = format!("julia_set_cx{:.3}_cy{:.3}.png", CX, CY);
img.save(filename).unwrap();
println!(
"Saving took {} ms",
start_time.elapsed().unwrap().as_millis()
);
}
fn fractal(x: f64, y: f64) -> Rgb<u8> {
let mut x = (x - WIDTH_F / 2.0) / PIXELS_PER_UNIT;
let mut y = (y - HEIGHT_F / 2.0) / PIXELS_PER_UNIT;
let mut iterations = 0;
while (x * x + y * y) < 4.0 {
(x, y) = (
x * x - y * y + CX, //
2.0 * x * y + CY,
);
iterations += 1;
if iterations == MAX_ITER {
return Rgb([0, 0, 0]);
}
}
let i = iterations.min(255) as u8;
Rgb([
i.saturating_mul(4),
i.saturating_mul(8),
i.saturating_mul(12),
])
}