You are on page 1of 10

Bresenham’s line Drawing

Algorithm

SHIVARAJ
1HK19CS147
Bresenham’s Line Drawing Algorithm
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 commonly used computer instruction
sets such as x86_64. It is an incremental error algorithm.
It is one of the earliest algorithms developed in the field
of computer graphics.
Bresenham’s Line Drawing Algorithm

• Given coordinate of two points A(x1, y1) and


B(x2, y2). The task to find all the
intermediate points required for drawing
line AB on the computer screen of pixels.
Note that every pixel has integer
coordinates.
Bresenham’s Line Drawing Algorithm

• Below are some assumptions to keep algorithm simple.

• We draw line 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.
Bresenham’s Line Drawing Algorithm

• 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).
Bresenham’s Line Drawing Algorithm
•# Python 3 program for Bresenham’s Line Generation
•# Assumptions :
•# 1) Line is drawn from left to right.
•# 2) x1 < x2 and y1 < y2
•# 3) Slope of the line is between 0 and 1.
•# We draw a line from lower left to upper
•# right
Bresenham’s Line Drawing Algorithm
• # function for line generation
• 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")

• # Add slope to increment angle formed
Bresenham’s Line Drawing Algorithm
• slope_error_new =slope_error_new + m_new

• # Slope error reached limit, time to
• # increment y and update slope error.
• if (slope_error_new >= 0):
• y=y+1
• slope_error_new =slope_error_new - 2 * (x2 - x1)
• # driver function
• if __name__=='__main__':
• x1 = 3
• y1 = 2
• x2 = 15
• y2 = 5

bresenham(x1, y1, x2, y2)
Bresenham’s Line Drawing Algorithm
• Output :

• Time Complexity: O(x2 –


x1)
• Auxiliary Space: O(1)
The above explanation is
to provides a rough idea
behind the algorithm
Thank you

You might also like