Search code examples
pythonmathgeometryconditional-statementsangle

the condition is the angle between two other angles


I have a vector from a certain point, and its angle, there are also lines described by two points. I need to find the points of intersection of a vector with lines. I calculated the intersection, through the equation of lines, but in this case I find intersections with lines that are on the opposite side of the direction of the vector, so I decided to check if the line is in the vector's direction, but I can't think of the right condition. The screenshot shows an example of what it means when an angle is between two other angles. At the moment the angles are from -180 to 180, but I would be very grateful for a solution with angles from 0 to 360 too.enter image description here

A sequence of actions to check, either mathematically or in code form would help me.


Solution

  • First get the order of the endpoints right. Sort them numerically; if the difference is < 180, then this step is done, otherwise add 360 to the lesser one and reverse them.

    Then see if the ray falls between them. If it is greater than the first point but less than the second, then there is an intersection; otherwise add 360 to the ray and test a second time.

    Example 1: Line is from 80 to 280, ray is at 350. The difference between the endpoints is 200, which is greater than 180, so change the lesser endpoint: the line is from 280 to 440. The ray, at 350, is between these endpoints, so the ray intersects the line.

    Example 2: Line is from 50 to 140, ray is at 260. The difference between the endpoints is 90, which is less than 180; no adjustment needed. The ray is not between them; add 360 to the ray to make it 620. The ray is still not between the endpoints, so the ray does not intersect the line.

    This solution works for angles measured from 0 to 360. If the given angles are given from -180 to 180, just add 180 to everything first.