Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 72 lines (56 sloc) 1.379 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
#include "imext.h"
#include <stdlib.h>

char evalstr[]="Mandlebrot renderer";

/* Example Mandlebrot generator */

/* input parameters
image is the image object.
*/


static
int
mandel(double x, double y, int max_iter) {
  double xn, yn;
  double xo, yo;
  int iter = 1;
  /* Z(n+1) = Z(n) ^2 + c */

  /* printf("(%.2f, %.2f) -> \n", x,y); */

  xo = x;
  yo = y;

  while( xo*xo+yo*yo <= 10 && iter < max_iter) {
    xn = xo*xo-yo*yo + x;
    yn = 2*xo*yo + y;
    xo=xn;
    yo=yn;
    iter++;
  }
  return (iter == max_iter)?0:iter;
}

void
mandelbrot(i_img *im, double minx, double miny, double maxx, double maxy, int max_iter) {

  int i;
  i_img_dim x,y;
  int idx;
  double divx, divy;

  i_color icl[256];
  srand(12235);
  for(i=1;i<256; i++) {
    icl[i].rgb.r = 100+(int) (155.0*rand()/(RAND_MAX+1.0));
    icl[i].rgb.g = 100+(int) (155.0*rand()/(RAND_MAX+1.0));
    icl[i].rgb.g = 100+(int) (155.0*rand()/(RAND_MAX+1.0));
  }

  icl[0].rgb.r = 0;
  icl[0].rgb.g = 0;
  icl[0].rgb.g = 0;
    
  if (maxx <= minx)
    maxx = minx + 1.0;
  if (maxy <= miny)
    maxy = miny + 1.0;

  divx = (maxx - minx) / im->xsize;
  divy = (maxy - miny) / im->ysize;

  for(y = 0; y < im->ysize; y ++) {
    for(x = 0; x < im->xsize; x ++ ) {
      idx = mandel(minx + x*divx , miny + y*divy, max_iter);
      idx = idx % 256;
      i_ppix(im,x,y,&icl[idx]);
    }
  }
}
Something went wrong with that request. Please try again.