Last active
February 20, 2020 04:29
-
-
Save christophergorexyz/546ad3b11ccf3116e83d5840baaa02f1 to your computer and use it in GitHub Desktop.
sample a complex number for membership in julia sets
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const MAX_SAMPLES = 1000; | |
| const MAX_RADIUS = 2 ** 4; | |
| /** | |
| * a complex number, c, belongs to The Mandelbrot Set if the iterative application | |
| * of the function f(z)=z^2+c, starting at z=0, does not result in divergence | |
| * beyond a specified radius before a specified number of iterations | |
| * | |
| * a complex number, z, belongs to The Julia Set if the iterative application | |
| * of the function f(z)=z^2+c, where c is a constant, does not result in divergence | |
| * beyond a specified radius before a specified number of iterations | |
| * | |
| * @param {Number?} zr the real part of the complex number | |
| * @param {Number?} zi the imaginary part of the complex number | |
| * @param {Number?} cr the real part of the complex number | |
| * @param {Number?} ci the imaginary part of the complex number | |
| * @param {Number?} maxRadius the maximum distance an orbit point may be | |
| * @param {Number?} maxSamples the maximum number of samples to take before assuming the orbit does not diverge | |
| */ | |
| export function samplePointJulia(zr = 0, zi = 0, cr = 0, ci = 0, maxRadius = MAX_RADIUS, maxSamples = MAX_SAMPLES) { | |
| let inSet = true; | |
| let escaped = false | |
| let orbitDistance = 0; | |
| let orbitPoints = [{ escaped, orbitDistance, zr, zi }]; | |
| //sample the entire range rather then aborting upon escape to maximize data | |
| while (orbitPoints.length < maxSamples) { | |
| let escaped = false | |
| //this is literally just the FOIL method | |
| //we subtract the Last terms from the First because `i**2===-1`, and they are | |
| //no longer imaginary, then we combine the Inside and Outside because they are | |
| let tempR = zr ** 2 - zi ** 2 + cr; | |
| zi = 2 * zr * zi + ci; | |
| zr = tempR; | |
| orbitDistance = zr ** 2 + zi ** 2; //pythagoras | |
| escaped = orbitDistance > maxRadius ** 2; | |
| inSet = inSet && !escaped; | |
| orbitPoints.push({ escaped, orbitDistance, zr, zi }); | |
| } | |
| return { orbitPoints, inSet }; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment