Advertisement
Geometrian

Untitled

Aug 6th, 2014
604
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.34 KB | None | 0 0
  1. //From:
  2. //  http://codegolf.stackexchange.com/questions/35569/tweetable-mathematical-art
  3.  
  4. #include <iostream>
  5. #include <cmath>
  6.  
  7.  
  8. #define DIM 256
  9. #define DM1 (DIM-1)
  10.  
  11. #define _sq(x) ((x)*(x))                           // square
  12. #define _cb(x) abs((x)*(x)*(x))                    // absolute value of cube
  13. #define _cr(x) (unsigned short)(pow((x),1.0/3.0))  // cube root
  14.  
  15. #define L(X,Y,Z) sqrt(X*X+Y*Y+Z*Z)
  16. #define N(X,Y,Z) {float l=L(X,Y,Z);X/=l;Y/=l;Z/=l;}
  17. float mandelbulb_de(float x,float y,float z, int n) {
  18.     if (L(x,y,z)>7.0) return L(x,y,z)-6.0;
  19.  
  20.     float x2=x,y2=y,z2=z;
  21.     float dr = 1.0;
  22.     float r = 0.0; //initialization only to keep compiler happy
  23.  
  24.     #define POWER 8.0
  25.  
  26.     for (int i=0;i<n;++i) {
  27.         r = L(x2,y2,z2);
  28.         if (r>6.0) break;
  29.  
  30.         //convert to polar coordinates
  31.         float theta = acos(z2/r);
  32.         float phi = atan2(y2,x2);
  33.         dr = pow(r,POWER-1.0)*POWER*dr + 1.0;
  34.  
  35.         //scale and rotate the point
  36.         float zr = pow(r,POWER);
  37.         theta *= POWER;
  38.         phi *= POWER;
  39.  
  40.         //convert back to cartesian coordinates
  41.         float s_theta = sin(theta);
  42.         x2 = zr * s_theta*cos(phi) + x;
  43.         y2 = zr * s_theta*sin(phi) + y;
  44.         z2 = zr * cos(theta)       + z;
  45.     }
  46.  
  47.     return 0.5*log(r)*r/dr;
  48. }
  49. unsigned char RD(int i,int j) {
  50.     float x=0.0f, y=4.0f, z=0.0f;
  51.  
  52.     float dx = (float)(i)/DIM - 0.5f;
  53.     float dz = (float)(j)/DIM - 0.5f;
  54.     float dy = -1.0f;
  55.     N(dx,dy,dz)
  56.  
  57.     float total = 0.0f;
  58.     while (total<10.0f) {
  59.         float d = mandelbulb_de(x,y,z,500);
  60.         total += d;
  61.         x += d * dx;
  62.         y += d * dy;
  63.         z += d * dz;
  64.         if (d<=0.01f) {
  65.             //return 0xFFu*total/5.0;
  66.             float c = mandelbulb_de(x,y,z,500);
  67.             float nx = mandelbulb_de(x+0.001,y,z,500) - c;
  68.             float ny = mandelbulb_de(x,y+0.001,z,500) - c;
  69.             float nz = mandelbulb_de(x,y,z+0.001,500) - c;
  70.             N(nx,ny,nz)
  71.             return 0xFFu*fmax(0,-nz);
  72.         }
  73.     }
  74.     return 0x00u;
  75. }
  76. unsigned char GR(int i,int j) {
  77.     return RD(i,j);
  78. }
  79. unsigned char BL(int i,int j) {
  80.     return RD(i,j);
  81. }
  82.  
  83. FILE* fp;
  84. void pixel_write(int i, int j){
  85.     static unsigned char color[3];
  86.     color[0] = RD(i,j)&DM1;
  87.     color[1] = GR(i,j)&DM1;
  88.     color[2] = BL(i,j)&DM1;
  89.     fwrite(color, sizeof(unsigned char),3, fp);
  90. }
  91. int main(int argc, char* argv[]) {
  92.     fp = fopen("MathPic.ppm","wb");
  93.     fprintf(fp, "P6\n%d %d\n255\n", DIM,DIM);
  94.     for (int j=0;j<DIM;++j) {
  95.         for (int i=0;i<DIM;++i) {
  96.             pixel_write(i,j);
  97.         }
  98.     }
  99.     fclose(fp);
  100.     return 0;
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement