Skip to content

Commit

Permalink
Restyled by rustfmt (#37)
Browse files Browse the repository at this point in the history
Co-authored-by: Restyled.io <commits@restyled.io>
  • Loading branch information
restyled-io[bot] and restyled-commits authored Nov 15, 2023
1 parent fda40f7 commit dd81bc6
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 35 deletions.
18 changes: 10 additions & 8 deletions client/src/julia.rs
Original file line number Diff line number Diff line change
@@ -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.
///
Expand Down Expand Up @@ -36,27 +36,29 @@ pub fn generate_julia_set(fragment_task: FragmentTask) -> ImageBuffer<Rgb<u8>, 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
Expand Down
14 changes: 7 additions & 7 deletions client/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 },
},
};

Expand Down
13 changes: 8 additions & 5 deletions complex/src/complex_operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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,
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion complex/src/fractal_operations.rs
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
12 changes: 4 additions & 8 deletions complex/src/iterated_sinz_impl.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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();
Expand All @@ -34,9 +33,7 @@ impl FractalOperations for IteratedSinZDescriptor {

impl IteratedSinZOperations for IteratedSinZDescriptor {
fn new(c: Complex) -> Self {
Self {
c,
}
Self { c }
}

///Fixed to 50
Expand All @@ -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() {
Expand Down
6 changes: 2 additions & 4 deletions complex/src/julia_descriptor_impl.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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();
Expand All @@ -31,7 +30,6 @@ impl FractalOperations for JuliaDescriptor {
}
}


impl JuliaOperations for JuliaDescriptor {
fn new(c: Complex, divergence_threshold_square: f64) -> Self {
Self {
Expand All @@ -43,4 +41,4 @@ impl JuliaOperations for JuliaDescriptor {
fn divergence_threshold_square(&self) -> f64 {
self.divergence_threshold_square
}
}
}
4 changes: 2 additions & 2 deletions complex/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit dd81bc6

Please sign in to comment.