mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-04 15:39:42 +08:00
chore: merge Fix/742 migrate doctest to jest (#749)
* Remove QuickSelect doctest There are more Jest test cases already. * Remove AverageMedian doctest Already migrated to jest * Migrate doctest for BinaryExponentiationRecursive.js (also remove inline "main" test method) * Migrate doctest for EulersTotient.js (also remove inline "main" test method) * Migrate doctest for PrimeFactors.js (also remove inline "main" test method) * Migrate doctest for BogoSort.js Re-write prototype-polluting helper methods, too. (also remove inline test driver code) * Migrate doctest for BeadSort.js (also remove inline test driver code) * Migrate doctest for BucketSort.js (also remove inline test driver code) * Migrate doctest for CocktailShakerSort.js (also remove inline test driver code) * Migrate doctest for MergeSort.js (also remove inline test driver code) * Migrate doctest for QuickSort.js (also remove inline test driver code) * Migrate doctest for ReverseString.js (also remove inline test driver code) * Migrate doctest for ReverseString.js * Migrate doctest for ValidateEmail.js * Migrate doctest for ConwaysGameOfLife.js (remove the animate code, too) * Remove TernarySearch doctest Already migrated to jest * Migrate doctest for BubbleSort.js (also remove inline test driver code) * Remove doctest from CI and from dependencies relates to #742 fixes #586 * Migrate doctest for RgbHsvConversion.js * Add --fix option to "standard" npm script * Migrate doctest for BreadthFirstSearch.js (also remove inline test driver code) * Migrate doctest for BreadthFirstShortestPath.js (also remove inline test driver code) * Migrate doctest for EulerMethod.js (also remove inline test driver code) Move manual test-code for plotting stuff in the browser in a distinct file, too. Those "*.manual-test.js" files are excluded from the UpdateDirectory.mjs script, as well. * Migrate doctest for Mandelbrot.js (also remove inline test driver code & moved manual drawing test into a *.manual-test.js) * Migrate doctest for FloodFill.js * Migrate doctest for KochSnowflake.js (also move manual drawing test into a *.manual-test.js) * Update npm lockfile * Update README and COMMITTING with a few bits & bobs regarding testing & code quality
This commit is contained in:
@ -1,43 +1,22 @@
|
||||
/**
|
||||
* 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.
|
||||
@ -45,10 +24,10 @@ Pixel inside the Mandelbrot set should be black.
|
||||
* @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,
|
||||
@ -83,9 +62,9 @@ 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)
|
||||
}
|
||||
}
|
||||
|
||||
@ -93,8 +72,9 @@ function getRGBData (
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
@ -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.
|
||||
@ -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.
|
||||
*/
|
||||
@ -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)
|
||||
}
|
||||
|
Reference in New Issue
Block a user