Bresenham Line Drawing Calculator, works for some coordinates and not for others why?

if input Point A(1,1) and B(4,7) does not work correctly.

should be:
1,1
2,2
2,3
3,4
3,5
4,6
4,7

I get:
1,1
2,2
3,3
4,4

int X=1;
int Y=1;
int Xn=4;
int Yn=7;
int Pk=0;
int Pkn=0;
int Delta_Y=0;
int Delta_X=0;

int Xk=X;
int Yk=Y;
int nXk=0;
int nYk=0;

void setup() {

Serial.begin(9600);
delay(1000);

Delta_Y=abs(Yn-Y);
Delta_X=abs(Xn-X);
Serial.print("Delta_Y = ");
Serial.print(Delta_Y);
Serial.print("  Delta_X = ");
Serial.println(Delta_X);

Pk=2*Delta_Y-Delta_X;

while(Xk<=Xn && Yk<=Yn){
if(Pk<0){

  Pkn=Pk+2*Delta_Y;
  nXk=Xk+1;
  nYk=Yk;
//Serial.println("** Pk<0 **");  
//Serial.print("Pk = ");
//Serial.print(Pk);
//Serial.print("  nPk = ");
//Serial.print(Pkn);
//Serial.print("  nXk = ");
//Serial.print(nXk);
//Serial.print("  nYk = ");
//Serial.print(nYk);
Serial.print("  Xk = ");
Serial.print(Xk);
Serial.print("  Yk = ");
Serial.println(Yk);
  Xk=nXk;
  Yk=nYk;
  Pk=Pkn;
  }

else if(Pk>=0){

  Pkn=Pk+2*Delta_Y-2*Delta_X;
  nXk=Xk+1;
  nYk=Yk+1;
//Serial.println("** Pk>=0 **");  
//Serial.print("Pk = ");
//Serial.print(Pk);
//Serial.print("  nPk = ");
//Serial.print(Pkn);
//Serial.print("  nXk = ");
//Serial.print(nXk);
//Serial.print("  nYk = ");
//Serial.print(nYk);
Serial.print("  Xk = ");
Serial.print(Xk);
Serial.print("  Yk = ");
Serial.println(Yk);
  Xk=nXk;
  Yk=nYk;
  Pk=Pkn;
  }
 }
}

void loop() {
  

}

That is not the Bresenham algorithm.

void drawline(int x0, int y0, int x1, int y1)
{
    int dx, dy, p, x, y;
 
dx=x1-x0;
dy=y1-y0;
 
x=x0;
y=y0;
 
p=2*dy-dx;
 
while(x<x1)
{
if(p>=0)
{
putpixel(x,y,7);
y=y+1;
p=p+2*dy-2*dx;
}
else
{
putpixel(x,y,7);
p=p+2*dy;
}
x=x+1;
}
}
 
1 Like

I'm using these steps from Bradenham Calculator :

And you get the wrong answer.

Get wrong answer with that code as well.

My mistake. That code is for the first octant only, and the line is actually in the second octant.

This is a more complete review, covering all the eight cases.

Is there a code to calculate it at any octant?

(there's a link there to a pretty good tutorial, as well.)

This is from the Adafruit GFX library, taken from Wikipedia page I linked above. Short and sweet.

/**************************************************************************/
/*!
   @brief    Write a line.  Bresenham's algorithm - thx wikpedia
    @param    x0  Start point x coordinate
    @param    y0  Start point y coordinate
    @param    x1  End point x coordinate
    @param    y1  End point y coordinate
    @param    color 16-bit 5-6-5 Color to draw with
*/
/**************************************************************************/
void Adafruit_GFX::writeLine(int16_t x0, int16_t y0, int16_t x1, int16_t y1,
                             uint16_t color) {
#if defined(ESP8266)
  yield();
#endif
  int16_t steep = abs(y1 - y0) > abs(x1 - x0);
  if (steep) {
    _swap_int16_t(x0, y0);
    _swap_int16_t(x1, y1);
  }

  if (x0 > x1) {
    _swap_int16_t(x0, x1);
    _swap_int16_t(y0, y1);
  }

  int16_t dx, dy;
  dx = x1 - x0;
  dy = abs(y1 - y0);

  int16_t err = dx / 2;
  int16_t ystep;

  if (y0 < y1) {
    ystep = 1;
  } else {
    ystep = -1;
  }

  for (; x0 <= x1; x0++) {
    if (steep) {
      writePixel(y0, x0, color);
    } else {
      writePixel(x0, y0, color);
    }
    err -= dy;
    if (err < 0) {
      y0 += ystep;
      err += dx;
    }
  }
}

Thanks.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.