lolamontes69

Python/ Calculate angle between 2 lines given their coords.

Jul 7th, 2013
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.19 KB | None | 0 0
  1. """ Calculate the angle between two lines that join at (x1, y1) """
  2.  
  3. from math import atan2, pi
  4.  
  5. def calculate_angle(x1,y1,x2,y2,x3,y3,x4,y4):
  6.     # Calculate the angles created.
  7.     delta_x = float(x2 - x1)
  8.     delta_y = float(y2 - y1)
  9.     answer = atan2(delta_y, delta_x)*180.0/pi
  10.  
  11.     delta_x = float(x4 - x3)
  12.     delta_y = float(y4 - y3)
  13.     answer1 = atan2(delta_y, delta_x)*180.0/pi
  14.  
  15.     # Get the answer
  16.     if answer > answer1:
  17.         return answer-answer1
  18.     else:
  19.         return answer1-answer
  20.  
  21.  
  22. # Input stuff
  23. print "Input coordinates for the first line"
  24. print "------------------------------------"
  25. x1 = float(raw_input('Enter x1 coord >'))
  26. y1 = float(raw_input('Enter y1 coord >'))
  27. x2 = float(raw_input('Enter x2 coord >'))
  28. y2 = float(raw_input('Enter y2 coord >'))
  29. print ""
  30. print "Input coordinates for the second line"
  31. print "Lets assume the node for the two lines is at (x1,y1)"
  32. print "------------------------------------"
  33. x3, y3 = x1 ,y1
  34. print "x3 =",x1
  35. print "y3 =",y1
  36. x4 = float(raw_input('Enter x4 coord >'))
  37. y4 = float(raw_input('Enter y4 coord >'))
  38.  
  39. result = calculate_angle(x1,y1,x2,y2,x3,y3,x4,y4)
  40. print "The angle between the two joined lines is",result
Advertisement
Add Comment
Please, Sign In to add comment