Given the coordinate of two points A(x1, y1) and B(x2, y2). The task is to find all the intermediate points required for drawing line AB on the computer screen of pixels. Note that every pixel has integer coordinates.
Examples:
Input : A(0,0), B(4,4)
Output : (0,0), (1,1), (2,2), (3,3), (4,4)
Input : A(0,0), B(4,2)
Output : (0,0), (1,0), (2,1), (3,1), (4,2)
Below are some assumptions to keep the algorithm simple.
- We draw lines from left to right.
- x1 < x2 and y1< y2
- Slope of the line is between 0 and 1. We draw a line from lower left to upper right.
Naive Approach:
C++
void naiveDrawLine(x1, x2, y1, y2)
{
m = (y2 - y1) / (x2 - x1);
for (x = x1; x <= x2; x++) {
y = round(mx + c);
print(x, y);
}
}
|
Java
import java.io.*;
class GFG {
public static void naiveDrawLine(x1, x2, y1, y2)
{
m = (y2 - y1) / (x2 - x1);
for (x = x1; x <= x2; x++)
{
y = round(mx + c);
print(x, y);
}
}
public static void main(String[] args) {}
}
|
Python3
def naiveDrawLine(x1, x2, y1, y2):
m = (y2 - y1) / (x2 - x1)
for x in range (x1, x2 + 1 ):
y = round (mx + c)
print (x, y)
|
C#
using System;
public class GFG {
public static void naiveDrawLine(x1, x2, y1, y2)
{
m = (y2 - y1) / (x2 - x1);
for (x = x1; x <= x2; x++) {
y = round(mx + c);
print(x, y);
}
}
static public void Main()
{
}
}
|
Javascript
function naiveDrawLine(x1, x2, y1, y2)
{
m = (y2 - y1) / (x2 - x1);
for (x = x1; x <= x2; x++) {
y = Math.round(mx + c);
print(x, y);
}
}
|
The above algorithm works, but it is slow. The idea of Bresenham’s algorithm is to avoid floating point multiplication and addition to compute mx + c, and then compute the round value of (mx + c) in every step. In Bresenham’s algorithm, we move across the x-axis in unit intervals.
We always increase x by 1, and we choose about next y, whether we need to go to y+1 or remain on y. In other words, from any position (Xk, Yk) we need to choose between (Xk + 1, Yk) and (Xk + 1, Yk + 1).
We would like to pick the y value (among Yk + 1 and Yk) corresponding to a point that is closer to the original line.
We need a decision parameter to decide whether to pick Yk + 1 or Yk as the next point. The idea is to keep track of slope error from the previous increment to y. If the slope 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 the error.
C++
void withDecisionParameter(x1, x2, y1, y2)
{
m = (y2 - y1) / (x2 - x1);
slope_error = [Some Initial Value];
for (x = x1, y = y1; x = 0.5) {
y++;
slope_error -= 1.0;
}
}
|
Java
import java.io.*;
class GFG {
public static void withDecisionParameter(x1, x2, y1, y2)
{
m = (y2 - y1) / (x2 - x1);
slope_error = [Some Initial Value];
for (x = x1, y = y1; x = 0.5 ) {
y++;
slope_error -= 1.0 ;
}
}
public static void main (String[] args) {
}
}
|
Python3
def withDecisionParameter(x1, x2, y1, y2):
m = (y2 - y1) / (x2 - x1)
slope_error = [Some Initial Value]
for x in range ( 0.5 ,x1) and y in range (y1):
y + = 1
slope_error - = 1.0
|
C#
using System;
public class GFG {
public static void withDecisionParameter(x1, x2, y1, y2)
{
m = (y2 - y1) / (x2 - x1);
slope_error = [ Some Initial Value ];
for (x = x1, y = y1; x = 0.5) {
y++;
slope_error -= 1.0;
}
}
static public void Main() {}
}
|
Javascript
function withDecisionParameter(x1, x2, y1, y2)
{
m = (y2 - y1) / (x2 - x1);
slope_error = [Some Initial Value];
for (x = x1, y = y1; x = 0.5) {
y++;
slope_error -= 1.0;
}
}
|
How to avoid floating point arithmetic
The above algorithm still includes floating point arithmetic. To avoid floating point arithmetic, consider the value below value m.
- m = (y2 – y1)/(x2 – x1)
- We multiply both sides by (x2 – x1)
- We also change slope_error to slope_error * (x2 – x1). To avoid comparison with 0.5, we further change it to slope_error * (x2 – x1) * 2.
- Also, it is generally preferred to compare with 0 than 1.
C++
void bresenham(x1, x2, y1, y2)
{
m_new = 2 * (y2 - y1) slope_error_new =
[Some Initial Value] for (x = x1, y = y1; x = 0)
{
y++;
slope_error_new -= 2 * (x2 - x1);
}
}
|
Java
public static void bresenham( int x1, int x2, int y1, int y2) {
int m_new = 2 * (y2 - y1);
int slope_error_new = 0 ;
for ( int x = x1, y = y1; x = 0 😉 {
y++;
slope_error_new -= 2 * (x2 - x1);
}
}
|
Python3
def bresenham(x1, x2, y1, y2):
m_new = 2 * (y2 - y1)
slope_error_new = 0
y = y1
for x in range (x1, 0 , - 1 ) {
y + = 1
slope_error_new - = 2 * (x2 - x1)
|
C#
using System;
public class GFG{
public static void bresenham(x1, x2, y1, y2)
{
m_new = 2 * (y2 - y1);
slope_error_new = [Some Initial Value];
for ( int x = x1, int y = y1; x = 0)
{
y++;
slope_error_new -= 2 * (x2 - x1);
}
}
static public void Main (){
}
}
|
Javascript
function bresenham(x1, x2, y1, y2)
{
let m_new = 2 * (y2 - y1);
let slope_error_new = 0;
for (let x = x1, let y = y1; x = 0) {
y++;
slope_error_new -= 2 * (x2 - x1);
}
}
|
The initial value of slope_error_new is 2*(y2 – y1) – (x2 – x1). Refer to this for proof of this value
Below is the implementation of the above algorithm:
C++
#include <bits/stdc++.h>
using namespace std;
void bresenham( int x1, int y1, int x2, int y2)
{
int m_new = 2 * (y2 - y1);
int slope_error_new = m_new - (x2 - x1);
for ( int x = x1, y = y1; x <= x2; x++) {
cout << "(" << x << "," << y << ")\n" ;
slope_error_new += m_new;
if (slope_error_new >= 0) {
y++;
slope_error_new -= 2 * (x2 - x1);
}
}
}
int main()
{
int x1 = 3, y1 = 2, x2 = 15, y2 = 5;
bresenham(x1, y1, x2, y2);
return 0;
}
|
Java
class GFG {
static void bresenham( int x1, int y1, int x2, int y2)
{
int m_new = 2 * (y2 - y1);
int slope_error_new = m_new - (x2 - x1);
for ( int x = x1, y = y1; x < = x2; x++) {
System.out.print(
"
( " + x + " , " + y + " )\n
& quot;);
slope_error_new += m_new;
if (slope_error_new& gt; = 0 ) {
y++;
slope_error_new -= 2 * (x2 - x1);
}
}
}
public static void main(String[] args)
{
int x1 = 3 , y1 = 2 , x2 = 15 , y2 = 5 ;
bresenham(x1, y1, x2, y2);
}
}
|
Python3
def bresenham(x1, y1, x2, y2):
m_new = 2 * (y2 - y1)
slope_error_new = m_new - (x2 - x1)
y = y1
for x in range (x1, x2 + 1 ):
print ( "(" , x, "," , y, ")\n" )
slope_error_new = slope_error_new + m_new
if (slope_error_new > = 0 ):
y = y + 1
slope_error_new = slope_error_new - 2 * (x2 - x1)
if __name__ = = '__main__' :
x1 = 3
y1 = 2
x2 = 15
y2 = 5
bresenham(x1, y1, x2, y2)
|
C#
using System;
class GFG {
static void bresenham( int x1, int y1, int x2, int y2)
{
int m_new = 2 * (y2 - y1);
int slope_error_new = m_new - (x2 - x1);
for ( int x = x1, y = y1; x < = x2; x++) {
Console.Write( " (" + x + "
, " + y + " )\n
& quot;);
slope_error_new += m_new;
if (slope_error_new& gt; = 0) {
y++;
slope_error_new -= 2 * (x2 - x1);
}
}
}
public static void Main()
{
int x1 = 3, y1 = 2, x2 = 15, y2 = 5;
bresenham(x1, y1, x2, y2);
}
}
|
PHP
<?php
function bresenham( $x1 , $y1 , $x2 , $y2 )
{
$m_new = 2 * ( $y2 - $y1 );
$slope_error_new = $m_new - ( $x2 - $x1 );
for ( $x = $x1 , $y = $y1 ; $x <= $x2 ; $x ++)
{
echo "(" , $x , "," , $y , ")\n" ;
$slope_error_new += $m_new ;
if ( $slope_error_new >= 0)
{
$y ++;
$slope_error_new -= 2 * ( $x2 - $x1 );
}
}
}
$x1 = 3; $y1 = 2; $x2 = 15; $y2 = 5;
bresenham( $x1 , $y1 , $x2 , $y2 );
?>
|
Javascript
function plotPixel(x1, y1, x2,
y2, dx, dy,
decide)
{
let pk = 2 * dy - dx;
for (let i = 0; i <= dx; i++) {
if (decide == 0){
console.log(x1 + "," + y1);
}
else {
console.log(y1 + "," + x1);
}
if (x1 < x2)
x1++;
else
x1--;
if (pk < 0) {
if (decide == 0) {
pk = pk + 2 * dy;
}
else
pk = pk + 2 * dy;
}
else {
if (y1 < y2)
y1++;
else
y1--;
pk = pk + 2 * dy - 2 * dx;
}
}
}
let x1 = 100, y1 = 110, x2 = 125, y2 = 120, dx, dy;
dx = Math.abs(x2 - x1);
dy = Math.abs(y2 - y1);
if (dx > dy) {
plotPixel(x1, y1, x2, y2, dx, dy, 0);
}
else {
plotPixel(y1, x1, y2, x2, dy, dx, 1);
}
|
Output
(3,2)
(4,3)
(5,3)
(6,3)
(7,3)
(8,4)
(9,4)
(10,4)
(11,4)
(12,5)
(13,5)
(14,5)
(15,5)
Time Complexity: O(x2 – x1)
Auxiliary Space: O(1)
The above explanation is to provide a rough idea behind the algorithm. For detailed explanation and proof, readers can refer below references.
The above program only works if the slope of the line is less than 1. Here is a program implementation for any kind of slope.
Output
100,110
101,110
102,111
103,111
104,112
105,112
106,112
107,113
108,113
109,114
110,114
111,114
112,115
113,115
114,116
115,116
116,116
117,117
118,117
119,118
120,118
121,118
122,119
123,119
124,120
125,120
Related Articles:
- Mid-Point Line Generation Algorithm
- DDA algorithm for line drawing
This article is contributed byShivam Pradhan. If you like neveropen and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the neveropen main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!