0

I am currently working on a project on arduino which involves drawing a line on a 8x8 rgb led display, given 2 random coordinates, how do I determine which pixels between them should be painted?

Any help is apprecieated

crashback
  • 25
  • 1
  • 5

1 Answers1

1

Here is a simple implementation of the line drawing algorithm mentioned above.

Note that this example will only work if x1 is less than x2.

void drawLine(int x1, int y1, int x2, int y2) {
  int dx = x2 - x1;
  int dy = y2 - y1;

  for (int x = x1; x < x2; x++){ 
    int y = y1 + dy * (x - x1) / dx;
    plot(x, y);
  }
}

void plot(int x, int y) {
  // Draw each pixel
}
  • you can do the [same without `*,/` operations](https://stackoverflow.com/a/24682318/2521214) (just ignore the subpixels) which make it faster than bresenham on most modern CPU architectures and easily portable to higher dimensions – Spektre Nov 30 '22 at 10:51