Is there a piece of code that makes you think human wisdom can be brilliant?

Source: Internet
Author: User
Tags acos

Is there a piece of code that makes you think human wisdom can be brilliant?

Citing a blog post from a well-known blogger, Matrix67:

"Generate a picture of 1024x1024 with a code within three 140 characters"

Kyle McCormick launched a game called Tweetable Mathematical Art on Stackexchange, and contestants need to use three of these long codes to generate a picture.
Specifically, entrants need to write the RD, GR, BL three functions in the C + + language, and each function cannot exceed 140 characters. Each function receives the I and J two integer parameters (0≤i, j≤1023) and then needs to return an integer between 0 and 255, representing the color value of the pixel at (i, j). For example, if RD (0, 0) and GR (0, 0) return 0, but BL (0, 0) returns 255, then the pixel in the top-left corner of the image is blue.
The code written by the entrant will be inserted into the program below (I made some minor changes) and will eventually generate a picture of size 1024x1024.

//Note:compile with g++ filename.cpp-std=c++11#include <iostream> #include <cmath> #include <cstdlib> #define DIM 1024#define DM1 (DIM-1) #define _SQ (x) ((x) * (x)) //Square#define _CB (x) ABS ((x) * (x) * (x))//absolute value of Cube#define _CR (x) (unsigned char) (POW ( (x), 1.0/3.0))//cube root unsigned CharGR (int,int);unsigned CharBL (int,int);unsigned CharRD (intIintj) {//YOUR code here}unsigned char GR (int i,int j) {//YOUR code here}unsigned char BL (int i,int j) {//YOUR code her E}voidPixel_write (int,int); FILE *FP;intMain () {fp = fopen ("mathpic.ppm","WB");fprintf(FP,"p6\n%d%d\n255\n", dim, dim); for(intj=0; j<dim;j++) for(intI=0; i<dim;i++) Pixel_write (I,J); Fclose (FP);return 0;}voidPixel_write (intIintj) {Static unsigned Charcolor[3]; color[0] = RD (i,j) &255; color[1] = GR (i,j) &255; color[2] = BL (i,j) &255; Fwrite (Color,1,3, FP);}

I chose some of my favorite works, put it below and share with you.
The first is a work from Martin Büttner:

Its code is as follows:

unsigned char RD (intIintj) {return(char) (_sq (COS (atan2 (j-512,i-512)/2))*255);} unsigned char GR (intIintj) {return(char) (_sq (COS (atan2 (j-512,i-512)/2-2*acos(-1)/3))*255);} unsigned char BL (intIintj) {return(char) (_sq (COS (atan2 (j-512,i-512)/2+2*acos(-1)/3))*255);}

The same works from Martin Büttner:

This is currently the first work in the provisional rankings. Its code is as follows:

