This seems to work with every combination of numbers I throw at it, and I've tested it with the divided by 10 concept and it works. Should allow my to plot points every mm of a straight line.
[code]
// Bresenham line algorithm prints out from x1,y1 to x2,y2 in whole numbers
boolean executed = false; // boolean variable to check if move is completed
int x1;
int y1;
float newx1;
float newy1;
void setup() {
// put your setup code here, to run once:
Serial.begin(57600);
}
void loop() {
// put your main code here, to run repeatedly:
if (executed == false ) {
Bresenham (65, -60, -50, 60);
executed = true;
}
}
void Bresenham(int x1, int y1, int const x2, int const y2)
{
int delta_x = (x2 - x1);
// if x1 == x2, then it does not matter what we set here
signed char const ix((delta_x > 0) - (delta_x < 0));
delta_x = abs(delta_x) << 1;
int delta_y = (y2 - y1);
// if y1 == y2, then it does not matter what we set here
signed char const iy((delta_y > 0) - (delta_y < 0));
delta_y = abs(delta_y) << 1;
plot(x1, y1);
if (delta_x >= delta_y)
{
// error may go below zero
int error = (delta_y - (delta_x >> 1));
while (x1 != x2)
{
// reduce error, while taking into account the corner case of error == 0
if ((error > 0) || (!error && (ix > 0)))
{
error -= delta_x;
y1 += iy;
}
// else do nothing
error += delta_y;
x1 += ix;
plot(x1, y1);
}
}
else
{
// error may go below zero
int error = (delta_x - (delta_y >> 1));
while (y1 != y2)
{
// reduce error, while taking into account the corner case of error == 0
if ((error > 0) || (!error && (iy > 0)))
{
error -= delta_y;
x1 += ix;
}
// else do nothing
error += delta_x;
y1 += iy;
plot(x1, y1);
}
}
}
void plot(int x1, int y1) {
float newx1 = (float)x1 / 10.0;
float newy1 = (float)y1 / 10.0;
Serial.print ("x1= ");
Serial.print (newx1);
Serial.print (" y1 = ");
Serial.println (newy1);
}
[/code]