#include <math.h>
#include <helianthus.h>
#define WIDTH 285.0
#define HEIGHT 200.0
#define RES 10.0
#define THIKNESS 0.25
#define ALPHA 0.50
#define DIAMETER 10.0
int cols;
int rows;
double border;
Framebuffer framebuffer;
Animation fbAnim;
void generate() {
double d = DIAMETER;
double r = d/2.0;
saveState();
target(framebuffer);
clear();
zoom(RES);
translate(WIDTH/2.0, HEIGHT/2.0);
noFill();
double c = 1 - ALPHA;
stroke(colorByRGBA(c, c, c, 1));
strokeWidth(THIKNESS);
translate(-(cols-1)*d, -(rows-1)*d);
double xx = (cols-1)*d*2;
double yy = (rows-1)*d*2;
circle( 0, 0, r);
circle( 0, yy, r);
circle(xx, 0, r);
circle(xx, yy, r);
for(int y = 0; y < rows; ++y) {
double py = y*d*2 - r;
for(int x = 1; x < (cols-1)*4; ++x) {
arc(x*r-d, py, d, d, -120, 0);
arc(x*r , py, d, d, 60, 180);
}
arc(-r , py, d, d, 90, 150);
arc(0 , py, d, d, 60, 120);
arc(xx-d, py, d, d,-120, -60);
arc(xx-r, py, d, d, -90, -30);
}
for(int x = 0; x < cols; ++x) {
double px = x*d*2 - r;
for(int y = 1; y < (rows-1)*4; ++y) {
arc(px, y*r-d, d, d, -30, 90);
arc(px, y*r , d, d, 150, 270);
}
arc(px, -r , d, d, 180, 240);
arc(px, 0 , d, d, 150, 210);
arc(px, yy-d, d, d, -30, 30);
arc(px, yy-r, d, d, 0, 60);
}
viewportSave("data/output/generated-round-grid.png");
restoreState();
}
void init() {
double border = 2*THIKNESS;
cols = ((int)floor((WIDTH - 2*border)/DIAMETER + 0.001) + 1)/2;
rows = ((int)floor((HEIGHT - 2*border)/DIAMETER + 0.001) + 1)/2;
int w = ceil(WIDTH*RES - 0.001);
int h = ceil(HEIGHT*RES - 0.001);
framebuffer = createFramebuffer(w, h);
fbAnim = createAnimationFromFramebuffer(framebuffer);
generate();
}
void draw() {
double w = windowGetWidth();
double h = windowGetHeight();
if (keyWentDown("space")) generate();
saveState();
translate(w/2, h/2);
zoom(w/WIDTH);
fill(COLOR_WHITE);
noStroke();
rectTextured(fbAnim, -WIDTH/2.0, -HEIGHT/2.0, WIDTH, HEIGHT);
restoreState();
}
int main() {
windowSetVariableFrameRate();
windowSetResizable(TRUE);
windowSetInit(&init);
windowSetDraw(&draw);
windowRun();
}