ArticleslgStudy

computer science

Bresenham's line algorithm

Bresenham's line algorithm is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Bresenham's line algorithm rather than just read about it. In short: Bresenham's line algorithm is a line drawing algorithm that determines the points of an n-dimensional raster that should be selected in order to form a close approximation to a straight line between two points. It is commonly used to draw line primitives in a bitmap image (e.g. on a computer screen), as it uses only integer addition, subtraction, and bit shifting, all of which are very cheap operations in historical…

Bresenham's line algorithm — main illustration
Bresenham's line algorithm — illustration

Key takeaways

  • Bresenham's line algorithm belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Bresenham's line algorithm to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Bresenham's line algorithm from memory before moving on to harder problems.

Reference excerpt

Bresenham's line algorithm is a line drawing algorithm that determines the points of an n-dimensional raster that should be selected in order to form a close approximation to a straight line between two points. It is commonly used to draw line primitives in a bitmap image (e.g. on a computer screen), as it uses only integer addition, subtraction, and bit shifting, all of which are very cheap operations in historically common computer architectures. It is an incremental error algorithm, and one of the earliest algorithms developed in the field of computer graphics. An extension to the original algorithm called the midpoint circle algorithm may be used for drawing circles. While algorithms such as Wu's algorithm are also frequently used in modern computer graphics because they can support antialiasing, Bresenham's line algorithm is still important because of its speed and simplicity. The algorithm is used in hardware such as plotters and in the graphics chips of modern graphics cards. It can also be found in many software graphics libraries. Because the algorithm is very simple, it is often implemented in either the firmware or the graphics hardware of modern graphics cards. The label "Bresenham" is used today for a family of algorithms extending or modifying Bresenham's original algorithm.

History Bresenham's line algorithm is named after Jack Elton Bresenham who developed it in 1962 at IBM. In 2001, Bresenham wrote:

I was working in the computation lab at IBM's San Jose development lab. A Calcomp plotter had been attached to an IBM 1401 via the 1407 typewriter console. [The algorithm] was in production use by summer 1962, possibly a month or so earlier. Programs in those days were freely exchanged among corporations so Calcomp (Jim Newland and Calvin Hefte) had copies. When I returned to Stanford in Fall 1962, I put a copy in the Stanford comp center library. A description of the line drawing routine was accepted for presentation at the 1963 ACM national convention in Denver, Colorado. It was a year in which no proceedings were published, only the agenda of speakers and topics in an issue of Communications of the ACM. A person from the IBM Systems Journal asked me after I made my presentation if they could publish the paper. I happily agreed, and they printed it in 1965.

Method

The following conventions will be applied:

the top-left is (0,0) such that pixel coordinates increase to the right and downwards (e.g. that the pixel at (7,4) is directly above the pixel at (7,5)), and the pixel centers have integer coordinates. The endpoints of the line are the pixels at (x0,y0) and (x1,y1), where the first coordinate of the pair is the column and the second is the row. The algorithm will be initially presented only for the octant in which the segment goes down and to the right (x0≤x1 and y0≤y1), and its horizontal projection x1−x0 is longer than the vertical projection y1−y0 (the line has a positive slope less than 1). In this octant, for each column x between x0 and x1, there is exactly one row y (computed by the algorithm) containing a pixel of the line, while each row between y0 and y1 may contain multiple rasterized pixels. Bresenham's algorithm chooses the integer y corresponding to the pixel center that is closest to the ideal (fractional) y for the same x; on successive columns y can remain the same or increase by 1. The general equation of the line through the endpoints is given by:

y−y0y1−y0=x−x0x1−x0. Since we know the column, x, the pixel's row, y, is given by rounding this quantity to the nearest integer:

y=y1−y0x1−x0(x−x0)+y0. The slope (y1−y0)/(x1−x0) only depends on the endpoint coordinates and can be precomputed, and the ideal y for successive integer values of x can be computed starting from y0 and repeatedly adding the slope. In practice, the algorithm does not keep track of the y coordinate, which increases by m = ∆y/∆x each time the x increases by one; it keeps an error bound at each stage, which represents the negative of the distance from (a) the point where the line exits the pixel to (b) the top edge of the pixel. This value is first set to y0−0.5 (due to using the pixel's center coordinates), and is incremented by m each time the x coordinate is incremented by one. If the error becomes greater than 0.5, we know that the line has moved upwards one pixel, and that we must increment our y coordinate and readjust the error to represent the distance from the top of the new pixel – which is done by subtracting one from error.

Derivation To derive Bresenham's algorithm, two steps must be taken: first, the equation of a line is transformed from the typical slope-intercept form into an implicit equation with integer coefficients; second, this new equation is used to draw the line based on the idea of error accumulation.

Line equation

The slope-intercept form of a line is written as

y=f(x)=mx+b where m is the slope and b is the y-intercept. Because this is a function of only x, it can't represent a vertical line. Therefore, it would be useful to make this equation written as a function of both x and y to be able to draw lines at any angle. The slope of a line can be expressed as "rise over run", or Δy/Δx. Then, using algebraic manipulation,

y=mx+by=ΔyΔxx+b(Δx)y=(Δy)x+(Δx)b0=(Δy)x−(Δx)y+(Δx)b Letting this last equation be a function of x and y, it can be written as

f(x,y):=Ax+By+C=0 where the constants are

A=Δy=y1−y0 B=−Δx=−(x1−x0) C=(Δx)b=(x1−x0)b The line is then defined for some constants A, B, and C anywhere f(x,y)=0. That is, for any (x,y) not on the line, f(x,y)≠0. This form involves only integers if x and y are integers, since the constants A, B, and C are defined as integers. As an example, the line y=12x+1 could be written as f(x,y)=x−2y+2. The point (2,2) is on the line

f(2,2)=x−2y+2=(2)−2(2)+2=2−4+2=0 and the point (2,3) is not on the line

f(2,3)=(2)−2(3)+2=2−6+2=−2 and neither is the point (2,1)

f(2,1)=(2)−2(1)+2=2−2+2=2 Notice that the points (2,1) and (2,3) are on opposite sides of the line and f(x,y) evaluates to positive or negative. A line splits a plane into halves. The half-plane that has a negative f(x,y) can be called the negative half-plane, and the other half can be called the positive half-plane. This observation is very important for the remainder of the derivation.

Algorithm The starting point is on the line

… excerpt ends here. Continue reading the full article.

Illustrations

Bresenham's line algorithm: y=f(x)=0.5x+1 or f(x,y)=x-2y+2=0
y=f(x)=0.5x+1 or f(x,y)=x-2y+2=0
Bresenham's line algorithm: Positive and negative half-planes
Positive and negative half-planes
Bresenham's line algorithm: Candidate point (2,2) in blue and two candidate points in green (3,2) and (3,3)
Candidate point (2,2) in blue and two candidate points in green (3,2) and (3,3)
Bresenham's line algorithm: Plotting the line from (0,1) to (6,4) showing a plot of grid lines and pixels
Plotting the line from (0,1) to (6,4) showing a plot of grid lines and pixels

Worked examples

Example 1 — a first encounter with Bresenham's line algorithm

Start with the simplest possible case. Write down what Bresenham's line algorithm claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Bresenham's line algorithm before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Bresenham's line algorithm ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Bresenham's line algorithm

In research
Bresenham's line algorithm appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Bresenham's line algorithm in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Bresenham's line algorithm is common in secondary-school and first-year university syllabi. It links to neighbouring topics Computer graphics algorithms, Digital geometry, so understanding it makes those chapters shorter.
In everyday life
Look for Bresenham's line algorithm outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Bresenham's line algorithm” →

How to study Bresenham's line algorithm in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Bresenham's line algorithm means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Bresenham's line algorithm out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Bresenham's line algorithm in simple terms?

Bresenham's line algorithm is a line drawing algorithm that determines the points of an n-dimensional raster that should be selected in order to form a close approximation to a straight line between two points. It is commonly used to draw line primitives in a bitmap image (e.g. on a computer screen…

Why does Bresenham's line algorithm matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Bresenham's line algorithm?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Bresenham's line algorithm.

Tags

  • Computer graphics algorithms
  • Digital geometry

Keep exploring