Reputation: 5721
I need to compute an arc length with some datas:
Start
point with coords (Xs, Ys, Zs)End
point with coords (Xe, Ye, Ze)Center
point with coords (Xc, Yc, Zc)Axis
vector with components (Xa, Ya, Za)The Arc
starts on Start
point, finishes on End
point, has a radius equals to DistanceBetween(Center, Start)
and DistanceBetween(Center, End)
and his direction is defined by the Axis
vector.
Example :
Arc { Start { -18.123, -11.805, 0 }, End { -17.167, -0.553, 0 }, Center { -9.421, -6,877, 0 }, Axis { 0, 0, -1 } }
Note: in this example, the arc has a clockwise direction
My problem is: I can compute the large or the small length but I am unable to determine the good value depending on the direction of the axis.
Some help will be welcome.
Upvotes: 3
Views: 1654
Reputation: 4100
Assuming that your data is all self-consistent (center has to be equidistant from the start and end, and the axis should be perpendicular to the plane containing start, end, and center), the arc length is the measure in radians of the angle Start -> Center -> End multiplied by the radius (distance from Center to Start, or equivalently, distance from Center to End). The measure of the angle can be found by the cosine law: C^2 = A^2 + B^2 - 2AB cos theta
where C
is the distance from Start to End, A
is the distance from Center to Start, B
is the distance from Center to End. In this case the formula can be simplified to C^2 = 2R^2 - 2R^2 cos theta
which can be solved for cos theta
and then inverse cosine taken to obtain theta
.
If I understand correctly, you have gotten that far, but the problem is that there are generally two values for the inverse of cos
, corresponding to the short and long arc between start and end, and you don't know which to pick.
The principal branch of inverse cosine, which is what you get if you use the library function arccos
, will always give the short angle. You will need another way to figure out whether the correct angle is short or long.
The way I recommend is to use the cross product. The formula of interest is
a x b = |a| |b| sin(\theta) n
where a
is the vector Start - Center, b
is the vector End - Center
, |a|=|b|=R
, and n
is the unit vector determined by your axis. You already have the angle theta, more or less, from the cosine law, so you don't need to calculate arcsin
; all you are really interested in at this point is the sign of sin(theta)
. If a x b
and n
are in the same direction, sin(theta)>0
and you take the short arc determined by the cosine law. If a x b
and n
are in opposite directions, sin(theta)<0
, and you take the long arc determined by the cosine law.
You can tell whether a x b
and n
are in the same or opposite directions by taking their dot product. If the dot product is positive, they are in the same direction. If the dot product is negative, they are in opposite directions.
Upvotes: 2