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

Source: Internet
Author: User
Tags acos cos

Below is a blog post by the famous blogger Matrix67, "Roast eggplant":

"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.

? 1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21st

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42//Note:compile with g++ filename.cpp-std=c++11

#include

#include

#include

#define DIM 1024

#define DM1 (DIM-1)

#define _SQ (x) ((x) * (x))//Square

#define _CB (x) ABS ((x) * (x) * (x))//absolute value of the cube

#define _CR (x) (unsigned char) (POW ((x), 1.0/3.0))//cube root

unsigned char GR (int,int);

unsigned char BL (int,int);

unsigned char RD (int i,int j) {

YOUR CODE Here

}

unsigned char GR (int i,int j) {

YOUR CODE Here

}

unsigned char BL (int i,int j) {

YOUR CODE Here

}

void Pixel_write (Int,int);

FILE *FP;

int main () {

fp = fopen ("mathpic.ppm", "WB");

fprintf (FP, "p6\n%d%d\n255\n", dim, dim);

for (int j=0;j

for (int i=0;i

Pixel_write (I,J);

Fclose (FP);

return 0;

}

void Pixel_write (int i, int j) {

static unsigned char color[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:

? 1

2

3

4

5

6

7

8

9

10

11unsigned char RD (int i,int j) {

Return (char) (_sq (cos (atan2 (j-512,i-512)/2) *255);

}

unsigned char GR (int i,int j) {

Return (char) (_sq (cos (atan2 (j-512,i-512)/2-2*acos (-1)/3)) *255);

}

unsigned char BL (int i,int j) {

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:

? 1

2

3

4

5

6

7

8

9

10

11

12unsigned char RD (int i,int j) {

#define R (N) (rand ()%n)

Static char C[1024][1024];return!c[i][j]?c[i][j]=!r (999) r (N): RD ((I+r (2))%1024, (J+r (2))%1024): C[i][j];

}

unsigned char GR (int i,int j) {

Static char C[1024][1024];return!c[i][j]?c[i][j]=!r (999) r (N): GR ((I+r (2))%1024, (J+r (2))%1024): C[i][j];

}

unsigned char BL (int i,int j) {

Static char C[1024][1024];return!c[i][j]?c[i][j]=!r (999) r (N): BL ((I+r (2))%1024, (J+r (2))%1024): C[i][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:

? 1

2

3

4

5

6

7

8

9

10

11unsigned char RD (int i,int j) {

Float X=0,y=0;int k;for (k=0;k++<256;) {float a=x*x-y*y+ (i-768.0)/512;y=2*x*y+ (j-512.0)/512;x=a;if (x*x+y*y>4) break;} Return log (k) *47;

}

unsigned char GR (int i,int j) {

Float X=0,y=0;int k;for (k=0;k++<256;) {float a=x*x-y*y+ (i-768.0)/512;y=2*x*y+ (j-512.0)/512;x=a;if (x*x+y*y>4) break;} Return log (k) *47;

}

unsigned char BL (int i,int j) {

Float X=0,y=0;int k;for (k=0;k++<256;) {float a=x*x-y*y+ (i-768.0)/512;y=2*x*y+ (j-512.0)/512;x=a;if (x*x+y*y>4) break;} Return 128-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:

? 1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20unsigned char RD (int i,int j) {

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-80)/800,3.);

}

unsigned char GR (int i,int j) {

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-80)/800,.7);

}

unsigned char BL (int i,int j) {

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-80)/800,.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!

? 1

2

3

4

5

6

7

8

9

10

11unsigned char RD (int i,int j) {

Static double K;k+=rand ()/1./rand_max;int L=k;l%=512;return l>255?511-l:l;

}

unsigned char GR (int i,int j) {

Static double K;k+=rand ()/1./rand_max;int L=k;l%=512;return l>255?511-l:l;

}

unsigned char BL (int i,int j) {

Static double K;k+=rand ()/1./rand_max;int L=k;l%=512;return l>255?511-l:l;

}

This is from Githubphagocyte's works:

  

Its code is as follows:

? 1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17unsigned char RD (int i,int j) {

Float s=3./(j+99);

Float y= (J+sin (i*i+_sq (j-700)/100./dim) *35) *s;

return (int (i+dim) *s+y)%2+int ((dim*2-i) *s+y)%2) *127;

}

unsigned char GR (int i,int j) {

Float s=3./(j+99);

Float y= (J+sin (i*i+_sq (j-700)/100./dim) *35) *s;

return (int (5* (I+dim) *s+y)%2+int (5* ((dim*2-i) *s+y))%2) *127;

}

unsigned char BL (int i,int j) {

Float s=3./(j+99);

Float y= (J+sin (i*i+_sq (j-700)/100./dim) *35) *s;

return (int (29* (I+dim) *s+y)%2+int (29* ((dim*2-i) *s+y))%2) *127;

}

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.

? 1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16unsigned 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=1

Return 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=f2) {b=f-1;} Else{++e%=4;d=e;if (!c[e]) {b=0; M=1;}}}} return M[I][J];

}

This last picture comes from Eric Tressler:

  

This is the Feigenbaum bifurcation graph obtained from the logistic map. And just like that, the corresponding code cleverly exploits the macro definition to save characters:

? 1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20unsigned char RD (int i,int j) {

#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 DIM

R BL (i,j) * (d-i)/D;

}

unsigned char GR (int i,int j) {

#define E DM1

#define F Static Float

#define G for (

#define H r=a*1.6/d+2.4;x=1.0001*b/d

R BL (i,j) * (D-J/2)/D;

}

unsigned char BL (int i,int j) {

F c[d][d];if (i+j<1) {A; B G;AD/2) {e=a;o= (e*x); c[e][o]+=0.01;}}}} R C (c[j][i]) *i/d;

}

Http://bbs.auto.sina.com.cn/112/thread-5135275-1-1.html

Http://city.club.sohu.com/zz2150/thread/3y4n2xf2kw9

Http://club2011.auto.163.com/post/100014970307.html

Http://bbs.pcauto.com.cn/topic-10250637.html

http://www.biyinjishi.com/kdoc/50100089/

http://www.biyinjishi.com/kdoc/50100097/

http://www.biyinjishi.com/kdoc/50100098/

Http://www.biyinjishi.com/kdoc/50100100/

http://www.biyinjishi.com/kdoc/50100101/

http://www.biyinjishi.com/kdoc/50100102/

http://www.biyinjishi.com/kdoc/50100096/

http://www.biyinjishi.com/kdoc/50100103/

http://www.biyinjishi.com/kdoc/50100090/

http://www.biyinjishi.com/kdoc/50100091/

http://www.biyinjishi.com/kdoc/50100092/

http://www.biyinjishi.com/kdoc/50100093/

http://www.biyinjishi.com/kdoc/50100094/

http://www.biyinjishi.com/kdoc/50100095/

http://www.biyinjishi.com/kdoc/50100099/

Http://www.cnblogs.com/biyinjishi/p/5329995.html

Http://www.cnblogs.com/biyinjishi/p/5329998.html

Http://www.cnblogs.com/biyinjishi/p/5330004.html

Http://www.cnblogs.com/biyinjishi/p/5330019.html

http://blog.itpub.net/30065054/viewspace-2065370/

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.