英文:
Robot Rotation Angle and Turn Direction
问题
Working on Robot project and need to figure New Heading rotation amount and turn direction.
Input parameters are Current Heading and New Heading, Output values are Rotation amount in degrees and turn direct Port (Left) or Starboard (Right). Have solution below but it does not look very elegant. Any thoughts for more elegant solution?
Many thanks in advance imk
void AngleDiff( int RobotHeading , int NewHeading)
{
int SteeringAngle = RobotHeading - NewHeading;
char TurnDirection; // P = Port (Left) or S = Starboard (Right)
if( SteeringAngle > 180 )
{
SteeringAngle -= 360;
SteeringAngle = abs( SteeringAngle );
TurnDirection = 'S';
return;
}
if( SteeringAngle < -180 )
{
SteeringAngle += 360;
TurnDirection = 'P';
return;
}
if( SteeringAngle < 0 )
{
SteeringAngle = abs( SteeringAngle );
TurnDirection = 'S';
}
else
TurnDirection = 'P';
}
英文:
Working on Robot project and need to figure New Heading rotation amount and turn direction.
Input parameters are Current Heading and New Heading, Output values are Rotation amount in degrees and turn direct Port (Left) or Starboard (Right). Have solution below but it does not look very elegant. Any thoughts for more elegant solution?
Many thanks in advance imk
void AngleDiff( int RobotHeading , int NewHeading)
{
int SteeringAngle = RobotHeading - NewHeading;
char TurnDirection; // P = Port (Left) or S = Starboard (Right)
if( SteeringAngle > 180 )
{
SteeringAngle -= 360;
SteeringAngle = abs( SteeringAngle );
TurnDirection = 'S';
return;
}
if( SteeringAngle < -180 )
{
SteeringAngle += 360;
TurnDirection = 'P';
return;
}
if( SteeringAngle < 0 )
{
SteeringAngle = abs( SteeringAngle );
TurnDirection = 'S';
}
else
TurnDirection = 'P';
}
答案1
得分: 1
也许是这样的:
if (SteeringAngle > 180)
{
SteeringAngle -= 360;
}
else if (SteeringAngle < -180)
{
SteeringAngle += 360;
}
TurnDirection = SteeringAngle < 0 ? 'S' : 'P';
SteeringAngle = abs(SteeringAngle);
英文:
Perhaps something like this:
if (SteeringAngle > 180)
{
SteeringAngle -= 360;
}
else if (SteeringAngle < -180)
{
SteeringAngle += 360;
}
TurnDirection = SteeringAngle < 0 ? 'S' : 'P';
SteeringAngle = abs(SteeringAngle);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论