Advertisement
Guest User

chaosd - random sierpinski fractal generator

a guest
Apr 25th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.18 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h> // rand()
  3. #include <math.h>  // rint()
  4. #include <jpeglib.h>
  5. //fixed. should be parsed from commandline or defaulted to max screen dimensions
  6. #define xmax 1024
  7. #define ymax 768
  8.  
  9. //unused yet. should be parsed from chaos.conf file somewhere in /local/share/ or smth.
  10. //dimensions of vga screen
  11. #define dxmax 320
  12. #define dymax 240
  13. #define vgamode 'G640x480x16'
  14.  
  15. unsigned char ns=12;
  16. int xnn=0,ynn=0,xp=0,yp=0,xk=0,yk=0,dx=0,dy=0,x=0,y=0;
  17. float s11[50], p[50],
  18. s12[50],
  19. s13[50],
  20. s23[50],
  21. s22[50],
  22. s21[50],
  23. s31[50],
  24. s41[50];
  25.  
  26. unsigned char ekran[xmax+1][ymax+1];
  27. unsigned char image_buffer[xmax*ymax+1]; /*JSAMPLE*/
  28. //char vgamode[20]="G640x480x256";
  29. int it ;
  30.  
  31. double my_rand(float a) {
  32.     return (1+(1.0*a*rand()/(RAND_MAX+1.0)));
  33. }
  34.  
  35. double my_frand() {
  36.     return (rand()/(RAND_MAX+1.0));
  37. }
  38.  
  39. void plot(int a, int b) {
  40.         ekran[a][b]++;  
  41. }
  42.   void cls() { int i,j;
  43.     for(i=0;i<xmax;i++){
  44.         for(j=0;j<ymax;j++){
  45.         ekran[i][j]=0;
  46.         }
  47.     }
  48. }
  49.  
  50. //extern JSAMPLE * image_buffer;    /* Points to large array of R,G,B-order data */
  51. //JSAMPLE * ekran;  /* Points to large array of R,G,B-order data */
  52. //int image_height=xmax;    /* Number of rows in image */
  53. //int image_width=ymax;     /* Number of columns in image */
  54.  
  55. void write_JPEG_file (char filename[255], int quality)
  56. { struct jpeg_compress_struct cinfo;
  57.   struct jpeg_error_mgr jerr;
  58.   FILE * outfile;       /* target file */
  59.   JSAMPROW row_pointer[1];  /* pointer to JSAMPLE row[s] */
  60.   int row_stride;       /* physical row width in image buffer */
  61.  
  62.   cinfo.err = jpeg_std_error(&jerr);
  63.   jpeg_create_compress(&cinfo);
  64.  
  65.   if ((outfile = fopen(filename, "wb")) == NULL) {
  66.     fprintf(stderr, "can't open", filename,".jpg\n"); exit(1);
  67.   }
  68.   jpeg_stdio_dest(&cinfo, outfile);
  69.  
  70.   cinfo.image_width = xmax;     /* image width and height, in pixels */
  71.   cinfo.image_height = ymax;
  72.   cinfo.input_components = 1;       /* # of color components per pixel */
  73.   cinfo.in_color_space = JCS_GRAYSCALE; /* colorspace of input image */
  74.   jpeg_set_defaults(&cinfo);
  75.   //jpeg_set_quality(&cinfo, quality, TRUE /* limit to baseline-JPEG values */);
  76.  
  77.   jpeg_start_compress(&cinfo, TRUE);
  78.   row_stride = xmax * 1;    /* JSAMPLEs per row in image_buffer */
  79.   while (cinfo.next_scanline < cinfo.image_height) {
  80.     row_pointer[0] = & image_buffer[cinfo.next_scanline * row_stride];
  81.     (void) jpeg_write_scanlines(&cinfo, row_pointer, 1);
  82.   }
  83.   jpeg_finish_compress(&cinfo);
  84.   fclose(outfile);
  85.   jpeg_destroy_compress(&cinfo);
  86. }
  87.  
  88.  
  89.  
  90. void dump(char filename[255]) { int i,j; unsigned char a;
  91.     for(j=0;j<ymax;j++) {
  92.     for(i=0;i<xmax;i++) {
  93.         image_buffer[i+j*xmax]=ekran[i][j];
  94.     }
  95.     }
  96.     write_JPEG_file ("/dev/stdout", 50);
  97. }
  98.  
  99.  
  100. void g545() { double a;
  101.     if (xp>xk) { a=xp; xp=xk; xk=a; a=yp; yp=yk; yk=a; }
  102.    
  103.     dx=fabs(xk-xp); dy=fabs(yk-yp);
  104.  
  105.     if (dy>dx) {
  106.     if (yp>yk) { a=xp; xp=xk; xk=a; a=yp; yp=yk; yk=a; }
  107.    
  108.     a=(xk-xp)/(yk-yp);
  109.     for (y=yp;y<=yk;y+=5) {
  110.         x=xp+a*(y-yp);
  111.     } /* next y */
  112.     }
  113.     else {
  114.     if (xp>xk) { a=xp; xp=xk; xk=a; a=yp; yp=yk; yk=a; }
  115.    
  116.     a=(yk-yp)/(xk-xp);
  117.     for (x=xp;x<=xk;x+=8) { y=yp+a*(x-xp);} /* next x */
  118.     }
  119. }
  120.  
  121. void g360(){ int i; double r1,r2,r3,xx[5],yy[5];
  122.     for (i=1;i<=ns;i++){
  123.     r1=my_frand()*(xmax);
  124.     r2=my_frand()*(xmax);
  125.     r3=my_frand()*(xmax);
  126.    
  127.     s11[i]=(r2-r1)/(xmax);
  128.     s12[i]=(r3-r1)/(ymax);
  129.     s13[i]=r1;
  130.    
  131.     r1=my_frand()*(ymax);
  132.     r2=my_frand()*(ymax);
  133.     r3=my_frand()*(ymax);
  134.  
  135.     s21[i]=(r2-r1)/(xmax);
  136.     s22[i]=(r3-r1)/(ymax);
  137.     s23[i]=r1;
  138.  
  139.     xx[0]=   0*s11[i]+   0*s12[i]+s13[i];
  140.     yy[0]=   0*s21[i]+   0*s22[i]+s23[i];
  141.     xx[1]=xmax*s11[i]+   0*s12[i]+s13[i];
  142.     yy[1]=xmax*s21[i]+   0*s22[i]+s23[i];
  143.     xx[2]=xmax*s11[i]+ymax*s12[i]+s13[i];
  144.     yy[2]=xmax*s21[i]+ymax*s22[i]+s23[i];
  145.     xx[3]=   0*s11[i]+ymax*s12[i]+s13[i];
  146.     yy[3]=   0*s21[i]+ymax*s22[i]+s23[i];
  147.  
  148.     xp=xx[0];
  149.     yp=yy[0];
  150.     xk=xx[1];
  151.     yk=yy[1];
  152.    
  153.     g545();
  154.    
  155.    
  156.     xp=xx[2];
  157.     yp=yy[2];
  158.     xk=xx[1];
  159.     yk=yy[1];
  160.    
  161.     g545();
  162.  
  163.    
  164.     xp=xx[2];
  165.     yp=yy[2];
  166.     xk=xx[3];
  167.     yk=yy[3];
  168.    
  169.     g545();
  170.        
  171.    
  172.     xp=xx[0];
  173.     yp=yy[0];
  174.     xk=xx[3];
  175.     yk=yy[3];
  176.    
  177.     g545();
  178.  
  179.  
  180.     } /* next i */
  181. }
  182.  
  183.  
  184. void iteration() {
  185.    
  186. }
  187.  
  188.  
  189. void randomize() {
  190.     ns=2+(int)(15*my_frand());
  191.     it=it*ns;
  192. }
  193.  
  194.  
  195.  
  196.  
  197. int main(){ int i=0,k=0,a=0,h=0,q=0; float z=0 ;  double pp=0,r=0; char filename[16] ;
  198.     srand((int)time(NULL));  
  199.     cls();
  200.  
  201.     q=0;
  202.     it=10000;
  203.     randomize();
  204.     g360();
  205.     cls();
  206.    
  207.  
  208.     pp=0;
  209.     for (i=1;i<=ns;i++) {
  210.     p[i]=fabs(s11[i]*s22[i]-s21[i]*s12[i])+0.3;
  211.     pp+=p[i];  
  212.     }
  213.    
  214.     z=0;
  215.     for (i=1;i<=ns;i++) {
  216.     z=z+p[i];
  217.     p[i]=z/pp;
  218.     }
  219.  
  220.     x=0;
  221.     ynn=0;
  222.     xnn=0;
  223.    
  224.     z=0;
  225.     for (r=1;r<(10*ns)*100000;r++) {
  226.  
  227.  
  228.  
  229.     z=rand()/(RAND_MAX+1.0);
  230.     i=1;
  231. //        while (z>=p[i] && i!=ns) {i++;}
  232.    
  233.     switch(ns%8){
  234.     case 0:     if (z>=p[i]) {i++;};
  235.     case 32:    if (z>=p[i]) {i++;};
  236.     case 31:    if (z>=p[i]) {i++;};
  237.     case 30:    if (z>=p[i]) {i++;};
  238.     case 29:    if (z>=p[i]) {i++;} else goto found;
  239.     case 28:    if (z>=p[i]) {i++;};
  240.     case 27:    if (z>=p[i]) {i++;};
  241.     case 26:    if (z>=p[i]) {i++;};
  242.     case 25:    if (z>=p[i]) {i++;};
  243.     case 24:    if (z>=p[i]) {i++;};
  244.     case 23:    if (z>=p[i]) {i++;};
  245.     case 22:    if (z>=p[i]) {i++;} else goto found;
  246.     case 21:    if (z>=p[i]) {i++;};
  247.     case 20:    if (z>=p[i]) {i++;};
  248.     case 19:    if (z>=p[i]) {i++;};
  249.     case 18:    if (z>=p[i]) {i++;};
  250.     case 17:    if (z>=p[i]) {i++;};
  251.     case 16:    if (z>=p[i]) {i++;};
  252.     case 15:    if (z>=p[i]) {i++;} else goto found;
  253.     case 14:    if (z>=p[i]) {i++;};
  254.     case 13:    if (z>=p[i]) {i++;};
  255.     case 12:    if (z>=p[i]) {i++;};
  256.     case 11:    if (z>=p[i]) {i++;};
  257.     case 10:    if (z>=p[i]) {i++;};
  258.     case 9:     if (z>=p[i]) {i++;};
  259.     case 8:     if (z>=p[i]) {i++;};
  260.     case 7:     if (z>=p[i]) {i++;} else goto found;
  261.     case 6:     if (z>=p[i]) {i++;};
  262.     case 5:     if (z>=p[i]) {i++;};
  263.     case 4:     if (z>=p[i]) {i++;};
  264.     case 3:     if (z>=p[i]) {i++;};
  265.     case 2:     if (z>=p[i]) {i++;};
  266. found:                  }
  267.    
  268.     x=xnn;
  269.     xnn=xnn*s11[i]+ynn*s12[i]+s13[i];
  270.    
  271.     ynn=  x*s21[i]+ynn*s22[i]+s23[i];
  272.     //moved x=xnn;
  273.     if (xnn<xmax && xnn>0 & ynn>0 && ynn<ymax) { ekran[(int)xnn][(int)ynn]++; }
  274.    
  275.     }  
  276.     q++; sprintf (filename, "%d.jpg",q) ;dump(filename);
  277.    
  278.      return 0;
  279. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement