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.
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++
// A naive way of drawing line
voidnaiveDrawLine(x1, x2, y1, y2)
{
m = (y2 - y1) / (x2 - x1);
for(x = x1; x <= x2; x++) {
// Assuming that the round function finds
// closest integer to a given float.
y = round(mx + c);
print(x, y);
}
}
Java
/*package whatever //do not write package name here */
importjava.io.*;
classGFG {
// A naive way of drawing line
publicstaticvoidnaiveDrawLine(x1, x2, y1, y2)
{
m = (y2 - y1) / (x2 - x1);
for(x = x1; x <= x2; x++)
{
// Assuming that the round function finds
// closest integer to a given float.
y = round(mx + c);
print(x, y);
}
}
publicstaticvoidmain(String[] args) {}
}
// This code is contributed by akashish__
Python3
# A naive way of drawing line
defnaiveDrawLine(x1, x2, y1, y2):
m =(y2 -y1) /(x2 -x1)
# for (x = x1; x <= x2; x++) {
forx inrange(x1, x2 +1):
# Assuming that the round function finds
# closest integer to a given float.
y =round(mx +c)
print(x, y)
# This code is contributed by akashish__
C#
usingSystem;
publicclassGFG {
// A naive way of drawing line
publicstaticvoidnaiveDrawLine(x1, x2, y1, y2)
{
m = (y2 - y1) / (x2 - x1);
for(x = x1; x <= x2; x++) {
// Assuming that the round function finds
// closest integer to a given float.
y = round(mx + c);
print(x, y);
}
}
staticpublicvoidMain()
{
// Code
}
}
// This code is contributed by akashish__
Javascript
// A naive way of drawing line
functionnaiveDrawLine(x1, x2, y1, y2)
{
m = (y2 - y1) / (x2 - x1);
for(x = x1; x <= x2; x++) {
// Assuming that the round function finds
// closest integer to a given float.
y = Math.round(mx + c);
print(x, y);
}
}
// This code is contributed by garg28harsh.
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++
// Modifying the naive way to use a parameter
// to decide next y.
voidwithDecisionParameter(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
/*package whatever //do not write package name here */
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.
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!