0

What is the best way to find the angle between the Blue and Green lines in this image?

The Blue line is straight, but the Green line is slightly tilted.

Green line coordinates are [(923, 286), (1495, 306)].
Blue line coordinates are [(901, 259), (901, 197)].

Here is my code of to calculate the angle:

def find_angle(A, B):
    # Calculate the slope of line A
    slope_A = (A[1][1] - A[0][1]) / (A[1][0] - A[0][0])
    # Calculate the slope of line B
    slope_B = (B[1][1] - B[0][1]) / (B[1][0] - B[0][0])
    # Calculate the angle between the lines
    print(slope_A,slope_B)
    angle = math.atan2(slope_B, slope_A)
    # Convert the angle from radians to degrees
    print(angle)
    angle_degrees = angle * 180 / math.pi
    return angle_degrees

My calculation result is 90 or 89, but the Green line is a little bit tilted, so the angle should be 91 or 93.

Here's another image.

What is the problem with my code and how to improve that?

CreepyRaccoon
  • 826
  • 1
  • 9
  • 19
  • 1
    Your code raises a ZeroDivisionError (B is vertical, slope is infinite). You could use `acos( u.v )` , with `u` and `v` the direction vector of the two lines (there are already lots of such answers on SO). – Demi-Lune Jan 09 '23 at 10:10
  • You should also use numpy, which lets you write things like `u = p2-p1` and `dot(u,v)` which is a lot easier to read than `A[...][...]`. – Demi-Lune Jan 09 '23 at 10:11
  • 1
    Does this answer your question? [Calculating angles between line segments (Python) with math.atan2](https://stackoverflow.com/questions/28260962/calculating-angles-between-line-segments-python-with-math-atan2) – Akshay Sehgal Jan 09 '23 at 11:48
  • No,When i find the problem that time. I will check that but not its not work – black sparrow Jan 09 '23 at 12:12

0 Answers0