# Tweetable Mathematical Art [closed]

Integer math can generate amazing patterns when laid out over a grid. Even the most basic functions can yield stunningly elaborate designs!

Write 3 Tweetable (meaning 140 characters or less) function bodies for the red, green, and blue values for a 1024x1024 image.

The input to the functions will be two integers i (column number for the given pixel) and j (row number for the given pixel) and the output will be an unsigned short between 0 and 1023, inclusive, which represents the amount of the given color present in the pixel (i,j).

For example, the following three functions produce the picture below:

/* RED */
return (unsigned short)sqrt((double)(_sq(i-DIM/2)*_sq(j-DIM/2))*2.0);
/* GREEN */
return (unsigned short)sqrt((double)(
(_sq(i-DIM/2)|_sq(j-DIM/2))*
(_sq(i-DIM/2)&_sq(j-DIM/2))
));
/* BLUE */
return (unsigned short)sqrt((double)(_sq(i-DIM/2)&_sq(j-DIM/2))*2.0);


/* RED */
return i&&j?(i%j)&(j%i):0;
/* GREEN */
return i&&j?(i%j)+(j%i):0;
/* BLUE */
return i&&j?(i%j)|(j%i):0;


# The Rules

• Given this C++ code, substitute in your functions. I have provided a few macros and have included the library, and you may include complex.h. You may use any functions from these libraries and/or my macros. Please do not use any external resources beyond this.
• If that version isn't working for you, make sure you're compiling with:

g++ filename.cpp -std=c++11


If that doesn't work, please use the alternate version using unsigned chars instead of unsigned shorts.

Michaelangelo has provided a cleaned up 24-bit or 48-bit color output version.

• You may implement your own version in another language, but it must behave in the same way as the provided C++ version, and only functions from C++'s built-ins, the library, or the provided macros may be used to make it fair.
• Please include either a smaller version or an embedded copy of your image. They are made into a ppm format and may need to be converted to another for proper viewing on stackexchange.
• Function bodies (not including signature) must be 140 characters or less.
• This is a popularity contest - most votes wins
• Added C++ tag because the nature of the rules excludes other languages. We generally prefer language-agnostic challenges unless they have a good reason to require a specific set. – algorithmshark Aug 2 '14 at 4:48
• To the close voters calling this too broad, please try writing an answer to this first. It's surprisingly restrictive... – trichoplax Aug 3 '14 at 22:24
• This is my favorite thing I've seen on here in, like, ever! – David Conrad Aug 4 '14 at 16:25
• I love that this question feels like an old-school demo scene. – mskfisher Aug 5 '14 at 13:31
• This type of question encourages participation in code golf. I'm generally disinclined to answer a straight golf question as I'm not confident of doing well. With this type of question the byte limit makes me try a simple answer, learn golfing techniques along the way, and then use them to make more complex answers. This is like a stepping stone into answering straight golf questions. I think it could be key in bringing more people in. – trichoplax Aug 7 '14 at 23:43

## Om nom nom

Continuing on my "classic games" theme, here's another entry... but not with pixel-art this time.

Better code management gave me space for more features, so it now features Inky, and also better colours and anti-aliasing. However, the suppression of the final cut-off dot had to go.

// RED
#define C(J,R)(sqrt(_sq((i+R)%(2*R)-R)+_sq(j-J))<R)
#define D(M,A,B)abs(i-M*50)<51?c?c-1?B:A:
#define Z 5;c%=3)v+=GR(3*i+k/3,3*j+k%3);return

// GREEN
int a=(i+30)/60%2,b=(i+150)/300;return b-1?C(1686,30)?b%2*a:C(1536,30)?a:abs(j-1611)<75?b%2:b%2*C(1536,150):C(1536,150)*(i-300<abs(j-1536));

// BLUE
static int c,v,k;for(c++,v=0,k=-4;k++<Z(D(2,1,1)0:D(6,1,0)0:D(10,1,.5).9:D(14,0,1).9:D(18,1,.7).2:1)*v*113;
#define RD BL
#define GR BL


• Very clever. Love it. Upvoted! – Rajkumar Madhuram Feb 2 '16 at 21:37

Colorful

Red:

function(i,j) {return (i+j)/4%256;}

Green:

function(i,j) { return (i+2*j)/4%256; }

Blue:

function(i,j) { return (2*i+j)/4%256; }


Diabolical

In this, I defined rad(x,y) = Math.sqrt(x*x + y*y);

Red:

Green:

function(i,j) { return 0; }

Blue:

function(i,j) { return 0; }


Construction Papers

Red:

function(i,j) {return (i%j)%256;}

Green:

function(i,j) { return (i*1.2%j)%256; }

Blue:

function(i,j) { return (i*1.5%j)%256; }


Cross on a hill

Red:

function(i,j) {
if(i>500&&i<524&&j>300&&j<600||i>400&&i<624&&j>400&&j<424)return 192;
if((1024-j)>425*cos((i-512)*0.2))return 126;return 0;
}

Green:

function(i,j) {
if(i>500&&i<524&&j>300&&j<600||i>400&&i<624&&j>400&&j<424)return 64;
if((1024-j)<425*cos((i-512)*0.2))return 200;return 192;
}

Blue:

function(i,j) {
if(i>500&&i<524&&j>300&&j<600||i>400&&i<624&&j>400&&j<424)return 0;
if((1024-j)>425*cos((i-512)*0.2))return 238;return 0;
}


## Binary Flash (C++)

Here's one I made with a modified version of your framework, since I apparently don't have a reasonable ppm viewer. This one uses lodepng. But first, the submission:

unsigned short red_fn(int i,int j){
double a=DM1-sqrt(_sq(i-DIM/2)*_sq(j-DIM/2)/8);return a<0?0:a;
}
unsigned short green_fn(int i,int j){
return (unsigned short)((i^j)/9*sqrt(_sq((i-DIM/2)/10.)*_sq((j-DIM/2)/10.)))%DM1/(1+3.*(i+j)/DIM);
}
unsigned short blue_fn(int i,int j){
return i&(~_sq(i/(j/10+1))|j);
}


(the quality of these uploads sucks apparently, so run the code yourself to see the result in its full glory)

## Triangular Limbo

I thought it couldn't hurt to submit a second one, a (thorough) modification of Binary Flash. Behold.

unsigned short red_fn(int i,int j){
return 256+128+( (int)(256*sin(5.*i/DIM*acos(-1)))^(int)(256*sin(5.*j/DIM*acos(-1))) )/(1+1.*(i+j)/DIM);
}
unsigned short green_fn(int i,int j){
return (int)(sqrt((i&(j-i))^j)/9*sqrt(_sq((i-DIM/2)/10.)*_sq((j-DIM/2)/10.)))%DM1/(7-2.*(i+j)/DIM);
}
unsigned short blue_fn(int i,int j){
return i<j?((int)sqrt(i*i+j*j)^(i&j/(i*j+1)))/2:(i&(j-i))^j;
}


My framework is as follows: (C++ code)

#include <stdlib.h>
#include <math.h>
#include <vector>
#include "lodepng.h"
#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 short)(pow((x),1.0/3.0))  // cube root

int main(){
int i,j;
std::vector<unsigned char> image;
image.resize(4*DIM*DIM);
for(j=0;j<DIM;j++){
for(i=0;i<DIM;i++){
image[4*(DIM*j+i)]=(double)(red_fn(i,j)&DM1)/DM1*255; //this is essentially the same as yours except that it transforms the [0,1023] range to [0,255] for lodepng
image[4*(DIM*j+i)+1]=(double)(green_fn(i,j)&DM1)/DM1*255;
image[4*(DIM*j+i)+2]=(double)(blue_fn(i,j)&DM1)/DM1*255;
image[4*(DIM*j+i)+3]=255;
}
}
lodepng::encode("MathPic.png",image,DIM,DIM);
return 0;
}


## Sharp Edges

Not safe for children.

## The code

unsigned char RD(int i,int j){
if(j<0){int k=9;while(1){if(i%(1<<k)==0){return k;}k--;}}return 255-(BL(i,(j+512)%DIM)|BL((i+512)%DIM,j));
}
unsigned char GR(int i,int j){
#define d(x,y,z,w) sqrt(_sq(x-z)+_sq(y-w))
#define A for(int k=8;k<DIM;k+=8){c[k]=RD(k,-1);}
return BL((j+512)%DIM,i);
}
unsigned char BL(int i,int j){
static int c[DIM];if(i+j<1){A}int p=0,k=0;if(j>512)j=DIM-j;for(;++k<DIM;){if(d(i,j,k,0)<pow(2,c[k]))p=1-p;}return 255*p;
}


I'm not really sure what to say about this one. There are a lot of ways to change the output based on this premise, but I can't find one that I personally find aesthetically pleasing.

# Projecting colors on a Hilbert curve

unsigned char RD(int i,int j){
int x,y,s=DIM/2,d=0;for(;s>0;s/=2){x=(i&s)>0;y=(j&s)>0;d+=s*s*((3*x)^y);if(!y){if(x){i=DM1-i;j=DM1-j;}i=i+j-(j=i);}}return(d/4);
}

unsigned char GR(int i,int j){
int x,y,s=DIM/2,d=0;for(;s>0;s/=2){x=(i&s)>0;y=(j&s)>0;d+=s*s*((3*x)^y);if(!y){if(x){i=DM1-i;j=DM1-j;}i=i+j-(j=i);}}return (d/32)>>2;
}

unsigned char BL(int i,int j){
int x,y,s=DIM/2,d=0;for(;s>0;s/=2){x=(i&s)>0;y=(j&s)>0;d+=s*s*((3*x)^y);if(!y){if(x){i=DM1-i;j=DM1-j;}i=i+j-(j=i);}}return(d/32)>>7;
}


Or a little red:

unsigned char RD(int i,int j){
int x,y,s=DIM/2,d=0;for(;s>0;s/=2){x=(i&s)>0;y=(j&s)>0;d+=s*s*((3*x)^y);if(!y){if(x){i=DM1-i;j=DM1-j;}i=i+j-(j=i);}}return(d/4);
}

unsigned char GR(int i,int j){
return ((int)RD(i,j)/8)>>2;
}

unsigned char BL(int i,int j){
return ((int)RD(i,j)/8)>>7;
}


# Be Happy

Palette is a little boring, but I don't have too much time to play with it and this is really just a novelty entry anyway.

edit -- this does seem to be OK on big endian machines. At least, i can traverse the pattern OK on one, so.... Thinking about it, I guess it makes sense; I'm only hard-coding the numerical values, not their bit representations.

unsigned char RD(int i,int j){
uint16_t p[]={0x1ffc,0x0ff0,0x03c0,0x03c0,0x2184,0x3006,0x2184,0,0,0,0x0100,0x1188,0xa3c4,0x03c1,0x0ff0,0x3ff8};
return p[j%16]&(1<<(i%16))?128:0;
}
unsigned char GR(int i,int j){
return RD(i,j)?(i+j)/8:0;
}
unsigned char BL(int i,int j){
return RD(i,j)?j/4:0;
}


I tweaked the pattern and used uint64_t for grins, it made the data smaller (less commas and 0x characters) but the code to access the bits got bigger. So I had to split the data into a #define in the red function and then declare and use it in the green function. Then I tweaked the color palette to ensure each smiley was drawn entirely in the same color. I like this a little better....

unsigned char RD(int x,int y){
#define d {0x44222002100C0FF0,0x800184218A514E71,0x4C39980980018001,0x7E00818318427E2}
return GR(x,y)?(x/16)*15+(y/16)*15:0;
}
unsigned char GR(int x,int y){
static uint64_t p[]=d;
x%=16;y%=16;
return p[y/4]&(((uint64_t)1)<<((y*16)%64+x))?128:0;
}
unsigned char BL(int x,int y){
return GR(x,y)?(x/16)*32+(y/16)*10:0;
#undef d
}


With this data line there's a little more separation between the smiley faces and I flipped the tertiary operator in the green function from "?128:0" to "?0:128" so it inverted the picture.

    #define d {0x1004080807F00000,0x400244224A522E72,0x27E4481240024002,0xFE010102188};


Ok, last one (I think, lol)...was bored, decided to see how far I could increase the size of the stipple map. Managed to get up to a 28x28 smiley with more #define trickery. The biggest tweak was the bit accessor, which could no longer take advantage of everything being evenly divided into 64 bits. To adjust, I had to compute the bit location in a separate variable, which bloated up the code 8 characters, pushing me really close to the edge.

So, here's the final image:

And the functions:

unsigned char RD(int x,int y){
#define d1 {0,0xFFE000FFF00000F8,0x7FFFF007FFFF007F,0x80FDFBF80FFFFF00,0xFF81FDFBF80F9F9F,
return BL(x,y)?(x/28)*15+(y/28)*15:0;
}
unsigned char GR(int x,int y){
#define d2 0xFFFFC3FFFFFC1FFF,0x3FFFFFC3FFFFFC3F,0x781EFFF783FFFFF8,0xFFF00F3FCF80E7FE,
return BL(x,y)?(x/28)*32+(y/28)*10:0;
}
unsigned char BL(int x,int y){
uint64_t p[]=d1 d2 0x7F0FF007E43F00FD,0x3FC0001FFF800,0x1F00,0};
x%=28;y%=28;
int b=y*28+x;
return p[b/64]&(((uint64_t)1)<<(b%64))?128:0;
}

• I love it - it's amazing what you can fit into 140 bytes – trichoplax Aug 7 '14 at 0:00
• As you mentioned potential viewing problems, just to let you know I see it as black circles with light eyes and mouth, and a pale colour gradient background. Is that the same for you? – trichoplax Aug 7 '14 at 0:01
• Are those 2 byte colours? You could probably golf it down even more if you use the char version of the code and used 1 byte colours - there's very little difference on a standard monitor (then you'd know it would work the same on different monitors...) – trichoplax Aug 7 '14 at 0:21
• @githubphagocyte, I see it the same and for the uint16_t array, it actually is 1 bit color (black/white). It's a 16x16 bit pattern, and the red function controls whether a bit is drawn or not. I give every drawn bit half-intensity red and for all bits drawn the green/blue is controlled by x,y coordinate. I could probably use uint64_t and lose a lot more of the 0x and comma characters (which is what bloated up using the char array), but this was nearly a direct conversion from an XBM file I created in Gimp. – DreamWarrior Aug 7 '14 at 1:35
• Ah - I understand now. I'll have to bear that in mind then - it could be useful in a char version too... – trichoplax Aug 7 '14 at 1:49

## Simple raycaster (now with invaders)

I might update this with a more interesting scene/better golfing, but this'll do for now. The way the shim forward-declares the green and blue functions, implying they could be called from the red function, inspired me to abuse cross-method-body macros a bit.

The raycaster only supports a single channel, i.e. "bluescale". :-)

// RED
#define A X=1.*i/DM1-.5,Y=1.*j/DM1-.5,L=sqrt(X*X+Y*Y+4),d=L-10/L,I=1./0,a=2/Y*L-L,r=d*d-L*L-4,b=fmin(-d-sqrt(r),-d+sqrt(r))
return 0;

// GREEN
#define B c=fmin(a>0?a:I,b>0?b:I)
#define F(v)(int)(v*a/L+1e6)&1
return 0;

// BLUE
float A,B;
return((c-b?F(2*X)^F(-5):fmod(9+3*atan2(3-2*b/L,-X-X*b/L),2)<1^fmod(9-6*asin(-Y-b*Y/L),2)<1)?1:.5)*(c-b?c==I?0:2*Y/L:-(d+b))*DM1;

• The define in the red channel (A) performs camera calculations, sends the ray towards the sphere, and performs ray-sphere and ray-plane intersection.

• The define in the green channel (B) finds which of the shapes were the closest and defines a helper macro used for the checkerboard pattern of the floor.

• Finally, the resulting code in the blue channel performs UV mapping (the checkerboard pattern) and stitches it all together.

githubphagocyte suggested I try to incorporate a Space Invaders sprite in my raycaster answer, but I thought a simple texture would be boring so I tried to raycast some 3D pixel art instead... I messed up somewhere, though, so the result isn't perfect. I'm still pretty happy with the result though. The lighting this time around is completely fake.

// RED
#define A float X=1.*i/DM1-.5,Y=1.*j/DM1-.5,L=sqrt(X*X+Y*Y+4),c=32*L-L,N=g(X*c/L,Y*c/L)&0<c,M,a=N?c:1e9,b=16/L*N
#define R return
R BL(i,j);

// GREEN
#define g(u,v)"\x5F\xB6\x5C\x98"[abs((int)(u+1e3)%8-4)]>>(int)(v+1e3)%8&1
#define h(d,u,v)if(66>2*d/L&2*d/L>62&g(u,v)&0<d&d<a)a=d,b=.2
R 0;

// BLUE
A;for(i=-16;i<16;i++){N=1.0315*i/Y*L-L,M=(N+L)*Y/X-L;h(N,X*N/L,i)+Y/L;h(N,X*N/L,i-1)-Y/L;h(M,i,Y*M/L)+X/L;h(M,i-1,Y*M/L)-X/L;}R b*DM1;


# Cellular Automata Overlay

This submission chooses three faux-random elementary cellular automata and iterates one in each color channel, starting with a single pixel in the top center of the image and progressing downwards. Halfway down when some rules meet the side of the image there is some corruption of the expected pattern because of missing data outside the image.

The faux-randomness changes once per second and could use some better constants. Many of the CA rules result in patterns approximating Sierpinski's Triangle, but there are many other patterns including at least one chaotic pattern (#30).

Code:

unsigned short red_fn(int i,int j){
static char p[1024][1026],e=e?e:257*time(0)&254|22;
return (p[j][i+1]=(j<1?i==512:(e&1<<p[j-1][i]*4+p[j-1][i+1]*2+p[j-1][i+2])>0))*DM1;
}
unsigned short green_fn(int i,int j){
static char p[1024][1026],e=e?e:511*time(0)&254|22;
return (p[j][i+1]=(j<1?i==512:(e&1<<p[j-1][i]*4+p[j-1][i+1]*2+p[j-1][i+2])>0))*DM1;
}
unsigned short blue_fn(int i,int j){
static char p[1024][1026],e=e?e:897*time(0)&254|22;
return (p[j][i+1]=(j<1?i==512:(e&1<<p[j-1][i]*4+p[j-1][i+1]*2+p[j-1][i+2])>0))*DM1;
}


Example output:

Thank you to Martin Büttner for the idea to use static variables within the functions for persistence and pixel lookup!

# Starry Night (C#)

Note: this uses RGB values 0...255

Image:

Code:

    private ushort generateRed(int i, int j)
{
var value = (Math.Tan((i) * Math.Pow(i, Math.Sqrt(j) * 0.1))); return (ushort)(value > 255 ? 255 : (value < 0 ? 0 : value));
}

private ushort generateGreen(int i, int j)
{
var value = (Math.Tan((i) * Math.Pow(i, Math.Sqrt(j) * 0.1))); return (ushort)(value > 255 ? 255 : (value < 0 ? 0 : value));
}

private ushort generateBlue(int i, int j)
{
var value = (Math.Tan((i) * Math.Pow(i, Math.Sqrt(j) * 0.1))); return (ushort)(value > 255 ? 255 : (value < 0 ? 0 : value));
}


The "stars" can be a little hard to make out so here is a link to the full-size image on imgur: http://i.stack.imgur.com/3o3Rd.png

Feel free to invent your own constellations from the image!

# Belousov-Zhabotinsky reaction

A simple simulation of this fascinating chemical reaction.

unsigned char RD(int i,int j){
#define R rand()%DIM
#define M m[(x+(d-1)%2+DIM)%DIM][(y+(d-2)%2+DIM)%DIM]
return(i+j)?140-BL(i,j)/3:0;}

unsigned char GR(int i,int j){
#define I if(m[x][y]==15)for(d=4;d--;)M
return(i+j)?80+BL(i,j)/3:0;}

unsigned char BL(int i,int j){static long m[DIM][DIM],n,x,y,d;if(i+j<1){for(n=1.2e8;n--;x=R,y=R){I=M?M:15;if(m[x][y])m[x][y]--;if(!(R||R))m[x][y]=15;}}return m[i][j]*16;}


The way the program works is simpler than you might expect. Each pixel is either zero, maximum, or somewhere in between.

• Zero: Nothing is happening but very occasionally spontaneously change to maximum
• Maximum: Will change any zero pixels adjacent to it to maximum, and then itself fade to maximum - 1
• Somewhere in between: Will fade by 1 until it reaches zero

This means that a point of maximum will spread out leaving a wake of somewhere in between behind it, which prevents it growing back against the direction of the wavefront.

• This is incredible. +10 if I could! – DLosc Aug 18 '14 at 21:29
• @DLosc thank you! It's actually simpler to program than it looks - I'd better add an explanation... – trichoplax Aug 19 '14 at 14:00
• The other thing I didn't understand till reading the code is that it doesn't propagate via a straightforward x & y loop but samples points randomly. Otherwise, I think you would end up with very geometric diamond patterns. – DLosc Aug 19 '14 at 21:36
• @DLosc yes processing pixels in raster order would give much more regular angular patterns, and also a bias towards growing left and down as the growth "surfs" the raster direction. – trichoplax Aug 20 '14 at 10:14
• How would you loop it? I would quite like see it go on for longer than your gif. – Beta Decay Aug 20 '14 at 10:29

# Playing with the Schwefel function

unsigned short RD(int i,int j){
unsigned short res=0;
res+=-i*sin(sqrt(i))+M_PI/2;
res+=-j*sin(sqrt(j));
res*=M_PI;
res=abs(res)/DIM;
return res;
}
unsigned short GR(int i,int j){
unsigned short res=0;
res+=-i*sin(sqrt(i)+M_PI/4*2);
res+=-j*sin(sqrt(j));
res*=M_PI;
res=abs(res)/DIM;
return res;
}
unsigned short BL(int i,int j){
unsigned short res=0;
res+=(-i*sin(sqrt(i)));
res+=(-j*sin(sqrt(j)+M_PI/4*2));
res*=M_PI;
res=abs(res)/DIM;
return res;
}


## Animation

Done by adding (pi/4)*k to the sin argument, with k iterating over [0,8] (a period)

# Stripes

JavaScript, with appropriate definitions of DIM and some Math functions:

function RD(x,y){
return x<0 ? 1+pow(round(((x+1)%DIM+DIM/2+1.5*BL(x,y))/(1+10*y/DIM)/5),3) : GR(sin(RD(x-DIM,y)),-1)
}

function GR(x,y){
return y<0 ? floor(DIM*(1.5+x)/2.5) : GR(sin(1+RD(x-2*DIM,y)),-1)
}

function BL(x,y) {
return x<0 ? 1.5*cos(x*x+y*y) : GR(sin(2+RD(x-3*DIM,y)),-1)
}


I had fun doubling up the uses of each function here. RD with a negative x computes the stripes, BL blurs the boundaries a little, and GR with a negative y scales the color values to the right range. If you look closely, you can see that the blurring is done independently for each color component:

Making the blur function a cosine wave instead gives a field of infinite lasagna:

function RD(x,y){
return x<0 ? 1+pow(round((x+1+DIM/2+BL(x,y))/(0.25+10*y/DIM)/6),3) : GR(sin(RD(x-DIM,y))+1,-1)
}

function GR(x,y){
return y<0 ? floor(DIM*(2+x)/4) : GR(sin(1+RD(x-DIM,y))+1,-1)
}

function BL(x,y) {
r=y/cos(x/DIM+0.5); return x<0 ? 12*cos(16*sqrt(r))*r/DIM : GR(sin(2+RD(x-DIM,y))-0.5,-1)
}


Tweaking RD can make the width of the stripes nonlinear:

function RD(x,y){
return x<0 ? 1+pow(round(((x+1)%DIM+DIM/2+BL(x,y))/(exp(3*y/DIM))/6),3) : GR(sin(RD(x-DIM,DIM-y))+0,-1)
}

function GR(x,y){
return y<0 ? floor(DIM*(2+x)/4) : GR(sin(1+RD(x-2*DIM,DIM-y))+1,-1)
}

