Blame simple/neural/test-matrix.c

025224
025224
#include <string.h></string.h>
025224
#include <stdlib.h></stdlib.h>
025224
025224
#include <helianthus.h></helianthus.h>
025224
025224
025224
#define WIDTH   64
025224
#define HEIGHT  48
025224
#define COUNT   256
025224
#define CELL    16
025224
025224
025224
typedef struct {
025224
  int x, y, r;
025224
} Point;
025224
025224
025224
int matrix[HEIGHT][WIDTH];
025224
Point order[ (HEIGHT*2 - 1)*(WIDTH*2 - 1) ];
025224
025224
025224
void initOrder() {
025224
  for(int y = 1-HEIGHT; y < HEIGHT; ++y) {
025224
    for(int x = 1-WIDTH; x < WIDTH; ++x) {
025224
      Point *p = &order[(y + HEIGHT - 1)*(WIDTH*2 - 1) + x + WIDTH - 1];
025224
      p->x = x;
025224
      p->y = y;
025224
025224
      int sector = x >= 0 && y > 0 ? 1
025224
                 : y >= 0 && x < 0 ? 2
025224
                 : x <= 0 && y < 0 ? 3
025224
                 : y <= 0 && x > 0 ? 4
025224
                 : 0;
025224
      int subsector = sector % 2 ? abs(x) >= abs(y) : abs(y) >= abs(x);
025224
025224
      p->r = (x*x + y*y)*10 + sector*2 + subsector;
025224
    }
025224
  }
025224
025224
  int size = sizeof(order)/sizeof(*order);
025224
  Point p;
025224
  while(1) {
025224
    int found = 0;
025224
    for(int i = 1; i < size; ++i) {
025224
      Point *p1 = &order[i], *p0 = p1 - 1;
025224
      if (p0->r > p1->r) {
025224
        memcpy(&p, p0, sizeof(p));
025224
        memcpy(p0, p1, sizeof(p));
025224
        memcpy(p1, &p, sizeof(p));
025224
        found = 1;
025224
      }
025224
    }
025224
    if (!found) break;
025224
  }
025224
}
025224
025224
025224
void prepareMatrix(int x, int y) {
025224
  if (x < 0) x = 0;
025224
  if (x >= WIDTH) x = WIDTH-1;
025224
  if (y < 0) y = 0;
025224
  if (y >= HEIGHT) y = HEIGHT-1;
025224
  memset(matrix, 0, sizeof(matrix));
025224
  Point *p = order;
025224
  for(int i = 0; i < COUNT; ++i) {
025224
    int xx, yy;
025224
    do {
025224
      xx = x + p->x;
025224
      yy = y + p->y;
025224
      ++p;
025224
    } while(xx < 0 || yy < 0 || xx >= WIDTH || yy >= HEIGHT);
025224
    matrix[yy][xx] = 1;
025224
  }
025224
}
025224
025224
025224
void init() {
025224
  initOrder();
025224
  windowSetSize(CELL*WIDTH, CELL*HEIGHT);
025224
}
025224
025224
025224
void draw() {
025224
  noStroke();
025224
  prepareMatrix(mouseX()/CELL, mouseY()/CELL);
025224
  for(int y = 0; y < HEIGHT; ++y) {
025224
    for(int x = 0; x < WIDTH; ++x) {
025224
      fill( matrix[y][x] ? COLOR_BLUE : COLOR_LIGHTBLUE );
025224
      rect(CELL*(x + 0.1), CELL*(y + 0.1), CELL*0.8, CELL*0.8);
025224
    }
025224
  }
025224
}
025224
025224
025224
int main() {
025224
  windowSetVariableFrameRate(TRUE);
025224
  windowSetInit(&init);
025224
  windowSetDraw(&draw);
025224
  windowRun();
025224
  return 0;
025224
}
025224