Bresenham algorithm in Javascript
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
The Bresenham algorithm is an efficient method for drawing lines on a grid or raster display, commonly used in computer graphics. It calculates the pixels that should be highlighted to create a close approximation of a straight line between two points. This algorithm accommodates integer-based arithmetic, ensuring a fast and resource-light execution, perfect for real-time applications or systems with limited processing capacity.
How the Bresenham Algorithm Works
The Bresenham algorithm determines the path from a start point to an end point on a grid by deciding which adjacent pixel represents the next point on the line. It only requires incremental integer calculations, making it highly efficient.
Key Concept
The algorithm works on the principle of minimizing the error term to decide which pixel should be plotted next. This can be explained as follows:
- Decision Variable: The main concept involves a decision variable that evaluates which two possible pixels are considered "next". If the line is more horizontal, it decides between two horizontal neighbors; if more vertical, it decides between two vertical neighbors.
- Error Accumulation: By maintaining an "error" value, the algorithm accumulates the deviation from the ideal line and periodically corrects it.
Implementation Steps
- Initialize the starting point `(x0, y0)` and the ending point `(x1, y1)`.
- Calculate the differences `dx = x1 - x0` and `dy = y1 - y0`.
- Determine the steps based on the direction of the line (positive or negative slope).
- Initialize the error term as half the `dy` for horizontal and `dx` for vertical dominant lines.
- Use a loop to step through the grid and plot each pixel, updating the decision variable.
Example in JavaScript
Below is a simple JavaScript function implementing the Bresenham line algorithm:
- Efficiency: Utilizes integer arithmetic, making it computationally inexpensive.
- Predictability: Produces consistent results across different systems.
- Simple Implementation: Easy to understand and incorporate into graphic systems.
Related reading
- Bridges in a connected graph
- Bron-Kerbosch algorithm for clique finding
- Brute-force, single-threaded prime factorization
- Bubble Shuffle - Weighted Shuffle
- Building JSON with Node.js with multiple queries
- Button background as transparent
- Bubble sort worst case example is Onn, how?
- Build a binary tree from an infix expression without using a stack

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.