Imprimir

Mandelbrot set

Mandelbrot set

mandlebrot.png

 

 

// Mandelbrot set for the HP Prime
// Date  : April 2013
// Version 1.0

iteration(c, bailoutValue, maxIter)
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 0+ 1*Ndx; END;
  IF Ndx < 62 THEN RETURN 31+ 32*(Ndx-31); END;
  IF Ndx < 93 THEN RETURN 1023- 1*(Ndx-62); END;
  IF Ndx < 124 THEN RETURN 992+ 1024*(Ndx-93); END;
  IF Ndx < 155 THEN RETURN 32736- 32*(Ndx-124); END;
  IF Ndx < 186 THEN RETURN 31744+ 1*(Ndx-155); END;
  RETURN 31775;
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:
  REPEAT UNTIL GETKEY() == -1;
  FREEZE;
END;