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

struct _img {
  unsigned int w;
  unsigned int h;
  unsigned char *ds;
};

struct _rect {
  int x;
  int y;
  unsigned int w;
  unsigned int h;
};

struct _pnt {
  int x;
  int y;
};

struct _img *
create_img(unsigned int w, unsigned int h)
{
  struct _img *usr_img;

  usr_img = malloc(sizeof(struct _img));

  usr_img->ds = malloc((w * h * 1) * sizeof(unsigned char));
  memset(usr_img->ds, 0, (w * h * 1) * sizeof(unsigned char));

  usr_img->w = w;
  usr_img->h = h;

  return usr_img;
}

void
free_img(struct _img *img)
{
  free(img);
}

void
draw_rect(struct _img *img, struct _rect *rect, unsigned char value)
{
  int _x = rect->x;
  int _y = rect->y;
  unsigned int _w = rect->w;
  unsigned int _h = rect->h;

  int x;
  int y;
  unsigned long n;

  for (y = _y; y < (_y + _h); y++) {
    for (x = _x; x < (_x + _w); x++) {
      if (x >= 0 && x < img->w) {
        if (y >= 0 && y < img->h) {
          n = (y * img->w) + x;
          img->ds[n] = value;
        }
      }
    }
  }
}

unsigned int
dist(struct _pnt *p1, struct _pnt *p2)
{
  int x1;
  int y1;
  int x2;
  int y2;
  int dx;
  int dy;

  int sq_dist;

  x1 = p1->x;
  y1 = p1->y;

  x2 = p2->x;
  y2 = p2->y;

  dx = x2 - x1;
  dy = y2 - y1;

  sq_dist = (dx * dx) + (dy * dy);

  return (int) sqrt(sq_dist);
}

void
draw_circ(struct _img *img, struct _pnt *c, unsigned int r, unsigned char value)
{
  unsigned int w = img->w;
  unsigned int h = img->h;

  int cx;
  int cy;

  int _x1;
  int _y1;
  int _x2;
  int _y2;

  int x;
  int y;

  unsigned int d;
  unsigned long n;

  cx = c->x;
  cy = c->y;

  _x1 = cx - r;
  _y1 = cy - r;
  _x2 = cx + r;
  _y2 = cy + r;

  for (y = _y1; y < _y2; y++) {
    for (x = _x1; x < _x2; x++) {
      struct _pnt p;
      p.x = x;
      p.y = y;
      d = dist(c, &p);
      if (x >= 0 && x < w) {
        if (y >= 0 && y < h) {
          if (d < r) {
            n = (y * img->w) + x;
            img->ds[n] = value;
          }
        }
      }
    }
  }
}

void
print_img(struct _img *img)
{
  unsigned int x;
  unsigned int y;
  unsigned int w;
  unsigned int h;
  unsigned char d;
  unsigned long n;
  w = img->w;
  h = img->h;
  printf("P1\n");
  printf("%d %d\n", w, h);
  for (y = 0; y < h; y++) {
    for (x = 0; x < w; x++) {
      n = (y * w) + x;
      d = img->ds[n];
      if (x == 0)
        printf("%d", d);
      else
        printf(" %d", d);
    }
    printf("\n");
  }
}

int
main1()
{
  unsigned int w;
  unsigned int h;
  struct _img *img;
  struct _rect my_rect;

  my_rect.x = 20;
  my_rect.y = 20;
  my_rect.w = 80;
  my_rect.h = 30;

  w = 120;
  h = 80;

  img = create_img(w, h);
  draw_rect(img, &my_rect, 1);
  print_img(img);
  free_img(img);
  return 0;
}

int
main2()
{
  unsigned int w;
  unsigned int h;
  struct _img *img;
  struct _rect my_rect;
  struct _pnt c1;
  unsigned int r1;

  w = 100;
  h = 100;

  r1 = 30;

  c1.x = 40;
  c1.y = 40;

  img = create_img(w, h);
  draw_circ(img, &c1, r1, 1);
  print_img(img);
  free_img(img);
  return 0;
}

int
main()
{
  //return main1();
  return main2();
}

