# Have you seen this interesting article?  —&gt;Tweetable Mathematical Art

Yesterday, I saw a interesting post, although it is a old one, but it is really interesting.

You can search it with “Tweetable Mathematical Art"

The content:

``````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[http://pastebin.com/uQkCQGhz], 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

Kyle McCormick
``````

In the post, I saw many types of code, but without Codea!

Why do not I create a codea version? So I port it to codea, the below is the codea code:

``````-- Codea Version: Tweetable Mathematical Art
-- Origin post: http://codegolf.stackexchange.com/questions/35569/tweetable-mathematical-art

displayMode(OVERLAY)

function setup()
rgbFunc = {}
colors = {}
DIM = 1024
DIM1 = DIM-1
MathPic = image(DIM,DIM)
print("Begin to create the art image...")
for j=0,DIM do
for i=0,DIM do
pixel_write(i,j)
end
end
print("Image is OK!!!")
end

function draw()
background(0)
sprite(MathPic,WIDTH/2,HEIGHT/2)
end

function pixel_write(i,j)
colors[1]=RD(i,j)&255 or 100
colors[2]=GR(i,j)&255 or 100
colors[3]=BL(i,j)&255 or 100
myColor = color(colors[1],colors[2],colors[3],255)
MathPic:set(i,j,myColor)
end

— I select a Julia set
— Here is the 3 functions
function RD(i,j)
local a,b,c,d,n=0,0,0,0,0
while (c+d<4 and n<880) do
c=a*a
d=b*b
n=n+1
b=2*a*b+j*8e-9-.645411
a=c-d+i*8e-9+.356888
end
return math.floor(255*math.pow((n-80)/800,3.))
end

function GR(i,j)
local a,b,c,d,n=0,0,0,0,0
while (c+d<4 and n<880) do
c=a*a
d=b*b
n=n+1
b=2*a*b+j*8e-9-.645411;a=c-d+i*8e-9+.356888
end
return math.floor(255*math.pow((n-80)/800,.7))
end

function BL(i,j)
local a,b,c,d,n=0,0,0,0,0
while (c+d<4 and n<880) do
c=a*a
d=b*b
n=n+1
b=2*a*b+j*8e-9-.645411;a=c-d+i*8e-9+.356888
end
return math.floor(255*math.pow((n-80)/800,.5))
end
``````

The origin info is below:

``````// Name: Julia set
// Author: Manuel Kasten
// C Code:
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);
}
``````

@binaryblues Thanks for sharing. Interesting article and code.

@dave1707 you are welcome!

I think Codea is the most fit one for this code show: it is very easy to write the small functions and see the result very soon.