Code:
Set 1: [0.5, 0.0, 0.0, 0.5, 0.0, 0.0]
Set 2: [0.5, 0.0, 0.0, 0.5, 1.0, 0.0]
Set 3: [0.5, 0.0, 0.0, 0.5, 0.5, 0.5]
Set 1: [Xmax = 0, Ymax = 0]
Set 2: [Xmax = 2, Ymax = 0]
Set 3: [Xmax = 1, Ymax = 1]
The highest maximums are Xmax = 2 and Ymax = 1 and for the slanted one I get the expected Xmax = 1.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <mt19937ar.h>
#include <SDL/SDL.h>
#include <windows.h>
#define WIDTH 640
#define HEIGHT 480
#define ITER 90000
#define COLOR 0x00ff00
#define FILLCOLOR 0x000000
#define PITCH (screen->pitch/4)
#define PIXELS ((unsigned int*) screen->pixels)
typedef struct {
double a, b, c, d, e, f;
double p;
} ifs_elem;
void draw_frac (ifs_elem* ifs);
SDL_Surface* screen;
int main (int argc, char** argv)
{
SDL_Event event;
ifs_elem sierp_slant[] = {{0.5, 0.0, 0.0, 0.5, 0.0*WIDTH, 0.0*HEIGHT, 1.0/3.0},
{0.5, 0.0, 0.0, 0.5, 0.0*WIDTH, 0.5*HEIGHT, 1.0/3.0},
{0.5, 0.0, 0.0, 0.5, 0.5*WIDTH, 0.5*HEIGHT, 1.0/3.0}};
ifs_elem sierp[] = {{0.5, 0.0, 0.0, 0.5, 0.0*WIDTH/2, 0.0*HEIGHT, 1.0/3.0},
{0.5, 0.0, 0.0, 0.5, 1.0*WIDTH/2, 0.0*HEIGHT, 1.0/3.0},
{0.5, 0.0, 0.0, 0.5, 0.5*WIDTH/2, 0.5*HEIGHT, 1.0/3.0}};
ifs_elem fern[] = {{ 0.0, 0.0, 0.0, 0.16, 0.0*WIDTH, 1.6*HEIGHT, 0.1},
{ 0.2, -0.26, 0.23, 0.22, 0.0*WIDTH, 0.44*HEIGHT, 0.08},
{-0.15, 0.28, 0.26, 0.24, 0.0*WIDTH, 1.6*HEIGHT, 0.08},
{ 0.75, 0.04, -0.04, 0.85, 0.0*WIDTH, 0.0*HEIGHT, 0.74}};
ifs_elem fern2[] = {{ 0.0, 0.0, 0.0, 0.16, 0.0*WIDTH, 0.0*HEIGHT, 0.1},
{ 0.2, -0.26, 0.23, 0.22, 0.0*WIDTH, 1.6*HEIGHT, 0.07},
{-0.15, 0.28, 0.26, 0.24, 0.0*WIDTH, 0.44*HEIGHT, 0.07},
{ 0.85, 0.04, -0.04, 0.85, 0.0*WIDTH, 1.6*HEIGHT, 0.85}};
ifs_elem* fracs[] = {sierp_slant, sierp, fern, fern2};
int i = 0;
if (SDL_Init(SDL_INIT_EVERYTHING) < 0) {
fprintf(stderr, "Error: Unable to init SDL: %s\n", SDL_GetError());
return 1;
}
atexit(SDL_Quit);
if ((screen = SDL_SetVideoMode(WIDTH, HEIGHT, 32, SDL_HWSURFACE)) == NULL) {
fprintf(stderr, "Error: Unable to init SDL: %s\n", SDL_GetError());
return 1;
}
init_genrand(time(NULL));
draw_frac(fracs[i]);
while (SDL_WaitEvent(&event)) {
if (event.type == SDL_KEYDOWN) {
switch (event.key.keysym.sym) {
case SDLK_q:
case SDLK_ESCAPE:
return 0;
case SDLK_f:
i++;
if (i == sizeof(fracs)/sizeof(*fracs))
i = 0;
draw_frac(fracs[i]);
break;
}
}
else if (event.type == SDL_QUIT)
return 0;
}
return 0;
}
void draw_frac (ifs_elem* ifs)
{
int i, j;
double x, y, oldx;
double rnd;
x = y = 0;
SDL_FillRect(screen, NULL, FILLCOLOR);
if (SDL_MUSTLOCK(screen))
SDL_LockSurface(screen);
for (i = 0; i < ITER; i++) {
rnd = genrand_real1();
for (j = 0; 1; j++) {
if (ifs[j].p >= rnd)
break;
rnd -= ifs[j].p;
}
oldx = x;
x = ifs[j].a*x + ifs[j].b*y + ifs[j].e;
y = ifs[j].c*oldx + ifs[j].d*y + ifs[j].f;
if (x < 0 || x >= WIDTH || y > HEIGHT || y < 1)
continue;
PIXELS[(int) x + (HEIGHT - (int) y)*PITCH] = COLOR;
}
if (SDL_MUSTLOCK(screen))
SDL_UnlockSurface(screen);
while (SDL_Flip(screen) < 0);
}