Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Line Drawing

Name: Anonymous 2011-12-27 13:41

Does anybody know a faster line-drawing algorithm? This is Bresenham's, C++.

void draw_line(SDL_Surface *screen, int x1, int y1, int x2, int y2, Uint8 color)
{
  /* Draw a line from (x1, y1) to (x2, y2) */
  int dy = y2 - y1;
  int dx = x2 - x1;
  int stepx;
  int stepy;

  if (dy < 0)
  {
    dy = -dy;
    stepy = -1;
  }
  else
  {
    stepy = 1;
  }

  if (dx < 0)
  {
    dx = -dx;
    stepx = -1;
  }
  else
  {
    stepx = 1;
  }

  dy <<= 1;
  dx <<= 1;

  set_pixel(screen, x1, y1, color); /* Draw first pixel  */

  if (dx > dy)
  {
    int fraction = dy - (dx >> 1);
    while (x1 != x2)
    {
      if (fraction >= 0)
      {
        y1 += stepy;
        fraction -= dx;
      }

      x1 += stepx;
      fraction += dy;
      set_pixel(screen, x1, y1, color);
    }
  }
  else
  {
    int fraction = dx - (dy >> 1);
    while (y1 != y2)
    {
      if (fraction >= 0)
      {
        x1 += stepx;
        fraction -= dy;
      }

      y1 += stepy;
      fraction += dx;
      set_pixel(screen, x1, y1, color);
    }
  }
}


I heard there were faster ones, but not sure.

Name: Anonymous 2011-12-27 15:16

Don't do stupid crap like this:

  dy <<= 1;
  dx <<= 1;


Your compiler optimizes these for you.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List