0% found this document useful (0 votes)
22 views3 pages

Bresenham Algorithm 2024

The document describes the Bresenham algorithm for drawing lines on a digital display. It defines a bres function that takes x and y coordinates as inputs, calculates the gradient and error term, and plots the line by incrementing x and conditionally incrementing y at each step.

Uploaded by

mahfujur752
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views3 pages

Bresenham Algorithm 2024

The document describes the Bresenham algorithm for drawing lines on a digital display. It defines a bres function that takes x and y coordinates as inputs, calculates the gradient and error term, and plots the line by incrementing x and conditionally incrementing y at each step.

Uploaded by

mahfujur752
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Bresenham Algorithm:

import matplotlib.pyplot as plt

plt.title("Bresenham Algorithm")

plt.xlabel("X Axis")

plt.ylabel("Y Axis")

def bres(x1,y1,x2,y2):

x,y = x1,y1

dx = abs(x2 - x1)

dy = abs(y2 -y1)

gradient = dy/float(dx)

if gradient > 1:

dx, dy = dy, dx

x, y = y, x

x1, y1 = y1, x1

x2, y2 = y2, x2

p = 2*dy - dx

print(f"x = {x}, y = {y}")

# Initialize the plotting points

xcoordinates = [x]

ycoordinates = [y]

for k in range(2, dx + 2):

if p > 0:

y = y + 1 if y < y2 else y - 1

p = p + 2 * (dy - dx)

else:
p = p + 2 * dy

x = x + 1 if x < x2 else x - 1

print(f"x = {x}, y = {y}")

xcoordinates.append(x)

ycoordinates.append(y)

plt.plot(xcoordinates, ycoordinates)

plt.show()

def main():

x1 = int(input("Enter the Starting point of x: "))

y1 = int(input("Enter the Starting point of y: "))

x2 = int(input("Enter the end point of x: "))

y2 = int(input("Enter the end point of y: "))

bres(x1, y1, x2, y2)

if __name__ == "__main__":

main()
Output:

You might also like