Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
b1ad456
Remove QuickSelect doctest
defaude Oct 5, 2021
4b1799e
Remove AverageMedian doctest
defaude Oct 5, 2021
852e16f
Migrate doctest for BinaryExponentiationRecursive.js
defaude Oct 5, 2021
25c29b6
Migrate doctest for EulersTotient.js
defaude Oct 5, 2021
7b8ec07
Migrate doctest for PrimeFactors.js
defaude Oct 5, 2021
e7836e4
Migrate doctest for BogoSort.js
defaude Oct 5, 2021
402ab5c
Migrate doctest for BeadSort.js
defaude Oct 5, 2021
3eff8fd
Migrate doctest for BucketSort.js
defaude Oct 5, 2021
9627cfb
Migrate doctest for CocktailShakerSort.js
defaude Oct 5, 2021
e0e3c19
Migrate doctest for MergeSort.js
defaude Oct 5, 2021
942f9fb
Migrate doctest for QuickSort.js
defaude Oct 5, 2021
3d48cbf
Migrate doctest for ReverseString.js
defaude Oct 5, 2021
dd619c9
Migrate doctest for ReverseString.js
defaude Oct 5, 2021
6b820f3
Migrate doctest for ValidateEmail.js
defaude Oct 5, 2021
db7f626
Migrate doctest for ConwaysGameOfLife.js
defaude Oct 5, 2021
f2aa40c
Remove TernarySearch doctest
defaude Oct 5, 2021
7f5d7fe
Migrate doctest for BubbleSort.js
defaude Oct 5, 2021
c3747e9
Remove doctest from CI and from dependencies
defaude Oct 5, 2021
8274f40
Migrate doctest for RgbHsvConversion.js
defaude Oct 6, 2021
a383356
Add --fix option to "standard" npm script
defaude Oct 6, 2021
bbeeefb
Migrate doctest for BreadthFirstSearch.js
defaude Oct 6, 2021
1244f3c
Migrate doctest for BreadthFirstShortestPath.js
defaude Oct 6, 2021
d019e85
Migrate doctest for EulerMethod.js
defaude Oct 6, 2021
bf68fdd
Migrate doctest for Mandelbrot.js
defaude Oct 6, 2021
cbece80
Migrate doctest for FloodFill.js
defaude Oct 6, 2021
59796a2
Migrate doctest for KochSnowflake.js
defaude Oct 6, 2021
a8ad3a3
Update npm lockfile
defaude Oct 6, 2021
78f5dc4
Update README and COMMITTING with a few bits & bobs regarding testing…
defaude Oct 6, 2021
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Migrate doctest for Mandelbrot.js
(also remove inline test driver code & moved manual drawing test into a *.manual-test.js)
  • Loading branch information
defaude committed Oct 6, 2021
commit bf68fdddbe362a4935efe49cd52c2b8e00283c8d
102 changes: 33 additions & 69 deletions Maths/Mandelbrot.js
Original file line number Diff line number Diff line change
@@ -1,54 +1,33 @@
/**
* The Mandelbrot set is the set of complex numbers "c" for which the series "z_(n+1) = z_n * z_n +
* c" does not diverge, i.e. remains bounded. Thus, a complex number "c" is a member of the
* Mandelbrot set if, when starting with "z_0 = 0" and applying the iteration repeatedly, the
* absolute value of "z_n" remains bounded for all "n > 0". Complex numbers can be written as "a +
* b*i": "a" is the real component, usually drawn on the x-axis, and "b*i" is the imaginary
* component, usually drawn on the y-axis. Most visualizations of the Mandelbrot set use a
* color-coding to indicate after how many steps in the series the numbers outside the set cross the
* divergence threshold. Images of the Mandelbrot set exhibit an elaborate and infinitely
* complicated boundary that reveals progressively ever-finer recursive detail at increasing
* magnifications, making the boundary of the Mandelbrot set a fractal curve. (description adapted
* from https://en.wikipedia.org/wiki/Mandelbrot_set ) (see also
* https://en.wikipedia.org/wiki/Plotting_algorithms_for_the_Mandelbrot_set )
*/

