Re: Algorithm to find the shortest rotation using degrees?
by "m0rdred" <idlerstyle@[EMAIL PROTECTED]
>
Oct 27, 2006 at 03:06 PM
> I think you haven't taken a close look at the atan function. I don't
> believe it's returning what you think it is. Something like the
following
> is more useful:
>
> double CalcTheta( const JVEC2 Point1, const JVEC2 Point2 )
> {
> double Theta = atan2( (Point2.x - Point1.x), (Point2.y - Point1.y)
);
> if ( Theta < 0 )
> return 2 * PI + Theta;
> else
> return Theta;
> }
>
> First atan2 simplies it a little in that it takes two points so you
don't
> have to do the devisor. It also handles the case if deltaX is 0 (in
your
> case would be devide by zero error).
Thanks Jim- based on your recommendation I came up with the following:
inline int shortestDirec(double start, double target)
{
if(start < target)
{
if( (target - start) < (360 - target + start) )
return 1;//turn left
else
return -1;//turn right
}
if(start > target)
{
if( (start - target) < (360 - start + target) )
return -1;//turn right
else
return 1;//turn left
}
else
return 0; // exactly aligned
}
am I overkilling with the two scenarios?
Thanks again
Scott