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.
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 fromStartPoint
toEndPoint
, the other where the result should be an anticlockwise arc.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 letThe purpose of the
while
statements is to ensure thatAngleFromTangent
is between $-180$ and $180$ even if the smallest turn angle fromTangentDirection
toLineSegmentDirection
crosses the border between the first and fourth quadrants. For example, ifTangentDirection
is $340$ andLineSegmentDirection
is $100$ thenLineSegmentDirection - TangentDirection
will be $-240$, but the firstwhile
statement will add $360$ and the final result will be $120,$ which is the correct angle fromTangentDirection
toLineSegmentDirection
. This is a simple-minded algorithm but is reasonably efficient ifTangentDirection
andLineSegmentDirection
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 thewhile
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 setsEndAngle
to 430, then 430 is the value we pass to the function that interpolates the arc. That's because this value ofEndAngle
is in the correct direction (positive or negative) fromStartAngle
using this value we ensure that all the angles computed by(where $t \in [0,1]$) are also in the correct direction from
StartAngle
.