---
title: "Bresenham's algorithm for all slopes"
subject: "Computer Graphics"
chapter: 2
key: "semester_5/computer_graphics:bresenham-all-slopes"
source: https://www.bctengineer.com/q/semester_5/computer_graphics/bresenham-all-slopes
site: BCT Engineering
---

# Bresenham's algorithm for all slopes

Computer Graphics · Chapter 2: Scan-Conversion

- Asked in: 2067 Mangsir (old course), 10 marks
- Video: [Bresenham's Line Drawing Algorithm With Example](https://www.youtube.com/watch?v=GKnzwTMmO1Y) — QuickCS
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_5/computer_graphics/bresenham-all-slopes
- Whole chapter: https://www.bctengineer.com/md/semester_5/computer_graphics/chapter-2

## Question

Write down Bresenham's line drawing algorithm for drawing a straight line with consideration of all the slope categories.

## Short answer

All-slope Bresenham uses step signs sx and sy. If |Δy| ≤ |Δx|, x is driving axis, p0 = 2Δy − Δx: x += sx; if p<0 p+=2Δy, else y += sy, p+=2Δy−2Δx. If |Δy| > |Δx|, y drives, p0 = 2Δx − Δy: y += sy; if p<0 p+=2Δx, else x += sx, p+=2Δx−2Δy.

## Answer

Bresenham's algorithm works with one coordinate as the unit-step axis (the "driving" axis) and the decision parameter chooses the other coordinate. For a line (x1,y1) to (x2,y2), let Δx = |x2 − x1|, Δy = |y2 − y1|, sx = sign(x2 − x1), sy = sign(y2 − y1).

#### Slope categories
| Slope | Driving axis | Other coordinate | p0 |
|---|---|---|---|
| 0 < m < 1 | x (+1) | y increases by 1 or stays | 2Δy − Δx |
| m > 1 | y (+1) | x increases by 1 or stays | 2Δx − Δy |
| −1 < m < 0 | x (+1) | y decreases by 1 or stays | 2Δy − Δx |
| m < −1 | y (−1) | x increases by 1 or stays | 2Δx − Δy |
| m = 0 | x | y stays | horizontal line |
| m = ∞ | y | x stays | vertical line |

#### Algorithm
```text
1. Input end points (x1,y1), (x2,y2).
2. Δx = |x2 − x1|, Δy = |y2 − y1|, sx = sign(x2−x1), sy = sign(y2−y1).
3. If Δy ≤ Δx  (|m| ≤ 1): case A, else case B.

Case A (x is the driving axis):
   p = 2Δy − Δx;  x = x1; y = y1; plot (x, y)
   repeat Δx times:
       x = x + sx
       if p < 0:  p = p + 2Δy
       else:      y = y + sy;  p = p + 2Δy − 2Δx
       plot (x, y)

Case B (y is the driving axis):
   p = 2Δx − Δy;  x = x1; y = y1; plot (x, y)
   repeat Δy times:
       y = y + sy
       if p < 0:  p = p + 2Δx
       else:      x = x + sx;  p = p + 2Δx − 2Δy
       plot (x, y)
```

#### C code
```c
void bres(int x1,int y1,int x2,int y2){
    int dx=abs(x2-x1), dy=abs(y2-y1);
    int sx=(x2>x1)?1:-1, sy=(y2>y1)?1:-1;
    int x=x1, y=y1, i, p;
    putpixel(x,y,WHITE);
    if(dy<=dx){                       /* |m| <= 1 */
        p=2*dy-dx;
        for(i=0;i<dx;i++){
            x+=sx;
            if(p<0) p+=2*dy;
            else { y+=sy; p+=2*dy-2*dx; }
            putpixel(x,y,WHITE);
        }
    } else {                          /* |m| > 1 */
        p=2*dx-dy;
        for(i=0;i<dy;i++){
            y+=sy;
            if(p<0) p+=2*dx;
            else { x+=sx; p+=2*dx-2*dy; }
            putpixel(x,y,WHITE);
        }
    }
}
```
The signs sx and sy handle lines drawn in any direction, so all slope categories (positive or negative, less than or greater than 1) are covered.

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. For a line with |m| > 1 in Bresenham's algorithm, which coordinate is the unit-step (driving) axis?
   A. y, stepping by 1 each time while the decision parameter chooses x
   B. x, stepping by 1 each time
   C. Both x and y step by 1
   D. The axis with the smaller coordinate value
   Answer: A. For steep lines y changes every step, and x either stays or changes by 1. (Key point: Driving axis)

2. For |m| <= 1 (case A) the initial decision parameter is:
   A. p0 = 2Δx − Δy
   B. p0 = Δy − 2Δx
   C. p0 = 2Δy − Δx
   D. p0 = 2(Δy − Δx)
   Answer: C. Case A uses x as the driving axis, giving p0 = 2Δy − Δx; steep lines swap the roles. (Key point: Initial parameter)

3. In case A, what happens when p >= 0?
   A. y stays and p = p + 2Δy
   B. y = y + sy and p = p + 2Δy − 2Δx
   C. y = y + sy and p = p + 2Δy
   D. x stays and p = p + 2Δx − 2Δy
   Answer: B. A non-negative p means the next pixel is the diagonal one, so y moves and p is increased by 2Δy − 2Δx. (Key point: Update when p >= 0)

4. What is the purpose of sx = sign(x2−x1) and sy = sign(y2−y1)?
   A. They swap the end points so the line goes left to right
   B. They scale the decision parameter
   C. They decide whether the line is horizontal
   D. They make the same code draw lines in any direction, covering all slope categories
   Answer: D. The signs give the direction of each step, so positive/negative slopes and any quadrant are handled. (Key point: Role of sx and sy)

## Related in Computer Graphics

- [DDA versus Bresenham line algorithm](https://www.bctengineer.com/question.md?key=semester_5%2Fcomputer_graphics%3Adda-vs-bresenham-line) — Computer Graphics, chapter 2, asked 6×
- [Write Bresenham's line algorithm](https://www.bctengineer.com/question.md?key=semester_5%2Fcomputer_graphics%3Abresenham-line-algorithm) — Computer Graphics, chapter 2, asked 3×
- [Bresenham: line (25,20) to (15,10)](https://www.bctengineer.com/question.md?key=semester_5%2Fcomputer_graphics%3Adigitize-line-25-20-15-10) — Computer Graphics, chapter 2
- [Bresenham decision parameter, negative slope](https://www.bctengineer.com/question.md?key=semester_5%2Fcomputer_graphics%3Abresenham-negative-slope-derive) — Computer Graphics, chapter 2, asked 2×
- [Bresenham: line (20,10) to (30,18)](https://www.bctengineer.com/question.md?key=semester_5%2Fcomputer_graphics%3Adigitize-line-20-10-30-18) — Computer Graphics, chapter 2, asked 4×
- [Bresenham: line (10,20) to (15,2)](https://www.bctengineer.com/question.md?key=semester_5%2Fcomputer_graphics%3Adigitize-line-10-20-15-2) — Computer Graphics, chapter 2, asked 4×

## Related in other subjects

- [Fit straight line: humidity and moisture](https://www.bctengineer.com/question.md?key=semester_5%2Fprobability_and_statistics%3Ahumidity-moisture-straight-line-fit) — Probability and Statistics, chapter 5
- [DDB with switchover to straight line](https://www.bctengineer.com/question.md?key=semester_6%2Fengineering_economics%3Addb-switchover-30000-7yr) — Engineering Economics, chapter 7
- [Camber elevations, straight and parabolic](https://www.bctengineer.com/question.md?key=semester_6%2Ftransportation_engineering_i%3An-65c-camber-320) — Transportation Engineering I, chapter 3
- [Finite and infinite slopes](https://www.bctengineer.com/question.md?key=semester_4%2Fsoil_mechanics%3Afinite-vs-infinite-slope) — Soil Mechanics, chapter 11, asked 2×
