naeem043

Python Bresenham 2

Oct 31st, 2019
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.40 KB | None | 0 0
  1. from graphics import *
  2. import time
  3.  
  4. def BresenhamLine(x1,y1,x2,y2):
  5.     """ Bresenham Line Drawing Algorithm For All Kind Of Slopes Of Line """
  6.    
  7.     dx = abs(x2 - x1)
  8.     dy = abs(y2 - y1)
  9.     slope = dy/float(dx)
  10.    
  11.     x, y = x1, y1  
  12.  
  13.     # creating the window
  14.     win = GraphWin('Brasenham Line', 600, 480)
  15.    
  16.     # checking the slope if slope > 1
  17.     # then interchange the role of x and y
  18.     if slope > 1:
  19.         dx, dy = dy, dx
  20.         x, y = y, x
  21.         x1, y1 = y1, x1
  22.         x2, y2 = y2, x2
  23.  
  24.     # initialization of the inital disision parameter
  25.     p = 2 * dy - dx
  26.    
  27.     PutPixle(win, x, y)
  28.  
  29.     for k in range(2, dx):
  30.         if p > 0:
  31.             y = y + 1 if y < y2 else y - 1
  32.             p = p + 2*(dy - dx)
  33.         else:
  34.             p = p + 2*dy
  35.  
  36.         x = x + 1 if x < x2 else x - 1
  37.        
  38.         # delay for 0.01 secs
  39.         time.sleep(0.01)
  40.         print('The points are (x,y) (', x,', ', y,')')
  41.         PutPixle(win, x, y)
  42.  
  43. def PutPixle(win, x, y):
  44.     """ Plot A Pixle In The Windows At Point (x, y) """
  45.     pt = Point(x,y)
  46.     pt.draw(win)
  47.  
  48. def main():
  49.     x1 = int(input("Enter Start X: "))
  50.     y1 = int(input("Enter Start Y: "))
  51.     x2 = int(input("Enter End X: "))
  52.     y2 = int(input("Enter End Y: "))
  53.  
  54.     BresenhamLine(x1, y1, x2, y2)
  55.        
  56. if __name__ == "__main__":
  57.     main()
  58.     a = input('Press any key to exit')
Add Comment
Please, Sign In to add comment