Italiano
HOME | PROGETTI | CLASSI C++ | Sorgenti c\c++ | Galleria | Guestbook | Server BW | Contattami | Login

<< Buddhabrot >>

visiste: 3605


Questo č un generatore dell'insieme di Mandelbrot con un particolare tipo di rendering, l'immagine di output viene chiamata Buddhabrot.

Le variabili importanti in gioco sono due: la profonditą con cui si controlla se il punto appartiene all'insieme di Mandelbrot e il numero di punti casuali presi in considerazione. La prevalenza della prima variabile porta ad un'immagine piu nitida, la prevalenza della seconda crea un'effetto nuvola.
La differenza si puo vedere nei due esempi qui sotto:




  1. //*************************************//
  2. //******** created by mamo139 *********//
  3. //*** http://mamo139.altervista.org ***//
  4. //*************************************//
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <time.h>
  9.  
  10. char *** crea_bmp(char *nome, long x, long y);
  11. void disegna_bmp(char *nome, char *** array, long x, long y);
  12.  
  13. struct complesso
  14. {
  15. double r;
  16. double i;
  17. };
  18.  
  19. //*** strumenti gestione colori ***//
  20. struct rgb {
  21. int red;
  22. int green;
  23. int blue;
  24. };
  25. struct rgb_d {
  26. double red;
  27. double green;
  28. double blue;
  29. };
  30. struct hsv {
  31. int hue;
  32. int saturation;
  33. int value;
  34. };
  35. struct rgb hsv_rgb(struct hsv hsv_map);
  36.  
  37. //****** MAIN *******//
  38.  
  39. int main (void){
  40.  
  41. char immagine_name[]="buddha_hsv.bmp";
  42. //*********** PARAMETRI *************//
  43. long larghezza = 1000;
  44. long altezza = 1000;
  45. long deep = 10000;
  46. long buddha = 1000000;
  47.  
  48. double y1 = 2;
  49. double x0 = -2, x1 = 2;
  50. double y0 = -2;
  51. //**********************************//
  52. //aggiustamento parametri
  53. double agg_temp;
  54. agg_temp=y1;
  55. y1=y0*(-1);
  56. y0=agg_temp*(-1);
  57. //**********************************//
  58.  
  59. time_t t;
  60. srand((unsigned) time(&t));
  61. long b=0;
  62. long x,y;
  63.  
  64. char *** matrice;
  65. matrice = crea_bmp(immagine_name,larghezza,altezza);
  66.  
  67. long ** buddha_value;
  68. buddha_value = (long**) malloc(larghezza * sizeof(long *));
  69. for(b=0; b<larghezza; b++)
  70. buddha_value[b] = (long*) malloc(altezza * sizeof(long));
  71. for(x=0;x<larghezza;x++)for(y=0;y<altezza;y++)buddha_value[x][y]=0;
  72.  
  73. struct complesso *history;
  74. history = (struct complesso*)malloc(deep * sizeof(struct complesso));
  75.  
  76. printf("operazioni iniziali effettuate, inizio cicli!\n");
  77.  
  78. for(b=0;b<buddha;b++){ //ciclo buddhabrot
  79.  
  80. //inizio elaborazione immagine
  81. long x,y,d,i,a;
  82. double real_x, real_y, colore;
  83. struct complesso z,c,temp;
  84.  
  85. x=(rand())%larghezza;
  86. y=(rand())%altezza;
  87.  
  88. real_x = ((x1-x0)/larghezza)*x+x0;
  89. real_y = ((y1-y0)/altezza)*y+y0;
  90. //analisi di mendelbrot
  91. c.r = real_x;
  92. c.i = real_y;
  93. z.r = 0;
  94. z.i = 0;
  95. d = 0;
  96. for(i=1;i<deep;i++){
  97. if((z.r*z.r)+(z.i*z.i) > 4) {
  98. d = i;
  99. break;
  100. }
  101. temp.r = z.r;
  102. temp.i = z.i;
  103.  
  104. z.r = (temp.r*temp.r) - (temp.i*temp.i) + c.r;
  105. z.i = (temp.r*temp.i)*2 + c.i;
  106.  
  107. history[i].r = z.r;
  108. history[i].i = z.i;
  109. }
  110.  
  111. //controllo punto dell'insieme di mendelbrot
  112. if(d!=0) {
  113. for(a=0;a<d;a++){ //disegno la strada del punto
  114. x=(long) ((double)(history[a].r-x0)*larghezza/(x1-x0));
  115. y=(long) ((double)(history[a].i-y0)*altezza/(y1-y0));
  116. if(x>0 && x<larghezza && y>0 && y<altezza){
  117. buddha_value[x][y]++;
  118. }
  119. }
  120. }
  121.  
  122.  
  123. if(b%100000==0)printf("%d su %d gruppi da 100.000 cicli completati!\n",b/100000,buddha/100000);
  124. } //fine buddha
  125.  
  126. //coloro buddha
  127. long grande=0,piccolo=buddha_value[0][0];
  128. double media=0;
  129. printf("inizio colorazione!\n");
  130. for(x=0;x<larghezza;x++)//cerco il valore piu grande e piu piccolo!
  131. for(y=0;y<altezza;y++){
  132. if(buddha_value[x][y]>grande) grande = buddha_value[x][y];
  133. if(buddha_value[x][y]<piccolo) piccolo = buddha_value[x][y];
  134. media = media + (double)buddha_value[x][y]/(larghezza*altezza);
  135. }
  136. printf("biggest value: %d smallest value: %d media: %f\n",grande,piccolo,media);
  137. struct hsv hsv_map; struct rgb rgb_map;
  138. for(x=0;x<larghezza;x++)
  139. for(y=0;y<altezza;y++){
  140. buddha_value[x][y]=buddha_value[x][y]*5;
  141. if(buddha_value[x][y] > 255) buddha_value[x][y] = 255;
  142.  
  143. hsv_map.hue = 50;
  144. hsv_map.saturation = 200;
  145. hsv_map.value = buddha_value[x][y];
  146.  
  147. rgb_map = hsv_rgb(hsv_map);
  148.  
  149. matrice[x][y][2]= rgb_map.red;
  150. matrice[x][y][1]= rgb_map.green;
  151. matrice[x][y][0]= rgb_map.blue;
  152. }
  153. printf("fine colorazione, inizio scrittura!\n");
  154.  
  155. disegna_bmp(immagine_name,matrice,larghezza,altezza);
  156. printf("fine!");
  157.  
  158. system(immagine_name);
  159. //getchar();
  160. return 0;
  161. }
  162.  
  163. char *** crea_bmp(char *nome, long x, long y){
  164.  
  165. FILE *stream, *cavia;
  166. stream = fopen(nome,"wb");
  167. cavia = fopen("cavia","rb");
  168.  
  169. char *buffer;
  170. buffer = (char *)malloc(54 * sizeof(char));
  171. long b = fread( buffer, 1, 54, cavia);
  172.  
  173. buffer[18] = x%256;//asse x
  174. buffer[19] = x/256;
  175. buffer[22] = y%256;//asse y
  176. buffer[23] = y/256;
  177.  
  178. fwrite(buffer, 1, b, stream);
  179. fclose(stream);
  180.  
  181. char *** array;
  182. int i, j, h;
  183. array = (char***)malloc(x * sizeof(char *));
  184. for(i=0; i<x; i++) {
  185. array[i] = (char**)malloc(y * sizeof(char *));
  186. for (j=0; j<y; j++)
  187. array[i][j] = (char*)malloc(3 * sizeof(char *));
  188. }
  189.  
  190. int i1,i2,i3;
  191. for(i1=0; i1<x; i1++)
  192. for (i2=0; i2<y; i2++)
  193. for (i3=0; i3<3; i3++)
  194. array[i1][i2][i3] = 0;
  195.  
  196. return array;
  197. }
  198.  
  199. void disegna_bmp(char *nome, char *** array, long x, long y){
  200.  
  201. FILE *stream;
  202. stream = fopen(nome,"ab");
  203.  
  204. int i1,i2;
  205. for (i2=y-1; i2>=0; i2--)
  206. for(i1=0; i1<x; i1++)
  207. fwrite(array[i1][i2], 1, 3, stream);
  208.  
  209. fclose(stream);
  210. }
  211.  
  212. struct rgb hsv_rgb(struct hsv hsv_map){
  213.  
  214. struct rgb_d rgb_map;
  215. struct rgb rgb_int;
  216.  
  217. double H = ((double)hsv_map.hue);
  218. double S = ((double)hsv_map.saturation)/255;
  219. double V = ((double)hsv_map.value)/255;
  220.  
  221. double hi = ((int)(H/60))%6;
  222. double f = (H/60) - (double)hi;
  223. double p = V*(1-S);
  224. double q = V*(1-(f*S));
  225. double t = V*(1-(1-f)*S);
  226.  
  227. if (hi == 0){ rgb_map.red=V; rgb_map.green=t; rgb_map.blue=p;}
  228. else if (hi == 1){ rgb_map.red=q*255; rgb_map.green=V; rgb_map.blue=p;}
  229. else if (hi == 2){ rgb_map.red=p*255; rgb_map.green=V; rgb_map.blue=t;}
  230. else if (hi == 3){ rgb_map.red=p*255; rgb_map.green=q; rgb_map.blue=V;}
  231. else if (hi == 4){ rgb_map.red=t*255; rgb_map.green=p; rgb_map.blue=V;}
  232. else if (hi == 5){ rgb_map.red=V*255; rgb_map.green=p; rgb_map.blue=q;}
  233. else printf("ERRORE!");
  234.  
  235. rgb_int.red = (int)(rgb_map.red*255);
  236. rgb_int.green = (int)(rgb_map.green*255);
  237. rgb_int.blue = (int)(rgb_map.blue*255);
  238.  
  239. return rgb_int;
  240. }
  241.  
Versione sito: 1.03.01 by mamo139. - Tutti i sorgenti presenti su questo sito sono rilasciati sotto licenza "GNU GPL version 3".