Blob Blame Raw

#include <string.h>
#include <stdlib.h>

#include <helianthus.h>


#define WIDTH   64
#define HEIGHT  48
#define COUNT   256
#define CELL    16


typedef struct {
  int x, y, r;
} Point;


int matrix[HEIGHT][WIDTH];
Point order[ (HEIGHT*2 - 1)*(WIDTH*2 - 1) ];


void initOrder() {
  for(int y = 1-HEIGHT; y < HEIGHT; ++y) {
    for(int x = 1-WIDTH; x < WIDTH; ++x) {
      Point *p = &order[(y + HEIGHT - 1)*(WIDTH*2 - 1) + x + WIDTH - 1];
      p->x = x;
      p->y = y;

      int sector = x >= 0 && y > 0 ? 1
                 : y >= 0 && x < 0 ? 2
                 : x <= 0 && y < 0 ? 3
                 : y <= 0 && x > 0 ? 4
                 : 0;
      int subsector = sector % 2 ? abs(x) >= abs(y) : abs(y) >= abs(x);

      p->r = (x*x + y*y)*10 + sector*2 + subsector;
    }
  }

  int size = sizeof(order)/sizeof(*order);
  Point p;
  while(1) {
    int found = 0;
    for(int i = 1; i < size; ++i) {
      Point *p1 = &order[i], *p0 = p1 - 1;
      if (p0->r > p1->r) {
        memcpy(&p, p0, sizeof(p));
        memcpy(p0, p1, sizeof(p));
        memcpy(p1, &p, sizeof(p));
        found = 1;
      }
    }
    if (!found) break;
  }
}


void prepareMatrix(int x, int y) {
  if (x < 0) x = 0;
  if (x >= WIDTH) x = WIDTH-1;
  if (y < 0) y = 0;
  if (y >= HEIGHT) y = HEIGHT-1;
  memset(matrix, 0, sizeof(matrix));
  Point *p = order;
  for(int i = 0; i < COUNT; ++i) {
    int xx, yy;
    do {
      xx = x + p->x;
      yy = y + p->y;
      ++p;
    } while(xx < 0 || yy < 0 || xx >= WIDTH || yy >= HEIGHT);
    matrix[yy][xx] = 1;
  }
}


void init() {
  initOrder();
  windowSetSize(CELL*WIDTH, CELL*HEIGHT);
}


void draw() {
  noStroke();
  prepareMatrix(mouseX()/CELL, mouseY()/CELL);
  for(int y = 0; y < HEIGHT; ++y) {
    for(int x = 0; x < WIDTH; ++x) {
      fill( matrix[y][x] ? COLOR_BLUE : COLOR_LIGHTBLUE );
      rect(CELL*(x + 0.1), CELL*(y + 0.1), CELL*0.8, CELL*0.8);
    }
  }
}


int main() {
  windowSetVariableFrameRate(TRUE);
  windowSetInit(&init);
  windowSetDraw(&draw);
  windowRun();
  return 0;
}