Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Cool nuggets of code

Name: Anonymous 2010-08-28 23:57

Starting with a C one, feel free to post yours.


 #include <stdio.h>
 #include <math.h>

 double jdist(double Zx, double Zy, double Cx, double Cy ,  int iter_max)
 {
 int i;
 double x = Zx, /* Z = x+y*i */
         y = Zy,
         /* Zp = xp+yp*1 = 1  */
         xp = 1,
         yp = 0,
         /* temporary */
         nz, 
         nzp,
         /* a = abs(z) */
         a;
 for (i = 1; i <= iter_max; i++)
  { /* first derivative   zp = 2*z*zp + 1 = xp + yp*i; */
    nz = 2*(x*xp - y*yp) + 1;
    yp = 2*(x*yp + y*xp);
    xp = nz;
    /* z = z*z + c = x+y*i */
    nz = x*x - y*y + Cx;
    y = 2*x*y + Cy;
    x = nz;
    /* */
    nz = x*x + y*y;
    nzp = xp*xp + yp*yp;
    if (nzp > 1e60 || nz > 1e60) break;
  }
 a=sqrt(nz);
 /* distance = 2 * |Zn| * log|Zn| / |dZn| */
 return 2* a*log(a)/sqrt(nzp);
 }
 /* ------------------------------------------------------*/
 int main(void)
 {
  const double Cx=-0.74543;
        const double Cy=0.11301;
 /* screen ( integer) coordinate */
        int iX,iY;
        const int iXmax = 10000;
        const int iYmax = 10000;
        /* world ( double) coordinate = parameter plane*/
        const double ZxMin=-2.;
        const double ZxMax=2.0;
        const double ZyMin=-2.0;
        const double ZyMax=2.0;
        /* */
        double PixelWidth=(ZxMax-ZxMin)/iXmax;
        double PixelHeight=(ZyMax-ZyMin)/iYmax;
         /* color component ( R or G or B) is coded from 0 to 255 */
        /* it is 24 bit color RGB file */
        const int MaxColorComponentValue=255;
        FILE * fp;
        char *filename="progorp.ppm";
        char *comment="# ";/* comment should start with # */
        static unsigned char color[3];
        double Zx, Zy,    /* Z=Zx+Zy*i   */
               Z0x, Z0y,  /* Z0 = Z0x + Z0y*i */
               Zx2, Zy2; /* Zx2=Zx*Zx;  Zy2=Zy*Zy  */
        /*  */
        int Iteration;
        const int IterationMax=2000;
        /* bail-out value , radius of circle ;  */
        const int EscapeRadius=400;
        int ER2=EscapeRadius*EscapeRadius;
         double distanceMax=PixelWidth/15; /*jdist( 0,0,Cx,Cy, IterationMax);*/
    /*create new file,give it a name and open it in binary mode  */
        fp= fopen(filename,"wb"); /* b -  binary mode */
        /*write ASCII header to the file*/
        fprintf(fp,"P6\n %s\n %d\n %d\n %d\n",comment,iXmax,iYmax,MaxColorComponentValue);
     /* compute and write image data bytes to the file*/
        for(iY=0;iY<iYmax;++iY)
        {
              Z0y=ZyMax - iY*PixelHeight; /* reverse Y  axis */
                 if (fabs(Z0y)<PixelHeight/2) Z0y=0.0; /*  */   
             for(iX=0;iX<iXmax;++iX)
             {    /* initial value of orbit Z0 */
                Z0x=ZxMin + iX*PixelWidth;
                  /* Z = Z0 */
                  Zx=Z0x;
                  Zy=Z0y;
                 Zx2=Zx*Zx;
                  Zy2=Zy*Zy;
                 /* */
                 for (Iteration=0;Iteration<IterationMax && ((Zx2+Zy2)<ER2);Iteration++)
                        {
                            Zy=2*Zx*Zy + Cy;
                            Zx=Zx2-Zy2 +Cx;
                            Zx2=Zx*Zx;
                            Zy2=Zy*Zy;
                        };
               /* compute  pixel color (24 bit = 3 bytes) */
               if (Iteration==IterationMax)
                        { /*  interior of Julia set  = black */
                           color[0]=0;
                           color[1]=0;
                           color[2]=0;                          
                        }
               else /* exterior of Filled-in Julia set  =  */
                       {  double distance=jdist(Z0x,Z0y,Cx,Cy,IterationMax);
                          if (distance<distanceMax)
                          { /*  Julia set  = white */
                             color[0]=255; /* Red*/
                             color[1]=255;  /* Green */
                             color[2]=255;/* Blue */
                          }
                          else
                          { /*  exterior of Julia set  = black */
                           color[0]=0;
                           color[1]=0;
                           color[2]=0;                          
                        };
                       }
                   /* check the orientation of Z-plane */
                   /* mark first quadrant of cartesian plane*/    
              /*     if (Z0x>0 && Z0y>0) color[0]=255-color[0];  */
            /*write color to the file*/
                  fwrite(color,1,3,fp);
                }
        }
        fclose(fp);
        printf("file saved ");
        getchar();
        return 0;
  }

Name: Anonymous 2010-08-29 1:24

No exceptions.

PS: Your code is shit. It makes me too lazy to use BBCode.

Name: Anonymous 2011-02-04 12:54

Don't change these.
Name: Email:
Entire Thread Thread List