#include <stdio.h>
#include <string.h>
#include <math.h>

double centerx, centery, scale;
double xmin, xmax, ymin, ymax;
double xprec, yprec;
double xratio, yratio;
double fwidth, fheight;
double xlen, ylen;
int width;
int height;

void InitView(int w, int h)
{
  double aspectRatio;

  width = w; height = h;
  // _buffer = new unsigned char[4 * w * h];

  fwidth = width;
  fheight = height;
  aspectRatio = fwidth / fheight;

  centerx = 0;
  centery = 0;
  scale = 2.3;
  xlen = aspectRatio * scale;
  ylen = 1.0 * scale;

  xmin = -(xlen * 0.5) + centerx;
  xmax = -xmin;
  ymin = -(ylen * 0.5) + centery;
  ymax = -ymin;

  xprec = xlen / fwidth;
  yprec = ylen / fheight;
  xratio = fwidth / scale / aspectRatio;
  yratio = fheight / scale;
}

double roundX(double x) { return floor(x / xprec) * xprec; }
double roundY(double y) { return floor(y / yprec) * yprec; }

void render1(void)
{
  double i, j;
  int cnt;
  char usedx[1 + 512 + 1];
  char usedy[1 + 512 + 1];

  printf("render1():\n");
  memset(usedx, 0, sizeof usedx);
  memset(usedy, 0, sizeof usedy);

  printf("x's:\n");
  for (cnt = 0, i = xmin; i < xmax; i += xprec)
  {
    int x = ((roundX(i) * xratio) - (xmin * xratio));
    printf("%d ", x);
    cnt++;
    usedx[1 + x] = 1;
  }
  printf("\ncount: %d\n", cnt);
  for (cnt = 1; cnt <= 512; cnt++)
    if (!usedx[cnt])
      printf("missing x: %d\n", cnt - 1);

  printf("y's:\n");
  for (cnt = 0, j = ymin; j < ymax; j += yprec)
  {
    int y = ((roundY(j) * yratio) - (ymin * yratio));
    printf("%d ", y);
    cnt++;
    usedy[1 + y] = 1;
  }
  printf("\ncount: %d\n", cnt);
  for (cnt = 1; cnt <= 512; cnt++)
    if (!usedy[cnt])
      printf("missing y: %d\n", cnt - 1);
}

void render2(void)
{
  double i, j;
  int cnt;
  char usedx[1 + 512 + 1];
  char usedy[1 + 512 + 1];

  printf("render2():\n");
  memset(usedx, 0, sizeof usedx);
  memset(usedy, 0, sizeof usedy);

  printf("x's:\n");
  for (cnt = 0, i = xmin; i < xmax; i += xprec)
  {
    int x = ((i * xratio) - (xmin * xratio) + .5);
    printf("%d ", x);
    cnt++;
    usedx[1 + x] = 1;
  }
  printf("\ncount: %d\n", cnt);
  for (cnt = 1; cnt <= 512; cnt++)
    if (!usedx[cnt])
      printf("missing x: %d\n", cnt - 1);

  printf("y's:\n");
  for (cnt = 0, j = ymin; j < ymax; j += yprec)
  {
    int y = ((j * yratio) - (ymin * yratio) + .5);
    printf("%d ", y);
    cnt++;
    usedy[1 + y] = 1;
  }
  printf("\ncount: %d\n", cnt);
  for (cnt = 1; cnt <= 512; cnt++)
    if (!usedy[cnt])
      printf("missing y: %d\n", cnt - 1);
}

int main(void)
{
  InitView(512, 512);
  render1();
  render2();
  return 0;
}
