Skip to content

Instantly share code, notes, and snippets.

@ShadowfeindX
Created May 7, 2017 04:00
Show Gist options
  • Save ShadowfeindX/577a868fcec68bff467cc608b4303899 to your computer and use it in GitHub Desktop.
Save ShadowfeindX/577a868fcec68bff467cc608b4303899 to your computer and use it in GitHub Desktop.
#include "canvas.hxx"
//#define SPEED
Canvas * canvas = nullptr;
Painter * scene = new Painter;
double dA = 1.0, dB = 0.5,
f = 0.055, k = 0.062;
int cols;
int index(int x, int y) {
if (x < 0 || y < 0 || x >= cols || y >= cols)
return -1;
return x + y * cols;
}
double map(double value,
double istart,
double istop,
double ostart,
double ostop) {
return ostart + (ostop - ostart) * ((value - istart) / (istop - istart));
}
template <typename T>
void constrain(T & value,
T min,
T max) {
value = value < min ? min : value > max ? max : value;
}
#ifndef SPEED
Vector<Pair<double, double> > grid, next;
void setup()
{
cols = 200;
canvas->setFixedSize(cols,cols);
for (int i = 0; i < cols*cols; ++i) {
grid << makePair(1.0,0);
next << makePair(1.0,0);
} for (int i = cols/2; i < cols/2 + 10; ++i)
for (int j = cols/2; j < cols/2 + 10; ++j)
grid[index(i,j)].second = 1.0;
}
auto L = [](bool d, int x, int y){
double sum = 0.0;
for (int j = -1; j < 2; ++j)
for (int i = -1; i < 2; ++i)
if (index(x + i,y + j)+1) {
double c = d ? grid[index(x + i,y + j)].first
: grid[index(x + i, y + j)].second;
if (i == 0 && j == 0) sum += -1.0 * c;
else if (i != 0 && j != 0) sum += 0.05 * c;
else sum += 0.2 * c;
} return sum;
};
void draw()
{
scene->begin(canvas);
for (int y = 0; y < 200; ++y) {
for (int x = 0; x < 200; ++x) {
double a2, b2, a1 = grid[index(x,y)].first, b1 = grid[index(x,y)].second;
constrain<double>(a2 = a1 + ((dA * L(1,x,y)) - (a1 * b1 * b1) + (f * (1 - a1))), 0, 1);
constrain<double>(b2 = b1 + ((dB * L(0,x,y)) + (a1 * b1 * b1) - (b1 * (k + f))), 0, 1);
next[index(x,y)] = makePair(a2, b2);
scene->setPen(Color::fromRgb(map(a1,0,1,0,255),0,map(b1,0,1,0,255)));
scene->drawPoint(x,y);
}
} scene->end();
grid.swap(next);
}
#endif
#ifdef SPEED
Image grid, next;
void setup()
{
cols = 200;
canvas->setFixedSize(cols, cols);
grid = Image(cols, cols, Image::Format_ARGB32_Premultiplied);
grid.fill(::red);
next = Image(grid);
scene->begin(&grid);
scene->setPen(::NoPen);
scene->setBrush(::magenta);
scene->drawRect(cols/2 - 5, cols/2 - 5, 10, 10);
scene->end();
}
auto a = [](Rgb cell) -> double {
return map(Red(cell), 0, 255, 0, 1);
};
auto b = [](Rgb cell) -> double {
return map(Blue(cell), 0, 255, 0, 1);
};
auto L = [](bool d, int x, int y){
double sum = 0.0;
for (int j = -1; j < 2; ++j)
for (int i = -1; i < 2; ++i)
if (index(x + i,y + j)+1) {
double c = d ? a(grid.pixel(x + i, y + j))
: b(grid.pixel(x + i, y + j));
if (i == 0 && j == 0) sum += -1.0 * c;
else if (i != 0 && j != 0) sum += 0.05 * c;
else sum += 0.2 * c;
} return sum;
};
void draw()
{
for (int y = 0; y < cols; ++y) {
for (int x = 0; x < cols; ++x) {
double a2, b2, a1 = a(grid.pixel(x,y)), b1 = b(grid.pixel(x,y));
constrain<double>(a2 = a1 + ((dA * L(1,x,y)) - (a1 * b1 * b1) + (f * (1 - a1))), 0, 1);
constrain<double>(b2 = b1 + ((dB * L(0,x,y)) + (a1 * b1 * b1) - (b1 * (k + f))), 0, 1);
next.setPixelColor(x, y, Color(a2*255, 0, b2*255));
}
} scene->begin(canvas);
scene->drawImage(0,0,grid);
scene->end();
grid.swap(next);
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment