Skip to content
doubtly

1 answer

Best answer

Answer: Bresenham's algorithm draws a line using only integer additions: at each step along the driving axis it uses the sign of a decision parameter \(p\) to decide whether the other coordinate also moves by 1.

Algorithm for \(0 \le m \le 1\), \(x_1 < x_2\)

  1. \(dx = x_2 - x_1\), \(dy = y_2 - y_1\); plot \((x_1, y_1)\).
  2. Initial decision parameter \(p_0 = 2dy - dx\).
  3. For each of the next dx steps: if \(p_k < 0\), the next point is \((x_k+1, y_k)\) and \(p_{k+1} = p_k + 2dy\); otherwise the next point is \((x_k+1, y_k+1)\) and \(p_{k+1} = p_k + 2dy - 2dx\).

For other slopes the same idea is used with step signs sx, sy = ±1, and for |m| > 1 the roles of x and y (dx and dy) are swapped. The program below handles every direction.

Program (Turbo C)

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <graphics.h>

/* Bresenham line for any slope and direction */
void lineBres(int x1, int y1, int x2, int y2)
{
    int dx = abs(x2 - x1), dy = abs(y2 - y1);
    int sx = (x2 >= x1) ? 1 : -1;         /* step direction in x */
    int sy = (y2 >= y1) ? 1 : -1;         /* step direction in y */
    int x = x1, y = y1, p, i, t, swapped = 0;

    if (dy > dx) {                        /* steep line: y is the driving axis */
        t = dx; dx = dy; dy = t;
        swapped = 1;
    }

    p = 2 * dy - dx;                      /* initial decision parameter */
    putpixel(x, y, WHITE);
    for (i = 0; i < dx; i++) {            /* dx more pixels, ends exactly at (x2,y2) */
        if (p >= 0) {                     /* move along both axes */
            if (swapped) x += sx; else y += sy;
            p = p - 2 * dx;
        }
        if (swapped) y += sy; else x += sx;   /* always move on the driving axis */
        p = p + 2 * dy;
        putpixel(x, y, WHITE);
    }
}

int main()
{
    int x1, y1, x2, y2;
    int gd = DETECT, gm;

    printf("Enter first point (x1 y1): ");
    scanf("%d %d", &x1, &y1);
    printf("Enter second point (x2 y2): ");
    scanf("%d %d", &x2, &y2);

    initgraph(&gd, &gm, "C:\\TC\\BGI");   /* double backslashes */
    lineBres(x1, y1, x2, y2);
    getch();
    closegraph();
    return 0;
}

Sample run for (20, 10) to (30, 18): dx = 10, dy = 8, \(p_0 = 6\). Pixels plotted:

(20,10) (21,11) (22,12) (23,12) (24,13) (25,14) (26,15) (27,16) (28,16) (29,17) (30,18)

The same line entered in reverse, and a steep line (5, 5) to (8, 15), were also tested:

(30,18) (29,17) (28,16) (27,16) (26,15) (25,14) (24,13) (23,12) (22,12) (21,11) (20,10)
(5,5) (5,6) (6,7) (6,8) (6,9) (7,10) (7,11) (7,12) (7,13) (8,14) (8,15)

Note: graphics.h is the Turbo C / BGI library (Turbo C++ or DOSBox; on Linux use the libgraph package). The program was compile-checked with gcc against a small stand-in graphics.h that prints each plotted pixel instead of drawing it, which is how the pixel list above was produced.

In C strings the path must be written "C:\\TC\\BGI" with double backslashes; with single backslashes \t becomes a tab and \b a backspace, and initgraph cannot find the driver.

Doubtly · 30 Apr 2023 Report
Was this answer helpful?

Your answer

Log in or create a free account to answer this question.