mirror of
https://github.com/Noratrieb/mandelbrot-rust.git
synced 2026-01-14 15:25:07 +01:00
draw
This commit is contained in:
parent
764d2ff644
commit
be61bdeaa5
2 changed files with 47 additions and 11 deletions
6
.idea/discord.xml
generated
Normal file
6
.idea/discord.xml
generated
Normal file
|
|
@ -0,0 +1,6 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="DiscordProjectSettings">
|
||||||
|
<option name="show" value="PROJECT_FILES" />
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
52
src/lib.rs
52
src/lib.rs
|
|
@ -12,15 +12,15 @@ pub fn main(config: Config) -> Result<String, Box<dyn Error>> {
|
||||||
Ok(String::from("hi"))
|
Ok(String::from("hi"))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn calculate_sample_points(config: &Config) -> Box<Vec<Vec<CNumber>>> {
|
fn calculate_sample_points(config: &Config) -> Vec<Vec<CNumber>> {
|
||||||
let step_size_x = 3.0 / config.width;
|
let step_size_x = 3.0 / config.width;
|
||||||
let step_size_y = 2.0 / config.height;
|
let step_size_y = 2.0 / config.height;
|
||||||
|
|
||||||
let offset_x = config.center.real - config.width / 2.0 * step_size_x;
|
let offset_x = config.center.real - config.width / 2.0 * step_size_x;
|
||||||
let offset_y = -(config.center.imag - config.height / 2.0 * step_size_y);
|
let offset_y = -(config.center.imag - config.height / 2.0 * step_size_y);
|
||||||
|
|
||||||
let mut coords: Box<Vec<Vec<CNumber>>> =
|
let mut coords: Vec<Vec<CNumber>> =
|
||||||
Box::from(vec![vec![CNumber::new(0.0, 0.0); config.width as usize]; config.height as usize]);
|
vec![vec![CNumber::new(0.0, 0.0); config.width as usize]; config.height as usize];
|
||||||
|
|
||||||
for i in 0..config.width as usize {
|
for i in 0..config.width as usize {
|
||||||
for j in 0..config.height as usize {
|
for j in 0..config.height as usize {
|
||||||
|
|
@ -37,10 +37,10 @@ fn check_mandelbrot(number: &CNumber, iter: i32, threshold: f64) -> i32 {
|
||||||
let mut n = CNumber::new(0.0, 0.0);
|
let mut n = CNumber::new(0.0, 0.0);
|
||||||
let c = number;
|
let c = number;
|
||||||
|
|
||||||
n = n + c;
|
n = n + *c;
|
||||||
|
|
||||||
for _ in 0..iter {
|
for _ in 0..iter {
|
||||||
n = n * n + c;
|
n = n * n + *c;
|
||||||
}
|
}
|
||||||
|
|
||||||
if n.real < threshold && n.imag < threshold {
|
if n.real < threshold && n.imag < threshold {
|
||||||
|
|
@ -50,9 +50,16 @@ fn check_mandelbrot(number: &CNumber, iter: i32, threshold: f64) -> i32 {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn draw(values: Box<Vec<Vec<i32>>>) -> String {
|
fn draw(values: Vec<Vec<&str>>) -> String {
|
||||||
let mut lines = vec![];
|
let mut out = String::new();
|
||||||
|
|
||||||
|
for line in values {
|
||||||
|
for char in line {
|
||||||
|
out += char;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
out
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Copy, Clone, Debug)]
|
#[derive(Copy, Clone, Debug)]
|
||||||
|
|
@ -67,10 +74,10 @@ impl CNumber {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Add for &CNumber {
|
impl Add for CNumber {
|
||||||
type Output = CNumber;
|
type Output = CNumber;
|
||||||
|
|
||||||
fn add(&self, b: CNumber) -> Self::Output {
|
fn add(self, b: CNumber) -> Self::Output {
|
||||||
let real = self.real + b.real;
|
let real = self.real + b.real;
|
||||||
let imag = self.imag + b.imag;
|
let imag = self.imag + b.imag;
|
||||||
|
|
||||||
|
|
@ -78,7 +85,7 @@ impl Add for &CNumber {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Mul for &CNumber {
|
impl Mul for CNumber {
|
||||||
type Output = CNumber;
|
type Output = CNumber;
|
||||||
|
|
||||||
fn mul(self, b: Self) -> Self::Output {
|
fn mul(self, b: Self) -> Self::Output {
|
||||||
|
|
@ -128,7 +135,7 @@ impl Config {
|
||||||
|
|
||||||
//#[cfg(tests)]
|
//#[cfg(tests)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use crate::{Config, calculate_sample_points};
|
use crate::{Config, calculate_sample_points, check_mandelbrot, CNumber, draw};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn correct_size_points() {
|
fn correct_size_points() {
|
||||||
|
|
@ -139,4 +146,27 @@ mod tests {
|
||||||
result[0][0];
|
result[0][0];
|
||||||
result[0][99];
|
result[0][99];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn check_mandelbrot_test() {
|
||||||
|
let iter = 1000;
|
||||||
|
let thr = 100.0;
|
||||||
|
|
||||||
|
assert_eq!(check_mandelbrot(&CNumber::new(1.0, 1.0), iter, thr), 0);
|
||||||
|
assert_eq!(check_mandelbrot(&CNumber::new(2.0, 0.0), iter, thr), 0);
|
||||||
|
assert_eq!(check_mandelbrot(&CNumber::new(0.0, 0.0), iter, thr), 1);
|
||||||
|
assert_eq!(check_mandelbrot(&CNumber::new(0.0, 3.0), iter, thr), 0);
|
||||||
|
assert_eq!(check_mandelbrot(&CNumber::new(0.8, 0.0), iter, thr), 0);
|
||||||
|
assert_eq!(check_mandelbrot(&CNumber::new(0.7, 0.0), iter, thr), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn draw_test() {
|
||||||
|
let vector = vec![vec!["a", "b", "c", "d", "e"]; 2];
|
||||||
|
let out = draw(vector);
|
||||||
|
println!("{}", out);
|
||||||
|
assert_eq!(out, "\
|
||||||
|
abcde\
|
||||||
|
abcde")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue