Strumenti Utente

Strumenti Sito


magistraleinformaticanetworking:spd:2018:mandel

Questa è una vecchia versione del documento!


Example code for mandelbrot

#include <iostream>
#include <string>
#include <algorithm>
#include "tbb/parallel_for.h"
#include "tbb/blocked_range.h"

using namespace tbb;
using namespace std;

// costanti di default

//square area, lower left angle and size
#define DEFAULT_X -2.0
#define DEFAULT_Y -2.0
#define DEFAULT_SIZE 4.0
#define DEFAULT_PIXELS 10
#define DEFAULT_ITERATIONS 1000

// we assume a point diverges if quared modulus exceeds this value
#define MAX_SMODULUS = 4

static int maxIter = DEFAULT_ITERATIONS;


//funzione per calcolo mandelbrot in un punto
//returns the number of iteration until divergence
int mand_compute( double cx, double cy)
{
    int i;
    double x = cx; double y = cy;
    double nx, ny;
    for (i=0; i<maxIter; i++)
    {
       // (x,y)^2 + c
       nx = x*x - y*y;
       ny = 2*x*y
       if ( nx*nx + ny*ny > MAX_SMODULUS ) break;
    }
    return i;
}

// class to hold the computation


//main 
int main () {

// inizializza iterazioni

// inizializza zona

//crea parallel range

//parallel for

}

Example code for saving an array as PPM graphics

Compile this file together with you code and be sure to insert the function prototye in the main source file

#include <stdlib.h>
#include <stdio.h>

/** 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://rosettacode.org/wiki/Bitmap/Write_a_PPM_file **/


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, "wb"); /* b - binary mode */
  (void) fprintf(fp, "P6\n%d %d\n255\n", dimx, dimy);
  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, 1, 3, fp);
    }
  }
  (void) fclose(fp);
  return 0;
}
magistraleinformaticanetworking/spd/2018/mandel.1524051714.txt.gz · Ultima modifica: 18/04/2018 alle 11:41 (7 anni fa) da Massimo Coppola

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki