magistraleinformaticanetworking:spd:2018:mandel
Differenze
Queste sono le differenze tra la revisione selezionata e la versione attuale della pagina.
Entrambe le parti precedenti la revisioneRevisione precedenteProssima revisione | Revisione precedente | ||
magistraleinformaticanetworking:spd:2018:mandel [16/04/2018 alle 15:38 (7 anni fa)] – Massimo Coppola | magistraleinformaticanetworking:spd:2018:mandel [18/04/2018 alle 15:03 (7 anni fa)] (versione attuale) – Massimo Coppola | ||
---|---|---|---|
Linea 1: | Linea 1: | ||
+ | ==== Example code for mandelbrot ==== | ||
+ | |||
+ | C/C++ skeleton code for Mandelbrot computation. | ||
+ | |||
< | < | ||
#include < | #include < | ||
Linea 5: | Linea 9: | ||
#include " | #include " | ||
#include " | #include " | ||
+ | // or use blocked_range2d if appropriate | ||
using namespace tbb; | using namespace tbb; | ||
using namespace std; | using namespace std; | ||
- | // costanti di default | + | // default |
- | //square area, lower left angle and size | + | // square area, lower left angle and size |
#define DEFAULT_X -2.0 | #define DEFAULT_X -2.0 | ||
#define DEFAULT_Y -2.0 | #define DEFAULT_Y -2.0 | ||
Linea 18: | Linea 23: | ||
#define DEFAULT_ITERATIONS 1000 | #define DEFAULT_ITERATIONS 1000 | ||
- | // we assume a point diverges if quared | + | // we assume a point diverges if its squared |
#define MAX_SMODULUS = 4 | #define MAX_SMODULUS = 4 | ||
+ | // a variable holding the number of iterations limit | ||
static int maxIter = DEFAULT_ITERATIONS; | static int maxIter = DEFAULT_ITERATIONS; | ||
- | //funzione per calcolo | + | //function computing the mandelbrot in asingle point |
//returns the number of iteration until divergence | //returns the number of iteration until divergence | ||
int mand_compute( double cx, double cy) | int mand_compute( double cx, double cy) | ||
{ | { | ||
- | | + | int i=0; |
double x = cx; double y = cy; | double x = cx; double y = cy; | ||
double nx, ny; | double nx, ny; | ||
Linea 34: | Linea 40: | ||
{ | { | ||
// (x,y)^2 + c | // (x,y)^2 + c | ||
- | nx = x*x - y*y; | + | nx = x*x - y*y + cx; |
- | ny = 2*x*y | + | ny = 2*x*y + cy; |
if ( nx*nx + ny*ny > MAX_SMODULUS ) break; | if ( nx*nx + ny*ny > MAX_SMODULUS ) break; | ||
+ | x=nx; y=ny; | ||
} | } | ||
return i; | return i; | ||
} | } | ||
- | // class to hold the computation | + | // define |
Linea 47: | Linea 54: | ||
int main () { | int main () { | ||
- | // inizializza iterazioni | + | // initialization |
- | // inizializza zona | + | // inizialize data if needed |
- | //crea parallel | + | // parallel |
+ | // with blocked_range | ||
- | //parallel for | ||
} | } | ||
</ | </ | ||
+ | |||
+ | ==== Example code for saving an array as PPM graphics ==== | ||
+ | |||
+ | Compile and link this file together with you code. | ||
+ | Remember to insert the function prototye in the main source file | ||
+ | |||
+ | < | ||
+ | #include < | ||
+ | #include < | ||
+ | |||
+ | /** C routine to save a 2d int array as an image to a simple graphic format **/ | ||
+ | /** edited form of code from the rosetta code project **/ | ||
+ | /** https:// | ||
+ | |||
+ | |||
+ | int saveimg(int dimx, int dimy, const char * filename, int * matrix, int max_value) | ||
+ | { | ||
+ | // filename must end with .ppm | ||
+ | // no parameter checking!!! you are on your own | ||
+ | | ||
+ | // const int dimx = 800, dimy = 800; | ||
+ | int i, j; | ||
+ | FILE *fp = fopen(filename, | ||
+ | (void) fprintf(fp, " | ||
+ | for (j = 0; j < dimy; ++j) | ||
+ | { | ||
+ | for (i = 0; i < dimx; ++i) | ||
+ | { | ||
+ | // here you may use any scaling function you prefer | ||
+ | int val = matrix[i +j*dimy]; | ||
+ | if (max_value!=255) | ||
+ | { val = (val *255) / max_value; // scale to 0 - 255 | ||
+ | } | ||
+ | // | ||
+ | // rgb values are still rounded in case of mistakes | ||
+ | static unsigned char color[3]; | ||
+ | color[0] = val % 256; /* red */ | ||
+ | color[1] = val % 256; /* green */ | ||
+ | color[2] = val % 256; /* blue */ | ||
+ | (void) fwrite(color, | ||
+ | } | ||
+ | } | ||
+ | (void) fclose(fp); | ||
+ | return 0; | ||
+ | } | ||
+ | |||
+ | </ | ||
+ |
magistraleinformaticanetworking/spd/2018/mandel.1523893087.txt.gz · Ultima modifica: 16/04/2018 alle 15:38 (7 anni fa) da Massimo Coppola