unsigned char RD(int i,int j){#define r(n)(rand()%n)static char c[1024][1024];return!c[j]?c[j]=!r(999)?r(256):RD((i+r(2))%1024,(j+r(2))%1024):c[j];}unsigned char GR(int i,int j){static char c[1024][1024];return!c[j]?c[j]=!r(999)?r(256):GR((i+r(2))%1024,(j+r(2))%1024):c[j];}unsigned char BL(int i,int j){static char c[1024][1024];return!c[j]?c[j]=!r(999)?r(256):BL((i+r(2))%1024,(j+r(2))%1024):c[j];}

The following picture still comes from the hand of Martin Büttner:

It is hard to imagine that Mandelbrot fractal graphics can only be drawn with such a little code:

unsigned char RD (intIintj) {Floatx=0,y=0;intK for(k=0;k++< the;) {float a=x*x-y*y+ (I-768.0)/ +;y=2*x*y+ (J-512.0)/ +;x=a;if(x*x+y*y>4) Break;}return Log(k)*47;} unsigned char GR (intIintj) {Floatx=0,y=0;intK for(k=0;k++< the;) {float a=x*x-y*y+ (I-768.0)/ +;y=2*x*y+ (J-512.0)/ +;x=a;if(x*x+y*y>4) Break;}return Log(k)*47;} unsigned char BL (intIintj) {Floatx=0,y=0;intK for(k=0;k++< the;) {float a=x*x-y*y+ (I-768.0)/ +;y=2*x*y+ (J-512.0)/ +;x=a;if(x*x+y*y>4) Break;}return  --Log(k)*23;}

Manuel Kasten also made a Mandelbrot set of pictures, unlike just now, the graph depicts the result of the Mandelbrot set in a local magnified place:

Its code is as follows:

unsigned char RD (intIintj) {Double a=0, b=0, c,d,n=0; while((c=a*a) + (d=b*b) <4&&n++<880) {b=2*a*b+j*8e-9-.645411; a=c-d+i*8e-9+.356888;}return 255*pow((n the)/ -,3.);} unsigned char GR (intIintj) {Double a=0, b=0, c,d,n=0; while((c=a*a) + (d=b*b) <4&&n++<880) {b=2*a*b+j*8e-9-.645411; a=c-d+i*8e-9+.356888;}return 255*pow((n the)/ -,.7);} unsigned char BL (intIintj) {Double a=0, b=0, c,d,n=0; while((c=a*a) + (d=b*b) <4&&n++<880) {b=2*a*b+j*8e-9-.645411; a=c-d+i*8e-9+.356888;}return 255*pow((n the)/ -,.5);}

This is another work of Manuel Kasten:

The code to generate this image is interesting: The function relies on the static variable to control the process of painting, without the use of both the I and J Parameters!

Here is the code for it:

unsigned CharRD (intIintj) {Static DoubleK;k+=rand ()/1./rand_max;intl=k;l%= +;returnL>255?511-l:l;}unsigned CharGR (intIintj) {Static DoubleK;k+=rand ()/1./rand_max;intl=k;l%= +;returnL>255?511-l:l;}unsigned CharBL (intIintj) {Static DoubleK;k+=rand ()/1./rand_max;intl=k;l%= +;returnL>255?511-l:l;}

This is another work from Githubphagocyte:

This is a picture obtained using the diffusion-limited aggregation model, which takes a lot of time to run. The code is interesting: the clever use of macro definitions, breaking the boundaries between functions and functions, the word limit of three pieces of code can be combined to use.

It's code:

unsigned char RD (int i,int j) {#define D dim#define M m[(x+d+ (d==0)-(d==2))%d][(y+d+ (d==1)-(d==3))%d] #define R rand ()%d#define B m[x][y]return (i+j) 256-(BL (i,j))/2:0;} unsigned char GR (int i,int j) {#define A static int m[D][D],e,x,y,d,c[4],f,n;if (i+j<1) {for (d=d*d;d;d--) {m[d%d][d/d]=D%6?0:rand ()%2000?1:255;} For (N=1return RD(i,J);} unsigned Char BL (int i,int J) {A; n; n+ +) {x=r;y=r;if (b==1) {f=1;for (d=0;d<4;d++) {c[d]=m;f=f<c[d]?c[d]:f;} if (f>2) {b=f-1;} Else{++e%=4;d=e;if (!c[e]) {b=0; M=1;}}}} return m[j];}

This last picture comes from Eric Tressler:

This is the Feigenbaum bifurcation graph obtained from the logistic map. As before, the corresponding code also cleverly exploits the macro definition to save characters.

Here is the code for it:

UnsignedCharRD (intIintj) {#define A float a=0,b,k,r,x#define B int e,o#define C (x) x>255?255:x#define R return#define D dimr BL (i,j) * (d-i)/D;UnsignedCharGR (intIintj) {#define E dm1#define F static float#define G for (#define H r=a*1.6/d+2.4;x= 1.0001*b/dr BL (i,j) * (D-J/2)/D;UnsignedCharBL (intIintj) {F c[d][d];if(i+j<1) {A; B g;a<d;a+=0.1) {G b=0; b<d;b++) {H; G k=0; k<d;k++) {x=r*x* (1-X);if(k>d/2) {e=a;o= (e*x); c[e][o]+=0.01;}}}}} R C (c[j]) *i/d;}

Is there a piece of code that makes you think human wisdom can be brilliant?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.