#include <stdio.h>
#include <stdlib.h>
void bresenhamLine(int x1, int y1, int x2, int y2) {
int dx = x2 - x1;
int dy = y2 - y1;
int d = 2 * dy - dx; // Initial decision parameter
int x = x1;
int y = y1;
// Plot initial point
printf("Point: (%d, %d)\n", x, y);
while (x < x2) {
x++; // Move east
if (d < 0) {
// Choose East pixel
d = d + 2 * dy;
} else {
// Choose Northeast pixel
y++;
d = d + 2 * (dy - dx);
}
printf("Point: (%d, %d)\n", x, y);
}
}
int main() {
int x1, y1, x2, y2;
// Get input coordinates
printf("Enter starting point (x1 y1): ");
scanf("%d %d", &x1, &y1);
printf("Enter ending point (x2 y2): ");
scanf("%d %d", &x2, &y2);
// Draw the line
printf("\nPlotting line from (%d,%d) to (%d,%d)\n", x1, y1, x2, y2);
bresenhamLine(x1, y1, x2, y2);
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KCnZvaWQgYnJlc2VuaGFtTGluZShpbnQgeDEsIGludCB5MSwgaW50IHgyLCBpbnQgeTIpIHsKICAgIGludCBkeCA9IHgyIC0geDE7CiAgICBpbnQgZHkgPSB5MiAtIHkxOwogICAgaW50IGQgPSAyICogZHkgLSBkeDsgICAgLy8gSW5pdGlhbCBkZWNpc2lvbiBwYXJhbWV0ZXIKICAgIGludCB4ID0geDE7CiAgICBpbnQgeSA9IHkxOwogICAgCiAgICAvLyBQbG90IGluaXRpYWwgcG9pbnQKICAgIHByaW50ZigiUG9pbnQ6ICglZCwgJWQpXG4iLCB4LCB5KTsKICAgIAogICAgd2hpbGUgKHggPCB4MikgewogICAgICAgIHgrKzsgICAgLy8gTW92ZSBlYXN0CiAgICAgICAgCiAgICAgICAgaWYgKGQgPCAwKSB7CiAgICAgICAgICAgIC8vIENob29zZSBFYXN0IHBpeGVsCiAgICAgICAgICAgIGQgPSBkICsgMiAqIGR5OwogICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgIC8vIENob29zZSBOb3J0aGVhc3QgcGl4ZWwKICAgICAgICAgICAgeSsrOwogICAgICAgICAgICBkID0gZCArIDIgKiAoZHkgLSBkeCk7CiAgICAgICAgfQogICAgICAgIAogICAgICAgIHByaW50ZigiUG9pbnQ6ICglZCwgJWQpXG4iLCB4LCB5KTsKICAgIH0KfQoKaW50IG1haW4oKSB7CiAgICBpbnQgeDEsIHkxLCB4MiwgeTI7CiAgICAKICAgIC8vIEdldCBpbnB1dCBjb29yZGluYXRlcwogICAgcHJpbnRmKCJFbnRlciBzdGFydGluZyBwb2ludCAoeDEgeTEpOiAiKTsKICAgIHNjYW5mKCIlZCAlZCIsICZ4MSwgJnkxKTsKICAgIAogICAgcHJpbnRmKCJFbnRlciBlbmRpbmcgcG9pbnQgKHgyIHkyKTogIik7CiAgICBzY2FuZigiJWQgJWQiLCAmeDIsICZ5Mik7CiAgICAKICAgIC8vIERyYXcgdGhlIGxpbmUKICAgIHByaW50ZigiXG5QbG90dGluZyBsaW5lIGZyb20gKCVkLCVkKSB0byAoJWQsJWQpXG4iLCB4MSwgeTEsIHgyLCB5Mik7CiAgICBicmVzZW5oYW1MaW5lKHgxLCB5MSwgeDIsIHkyKTsKICAgIAogICAgcmV0dXJuIDA7Cn0=