line.c 963 Bytes
#include "libfb.h"
/*---------------------------------------------------------------------
                  Make a line on the screen
  ---------------------------------------------------------------------*/
void
fbLine(int x0, int y0, int x1, int y1, u16 color)
{
  int	dx, dy, sx, sy, i, e;
  int	 x = x0;
  int	 y = y0;

  if (x1-x0 > 0){
    sx = 1;
    dx = x1-x0;
  } else if (x1-x0 < 0) {
    sx = -1;
    dx = x0-x1;
  } else {
    sx = 0;
    dx = 0;
  }

  if (y1-y0 > 0){
    sy = 1;
    dy = y1-y0;
  } else if (y1-y0 < 0) {
    sy = -1;
    dy = y0-y1;
  } else {
    sy = 0;
    dy = 0;
  }

  if (dx >= dy) {
    e = -dx;
    for (i=0; i<= dx; i++){
      fbPixel(x, y, color);
      x += sx;
      e += 2*dy;
      if (e>=0) {
	y += sy;
	e -= 2*dx;
      }
    }
  } else {
    e = -dy;
    for (i = 0; i <= dy; i ++){
      fbPixel(x, y, color);
      y += sy;
      e += 2*dx;
      if (e >= 0) {
	x += sx;
	e -= 2*dy;
      }
    }
  }    
}