mercoledì 12 settembre 2012

Esempio insieme di Mandelbrot in C/SDL

Un esempio di creazione dell'insieme di Mandelbrot impiegando la libreria SDL per la parte grafica


--------------------------------------------

#include "SDL/SDL.h"
#include <stdio.h>

#define SCREEN_WIDTH 640
#define SCREEN_HEIGHT 480
#define SCREEN_DEPTH 8

float re_min = -2.0;
float im_min = -1.2;

float re_max = 1.0;
float im_max = 1.2;
int iterazioni = 255;


float a,b;
float x,y,x_new,y_new;

int test;

int k,j,i;

int keypress = 0;


int main() {

SDL_Surface *screen;
Uint8       *p;
SDL_Event event;

float re_factor = (re_max-re_min);
float im_factor = (im_max-im_min);
     
SDL_Init(SDL_INIT_VIDEO);
screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_DEPTH, SDL_SWSURFACE);

for (i=0;i<SCREEN_HEIGHT;i++)
{
for (j=0;j<SCREEN_WIDTH;j++)
{
a = re_min+(j*re_factor/SCREEN_WIDTH);
b = im_min+(i*im_factor/SCREEN_HEIGHT);

x = 0;
y = 0;
test = 0;

for (k=0;k<iterazioni;k++)
{
x_new = (x*x)-(y*y)+a;
y_new = (2*x*y)+b;
if (((x_new*x_new)+(y_new*y_new))>4)
{
test = k;
p = (Uint8 *)screen->pixels + i * screen->pitch + j * screen->format->BytesPerPixel;
*p=(k%255);
break;
}
x = x_new;
y = y_new;
}
       
}

}
SDL_Flip(screen);

printf("Finito\n\r");

while (!keypress)
{
while (SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_KEYDOWN:
keypress = 1;
}
}
}
SDL_Quit();

return(0);

}
--------------------------------------------

il progetto puo' essere scaricato a questo indirizzo link

2 commenti:

  1. Perchè mi dice include "SDL/SDL.h" not found? Ho installato da Google play il plug in SDL.
    Thx

    RispondiElimina
    Risposte
    1. Questo esempio e' stato sviluppato su Linux e non su Android
      Per compilare su Linux e' necessario installare i pacchetti dev della libreria SDL (la modalita' varia da distribuzione a distribuzione)
      Su Android il programma funziona con C4Droid (programma a pagamento)

      Elimina

Physics informed neural network Fukuzono

Visto che puro ML non funziona per le serie tempo di cui mi sto occupando ed le regressioni basate su formule analitiche mostrano dei limiti...