function BL(x,y) {
return x<0 ? 1.5*cos(x*x+y*x)*y/256 : GR(sin(2+RD(x-3*DIM,DIM-y))-0,-1)
}


function RD(x,y){
pi=acos(-1);return x<0 ? 1+pow(round((x+DIM/2+BL(x,y))/(cos(4*pi*(y/DIM-0.5)+pi)+2)/8),3) : GR(sin(RD(x-DIM,y)),-1)
}

function GR(x,y){
return y<0 ? floor(DIM*(2+x)/4) : GR(sin(1+RD(x-DIM,y))-1,-1)
}

function BL(x,y) {
return x<0 ? 0 : GR(sin(2+RD(x-DIM,y)),-1)
}


My favorite for last. This was a "beautiful mistake," when I was experimenting with different blur functions and made the amplitude really big. The full PNG is too awesome large for StackExchange, so here's the code, a JPG version, and a close-up:

function RD(x,y){
return x<0 ? 1+pow(round(((x+1)%DIM+DIM/2+BL(x,y))/(0.25+10*y/DIM)/6),3) : GR(sin(RD(x-DIM,y))+1,-1)
}

function GR(x,y){
return y<0 ? floor(DIM*(2+x)/4) : GR(sin(1+RD(x-2*DIM,y))+1,-1)
}

function BL(x,y) {
return x<0 ? 21*cos(x*x+y*y)*pow(1+y/128,0.5) : GR(sin(2+RD(x-3*DIM,y))-0.5,-1)
}


Who knew that an unassuming cos(x*x+y*y) term could do that?!

• Was this in Javascript? – Beta Decay Aug 22 '14 at 15:34
• @BetaDecay Yes, edited. – DLosc Aug 22 '14 at 15:59

# Fractal Carpets

I wanted to do Sierpinski's carpet, but dividing by 3 would be a bit of a pain in a 1024x1024 image. So--why not use a 4x4 pattern instead?

Full props to user1455003's JavaScript framework, since I don't have C++ available. I made a couple extra definitions to emulate the OP's macros:

var DIM = 1024;
var DM1 = DIM - 1;
var floor = Math.floor;


First, a plain blue 4x4 Sierpinski carpet:

function red(x,y){
return 0
}

function green(x,y){
return 0
}

function blue(x,y) {
for(b=256;b>=1;b/=4){a='1111100110011111';m=floor(x%(b*4)/b);n=floor(y%(b*4)/b);if(a[m+4*n]<1)return 0}
return DM1
}


(Open images in separate tab for full resolution.)

Next I tweaked the color scheme by giving squares a little color if they were removed in later steps:

function red(x,y){
return 0
}

function green(x,y){
return 0
}

function blue(x,y) {
for(b=DIM/4,k=0;b>=1;b/=4,k++){a='1111100110011111';m=floor(x%(b*4)/b);n=floor(y%(b*4)/b);if(a[m+4*n]<1)break}
return DM1*k/5
}


Now it's time to overlay different patterns for each color!

function red(x,y){
for(b=DIM/4,k=0;b>=1;b/=4,k++){a='1111000100010001';m=floor(x%(b*4)/b);n=floor(y%(b*4)/b);if(a[m+4*n]<1)break}
return DM1*k/5
}

function green(x,y){
for(b=DIM/4,k=0;b>=1;b/=4,k++){a='0000011001100000';m=floor(x%(b*4)/b);n=floor(y%(b*4)/b);if(a[m+4*n]<1)break}
return DM1*k/5
}

function blue(x,y) {
for(b=DIM/4,k=0;b>=1;b/=4,k++){a='1000100010001111';m=floor(x%(b*4)/b);n=floor(y%(b*4)/b);if(a[m+4*n]<1)break}
return DM1*k/5
}


Checkerboard: 1010010110100101, 0101101001011010, 0101101001011010

1001011001101001, 0000011001100000, 0110100110010110

0100001000100001, 0000011111100000, 0010010001001000

Of course, we need some Sierpinski triangles too: 1111011100110001, 0001001101111111, 1000110011101111

# Wavy Chessboard (Python)

DIM = 1024

def red(i, j):
i+=sin(4*pi*j/DIM)*DIM/84;i=max(1,min(i,DIM-1))
return ceil(4+sin(8*pi*i/DIM)*sin(8*pi*j/DIM)*3)*32

def green(i, j):
return red(i,j)

def blue(i, j):
return red(i,j)-cos(4*pi*i/DIM)*16


The code uses PIL to generate an image, so the functions return a value out of 256, not 1024.

• This makes me feel vaguely queasy – James_pic Aug 12 '14 at 10:24
• This would make good wallpaper for a room for someone you hate. – Eric Tressler Aug 14 '14 at 15:27

# Because why not?

I happened to stumble upon a cool map. It iteratively applies cosine, sine, cosine, sine,.... starting with some initial value.

    unsigned short int RD(int i, int j){
float x=i*j;for(int k=0;k++<15;){x=k%2==0?cos(x):sin(x);}return (DIM-1)*x;
}
unsigned short GR(int i,int j){
float x=i*j;for(int k=0;k++<15;){x=k%2==0?cos(x):sin(x);}return (DIM-1)*x;
}
unsigned short BL(int i,int j){
return 0;
}


Also, remember Happy Numbers? Well, I thought it would be cool to see the distribution of the first 1048576 happy numbers. To fill the other two streams, I used happy numbers where we take the sum of the cubes of the digits instead of the squares, and Harshad Numbers.

    unsigned short int RD(int i, int j){
int h=DIM*j+i+1;int t=h;for(int k=0; k<DIM; k++){int s=0;while(h!=0){s=s+_sq(h%10);h=h/10;}h=s;if(h==1)return DM1;if(h==t)break;}return 0;
};
unsigned short int GR(int i, int j){
int h=DIM*j+i+1;int t=h;for(int k=0; k<DIM; k++){int s=0;while(h!=0){s=s+_cb(h%10);h=h/10;}h=s;if(h==1)return DM1;if(h==t)break;}return 0;
};
unsigned short int BL(int i, int j){
int h=DIM*j+i+1;int s=0;int t=h;while(h!=0){s=s+h%10;h=h/10;}if(t%s==0)return DM1;return 0;
}

