mirror of
https://github.com/Noratrieb/mandelbrot-rust.git
synced 2026-01-14 15:25:07 +01:00
works
This commit is contained in:
parent
fd6aa99b99
commit
20ea90e0d7
4 changed files with 420 additions and 42 deletions
325
Cargo.lock
generated
325
Cargo.lock
generated
|
|
@ -1,5 +1,330 @@
|
||||||
# This file is automatically @generated by Cargo.
|
# This file is automatically @generated by Cargo.
|
||||||
# It is not intended for manual editing.
|
# It is not intended for manual editing.
|
||||||
|
[[package]]
|
||||||
|
name = "adler"
|
||||||
|
version = "1.0.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "adler32"
|
||||||
|
version = "1.2.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "aae1277d39aeec15cb388266ecc24b11c80469deae6067e17a1a7aa9e5c1f234"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "autocfg"
|
||||||
|
version = "1.0.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "bitflags"
|
||||||
|
version = "1.2.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "bytemuck"
|
||||||
|
version = "1.5.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "bed57e2090563b83ba8f83366628ce535a7584c9afa4c9fc0612a03925c6df58"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "byteorder"
|
||||||
|
version = "1.4.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "ae44d1a3d5a19df61dd0c8beb138458ac2a53a7ac09eba97d55592540004306b"
|
||||||
|
|
||||||
|
[[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.2.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "81156fece84ab6a9f2afdb109ce3ae577e42b1228441eded99bd77f627953b1a"
|
||||||
|
dependencies = [
|
||||||
|
"cfg-if",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "crossbeam-channel"
|
||||||
|
version = "0.5.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "dca26ee1f8d361640700bde38b2c37d8c22b3ce2d360e1fc1c74ea4b0aa7d775"
|
||||||
|
dependencies = [
|
||||||
|
"cfg-if",
|
||||||
|
"crossbeam-utils",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "crossbeam-deque"
|
||||||
|
version = "0.8.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "94af6efb46fef72616855b036a624cf27ba656ffc9be1b9a3c931cfc7749a9a9"
|
||||||
|
dependencies = [
|
||||||
|
"cfg-if",
|
||||||
|
"crossbeam-epoch",
|
||||||
|
"crossbeam-utils",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "crossbeam-epoch"
|
||||||
|
version = "0.9.3"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "2584f639eb95fea8c798496315b297cf81b9b58b6d30ab066a75455333cf4b12"
|
||||||
|
dependencies = [
|
||||||
|
"cfg-if",
|
||||||
|
"crossbeam-utils",
|
||||||
|
"lazy_static",
|
||||||
|
"memoffset",
|
||||||
|
"scopeguard",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "crossbeam-utils"
|
||||||
|
version = "0.8.3"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "e7e9d99fa91428effe99c5c6d4634cdeba32b8cf784fc428a2a687f61a952c49"
|
||||||
|
dependencies = [
|
||||||
|
"autocfg",
|
||||||
|
"cfg-if",
|
||||||
|
"lazy_static",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "deflate"
|
||||||
|
version = "0.8.6"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "73770f8e1fe7d64df17ca66ad28994a0a623ea497fa69486e14984e715c5d174"
|
||||||
|
dependencies = [
|
||||||
|
"adler32",
|
||||||
|
"byteorder",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "either"
|
||||||
|
version = "1.6.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "e78d4f1cc4ae33bbfc157ed5d5a5ef3bc29227303d595861deb238fcec4e9457"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "gif"
|
||||||
|
version = "0.11.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "02efba560f227847cb41463a7395c514d127d4f74fff12ef0137fff1b84b96c4"
|
||||||
|
dependencies = [
|
||||||
|
"color_quant",
|
||||||
|
"weezl",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "hermit-abi"
|
||||||
|
version = "0.1.18"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "322f4de77956e22ed0e5032c359a0f1273f1f7f0d79bfa3b8ffbc730d7fbcc5c"
|
||||||
|
dependencies = [
|
||||||
|
"libc",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "image"
|
||||||
|
version = "0.23.14"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "24ffcb7e7244a9bf19d35bf2883b9c080c4ced3c07a9895572178cdb8f13f6a1"
|
||||||
|
dependencies = [
|
||||||
|
"bytemuck",
|
||||||
|
"byteorder",
|
||||||
|
"color_quant",
|
||||||
|
"gif",
|
||||||
|
"jpeg-decoder",
|
||||||
|
"num-iter",
|
||||||
|
"num-rational",
|
||||||
|
"num-traits",
|
||||||
|
"png",
|
||||||
|
"scoped_threadpool",
|
||||||
|
"tiff",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "jpeg-decoder"
|
||||||
|
version = "0.1.22"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "229d53d58899083193af11e15917b5640cd40b29ff475a1fe4ef725deb02d0f2"
|
||||||
|
dependencies = [
|
||||||
|
"rayon",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "lazy_static"
|
||||||
|
version = "1.4.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "libc"
|
||||||
|
version = "0.2.88"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "03b07a082330a35e43f63177cc01689da34fbffa0105e1246cf0311472cac73a"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "mandelbrot_set"
|
name = "mandelbrot_set"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"image",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "memoffset"
|
||||||
|
version = "0.6.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "157b4208e3059a8f9e78d559edc658e13df41410cb3ae03979c83130067fdd87"
|
||||||
|
dependencies = [
|
||||||
|
"autocfg",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "miniz_oxide"
|
||||||
|
version = "0.3.7"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "791daaae1ed6889560f8c4359194f56648355540573244a5448a83ba1ecc7435"
|
||||||
|
dependencies = [
|
||||||
|
"adler32",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "miniz_oxide"
|
||||||
|
version = "0.4.4"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "a92518e98c078586bc6c934028adcca4c92a53d6a958196de835170a01d84e4b"
|
||||||
|
dependencies = [
|
||||||
|
"adler",
|
||||||
|
"autocfg",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "num-integer"
|
||||||
|
version = "0.1.44"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "d2cc698a63b549a70bc047073d2949cce27cd1c7b0a4a862d08a8031bc2801db"
|
||||||
|
dependencies = [
|
||||||
|
"autocfg",
|
||||||
|
"num-traits",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "num-iter"
|
||||||
|
version = "0.1.42"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "b2021c8337a54d21aca0d59a92577a029af9431cb59b909b03252b9c164fad59"
|
||||||
|
dependencies = [
|
||||||
|
"autocfg",
|
||||||
|
"num-integer",
|
||||||
|
"num-traits",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "num-rational"
|
||||||
|
version = "0.3.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "12ac428b1cb17fce6f731001d307d351ec70a6d202fc2e60f7d4c5e42d8f4f07"
|
||||||
|
dependencies = [
|
||||||
|
"autocfg",
|
||||||
|
"num-integer",
|
||||||
|
"num-traits",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "num-traits"
|
||||||
|
version = "0.2.14"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "9a64b1ec5cda2586e284722486d802acf1f7dbdc623e2bfc57e65ca1cd099290"
|
||||||
|
dependencies = [
|
||||||
|
"autocfg",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "num_cpus"
|
||||||
|
version = "1.13.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "05499f3756671c15885fee9034446956fff3f243d6077b91e5767df161f766b3"
|
||||||
|
dependencies = [
|
||||||
|
"hermit-abi",
|
||||||
|
"libc",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "png"
|
||||||
|
version = "0.16.8"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "3c3287920cb847dee3de33d301c463fba14dda99db24214ddf93f83d3021f4c6"
|
||||||
|
dependencies = [
|
||||||
|
"bitflags",
|
||||||
|
"crc32fast",
|
||||||
|
"deflate",
|
||||||
|
"miniz_oxide 0.3.7",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "rayon"
|
||||||
|
version = "1.5.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "8b0d8e0819fadc20c74ea8373106ead0600e3a67ef1fe8da56e39b9ae7275674"
|
||||||
|
dependencies = [
|
||||||
|
"autocfg",
|
||||||
|
"crossbeam-deque",
|
||||||
|
"either",
|
||||||
|
"rayon-core",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "rayon-core"
|
||||||
|
version = "1.9.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "9ab346ac5921dc62ffa9f89b7a773907511cdfa5490c572ae9be1be33e8afa4a"
|
||||||
|
dependencies = [
|
||||||
|
"crossbeam-channel",
|
||||||
|
"crossbeam-deque",
|
||||||
|
"crossbeam-utils",
|
||||||
|
"lazy_static",
|
||||||
|
"num_cpus",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "scoped_threadpool"
|
||||||
|
version = "0.1.9"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "1d51f5df5af43ab3f1360b429fa5e0152ac5ce8c0bd6485cae490332e96846a8"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "scopeguard"
|
||||||
|
version = "1.1.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "tiff"
|
||||||
|
version = "0.6.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "9a53f4706d65497df0c4349241deddf35f84cee19c87ed86ea8ca590f4464437"
|
||||||
|
dependencies = [
|
||||||
|
"jpeg-decoder",
|
||||||
|
"miniz_oxide 0.4.4",
|
||||||
|
"weezl",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "weezl"
|
||||||
|
version = "0.1.4"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "4a32b378380f4e9869b22f0b5177c68a5519f03b3454fde0b291455ddbae266c"
|
||||||
|
|
|
||||||
|
|
@ -1,12 +1,13 @@
|
||||||
[package]
|
[package]
|
||||||
name = "mandelbrot_set"
|
name = "mandelbrot_set"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
authors = ["Nilstrieb <nils.heydecker@gmx.ch>"]
|
authors = ["Nilstrieb <nilstrieb@gmail.com>"]
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
|
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
image = "0.23.14"
|
||||||
|
|
||||||
[profile.dev]
|
[profile.dev]
|
||||||
opt-level = 3
|
opt-level = 3
|
||||||
|
|
|
||||||
BIN
img.png
Normal file
BIN
img.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 383 KiB |
134
src/lib.rs
134
src/lib.rs
|
|
@ -2,28 +2,39 @@ use std::error::Error;
|
||||||
use std::time::SystemTime;
|
use std::time::SystemTime;
|
||||||
use std::ops::{Add, Mul};
|
use std::ops::{Add, Mul};
|
||||||
use std::env::Args;
|
use std::env::Args;
|
||||||
use std::fmt::{Display, Formatter, Pointer};
|
use std::fmt::{Display, Formatter};
|
||||||
|
use image::{RgbImage, ImageBuffer, ImageResult};
|
||||||
|
|
||||||
pub fn run(config: Config) -> Result<String, Box<dyn Error>> {
|
pub fn run(config: Config) -> Result<String, Box<dyn Error>> {
|
||||||
println!("w={}", config.width);
|
|
||||||
|
|
||||||
let start_time = SystemTime::now();
|
let start_time = SystemTime::now();
|
||||||
|
let debug = config.debug;
|
||||||
|
|
||||||
let coords = calculate_sample_points(&config);
|
let coords = calculate_sample_points(&config);
|
||||||
println!("coords done after: {}μs", start_time.elapsed()?.as_micros());
|
if debug { println!("sample points calculated after: {}μs", start_time.elapsed()?.as_micros()); }
|
||||||
|
|
||||||
let result = check_whole_mandelbrot(&coords, config.iterations, config.threshold);
|
let result = check_whole_mandelbrot(&coords, config.iterations, config.threshold);
|
||||||
let draw = draw(result, config.iterations);
|
if debug { println!("calculated after: {}ms", start_time.elapsed()?.as_millis()); }
|
||||||
println!("{}", draw);
|
|
||||||
|
|
||||||
println!("Total Time: {}ms", start_time.elapsed()?.as_millis());
|
if config.is_image {
|
||||||
Ok(String::from("hi"))
|
create_image(&result, config.iterations, "img.png")?;
|
||||||
|
if debug { println!("image created after: {}ms", start_time.elapsed()?.as_millis()); }
|
||||||
|
}
|
||||||
|
if config.is_console {
|
||||||
|
let draw = draw(&result, config.iterations);
|
||||||
|
println!("{}", draw);
|
||||||
|
if debug { println!("drawn after: {}μs", start_time.elapsed()?.as_micros()); }
|
||||||
|
}
|
||||||
|
|
||||||
|
if debug { println!("Total Time: {}ms", start_time.elapsed()?.as_millis()); }
|
||||||
|
Ok(String::new())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn calculate_sample_points(config: &Config) -> Vec<Vec<CNumber>> {
|
fn calculate_sample_points(config: &Config) -> Vec<Vec<CNumber>> {
|
||||||
let start_time = SystemTime::now();
|
let height = if config.is_image {
|
||||||
|
config.width as f64 * 2.0 / 3.0
|
||||||
let height = config.width as f64 * 0.2;
|
} else {
|
||||||
|
config.width as f64 * 0.2
|
||||||
|
};
|
||||||
|
|
||||||
let step_size_x = 3.0 / config.width;
|
let step_size_x = 3.0 / config.width;
|
||||||
let step_size_y = 2.0 / height;
|
let step_size_y = 2.0 / height;
|
||||||
|
|
@ -34,8 +45,6 @@ fn calculate_sample_points(config: &Config) -> Vec<Vec<CNumber>> {
|
||||||
let mut coords: Vec<Vec<CNumber>> =
|
let mut coords: Vec<Vec<CNumber>> =
|
||||||
vec![vec![CNumber::new(0.0, 0.0); config.width as usize]; height as usize];
|
vec![vec![CNumber::new(0.0, 0.0); config.width as usize]; height as usize];
|
||||||
|
|
||||||
println!("Allocated sample vector after {}μs", start_time.elapsed().unwrap().as_micros());
|
|
||||||
|
|
||||||
for i in 0..config.width as usize {
|
for i in 0..config.width as usize {
|
||||||
for j in 0..height as usize {
|
for j in 0..height as usize {
|
||||||
coords[j][i].real = offset_x + step_size_x * i as f64;
|
coords[j][i].real = offset_x + step_size_x * i as f64;
|
||||||
|
|
@ -47,30 +56,23 @@ fn calculate_sample_points(config: &Config) -> Vec<Vec<CNumber>> {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn check_whole_mandelbrot(nums: &Vec<Vec<CNumber>>, iter: i32, threshold: f64) -> Vec<Vec<i32>> {
|
fn check_whole_mandelbrot(nums: &Vec<Vec<CNumber>>, iter: i32, threshold: f64) -> Vec<Vec<i32>> {
|
||||||
let start_time = SystemTime::now();
|
|
||||||
println!("Started calculating");
|
|
||||||
|
|
||||||
let height = nums.len();
|
let height = nums.len();
|
||||||
let width = nums[0].len();
|
let width = nums[0].len();
|
||||||
|
|
||||||
let mut result: Vec<Vec<i32>> = vec![vec![0; nums[0].len()]; nums.len()];
|
let mut result: Vec<Vec<i32>> = vec![vec![0; nums[0].len()]; nums.len()];
|
||||||
|
|
||||||
|
|
||||||
for i in 0..height {
|
for i in 0..height {
|
||||||
for j in 0..width {
|
for j in 0..width {
|
||||||
result[i][j] = check_mandelbrot(&nums[i][j], iter, threshold);
|
result[i][j] = check_mandelbrot(&nums[i][j], iter, threshold);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
println!("{:.2} of 100%", i as f64 / height as f64 * 100.0);
|
||||||
}
|
}
|
||||||
|
|
||||||
println!("Calculated results after {}ms", start_time.elapsed().unwrap().as_millis());
|
|
||||||
|
|
||||||
|
|
||||||
result
|
result
|
||||||
}
|
}
|
||||||
|
|
||||||
fn check_mandelbrot(number: &CNumber, iter: i32, threshold: f64) -> i32 {
|
fn check_mandelbrot(number: &CNumber, iter: i32, threshold: f64) -> i32 {
|
||||||
//let start_time = SystemTime::now();
|
|
||||||
|
|
||||||
let mut n = CNumber::new(0.0, 0.0);
|
let mut n = CNumber::new(0.0, 0.0);
|
||||||
let c = number;
|
let c = number;
|
||||||
|
|
||||||
|
|
@ -90,22 +92,35 @@ fn check_mandelbrot(number: &CNumber, iter: i32, threshold: f64) -> i32 {
|
||||||
static HIGH: &str = "#";
|
static HIGH: &str = "#";
|
||||||
static LOW: &str = " ";
|
static LOW: &str = " ";
|
||||||
|
|
||||||
fn draw(values: Vec<Vec<i32>>, iterations: i32) -> String {
|
fn draw(values: &Vec<Vec<i32>>, iterations: i32) -> String {
|
||||||
let start_time = SystemTime::now();
|
|
||||||
let mut out = String::new();
|
let mut out = String::new();
|
||||||
|
|
||||||
for line in values {
|
for line in values {
|
||||||
for char in line {
|
for char in line {
|
||||||
out += if char < iterations { LOW } else { HIGH };
|
out += if char < &iterations { LOW } else { HIGH };
|
||||||
}
|
}
|
||||||
out += "\n";
|
out += "\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
println!("Finished drawing after {}μs", start_time.elapsed().unwrap().as_micros());
|
|
||||||
|
|
||||||
out
|
out
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn create_image(values: &Vec<Vec<i32>>, iterations: i32, path: &str) -> ImageResult<()> {
|
||||||
|
let w = values[0].len() as u32;
|
||||||
|
let h = values.len() as u32;
|
||||||
|
|
||||||
|
let mut image: RgbImage = ImageBuffer::new(w as u32, h as u32);
|
||||||
|
|
||||||
|
for y in 0..h {
|
||||||
|
for x in 0..w {
|
||||||
|
let val = values[y as usize][x as usize];
|
||||||
|
*image.get_pixel_mut(x, y) = if val < iterations { image::Rgb([255, 255, 255]) } else { image::Rgb([0, 0, 0]) };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
image.save(path)
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Copy, Clone, Debug, PartialEq)]
|
#[derive(Copy, Clone, Debug, PartialEq)]
|
||||||
struct CNumber {
|
struct CNumber {
|
||||||
real: f64,
|
real: f64,
|
||||||
|
|
@ -148,29 +163,46 @@ pub struct Config {
|
||||||
//-- calculated
|
//-- calculated
|
||||||
center: CNumber,
|
center: CNumber,
|
||||||
iterations: i32,
|
iterations: i32,
|
||||||
|
is_image: bool,
|
||||||
|
is_console: bool,
|
||||||
|
image_path: String,
|
||||||
|
debug: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Config {
|
impl Config {
|
||||||
pub fn from(args: Args) -> Result<Config, Box<dyn Error>> {
|
pub fn from(args: Args) -> Result<Config, Box<dyn Error>> {
|
||||||
let mut config = Config::default();
|
let mut config = Config::default();
|
||||||
|
|
||||||
for arg in args {
|
for arg in args.into_iter().skip(1) {
|
||||||
if arg.contains("=") {
|
let mut split = arg.split("=");
|
||||||
let mut split = arg.split("=");
|
let key = split.next();
|
||||||
let key = split.next();
|
let value = split.next();
|
||||||
let value = split.next();
|
|
||||||
|
|
||||||
let value_f64: f64 = value.ok_or_else(||PropertyError {msg: format!("Error while parsing argument {}", arg)})?.parse()?;
|
match value {
|
||||||
config.set_value(key, value_f64);
|
None => config.set_value_flag(key)?,
|
||||||
println!("k={}, v={}, v64={}", key.unwrap(), value.unwrap(), value_f64);
|
Some(_) => config.set_value_value(key, value, &arg)?
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(config)
|
Ok(config)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_value(&mut self, key: Option<&str>, value: f64) -> Result<(), Box<dyn Error>> {
|
fn set_value_value(&mut self, key: Option<&str>, value: Option<&str>, arg: &String) -> Result<(), Box<dyn Error>> {
|
||||||
println!("setting arg value");
|
let val = value.ok_or_else(|| PropertyError { msg: format!("Error while parsing argument {}", arg) })?;
|
||||||
|
|
||||||
|
match key {
|
||||||
|
Some("path") | Some("p") =>
|
||||||
|
self.image_path = String::from(val),
|
||||||
|
_ => {
|
||||||
|
let value_f64: f64 = val.parse()?;
|
||||||
|
self.set_value_f64(key, value_f64);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn set_value_f64(&mut self, key: Option<&str>, value: f64) -> Result<(), Box<dyn Error>> {
|
||||||
match key {
|
match key {
|
||||||
Some("iter") | Some("iterations") =>
|
Some("iter") | Some("iterations") =>
|
||||||
self.iterations = value as i32,
|
self.iterations = value as i32,
|
||||||
|
|
@ -180,17 +212,33 @@ impl Config {
|
||||||
self.width = value,
|
self.width = value,
|
||||||
Some("quality") | Some("q") =>
|
Some("quality") | Some("q") =>
|
||||||
self.iterations = value as i32,
|
self.iterations = value as i32,
|
||||||
|
|
||||||
_ => return Err(Box::new(PropertyError { msg: format!("Property not found: {}", key.unwrap_or_else(|| "")) }))
|
_ => return Err(Box::new(PropertyError { msg: format!("Property not found: {}", key.unwrap_or_else(|| "")) }))
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn default() -> Config {
|
fn set_value_flag(&mut self, key: Option<&str>) -> Result<(), Box<dyn Error>> {
|
||||||
Config::new(1, 3, 100, 100.0)
|
match key {
|
||||||
|
Some("img") | Some("image") =>
|
||||||
|
self.is_image = true,
|
||||||
|
Some("console") | Some("cli") =>
|
||||||
|
self.is_console = true,
|
||||||
|
Some("debug") | Some("dbg") =>
|
||||||
|
self.debug = true,
|
||||||
|
_ => return Err(Box::new(PropertyError { msg: format!("Property not found: {}", key.unwrap_or_else(|| "")) }))
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn new(point_number: usize, quality: i32, width: i32, threshold: f32) -> Config {
|
|
||||||
|
pub fn default() -> Config {
|
||||||
|
Config::new(1, 3, 100, 100.0, false, String::from("img.png"), true, false)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn new(point_number: usize, quality: i32, width: i32, threshold: f32, is_image: bool, image_path: String, is_console: bool, debug: bool) -> Config {
|
||||||
let interesting_points = vec![CNumber::new(-0.75, 0.0), CNumber::new(-0.77568377, 0.13646737)];
|
let interesting_points = vec![CNumber::new(-0.75, 0.0), CNumber::new(-0.77568377, 0.13646737)];
|
||||||
let center = interesting_points[point_number];
|
let center = interesting_points[point_number];
|
||||||
let iterations = config_iter_from_quality(quality);
|
let iterations = config_iter_from_quality(quality);
|
||||||
|
|
@ -200,6 +248,10 @@ impl Config {
|
||||||
center,
|
center,
|
||||||
iterations,
|
iterations,
|
||||||
threshold: threshold as f64,
|
threshold: threshold as f64,
|
||||||
|
is_image,
|
||||||
|
is_console,
|
||||||
|
image_path,
|
||||||
|
debug,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue