Getting the start and end angle of arcs

circlestrigonometry

How do you find the angles of the arc of two points from a starting point to an ending point on the circle with a given direction of clockwise and anti clockwise?

I am having all kind of problems due to the repeating nature of angles.

For an example in the image below, if i get the angles in the range of [0,360] then an arc from quadrant 4 to quadrant 1 anti clockwise would be break.

enter image description here

Reason it breaks is because if you were to interpolate from 320 to 40 it would be clockwise.

By interpolation i mean this:

 startAngle + (endAngle - startAngle) * t  // where t = [0,1]

So how do you represent an arc between 2 points with a given direction for all configurations in a simple logical manner – i'm struggling to figure out the logic to easily find the two angles.

I want the final arc data to basically have:

CreateArc(StartPoint, TangentDirection, EndPoint)¬

    StartPoint
    StartAngle
    EndPoint
    EndAngle

Best Answer

Suppose the red arrow in the figure below is pointing in the direction of TangentDirection. There are two cases, one where the result should be a clockwise arc from StartPoint to EndPoint, the other where the result should be an anticlockwise arc.

enter image description here

The arc from $A$ to $B$ is anticlockwise. Notice that if the directed line segment from $A$ to $B$ is at an angle $\alpha$ anticlockwise from TangentDirection, the central angle of the arc is $2\alpha$ anticlockwise (by a theorem of geometry).

The arc from $A$ to $C$ is clockwise. If the directed line segment from $A$ to $C$ is at an angle $\beta$ clockwise from TangentDirection, the central angle of the arc is $2\beta$ clockwise.

In either case, the central angle is twice the angle from TangentDirection to the line segment, and in the same direction.

So we first compute the angle from TangentDirection to the line segment. Assuming you're setting all your angle variables to the degree measurements of the angles, we let

LineSegmentDirection = direction from StartPoint to EndPoint
AngleFromTangent = LineSegmentDirection - TangentDirection
while (AngleFromTangent < -180) AngleFromTangent = AngleFromTangent + 360
while (AngleFromTangent > 180) AngleFromTangent = AngleFromTangent - 360
AngleOfArc = 2 * AngleFromTangent 
EndAngle = StartAngle + AngleOfArc

The purpose of the while statements is to ensure that AngleFromTangent is between $-180$ and $180$ even if the smallest turn angle from TangentDirection to LineSegmentDirection crosses the border between the first and fourth quadrants. For example, if TangentDirection is $340$ and LineSegmentDirection is $100$ then LineSegmentDirection - TangentDirection will be $-240$, but the first while statement will add $360$ and the final result will be $120,$ which is the correct angle from TangentDirection to LineSegmentDirection. This is a simple-minded algorithm but is reasonably efficient if TangentDirection and LineSegmentDirection are within $360$ degrees of each other. (It will even be OK if they are within $720$ degrees of each other!)

Alternatively, if you have mastered the use of a floating-point "mod" function and can make it give the same results as the two while statements, you can use the "mod" function instead of the while statements.

Note that we do not (most emphatically not) apply any such "correction" to the value of EndAngle. That is, if (for example) the code above sets EndAngle to 430, then 430 is the value we pass to the function that interpolates the arc. That's because this value of EndAngle is in the correct direction (positive or negative) from StartAngle using this value we ensure that all the angles computed by

StartAngle + (EndAngle - StartAngle) * t

(where $t \in [0,1]$) are also in the correct direction from StartAngle.