• The first one looks trippy while scrolling (at least on my screen)! :) – Gaffi Aug 8 '14 at 20:34

# TweetArt - A Mac App

Sorry for the unoriginal, boring name

I created this app because if this contest is all about tweets, why not use some actual tweets from actual users? Just enter a @username to pull the first tweet from that user. From this tweet, the tweet then goes through an entropy generator (I'm not sure if that's the correct usage of the word but in this context it just means its randomness), which outputs a random number between 0 and 1024. If you run the same tweet through, you'll get the same number. This then can be used as sort of seed for a math function/equation, or used directly as a value.

For example:

• As the seed for srand(); or another PRNG.
• Used to generate coordinates and a zoom level for a fractal.
• As the hue for a color. e.g. e % 255 = color.

The possibilities are endless!

So what else can this app do?

• Save as both PNG and BMP. So no need for any other applications to view your art.
• Fully C++ compatible wrapped in Objective C i.e. Objective C++.
• Supports multiple equations.
• Randomize equations to generate art truly random.
• Easily add new equations with the help of Objective-C's awesome built-in reflection support.

Here's a screenshot:

As you can see, Martin Buttner's art does not look exactly like his submission, but that's because I've modified it slightly to add the effects of entropy. I've included seven submissions from users whose submissions I particularly enjoyed. Not to discriminate of course but adopting an equation which I have absolutely no understanding of how it works, is time consuming so I stopped at seven.

So how do I add an equation you might be wondering? Well there are just a few simple steps:

1. In ArtEquations.h, copy and paste the three methods for each channel and change the number to the next number in the list. For example, adding one new art thing will add three new methods called:

 -(unsigned char)r8WithEntropy:(int)e column:(int)i row:(int)j;
-(unsigned char)g8WithEntropy:(int)e column:(int)i row:(int)j;
-(unsigned char)b8WithEntropy:(int)e column:(int)i row:(int)j;


If this syntax looks alien to you, it's pretty simple. unsigned char is the return type, and the words followed by colons make up the message name (in this example, the whole message name is r8WithEntropy:column:row:. The (int)x are the parameters associated with each message fragment. For each art thing you add, simply increment the number in the first message part by 1 r9, r10, r11, etc.

2) In ArtEquations.mm (the file extension for ObjC++ implementation files), under number of equations, increment that number to how every many art things you have (how intutive!).

3) In the EquationNames, add a name for your art using the syntax of @"Name", and add it to the end before the bracket, and separate the names by commas.

4) At the bottom of the file, paste in the three methods you added from step 1., and replace the semicolons with open and closed braces.

5) Inside those braces, add your C++ code! Add all your C++ goodness right inside that Objective-C method. Cool right? If you need to call say r8 from g8, feel free to define a Macro for it with the syntax of: #define RD(I,J) [self r8WithEntropy:e column:I row:J]. And replace RD with whatever you need and r8 with whatever channel you need.

6) And that's it! That's all you need. run the application and your art thing should show up right in the picker.

If you don't have Xcode, see this link for how to compile an Xcode project from the command line.

Todo:

• I can currently use only unsigned chars. If anyone wants to figure out how to use unsigned shorts, please fork and comment. I'll gladly merge.
• Maybe pull code directly from this website??? :)
• Add as many arts as I can!
• Clear const values after generation. Currently, because this doesn't just exit after generation as in the other ones, consts aren't cleared. If anyone knows how to clear them, please tell me.

Here's a few pictures I saved, feel free to add more. I don't actually have the tweets, but if you add more, please add the tweets and functions so we can generate them ourselves :)

Also I'd like to give credit to faubiguy because I used his art in the icon:

• While this is a really great idea and you've probably put a lot of effort in it, this is technically not an answer to the question. I think this belongs in comment with a link to the GitHub repo and all the content of your post could go in the README.md over there. – Martin Ender Aug 16 '14 at 14:37
• @MartinBüttner Thanks for the comment. You're right, this isn't really an answer, so I'll remove at and add it as a comment, I'm just worried it'll get buried in the 36 comments that already exist. – Milo Aug 19 '14 at 7:17
• I never would have noticed this had it not been posted as an answer. Milo, maybe you could post an actual answer (in order to technically comply with the rules) and then include your application below it as a bonus? – Todd Lehman Aug 22 '14 at 20:50
• @ToddLehman that's a good idea. I made it community wiki because it's still not really an answer but I'll do that for sure. – Milo Aug 22 '14 at 22:10

# Simple StackOverflow "Logo"

(i know it would be a terrible logo, but i don't know how to call it)
Definitely not something as cool as rest of images - it's rather proof concept that i made to check whether it is possible to place some text (in this case shortcut of "stackoverflow" - letters "SO") on image and draw something more interesting than very simple background. C++ version, compiled and tested on Mac OS X 10.9.3 with clang.

unsigned short RD(int i,int j){
int d=sqrt(8*_sq(i-450)+_sq(j-450));
return(((GR(i,j)>254||BL(i,j)>254))|(d<=100&d>80&i<=450))*255;
}
unsigned short GR(int i,int j){
int d=sqrt(8*_sq(i-575)+_sq(j-540));
return(d<=190&d>170)?255:fmin(254, (10*(j+150.*sin(j*15./DIM))/++i));
}
unsigned short BL(int i,int j){
int d=sqrt(8*_sq(i-450)+_sq(j-630));
return(((GR(i,j)>254))|(d<=100&d>80&i>=450))?255:fmin(254, 8*(DIM-i)/++j);
}


To run just copy this code inside "framework" and run:

clang++ code_golf_tweetable_math_art.cpp -std=c++11 && ./a.out && convert MathPic q.bmp && open q.bmp


Note that you have to have Image Magick installed and MathPic file created.

• Really impressive you got the letters to show! – Rajkumar Madhuram Aug 9 '14 at 4:48

# Plaid Trip

unsigned char RD(int i,int j){
return i<512||j<512?4096*sin((i/((j%512)+1)))+1024*sin((j/((i%512)+1))):sqrt(4096*sin((i/((j%512)+1)))*1024*sin((j/((i%512)+1))));
}
unsigned char GR(int i,int j){
i=(1023-i);
return 2048*sin((i/((j%256)+1)))-512*sin((j/((i%256)+1)));
}
unsigned char BL(int i,int j){
i+=1024;j+=1024;
return 1024*sin((i/((j%128)+1)))-256*sin((j/((i%128)+1)));
}

• First thing i thought when i saw this - psychedelic ;) – cyriel Aug 6 '14 at 22:55

