diff --git a/client/src/julia.rs b/client/src/julia.rs index a037c25..cf112e6 100644 --- a/client/src/julia.rs +++ b/client/src/julia.rs @@ -1,10 +1,10 @@ use complex::complex_operations::ComplexOperations; +use complex::fractal_operations::FractalOperations; +use complex::iterated_sinz_impl::IteratedSinZOperations; use image::{ImageBuffer, Rgb}; use shared::types::complex::Complex; use shared::types::fractal_descriptor::FractalType::{IteratedSinZ, Julia}; -use complex::iterated_sinz_impl::IteratedSinZOperations; use shared::types::messages::FragmentTask; -use complex::fractal_operations::FractalOperations; /// Generates an image of the Julia set fractal based on the provided fragment task. /// @@ -36,27 +36,29 @@ pub fn generate_julia_set(fragment_task: FragmentTask) -> ImageBuffer, V let scaled_y = y as f64 * scale_y + range.min.y; let complex_point = Complex::new(scaled_x, scaled_y); - let iterations = descriptor.iterate_complex_point(&complex_point, fragment_task.max_iteration); + let iterations = + descriptor.iterate_complex_point(&complex_point, fragment_task.max_iteration); *pixel = Rgb(color((iterations as f32) / 255.0)); } img } - ///Gets a number between 0 and 1 and return the color that correspond to its intensity fn color(intensity: f32) -> [u8; 3] { let brightness = (0.5, 0.5, 0.5); let bright_color = (0.5, 0.5, 0.5); let frequency_change = (1.0, 1.0, 1.0); let base_color = (0.1, 0.2, 0.3); - let r = bright_color.0 * (6.28318 * (frequency_change.0 * intensity + base_color.0)).cos() + brightness.0; - let g = bright_color.1 * (6.28318 * (frequency_change.1 * intensity + base_color.1)).cos() + brightness.1; - let b = bright_color.2 * (6.28318 * (frequency_change.2 * intensity + base_color.2)).cos() + brightness.2; + let r = bright_color.0 * (6.28318 * (frequency_change.0 * intensity + base_color.0)).cos() + + brightness.0; + let g = bright_color.1 * (6.28318 * (frequency_change.1 * intensity + base_color.1)).cos() + + brightness.1; + let b = bright_color.2 * (6.28318 * (frequency_change.2 * intensity + base_color.2)).cos() + + brightness.2; [(255.0 * r) as u8, (255.0 * g) as u8, (255.0 * b) as u8] } - fn iterations_to_color(mut iterations: u16, max_iterations: u16) -> u8 { if iterations == max_iterations { 0 diff --git a/client/src/main.rs b/client/src/main.rs index 06f11de..82aa124 100644 --- a/client/src/main.rs +++ b/client/src/main.rs @@ -5,7 +5,7 @@ use crate::image::open_image; use crate::julia::generate_julia_set; use shared::types::filesystem::FileExtension; -use shared::types::fractal_descriptor::FractalType::{IteratedSinZ}; +use shared::types::fractal_descriptor::FractalType::IteratedSinZ; use shared::types::fractal_descriptor::{FractalDescriptor, IteratedSinZDescriptor}; use shared::types::messages::FragmentTask; use shared::types::point::Point; @@ -28,17 +28,17 @@ fn main() { }, fractal: FractalDescriptor { fractal_type: IteratedSinZ(IteratedSinZDescriptor { - c: Complex { - re: 0.2, - im: 1.0, - } + c: Complex { re: 0.2, im: 1.0 }, }), }, max_iteration: 64, resolution: Resolution { nx: 1080, ny: 1920 }, range: Range { - min: Point { x: -2.0, y: -3.55556 }, - max: Point { x: 2.0, y: 3.55556, }, + min: Point { + x: -2.0, + y: -3.55556, + }, + max: Point { x: 2.0, y: 3.55556 }, }, }; diff --git a/complex/src/complex_operations.rs b/complex/src/complex_operations.rs index 7bebd87..4fc09a6 100644 --- a/complex/src/complex_operations.rs +++ b/complex/src/complex_operations.rs @@ -61,7 +61,10 @@ impl ComplexOperations for Complex { ///Re(Sin(z)) = Sin(a) * Cosh(b) ///Im(Sin(z)) = Cos(a) * Sinh(b) fn sin(&self) -> Self { - Complex::new(self.re.sin() * self.im.cosh(), self.re.cos() * self.im.sinh()) + Complex::new( + self.re.sin() * self.im.cosh(), + self.re.cos() * self.im.sinh(), + ) } ///exp(a+ib) = exp(a) * exp(ib) = exp(a) * (cos(b) + isin(b)) @@ -74,8 +77,8 @@ impl ComplexOperations for Complex { #[cfg(test)] mod complex_tests { - use std::f64::consts::{E, PI}; use super::*; + use std::f64::consts::{E, PI}; // Series of tests for each operation, verifying the correctness // of complex number arithmetic such as addition, subtraction, @@ -172,7 +175,7 @@ mod complex_tests { #[test] fn test_exp_where_im_equal_half_pi() { - let a = Complex::new(0.0, PI/2.0); + let a = Complex::new(0.0, PI / 2.0); let result = a.exp(); assert_eq!(round(&result.re), 0.0); @@ -181,7 +184,7 @@ mod complex_tests { #[test] fn test_exp_where_im_equal_minus_half_pi() { - let a = Complex::new(0.0, -PI/2.0); + let a = Complex::new(0.0, -PI / 2.0); let result = a.exp(); assert_eq!(round(&result.re), 0.0); @@ -229,7 +232,7 @@ mod complex_tests { #[test] fn test_exp_with_reel_2() { - let z = Complex::new(2.0, PI/4.0); + let z = Complex::new(2.0, PI / 4.0); let result = z.exp(); let exp_value = E.powi(2) / 2.0_f64.sqrt(); diff --git a/complex/src/fractal_operations.rs b/complex/src/fractal_operations.rs index f019d5c..91c20b7 100644 --- a/complex/src/fractal_operations.rs +++ b/complex/src/fractal_operations.rs @@ -1,6 +1,6 @@ +use crate::complex_operations::ComplexOperations; use shared::types::complex::Complex; use shared::types::resolution::Resolution; -use crate::complex_operations::ComplexOperations; pub trait FractalOperations { /// Converts pixel coordinates to a complex number based on the resolution. diff --git a/complex/src/iterated_sinz_impl.rs b/complex/src/iterated_sinz_impl.rs index a465428..1437098 100644 --- a/complex/src/iterated_sinz_impl.rs +++ b/complex/src/iterated_sinz_impl.rs @@ -1,7 +1,7 @@ use crate::complex_operations::ComplexOperations; +use crate::fractal_operations::FractalOperations; use shared::types::complex::Complex; use shared::types::fractal_descriptor::IteratedSinZDescriptor; -use crate::fractal_operations::FractalOperations; /// Provides operations specific to the Iterated Sin(z) fractal. pub trait IteratedSinZOperations { @@ -11,7 +11,6 @@ pub trait IteratedSinZOperations { fn max_iteration(&self) -> u16; } - impl FractalOperations for IteratedSinZDescriptor { fn iterate_complex_point(&self, complex_point: &Complex, max_iteration: u16) -> u16 { let mut z = complex_point.clone(); @@ -34,9 +33,7 @@ impl FractalOperations for IteratedSinZDescriptor { impl IteratedSinZOperations for IteratedSinZDescriptor { fn new(c: Complex) -> Self { - Self { - c, - } + Self { c } } ///Fixed to 50 @@ -45,13 +42,12 @@ impl IteratedSinZOperations for IteratedSinZDescriptor { } } - #[cfg(test)] mod iterated_sinz_tests { - use shared::types::complex::Complex; - use shared::types::fractal_descriptor::IteratedSinZDescriptor; use crate::complex_operations::ComplexOperations; use crate::iterated_sinz_impl::IteratedSinZOperations; + use shared::types::complex::Complex; + use shared::types::fractal_descriptor::IteratedSinZDescriptor; #[test] fn test_max_iteration_return_50() { diff --git a/complex/src/julia_descriptor_impl.rs b/complex/src/julia_descriptor_impl.rs index 56d03be..c9bacd2 100644 --- a/complex/src/julia_descriptor_impl.rs +++ b/complex/src/julia_descriptor_impl.rs @@ -1,7 +1,7 @@ use crate::complex_operations::ComplexOperations; +use crate::fractal_operations::FractalOperations; use shared::types::complex::Complex; use shared::types::fractal_descriptor::JuliaDescriptor; -use crate::fractal_operations::FractalOperations; /// Provides operations specific to the Julia fractal. pub trait JuliaOperations { @@ -12,7 +12,6 @@ pub trait JuliaOperations { fn divergence_threshold_square(&self) -> f64; } - impl FractalOperations for JuliaDescriptor { fn iterate_complex_point(&self, complex_point: &Complex, max_iteration: u16) -> u16 { let mut z = complex_point.clone(); @@ -31,7 +30,6 @@ impl FractalOperations for JuliaDescriptor { } } - impl JuliaOperations for JuliaDescriptor { fn new(c: Complex, divergence_threshold_square: f64) -> Self { Self { @@ -43,4 +41,4 @@ impl JuliaOperations for JuliaDescriptor { fn divergence_threshold_square(&self) -> f64 { self.divergence_threshold_square } -} \ No newline at end of file +} diff --git a/complex/src/lib.rs b/complex/src/lib.rs index 70e8fc6..61b7ab1 100644 --- a/complex/src/lib.rs +++ b/complex/src/lib.rs @@ -1,4 +1,4 @@ pub mod complex_operations; -pub mod julia_descriptor_impl; -pub mod iterated_sinz_impl; pub mod fractal_operations; +pub mod iterated_sinz_impl; +pub mod julia_descriptor_impl;