
//
//
//   雰囲気だけ（わらぃ
//    本当にこれらの関数を使うなら，あと dot() 関数も必要です。
//
//

#define WIDTH  640
#define HEIGHT 480


// ブレゼンハム
void bresenham( int x1, int y1, int x2, int y2 ) {
  int dx = x2 - x1;  if (dx < 0) dx = -dx;
  int dy = y2 - y1;  if (dy < 0) dy = -dy;
  int t, sign = 1;

  if (dx >= dy) {

    if (x2 < x1) {
      t = x1;  x1 = x2;  x2 = t;
      t = y1;  y1 = y2;  y2 = t;
    }
    if (y2 < y1) sign = -1;

    t = 0;
    while ( x1 <= x2 ) {
      if (0<=x1 && x1<WIDTH && 0<=y1 && y1<HEIGHT) {
        dot( x1, y1 );
      }
      if ( (t += dy) >= dx) {
        y1 += sign;
        t -= dx;
      }
      ++x1;
    }

  } else {

    if (y2 < y1) {
      t = x1;  x1 = x2;  x2 = t;
      t = y1;  y1 = y2;  y2 = t;
    }
    if (x2 < x1) sign = -1;

    t = 0;
    while ( y1 <= y2 ) {
      if (0<=x1 && x1<WIDTH && 0<=y1 && y1<HEIGHT) {
        dot( x1, y1 );
      }
      if ( (t += dx) >= dy) {
        x1 += sign;
        t -= dy;
      }
      ++y1;
    }
  }
}


// 固定小数点
void fixedpoint( int x1, int y1, int x2, int y2 ) {
  int dx = x2-x1, dy=y2-y1;
  int abs_dx = (dx<0) ? -dx : dx;
  int abs_dy = (dy<0) ? -dy : dy;

  if ( abs_dy <= abs_dx) {

    if (x2 < x1) {
      int t=x1; x1=x2; x2=t;
      t = y1; y1=y2; y2=t;
    }

    y1 <<= 4;
    dy <<= 4;

    while ( x1 <= x2 ) {
      if (0<=x1 && x1<WIDTH && 0<=y1 && y1 < (HEIGHT<<4)) {
        dot( x1, y1 >> 4 );
      }
      x1 += 1;
      y1 += dy/dx;
    }

  }else{

    if (y2 < y1) {
      int t=y1; y1=y2; y2=t;
      t = x1; x1=x2; x2=t;
    }

    x1 <<= 4;
    dx <<= 4;

    while ( y1 <= y2 ) {
      if (0<=x1 && x1<(WIDTH<<4) && 0<=y1 && y1 < HEIGHT) {
        dot( x1 >> 4, y1 );
      }
      y1 += 1;
      x1 += dx/dy;
    }

  }
}


