Scarica il sorgente database/15-mandelbrot_file_generator.cpp
  1. //*************************************//
  2. //******** created by mamo139 *********//
  3. //*** http://mamo139.altervista.org ***//
  4. //*************************************//
  5.  
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9.  
  10.  
  11. //*** strumenti genstione numeri complessi ***//
  12. struct complesso
  13. {
  14. double r;
  15. double i;
  16. };
  17. //*** strumenti gestione db
  18. int ** crea_matrice(long x, long y);
  19. void scrivi_matrice(char * file_name, int ** matrice, long xl, long yl);
  20.  
  21. //*** MAIN ***//
  22. int main (void){
  23.  
  24. char db_name[]="ma";
  25. //*********** PARAMETRI *************//
  26. long larghezza = 2000;
  27. long altezza = 2000;
  28. long deep = 10000;
  29.  
  30. double y1 = 2;
  31. double x0 = -2, x1 = 2;
  32. double y0 = -2;
  33. //**********************************//
  34. //aggiustamento parametri
  35. double agg_temp;
  36. agg_temp=y1;
  37. y1=y0*(-1);
  38. y0=agg_temp*(-1);
  39. //**********************************//
  40.  
  41. int ** matrice;
  42. matrice = crea_matrice(larghezza,altezza);
  43.  
  44. //inizio elaborazione immagine
  45. long x,y,d,i;
  46. double real_x,real_y, colore;
  47. struct complesso z,c,temp;
  48.  
  49. for(x=0;x<larghezza;x++){
  50. real_x = ((x1-x0)/larghezza)*x+x0;
  51. for(y=0;y<altezza;y++){
  52. real_y = ((y1-y0)/altezza)*y+y0;
  53.  
  54. //analisi di mendelbrot
  55. c.r = real_x;
  56. c.i = real_y;
  57. z.r = 0;
  58. z.i = 0;
  59. d = 0;
  60. for(i=0;i<deep;i++){
  61. if((z.r*z.r)+(z.i*z.i) > 4) {
  62. d = i;
  63. break;
  64. }
  65. temp.r = z.r;
  66. temp.i = z.i;
  67.  
  68. z.r = (temp.r*temp.r) - (temp.i*temp.i) + c.r;
  69. z.i = (temp.r*temp.i)*2 + c.i;
  70. }
  71.  
  72. matrice[x][y] = d;
  73. }
  74. printf("%d%%\n",x*100/larghezza);
  75. }
  76.  
  77. scrivi_matrice(db_name,matrice,larghezza,altezza);
  78. printf("fine!");
  79.  
  80.  
  81. getchar();
  82. return 0;
  83. }
  84. //****** FUNZIONI *******//
  85. int ** crea_matrice(long x, long y){
  86.  
  87. int ** array;
  88. int i, j;
  89. array = (int**)malloc(x * sizeof(int *));
  90. for(i=0; i<x; i++) {
  91. array[i] = (int*)malloc(y * sizeof(int *));
  92. }
  93.  
  94. int i1,i2;
  95. for(i1=0; i1<x; i1++)
  96. for (i2=0; i2<y; i2++)
  97. array[i1][i2] = 0;
  98.  
  99. return array;
  100. }
  101. void scrivi_matrice(char * file_name, int ** matrice, long xl, long yl){
  102. FILE *s;
  103. s = fopen(file_name,"wb");
  104.  
  105. int i1,i2;
  106. for(i1=0; i1<xl; i1++)
  107. for (i2=0; i2<yl; i2++)
  108. fprintf(s,"%d%c",matrice[i1][i2],0);
  109.  
  110. fclose(s);
  111.  
  112. }
  113.