# Marbelous

The actual output image is rather mundane compared to other entries here, the equivalent of about 25 bytes of C. I had more fun writing the main loop than the R/G/B functions.

Note that spaces between cells are optional, included here for clarity. The Gren function is actually 53 bytes of code, and the other two about that much combined. There's definitely more that could be done with the remaining space, since I've only used about 1/4 of it in total. Specifically:

Multiplication may be more compact with bit shifts instead of decrements.

I haven't added square or cube or cube root helpers, as the challenge allows.

I haven't made use of recursion.

PS: My main function only makes a 256x256 image because Marbelous is an 8 bit language. I could easily modify the program to output 16 tiled copies of the result. Making it operate on 10-bit integers would be significantly more challenging (and would require a faster interpreter).

# generates a 256x256 24bit RGB image
# pseudocode:
# print "P6\n256 256 255\n"
# for y in range(256)
#  for x in range(256)
#   print Redd(y,x)
#   print Gren(y,x)
#   print Blue(y,x)
.. .. .. .. .. .. P0 P1 .. .. .. .. .. ..
.. .. .. .. P2 .. 00 // .. .. PX PY .. ..
.. .. .. .. .. Nx /\ .. .. .. Ba il .. ..
.. .. .. .. 00 P0 .. /\ .. .. XX .. .. ..
.. .. .. P2 /\ .. PA .. /\ .. .. .. .. ..
.. .. .. .. .. // .. PB .. PC .. .. .. ..
.. .. .. .. /\ .. .. .. .. .. .. .. .. ..
.. .. PD /\ PE \\ PF .. .. .. .. .. .. ..
.. .. .. PA .. .. .. .. .. .. .. .. .. ..
.. .. .. .. .. .. .. .. .. .. .. .. .. ..
.. .. .. .. .. PB .. .. .. .. .. .. .. ..
.. .. .. .. PE .. PF .. PC .. 32 35 35 0A
.. PD .. .. .. .. .. .. /\ .. 32 35 36 20
PX /\ .. .. .. .. .. .. .. PY 32 35 36 20
.. .. Re dd Gr en Bl ue .. .. 50 36 0A ..
.. .. .. .. .. .. .. .. .. .. .. .. .. ..
:Nx
# O0 = I0+1
# overflow bit to the left side
I0 00 01
++ O< S0
=0 S0 O<
O0 O0 ..
:Bail
# if I0==255 and I1==255 then output 1
I0 .. I1 ..
++ .. ++ ..
=0 ++ =0 ..
XX O0 XX O0
:Redd
# ((I0/2)+(~I1))*4
I0
>> I1
.. !!
.. //
<<
<<
O0
:Gren
# I0*I1
I0 P0 .. P1 I1
\\ >0 S0 \\ S0
.. S1 -- 00 /\ P1
.. \/ P0 S1
.. .. .. O0
:Blue
# ((I0)+(I1/2))*4;
I1
>> I0
.. //
<<
<<
O0


# Powers FTW!

Returning to the question's language, I started playing about with pow(). These images look better than my Python submissions because I was programming on my phone. I just got bored after waiting five minutes for a random scattering of dots to be generated so I posted the best of the lot.

One thing led to another, and suddenly some nice looking stripes appeared. The ppm looks nicer than the png, I think Imagemagick messed it up a bit...

unsigned short RD(int i,int j){
return pow(i+j, 0.25);
}
unsigned short GR(int i,int j){
return pow(i+j, 0.5);
}
unsigned short BL(int i,int j){
return pow(i+j, 1/3);
}


I stayed with pow(), finding nice patterns as the different frequencies overlapped. This time I didn't go for nth root, making it a lot harder to get a decent image. Eventually, using small numbers worked.

unsigned short RD(int i,int j){
return pow(1.009, i-j)+pow(1.008, i+j);
}
unsigned short GR(int i,int j){
return pow(1.01, i-j)+pow(1.007, i+j);
}
unsigned short BL(int i,int j){
return pow(1.02, i-j)+pow(1.006, i+j);
}


## Edit:

Thanks to Milo, he notified me that when I was performing the cube root, I wasn't formatting it as a float. Here's the updated image and code:

unsigned short RD(int i,int j){
return pow(i+j, 0.25);
}
unsigned short GR(int i,int j){
return pow(i+j, 0.5);
}
unsigned short BL(int i,int j){
return pow(i+j, 1.0/3.0);
}