/*
Doctests
Test black and white
Pixel outside the Mandelbrot set should be white.
Pixel inside the Mandelbrot set should be black.
> getRGBData(800, 600, -0.6, 0, 3.2, 50, false)[0][0]
[255, 255, 255]
> getRGBData(800, 600, -0.6, 0, 3.2, 50, false)[400][300]
[0, 0, 0]

Test color-coding
Pixel distant to the Mandelbrot set should be red.
Pixel inside the Mandelbrot set should be black.
> getRGBData(800, 600, -0.6, 0, 3.2, 50, true)[0][0]
[255, 0, 0]
> getRGBData(800, 600, -0.6, 0, 3.2, 50, true)[400][300]
[0, 0, 0]
*/

/**
* Method to generate the image of the Mandelbrot set. Two types of coordinates are used:
* image-coordinates that refer to the pixels and figure-coordinates that refer to the complex
* numbers inside and outside the Mandelbrot set. The figure-coordinates in the arguments of this
* method determine which section of the Mandelbrot set is viewed. The main area of the Mandelbrot
* set is roughly between "-1.5 < x < 0.5" and "-1 < y < 1" in the figure-coordinates.
* Method to generate the image of the Mandelbrot set.
*
* Two types of coordinates are used: image-coordinates that refer to the pixels and figure-coordinates that refer to
* the complex numbers inside and outside the Mandelbrot set. The figure-coordinates in the arguments of this method
* determine which section of the Mandelbrot set is viewed. The main area of the Mandelbrot set is roughly between
* "-1.5 < x < 0.5" and "-1 < y < 1" in the figure-coordinates.
*
* The Mandelbrot set is the set of complex numbers "c" for which the series "z_(n+1) = z_n * z_n + c" does not diverge,
* i.e. remains bounded. Thus, a complex number "c" is a member of the Mandelbrot set if, when starting with "z_0 = 0"
* and applying the iteration repeatedly, the absolute value of "z_n" remains bounded for all "n > 0". Complex numbers
* can be written as "a + b*i": "a" is the real component, usually drawn on the x-axis, and "b*i" is the imaginary
* component, usually drawn on the y-axis. Most visualizations of the Mandelbrot set use a color-coding to indicate
* after how many steps in the series the numbers outside the set cross the divergence threshold. Images of the
* Mandelbrot set exhibit an elaborate and infinitely complicated boundary that reveals progressively ever-finer
* recursive detail at increasing magnifications, making the boundary of the Mandelbrot set a fractal curve.
*
* (description adapted from https://en.wikipedia.org/wiki/Mandelbrot_set)
* @see https://en.wikipedia.org/wiki/Plotting_algorithms_for_the_Mandelbrot_set
*
* @param {number} imageWidth The width of the rendered image.
* @param {number} imageHeight The height of the rendered image.
* @param {number} figureCenterX The x-coordinate of the center of the figure.
* @param {number} figureCenterY The y-coordinate of the center of the figure.
* @param {number} figureWidth The width of the figure.
* @param {number} maxStep Maximum number of steps to check for divergent behavior.
* @param {number} useDistanceColorCoding Render in color or black and white.
* @param {boolean} useDistanceColorCoding Render in color or black and white.
* @return {object} The RGB-data of the rendered Mandelbrot set.
*/
function getRGBData (
export function getRGBData (
imageWidth = 800,
imageHeight = 600,
figureCenterX = -0.6,
Expand Down Expand Up @@ -83,18 +62,19 @@ function getRGBData (

// color the corresponding pixel based on the selected coloring-function
rgbData[imageX][imageY] =
useDistanceColorCoding
? colorCodedColorMap(distance)
: blackAndWhiteColorMap(distance)
useDistanceColorCoding
? colorCodedColorMap(distance)
: blackAndWhiteColorMap(distance)
}
}

return rgbData
}

