Generate a picture of 1024x1024 with a code that is within 140 characters of three paragraphs

Source: Internet
Author: User
Tags acos cos pow rand sin

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.

#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#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 () {
int I, J;
fp = fopen ("mathpic.ppm", "WB");
fprintf (FP, "p6\n%d%d\n255\n", dim, dim);
for (j=0;j<dim;j++)
for (i=0;i<dim;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:

unsigned 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:

unsigned 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:

unsigned 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:

unsigned 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 using the two parameters of I and J at all.

unsigned 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:

unsigned 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. This 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.

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=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=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[I][J];
}

This last picture comes from Eric Tressler:

This is the Feigenbaum bifurcation graph obtained from the logistic map. Like just now, this code also cleverly exploits the macro definition to save characters:

unsigned 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;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]) *i/d;
} reprinted from:  http://www.matrix67.com/blog/archives/6039

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.