Bresenham Line Drawing Algorithm in Python

Bresenham Line Drawing Algorithm in Python

Draw a line using the Bresenham's algorithm where the starting point is (32, 35) and the ending point is (41, 41).

Theory: Bresenham's Line Drawing Algorithm is a fundamental method in computer graphics used to draw lines efficiently on a pixel grid. Developed by Jack E. Bresenham in 1962, this algorithm calculates the coordinates of pixels that lie on a line segment between two given points. It is widely used in computer graphics and digital image processing due to its speed and simplicity. The core concept behind Bresenham's Algorithm is to decide which pixel to plot at each step along the line by minimizing the error between the calculated line and the ideal line. The algorithm calculates the coordinates of pixels one at a time, making it highly efficient for drawing lines.



import matplotlib.pyplot as plt

print("Enter the value of x1")
x1 = int(input())
print("Enter the value of x2")
x2 = int(input())
print("Enter the value of y1")
y1 = int(input())
print("Enter the value of y2")
y2 = int(input())

dx = x2 - x1
dy = y2 - y1
dy2 = dy * 2
dx2 = dx * 2
pk = dy2 - dx

if abs(dx) > abs(dy):
    steps = abs(dx)
else:
    steps = abs(dy)

xcoordinates = []
ycoordinates = []

i = 0
while i < steps:
    i += 1
    if pk >= 0:
        pk = pk + dy2 - dx2
        x1 = x1 + 1
        y1 = y1 + 1
        print("x1: ", x1, "y1:", y1)
        xcoordinates.append(x1)
        ycoordinates.append(y1)
    else:
        pk = pk + dy2
        x1 = x1 + 1
        y1 = y1
        print("x1: ", x1, "y1:", y1)
        xcoordinates.append(x1)
        ycoordinates.append(y1)

plt.plot(xcoordinates, ycoordinates)
plt.xlabel("X Axis")
plt.ylabel("Y Axis")
plt.title("Bresenham's Algorithm")
plt.show()




Code Explaination:
This Python code implements Bresenham's line drawing algorithm, which is used to draw a line between two given points (x1, y1) and (x2, y2) in a grid or on a canvas. Here's a brief explanation of the code:

It first takes input for the coordinates of two points (x1, y1) and (x2, y2) from the user.
It calculates the differences in x and y coordinates (dx and dy) between the two points.
It computes dy2 (2 times dy) and dx2 (2 times dx).
The variable pk is initialized to dy2 - dx, which is used to determine which pixel to choose in each step.
It calculates the total number of steps (either in the x-direction or y-direction) needed to reach the endpoint, which is stored in the variable 'steps.'
The code then initializes empty lists 'xcoordinates' and 'ycoordinates' to store the coordinates of the line pixels.
It enters a loop that runs for 'steps' iterations.
In each iteration, it checks whether pk is greater than or equal to 0. If it is, the code selects the next pixel at (x1 + 1, y1 + 1) and updates pk accordingly. If not, it selects the next pixel at (x1 + 1, y1) and updates pk.
The coordinates of the selected pixel (x1, y1) are printed and added to the 'xcoordinates' and 'ycoordinates' lists.
Finally, it uses Matplotlib to plot the line by connecting the coordinates in 'xcoordinates' and 'ycoordinates' and displays the plot with a title.
In summary, this code takes two points and uses Bresenham's algorithm to find the coordinates of the pixels that make up the line between them. It then plots the line using Matplotlib.



How to run python code in Replit:
Replit is an online platform that provides a coding environment for various programming languages, including Python. To run Python code in Replit, follow these steps:

Create a Replit Account (if you don't have one):

Visit the Replit website (https://replit.com).
Click the "Sign Up" or "Log In" button to create an account or log in if you already have one.
Create a New Python Replit:

After logging in, click the "New Repl" button to create a new project.
Choose "Python" as the programming language.
Writing Python Code:

You'll be presented with an online code editor where you can write your Python code. Write or paste your Python code into the editor.
Running Python Code:

To run your Python code, click the green "Run" button (a triangle icon) located near the top of the screen.
Viewing Output:

The output of your Python code will appear in the console below the code editor. This is where you can see the results of your program.
Save and Share:

Replit automatically saves your work as you go along, but you can also use the "Save" button to manually save your project.
You can share your Replit with others by clicking the "Share" button, which generates a link to your project.
Collaboration (if needed):

If you're working with others, Replit allows real-time collaboration. You can invite collaborators by clicking the "Invite" button and sharing the invite link.
Stopping Your Code:

If your code is running indefinitely or you want to stop it, you can click the "Stop" button (a square icon).
That's it! You can write, run, and experiment with your Python code in Replit's online environment. It's a great platform for quickly trying out code, collaborating with others, and learning Python without needing to install anything on your local computer.