/**
* Black and white color-coding that ignores the relative distance. The Mandelbrot set is black,
* everything else is white.
* Black and white color-coding that ignores the relative distance.
*
* The Mandelbrot set is black, everything else is white.
*
* @param {number} distance Distance until divergence threshold
* @return {object} The RGB-value corresponding to the distance.
Expand All @@ -104,7 +84,9 @@ function blackAndWhiteColorMap (distance) {
}

/**
* Color-coding taking the relative distance into account. The Mandelbrot set is black.
* Color-coding taking the relative distance into account.
*
* The Mandelbrot set is black.
*
* @param {number} distance Distance until divergence threshold
* @return {object} The RGB-value corresponding to the distance.
Expand Down Expand Up @@ -145,11 +127,12 @@ function colorCodedColorMap (distance) {

/**
* Return the relative distance (ratio of steps taken to maxStep) after which the complex number
* constituted by this x-y-pair diverges. Members of the Mandelbrot set do not diverge so their
* distance is 1.
* constituted by this x-y-pair diverges.
*
* Members of the Mandelbrot set do not diverge so their distance is 1.
*
* @param {number} figureX The x-coordinate within the figure.
* @param {number} figureX The y-coordinate within the figure.
* @param {number} figureY The y-coordinate within the figure.
* @param {number} maxStep Maximum number of steps to check for divergent behavior.
* @return {number} The relative distance as the ratio of steps taken to maxStep.
*/
Expand All @@ -171,22 +154,3 @@ function getDistance (figureX, figureY, maxStep) {
}
return currentStep / (maxStep - 1)
}

// plot the results if the script is executed in a browser with a window-object
if (typeof window !== 'undefined') {
const rgbData = getRGBData()
const width = rgbData.length
const height = rgbData[0].length
const canvas = document.createElement('canvas')
canvas.width = width
canvas.height = height
const ctx = canvas.getContext('2d')
for (let x = 0; x < width; x++) {
for (let y = 0; y < height; y++) {
const rgb = rgbData[x][y]
ctx.fillStyle = 'rgb(' + rgb[0] + ',' + rgb[1] + ',' + rgb[2] + ')'
ctx.fillRect(x, y, 1, 1)
}
}
document.body.append(canvas)
}
20 changes: 20 additions & 0 deletions Maths/test/Mandelbrot.manual-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { getRGBData } from '../Mandelbrot'

// plot the results if the script is executed in a browser with a window-object
if (typeof window !== 'undefined') {
const rgbData = getRGBData()
const width = rgbData.length
const height = rgbData[0].length
const canvas = document.createElement('canvas')
canvas.width = width
canvas.height = height
const ctx = canvas.getContext('2d')
for (let x = 0; x < width; x++) {
for (let y = 0; y < height; y++) {
const rgb = rgbData[x][y]
ctx.fillStyle = 'rgb(' + rgb[0] + ',' + rgb[1] + ',' + rgb[2] + ')'
ctx.fillRect(x, y, 1, 1)
}
}
document.body.append(canvas)
}
21 changes: 21 additions & 0 deletions Maths/test/Mandelbrot.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { getRGBData } from '../Mandelbrot'

describe('Mandelbrot', () => {
it('should produce black pixels inside the set', () => {
const blackAndWhite = getRGBData(800, 600, -0.6, 0, 3.2, 50, false)
expect(blackAndWhite[400][300]).toEqual([0, 0, 0]) // black

const colorCoded = getRGBData(800, 600, -0.6, 0, 3.2, 50, true)
expect(colorCoded[400][300]).toEqual([0, 0, 0]) // black
})

it('should produce white pixels outside of the set', () => {
const blackAndWhite = getRGBData(800, 600, -0.6, 0, 3.2, 50, false)
expect(blackAndWhite[0][0]).toEqual([255, 255, 255]) // black
})

it('should produce colored pixels distant to the set', () => {
const colorCoded = getRGBData(800, 600, -0.6, 0, 3.2, 50, true)
expect(colorCoded[0][0]).toEqual([255, 0, 0]) // red
})
})