Scarica il sorgente database/15-anti_buddhabrot_file_generator.cpp
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <time.h>
  5.  
  6. struct complesso
  7. {
  8. double r;
  9. double i;
  10. };
  11.  
  12. //****** MAIN *******//
  13.  
  14. int main (void){
  15.  
  16. char immagine_name[]="bu";
  17. //*********** PARAMETRI *************//
  18. long larghezza = 2000;
  19. long altezza = 2000;
  20. long deep = 1000;
  21. long buddha = 10000000;
  22.  
  23. double y1 = 2;
  24. double x0 = -2, x1 = 2;
  25. double y0 = -2;
  26. //**********************************//
  27. //aggiustamento parametri
  28. double agg_temp;
  29. agg_temp=y1;
  30. y1=y0*(-1);
  31. y0=agg_temp*(-1);
  32. //**********************************//
  33.  
  34. time_t t;
  35. srand((unsigned) time(&t));
  36. long b=0;
  37. long x,y;
  38.  
  39. long ** buddha_value;
  40. buddha_value = (long**) malloc(larghezza * sizeof(long *));
  41. for(b=0; b<larghezza; b++)
  42. buddha_value[b] = (long*) malloc(altezza * sizeof(long));
  43. for(x=0;x<larghezza;x++)for(y=0;y<altezza;y++)buddha_value[x][y]=0;
  44.  
  45. struct complesso *history;
  46. history = (struct complesso*)malloc(deep * sizeof(struct complesso));
  47.  
  48. printf("operazioni iniziali effettuate, inizio cicli!\n");
  49.  
  50. for(b=0;b<buddha;b++){ //ciclo buddhabrot
  51.  
  52. //inizio elaborazione immagine
  53. long x,y,d,i,a;
  54. double real_x, real_y, colore;
  55. struct complesso z,c,temp;
  56.  
  57. x=(rand())%larghezza;
  58. y=(rand())%altezza;
  59.  
  60. real_x = ((x1-x0)/larghezza)*x+x0;
  61. real_y = ((y1-y0)/altezza)*y+y0;
  62. //analisi di mendelbrot
  63. c.r = real_x;
  64. c.i = real_y;
  65. z.r = 0;
  66. z.i = 0;
  67. d = 0;
  68. for(i=1;i<deep;i++){
  69. if((z.r*z.r)+(z.i*z.i) > 4) {
  70. d = i;
  71. break;
  72. }
  73. temp.r = z.r;
  74. temp.i = z.i;
  75.  
  76. z.r = (temp.r*temp.r) - (temp.i*temp.i) + c.r;
  77. z.i = (temp.r*temp.i)*2 + c.i;
  78.  
  79. history[i].r = z.r;
  80. history[i].i = z.i;
  81. }
  82.  
  83. //controllo punto dell'insieme di mendelbrot
  84. if(d==0) {
  85. for(a=0;a<deep;a++){ //disegno la strada del punto
  86. x=(long) ((double)(history[a].r-x0)*larghezza/(x1-x0));
  87. y=(long) ((double)(history[a].i-y0)*altezza/(y1-y0));
  88. if(x>0 && x<larghezza && y>0 && y<altezza){
  89. buddha_value[x][y]++;
  90. }
  91. }
  92. }
  93.  
  94.  
  95. if(b%1000000==0)printf("%d su %d gruppi da 1000.000 cicli completati!\n",b/1000000,buddha/1000000);
  96. } //fine buddha
  97.  
  98. //creo db
  99. printf("\nCreazione db in corso...\n");
  100. FILE *s;
  101. s = fopen(immagine_name,"wb");
  102. for(x=0;x<larghezza;x++)
  103. for(y=0;y<altezza;y++){
  104. fprintf(s,"%d%c",buddha_value[x][y],0);
  105. }
  106. fclose(s);
  107. printf("db creato.\n");
  108.  
  109. printf("fine!");
  110.  
  111. getchar();
  112. return 0;
  113. }
  114.  
  115.