• I really like that first image! The blend of the coloring looks very modern! BTW 1/3 evaluates to zero (or it should at least. Add a dot (.) after 1 to make it evaluate to 0.3 repeating. – Milo Aug 19 '14 at 7:14
• @Milo Hey thanks, I've edited it now – Beta Decay Aug 19 '14 at 9:34
• It looks even better now with all the colors! – Milo Aug 19 '14 at 9:37
• @Milo If you look away slightly, it looks like it's moving – Beta Decay Aug 19 '14 at 9:39

# JavaScript

Use code like that of user1455003 to view these full-scale.

## 101

function red(x,y){
return (x*y) % 1024;
}
function green(x,y){
return x-y;
}
function blue(x,y) {
return (x+y) % 1024;
}


## 202

function red(x,y){
return x*Math.cos(x);
}
function green(x,y){
return y*Math.cos(y);
}
function blue(x,y) {
return x*y/Math.tan(x*y) % 1024;
}


In full scale, the red and green don't form these stripes, they're just gradients over a series of thin lines. For compensation, the blue patterns look more intriguing, especially in the upper left corner.

## Sparkling

function red(x,y){
return 0.7*(Math.pow(3.25, Math.log(x*y)) % 1024);
}
function green(x,y){
return 0.3*(Math.pow(2.75, Math.log(Math.pow(Math.log(x),Math.log(y)))) % 1024);
}
function blue(x,y) {
return Math.pow(3, Math.log(x*y)) % 1024;
}


I don't if I've followed your code exactly, but here's what I've managed to put together:

import Image, math

# Functions here

img = Image.new('RGB', (1024, 1024), "black")

for i in range(img.size[0]):
for j in range(img.size[1]):
pixels[i, j] = (r(i,j),g(i,j),b(i,j))

img.save('/storage/emulated/0/image.jpg', 'JPEG')


## Argyle Christmas Sweater - 177 chars

def r(x,y):
return int(math.tan(x+y))
def g(x,y):
return int(math.tan(x-y))
def b(x,y):
return int(math.tan(x*y))


I don't exactly know what the blue does... The pattern is nice though.

## Christmas Stars/Snow - 102 chars

As you can see, I'm keeping to a Christmas theme. You either interpret this as a starry night or snow falling.

def r(x,y):
return int(math.tan(x*y)*10)
def g(x,y):
return r(x,y)
def b(x, y):
return r(x,y)


# Zebra Stripes

Ugh, lol this challenge is killing me and my work productivity, haha! Good thing it's slow right now.

Anyway, stealing from the random ideas; this guy walks the blue up or down a small amount. After half a row in the same "direction" (512 "stable" pixels), it has a 25% chance of "turning around". The red and green use the same generator. Result:

With the red "out of phase", it makes for some colorful stripes (which I personally like better). To get this, just add 128 to the return value of the red function.

Toying with placing the red/green out of phase different amounts yields various other results. Here, I subtract 50 from the red and add 95 to the green.

Here's the code:

unsigned char RD(int i,int j){
#define M if(s++>512){d*=(rand()%1000)>750?-1:1;s=0;}c+=(rand()/1./RAND_MAX)*(float)d;
return BL(i,j);
}
unsigned char GR(int i,int j){
#define C if(c>255||c<0){d*=-1;c+=d;s=0;}
return BL(i,j);
}
unsigned char BL(int i,int j){
static float c=0;static int d=1,s=0;
M C return((int)c);
}


If you find any neat alignments for red/green, post 'em in the comments! You can also toy with the constants in M; the >512 changes the "stability" (it'll be less likely to have long strings of increasing/decreasing intensities) while the >750 is 750/1000 (75%) chance (due to the %1000 on rand) the direction stays the same (or 25% chance it changes, depending on how you look at it).

Creating something "nice" and interesting takes some balancing of the stability and random chance it switches after reaching stability. For example, with the last example, taking the "stability" down to 100 makes the image look like this:

While the original, with the chance for change up'd to 90% (replace >750 with >100), creates this:

Have fun! I think I'm done now, haha. I'm beginning to have a love/hate relationship with this challenge, lol!

# JavaScript

Use user1455003's code to display the image.

function red(x, y) {
return (x - Math.cos(y)) % 256;
}

function green(x, y) {
return (Math.sqrt(x * y)) % 256;
}

function blue(x, y) {
return Math.sqrt(x + y + x * y) % 256;
}


# Generated by tweets

I had no idea what to do, but I wanted to participate.

So I though that I could transform tweets into formulae.

I take the first tweet searching for red/green/blue and transform letters into cos/sin/i/j/digits. Then depending on the number, I add or mult\ iply to the next item. I then shorten the formula to be ≤140.

Results are pretty poor though… The transformation should be improved to include more operations, but I had fun.

Sometimes, during compilation, I have:

warning: integer overflow in expression [-Woverflow]


Sample functions:

return fmod((114*i+100*i+109*i+cos(99)*j+cos(110)*122*cos(sin(sin(sin(sin(sin(sin(sin(sin(cos(cos(110)))))))))))*98*115*112),1024);}
return fmod((109*112*sin(cos(i))+100*104*233*114*i+114*i+cos(224)*cos(108)*cos(cos(sin(sin(sin(cos(85))))))*77*80),1024);}
return fmod((114*i+100*i+109*i+cos(99)*j+cos(110)*122*cos(sin(sin(sin(sin(sin(sin(sin(sin(cos(cos(110)))))))))))*98*115*112),1024);}
return fmod((j+114*100*i+110*76*j+j+i+115*i+114*i+100*cos(cos(cos(i)))+108*j+115*109*cos(106)*j+j+114*100*i+110),1024);}
return fmod((cos(cos(114))*sin(103)*84*sin(sin(cos(110)))*98*115*112*sin(cos(79))*108*j+119*i+sin(j)+114*116*i+115*84*119*i+i+116),1024);}
return fmod((cos(116)*cos(115)*cos(103)*i+110*103*103*114*i+i+110*98*j+j+j+j),1024);}


Some images:

I will update if I generate more interesting patterns.

Oh, almost forgot the python code:

from requests import session
from BeautifulSoup import BeautifulSoup

s = session()

def transform(t):
l1 = [str(ord(i)) if (i not in 'aeiouy') else 'i' if i in 'aei' else 'j' for i in t]
l2 = [i if (i in 'ij') or int(i)>75 else 'cos' if int(i)<50 else 'sin'  for i in l1]
res = ''
cpt = 0
for l in l2:
if l in ('sin','cos'):
res += l + '('
cpt +=1
else:
if l in 'ij':
ope = '+'
else:
ope = '*'
res += l+')'*cpt + ope
cpt = 0
res = res[-119:]
while (res[0] not in '+*'):
res = res[1:]
while (res[-1] not in '+*'):
res = res[:-1]
return 'return fmod(('+res[1:-1]+'),1024);'

r,g,b = [i.findAll("p","js-tweet-text tweet-text") for i in (r,g,b)]

rgb = [transform(i.text) for i in (r[0],g[0],b[0])]

with open('filename.cpp', 'r') as f, open('filename.res.cpp', 'w') as g:
for l in f:
if('// YOUR CODE HERE' not in l):
g.write(l)
else:
g.write(rgb[0])
rgb=rgb[1:]

• The last picture you posted is an optical illusion; the top and bottom edges look significantly slanted to the lower right. – Eric Tressler Aug 8 '14 at 19:32
• These are some really beautiful textures. What percentage of tweets result in good output like these? (Also, this made me wonder if tweets could be interpreted as Pyth code. ;) Might not fit in 140 characters, though.) – DLosc Aug 8 '14 at 22:04