Good day all,
I am struggling to complete an algorithm for bresenham line equation.
The output I get is the output in black and the output expected is in white. The bresenham algorithm is used to print the two diagonal lines in the middle of the rectangle. Can someone please correct me on my code in order to fix it.
Note:
bx is equivalent to x2 and super.x x1 similarly for y.
public void draw( char [][] matrix ) {
yCoord = super.y;
xCoord = super.x;
deltaX = Math.abs(bx - xCoord);
deltaY = Math.abs(by - yCoord);
int sx = xCoord < bx ? 1 : -1;
int sy = yCoord < by ? 1 : -1;
delta = deltaX - deltaY;
int err2;
while (true) {
matrix[xCoord][yCoord] = '*';
if (xCoord == bx || yCoord == by) {
break;
}
err2 = 2*delta;
if (err2 > -deltaY) {
delta -= deltaY;
xCoord += sx;
} else if (err2 < deltaX) {
delta += deltaX;
yCoord += sy;
}
}
}