角度与弧度的转换

转换关系

radians = degrees x Π / 180
degrees = radians x 180 / Π

代码实现如下:

  /*!
   * \brief Convert degrees to radians
   */

  static inline double from_degrees(double degrees)
  {
    return degrees * M_PI / 180.0;
  }

  /*!
   * \brief Convert radians to degrees
   */
  static inline double to_degrees(double radians)
  {
    return radians * 180.0 / M_PI;
  }


  /*!
   * \brief normalize_angle_positive
   *
   *        Normalizes the angle to be 0 to 2*M_PI
   *        It takes and returns radians.
   */
  static inline double normalize_angle_positive(double angle)
  {
    const double result = fmod(angle, 2.0*M_PI);
    if(result < 0) return result + 2.0*M_PI;
    return result;
  }


  /*!
   * \brief normalize
   *
   * Normalizes the angle to be -M_PI circle to +M_PI circle
   * It takes and returns radians.
   *
   */
  static inline double normalize_angle(double angle)
  {
    const double result = fmod(angle + M_PI, 2.0*M_PI);
    if(result <= 0.0) return result + M_PI;
    return result - M_PI;
  }

计算两点之间的角度:

double getAngle(double startX, double startY, double destX, double destY)
{
    auto dx = destX - startX;
    auto dy = destY - startY;

    auto angle = std::atan2(dy, dx);
    angle = (angle >= 0) ? angle : 2 * M_PI + angle;
    return angle;
}
posted @ 2023-04-18 21:00  卡尔的思索  阅读(96)  评论(0编辑  收藏  举报