Skip to content

Commit

Permalink
Add more tests and documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
rosslh committed Feb 11, 2024
1 parent 6b76457 commit 967cd9d
Show file tree
Hide file tree
Showing 391 changed files with 1,030 additions and 40 deletions.
7 changes: 4 additions & 3 deletions client/app/main.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import "./static";
import debounce from "lodash/debounce";
import * as L from "leaflet";
import MandelbrotMap from "./MandelbrotMap";

type MandelbrotConfig = {
Expand Down Expand Up @@ -228,7 +227,7 @@ const map = new MandelbrotMap({
});
handleDom(map);

window.addEventListener("load", function () {
const setConfigFromUrl = () => {
const queryParams = new URLSearchParams(window.location.search);
const re = queryParams.get("re");
const im = queryParams.get("im");
Expand Down Expand Up @@ -278,4 +277,6 @@ window.addEventListener("load", function () {
map.refresh();
}
}
});
};

window.addEventListener("load", setConfigFromUrl);
47 changes: 44 additions & 3 deletions mandelbrot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,18 @@ use wasm_bindgen::prelude::*;
#[global_allocator]
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;

// how many iterations does it take to escape?
/// Calculates the number of iterations it takes for a complex number to escape the set,
/// based on the given coordinates, maximum iterations, escape radius, and exponent.
///
/// # Parameters
/// - `x`: The real part of the complex number.
/// - `y`: The imaginary part of the complex number.
/// - `max_iterations`: The maximum number of iterations to perform.
/// - `escape_radius`: The escape radius beyond which the function considers the number to have escaped.
/// - `exponent`: The exponent used in the escape time algorithm.
///
/// # Returns
/// A tuple containing the number of iterations it took to escape and the final value of the complex number.
fn get_escape_iterations(
x: f64,
y: f64,
Expand Down Expand Up @@ -44,8 +55,20 @@ fn get_escape_iterations(
(iter, z)
}

// Mandelbrot set is simply connected.
// So if the border of the rectangle is in the set, we know the rest of it is too.
/// Checks if a rectangle, defined by ranges of real and imaginary values, is completely within the Mandelbrot set.
/// This is determined using a specified maximum number of iterations, escape radius, and exponent for the escape time
/// algorithm. The Mandelbrot set is simply connected, meaning if the rectangle's border is in the set, the entire
/// rectangle is guaranteed to be in the set as well.
///
/// # Parameters
/// - `re_range`: A range of real values to check.
/// - `im_range`: A range of imaginary values to check.
/// - `max_iterations`: The maximum number of iterations to perform.
/// - `escape_radius`: The escape radius beyond which the function considers a point to have escaped the set.
/// - `exponent`: The exponent used in the escape time algorithm.
///
/// # Returns
/// `true` if the entire rectangle is within the Mandelbrot set, `false` otherwise.
fn rect_in_set(
re_range: itertools_num::Linspace<f64>,
im_range: itertools_num::Linspace<f64>,
Expand Down Expand Up @@ -138,6 +161,22 @@ static REVERSE_COLOR_PALETTES: Lazy<HashMap<String, colorous::Gradient>> = Lazy:
map
});

/// Generates a tile of the Mandelbrot set given the bounds, number of iterations,
/// exponent for escape time algorithm, image side length, and color scheme.
///
/// # Parameters
/// - `re_min`: The minimum real value (left side of the image).
/// - `re_max`: The maximum real value (right side of the image).
/// - `im_min`: The minimum imaginary value (bottom of the image).
/// - `im_max`: The maximum imaginary value (top of the image).
/// - `max_iterations`: The maximum number of iterations to perform.
/// - `exponent`: The exponent used in the escape time algorithm.
/// - `image_side_length`: The length of one side of the square image, in pixels.
/// - `color_scheme`: The name of the color scheme to use.
/// - `_reverse_colors`: Whether to reverse the colors of the color scheme.
///
/// # Returns
/// A vector of bytes representing the RGBA color values of the image.
#[wasm_bindgen]
pub fn get_tile(
re_min: f64,
Expand Down Expand Up @@ -232,6 +271,8 @@ pub fn get_tile(
img
}

/// Initializes the module. This function is specifically designed to be called
/// from WebAssembly to perform necessary initializations.
#[wasm_bindgen]
pub fn init() {
utils::init();
Expand Down
76 changes: 52 additions & 24 deletions mandelbrot/src/lib_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@ use super::*;
#[cfg(test)]
mod lib_test {
const MAX_ITERATIONS: u32 = 200;
const SQUARES: [(f64, f64, f64, f64); 8] = [
const SQUARES: [(f64, f64, f64, f64); 6] = [
(-0.75, -0.25, 0.25, 0.75),
(-1.5, 0.0, -0.5, 0.5),
(-2.0, -1.0, 0.0, 1.0),
(-2.0, 0.5, -1.0, 1.0),
(-2.5, 1.0, -1.5, 1.5),
(0.0, 1.5, 0.0, 1.0),
(0.0, 2.0, 0.0, 2.0),
(
-0.38239270518262103,
-0.38239196302361966,
Expand Down Expand Up @@ -124,28 +122,58 @@ mod lib_test {

#[test]
fn test_get_tile_snapshot() {
for (index, &(x_min, x_max, y_min, y_max)) in SQUARES.iter().enumerate() {
let response = super::get_tile(
x_min,
x_max,
y_min,
y_max,
MAX_ITERATIONS,
2,
100,
"turbo".to_string(),
false,
);
insta::assert_snapshot!(format!("snapshot{}", index), format!("{:?}", response));
let max_iterations = [100, 200];
let exponents = [2, 3];
let image_side_lengths = [50, 100];
let color_schemes = ["turbo", "inferno"];
let reverse_color_options = [true, false];

for &max_iterations in max_iterations.iter() {
for &exponent in exponents.iter() {
for &image_side_length in image_side_lengths.iter() {
for color_scheme in color_schemes.iter() {
for &reverse_color in reverse_color_options.iter() {
for (index, &(x_min, x_max, y_min, y_max)) in SQUARES.iter().enumerate()
{
let response = super::get_tile(
x_min,
x_max,
y_min,
y_max,
max_iterations,
exponent,
image_side_length,
color_scheme.to_string(),
reverse_color,
);
let snapshot_name = format!(
"snapshot_{}_{}_{}_{}_{}_{}",
index,
max_iterations,
exponent,
image_side_length,
color_scheme,
reverse_color
);
insta::assert_snapshot!(
snapshot_name.clone(),
format!("{:?}", response)
);

if let Err(err) = image::save_buffer(
format!("./src/snapshots/snapshot{}.png", index),
&response,
100,
100,
image::ColorType::Rgba8,
) {
panic!("Failed to save image: {}", err);
let image_name = format!("./src/snapshots/{}.png", snapshot_name);
if let Err(err) = image::save_buffer(
image_name,
&response,
image_side_length as u32,
image_side_length as u32,
image::ColorType::Rgba8,
) {
panic!("Failed to save image: {}", err);
}
}
}
}
}
}
}
}
Expand Down

This file was deleted.

This file was deleted.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Binary file removed mandelbrot/src/snapshots/snapshot4.png
Binary file not shown.
Binary file removed mandelbrot/src/snapshots/snapshot6.png
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes
File renamed without changes
Loading

0 comments on commit 967cd9d

Please sign in to comment.