# Tweetable Art Code

Andy Balaam
artificialworlds.net/blog

# Challenge

Create some Tweetable Mathematical Art.

• Draw a picture (1024x1024)
• Code must fit in 3 tweets
• One function to get red pixel values, another for green, another for blue
• Function returns the pixel value (0-1024) for a point
https://codegolf.stackexchange.com/questions/35569/tweetable-mathematical-art

# Example: red

```unsigned short RD( int i, int j ) {
return 1023;
}

unsigned short GR( int i, int j ) {
return 0;
}

unsigned short BL( int i, int j ) {
return 0;
}
```

```unsigned short RD( int i, int j ) {
return i;
}

unsigned short GR( int i, int j ) {
return i;
}

unsigned short BL( int i, int j ) {
return j;
}
```

Credit:

teh internets is made of catz

```int t[64],k=0,l,e,d=2e7;srand(time(0));while(k<64){t[k]=rand()%DIM;if((e=_sq(i-t[k])+_sq(j-t[42&k++]))<d)d=e,l=k;}return t[l];
```
```static int t[64];int k=0,l,e,d=2e7;while(k<64){if(!t[k])t[k]=rand()%DIM;if((e=_sq(i-t[k])+_sq(j-t[42&k++]))<d)d=e,l=k;}return t[l];
```
```static int t[64];int k=0,l,e,d=2e7;while(k<64){if(!t[k])t[k]=rand()%DIM;if((e=_sq(i-t[k])+_sq(j-t[42&k++]))<d)d=e,l=k;}return t[l];
```

(127 chars, 132 chars, 132 chars)

3 superimposed "Voronoi" diagrams

• Choose some sites (points)
• Regions are defined as "all points closer to this site than any other"

```unsigned short RD( int i, int j ) {

int t[64], // coords of 64 sites
k=0,   // loop counter
l,     // index of closest site
e,     // temporary
d=2e7; // distance to closest
```

```hacks
while(k<64){ // For each point
hacks that set up t[k]s
if(      // if shorter distance
( e = _sq( i - t[k] ) +
_sq( j - hack )
) < d
)
d=e, // store distance
l=k; // and remember index
}
```

```    // l is the index of the
// closest site.  Return
// the colour of that site.
return t[l]
}
```

```unsigned short GR( int i, int j ) {
unsigned short BL( int i, int j ) {

// Both same as red
// (except for hacks)
```

# Hacks

```int t[64],k=0,l,e,d=2e7;
// At the start of every call to RD
// reset the random seed.
// This gets us the same sites
// every time without storing them.
srand(time(0));
while(k<64)

// Just hope we don't cross
// a second boundary!
```

# Hacks

```while(k<64){
// Then the x coord is made
// by a call to rand()
t[k]=rand()%DIM;
if((e=_sq(...
```

# Hacks

```if(
( e = _sq( i - t[k] ) +
_sq( j - t[42&k++])
) < d
)
// The y coord is the x coord of
// the site with index m, where
// m = 00101010 & k

// Note m <= k, so
// t[m] is initialised.
```

# Hacks

```        ) < d
)
d=e, // comma avoids needing
l=k; // {} around these.
}
```

# Hacks

```
d=e,
l=k;
}
// The colour we return is
// the x coord of the site
return t[l];
}
```

Credit: githubphagocyte

Credit: user11153 (183 bytes over)

Credit: FireFly

Credit: cjfaure