- 2

# HELP

who knows how to calculate the fractal number, I need to write the code

1 Answer

+ 4

There are many kinds of fractals but it sounds like you're looking for math to calculate a value at a point in a Mandelbrot or Julia Set fractal. Here is how that math can look in JavaScript for the Mandelbrot Set:
function(x, y) {
var xt;
var zx = 0, zy = 0;
var cy = y;
var cx = x;
for (var i = 0; i < 255 && zx < 2; i++) {
xt = zx * zy;
zx = zx * zx - zy * zy + cx;
zy = 2 * xt + cy;
}
return i;
}
The most interesting patterns from that function show in the range of x from -2 to 2 and y from -2 to 2. If you want to see how I used that in one of my published codes, select the "Mandelbrot Set" in the example at: https://code.sololearn.com/WVhoMHzy3K81/#js I used almost that exact function to define the Mandelbrot Set.
Numbers at each point in the Julia Set are calculated in a slightly more complex way because each depends on 2 separate real/constant numbers.
Here is some math for the Julia Set fractal expressed with JavaScript:
// These numbers can be changed to make a wide variety of different Julia set patterns.
var constant_x = -0.704029749122184;
var constant_y = -0.3383527246917294;
function(x, y) {
var n = 255;
var cRe = constant_x;
var cIm = constant_y;
var newRe = x;
var newIm = y;
for(var i = 0; i < 255; i++)
{
//remember value of previous iteration
var oldRe = newRe;
var oldIm = newIm;
//the actual iteration, the real and imaginary part are calculated
newRe = oldRe * oldRe - oldIm * oldIm + cRe;
newIm = 2 * oldRe * oldIm + cIm;
//if the point is outside the circle with radius 2: stop
if((newRe * newRe + newIm * newIm) > 4) break;
}
return i;
}