HP Prime for All
English
Русский
Name | Mandelbrot |
Description | This is a very basic and straight forward approach to programming the Mandelbrot set on the Prime. It just shows how programming and using complex numbers/variables in a program work on the Prime. It is in no way optimized for speed, neither mathematically nor in terms of implementation. Also, it's not a Prime App (thus not using the Symb/Plot/Num and their Setup keys) and has no interactivity (like zooming) at all. The area of the Mandelbrot set is defined by setting the corresponding variables in the source code. Take it as a starting point for your further explorations, be it in Prime programming or in math. And feel free to expand it and maybe make an App out of it with interactivity. |
Author | Stefan Wolfrum |
Source code formatted by website engine
BEGIN
LOCAL iter := 0;
LOCAL z := (0, 0);
WHILE (ABS(z) < = bailoutValue) AND (iter < maxIter) DO
z := z*z+c;
iter := iter+1;
END;
RETURN iter;
END;
LSclr(Ndx)
BEGIN
Ndx := ROUND(Ndx*186, 0);
IF Ndx < 31 THEN RETURN RGB(0, 0, Ndx*8); END;
IF Ndx < 62 THEN RETURN RGB(0, (Ndx-31) * 8, 31*8); END;
IF Ndx < 93 THEN RETURN RGB(0, 31*8, (92-Ndx) * 8); END;
IF Ndx < 124 THEN RETURN RGB((Ndx-93) * 8, 31*8, 0); END;
IF Ndx < 155 THEN RETURN RGB(31*8, (154-Ndx) * 8, 0); END;
IF Ndx < 186 THEN RETURN RGB(31*8, 0, (Ndx-155) * 8); END;
RETURN RGB(31*8, 0, 31*8);
END;
colorize(itVal, maxIt)
BEGIN
IF itVal == maxIt THEN
// we're inside the Mandelbrot map
// so draw the pixel in black
RETURN 0;
ELSE
RETURN LSclr(itVal/maxIt);
END;
END;
EXPORT Mandelbrot()
BEGIN
// clear the screen (G0):
RECT();
LOCAL dx, dy, c, xp, yp;
LOCAL iter, color;
// these 4 variables define
// our window of the complex
// plane:
LOCAL xmin, xmax, ymin, ymax;
LOCAL maxIterations := 50;
LOCAL maxRadius := 2;
// Location
// ratio width:height should be 4:3
xmin := -2.5;
xmax := 1.5;
ymin := -1.5;
ymax := 1.5;
// another nice set of parameters:
//xmin := 0.315625;
//xmax := 0.515625;
//ymin := 0.28125;
//ymax := 0.43125;
dx := (xmax-xmin) / 320;
dy := (ymax-ymin) / 240;
c := (xmin, ymin);
// we loop over every pixel
// of the Prime's screen:
FOR yp FROM 0 TO 239 DO
FOR xp FROM 0 TO 319 DO
// create the complex number c
// we need for the iteration:
c := (xmin+xp*dx, ymax-yp*dy);
// now iterate the formula and
// get back the number of
// iteration steps it took until
// the complex number jumped out
// of the convergence radius:
iter := iteration(c, maxRadius, maxIterations);
// determine a color for this iteration number:
color := colorize(iter, maxIterations);
// set the pixel in that color:
PIXON_P(xp, yp, color);
END;
END;
// leave the image on the screen
// until a key is pressed:
FREEZE;
WAIT(0);
END;