Bresenham Line Drawing Algorithm Code in PythonBresenham Algorithm is an algorithm that uses integer values to determine the points lying between the starting and end points within the space. It is a type of incremental scan conversion algorithm for line drawing. It determines all the points lying between the starting and end points. The Bresenham Line Drawing algorithm uses different mathematical operations, including addition and subtraction. Still, it does not use multiplication and division, as these operations are heavy, resulting in slow processing of the lines. Problem StatementWe need to draw a line AB. The coordinates of the points of lines are A (a1, b1) and B (a2, b2). We must find the points needed to draw the line on the AB on the screen of pixels. Let's understand this problem statement using a few examples. Examples: Input: Output: (1, 1), (2, 2), (3, 3), (4, 4), (5, 5) Input: Output: (0, 0), (1, 0), (2, 1), (3, 2) Assumption in the Bresenham Line Drawing AlgorithmThese are some assumptions to make the algorithm more simple:
How does the Bresenham Algorithm Work?The Bresenham algorithm aims to avoid multiplying the floating point to calculate mx =c and then calculate the value of mx + c in every step. In this algorithm, we are moving across the x-axis in equal intervals. The value of the a coordinate will be increased by 1, and the value of b coordinates will be checked if they need to increase by 1 or remain the same. For any point n, we need to decide the coordinates between (an + 1, bn) and (an + 1, bn + 1). We will use a decision variable to choose if bn + 1 is used or bn ¬for the next point. The main aim is to maintain the slope error from the last increased value of b. If the slope error becomes more than 0.5, it represents that the line has moved up one pixel. It represents that we must increase the value of b by 1 to calculate the slope error. The slope error is calculated by subtracting one from the error, representing the distance from the top of the new pixel.ImplementationThe matplotlib library displays the lines made using the bresenham line drawing algorithm. Code: Output: Enter the Starting point of a: 23 Enter the Starting point of b: 34 Enter the end point of a: 45 Enter the end point of b: 44 a = 23, b = 34 a = 24, b = 34 a = 25, b = 35 a = 26, b = 35 a = 27, b = 36 a = 28, b = 36 a = 29, b = 37 a = 30, b = 37 a = 31, b = 38 a = 32, b = 38 a = 33, b = 39 a = 34, b = 39 a = 35, b = 39 a = 36, b = 40 a = 37, b = 40 a = 38, b = 41 a = 39, b = 41 a = 40, b = 42 a = 41, b = 42 a = 42, b = 43 a = 43, b = 43 a = 44, b = 44 a = 45, b = 44 Explanation: We imported the matplotlib library to show the lines. Using the title, xlabel, and ylabel functions, we labeled the title and labels of the graphs. The X and Y axis coordinates are declared with a and b variables, respectively. These are evaluated based on the slope error value, which is determined using the absolute value of a and b. Next TopicCnn-algorithm-code-in-python |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India