qwt_point_polar.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
  2. * Qwt Widget Library
  3. * Copyright (C) 1997 Josef Wilgen
  4. * Copyright (C) 2002 Uwe Rathmann
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the Qwt License, Version 1.0
  8. *****************************************************************************/
  9. /*! \file */
  10. #ifndef _QWT_POINT_POLAR_H_
  11. #define _QWT_POINT_POLAR_H_ 1
  12. #include "qwt_global.h"
  13. #include "qwt_math.h"
  14. #include <qpoint.h>
  15. #ifndef QT_NO_DEBUG_STREAM
  16. #include <qdebug.h>
  17. #endif
  18. /*!
  19. \brief A point in polar coordinates
  20. In polar coordinates a point is determined by an angle and a distance.
  21. See http://en.wikipedia.org/wiki/Polar_coordinate_system
  22. */
  23. class QWT_EXPORT QwtPointPolar
  24. {
  25. public:
  26. QwtPointPolar();
  27. QwtPointPolar( double azimuth, double radius );
  28. QwtPointPolar( const QPointF & );
  29. void setPoint( const QPointF & );
  30. QPointF toPoint() const;
  31. bool isValid() const;
  32. bool isNull() const;
  33. double radius() const;
  34. double azimuth() const;
  35. double &rRadius();
  36. double &rAzimuth();
  37. void setRadius( double );
  38. void setAzimuth( double );
  39. bool operator==( const QwtPointPolar & ) const;
  40. bool operator!=( const QwtPointPolar & ) const;
  41. QwtPointPolar normalized() const;
  42. private:
  43. double d_azimuth;
  44. double d_radius;
  45. };
  46. /*!
  47. Constructs a null point, with a radius and azimuth set to 0.0.
  48. \sa QPointF::isNull()
  49. */
  50. inline QwtPointPolar::QwtPointPolar():
  51. d_azimuth( 0.0 ),
  52. d_radius( 0.0 )
  53. {
  54. }
  55. /*!
  56. Constructs a point with coordinates specified by radius and azimuth.
  57. \param azimuth Azimuth
  58. \param radius Radius
  59. */
  60. inline QwtPointPolar::QwtPointPolar( double azimuth, double radius ):
  61. d_azimuth( azimuth ),
  62. d_radius( radius )
  63. {
  64. }
  65. //! Returns true if radius() >= 0.0
  66. inline bool QwtPointPolar::isValid() const
  67. {
  68. return d_radius >= 0.0;
  69. }
  70. //! Returns true if radius() >= 0.0
  71. inline bool QwtPointPolar::isNull() const
  72. {
  73. return d_radius == 0.0;
  74. }
  75. //! Returns the radius.
  76. inline double QwtPointPolar::radius() const
  77. {
  78. return d_radius;
  79. }
  80. //! Returns the azimuth.
  81. inline double QwtPointPolar::azimuth() const
  82. {
  83. return d_azimuth;
  84. }
  85. //! Returns the radius.
  86. inline double &QwtPointPolar::rRadius()
  87. {
  88. return d_radius;
  89. }
  90. //! Returns the azimuth.
  91. inline double &QwtPointPolar::rAzimuth()
  92. {
  93. return d_azimuth;
  94. }
  95. //! Sets the radius to radius.
  96. inline void QwtPointPolar::setRadius( double radius )
  97. {
  98. d_radius = radius;
  99. }
  100. //! Sets the atimuth to atimuth.
  101. inline void QwtPointPolar::setAzimuth( double azimuth )
  102. {
  103. d_azimuth = azimuth;
  104. }
  105. #ifndef QT_NO_DEBUG_STREAM
  106. QWT_EXPORT QDebug operator<<( QDebug, const QwtPointPolar & );
  107. #endif
  108. inline QPoint qwtPolar2Pos( const QPoint &pole,
  109. double radius, double angle )
  110. {
  111. const double x = pole.x() + radius * qCos( angle );
  112. const double y = pole.y() - radius * qSin( angle );
  113. return QPoint( qRound( x ), qRound( y ) );
  114. }
  115. inline QPoint qwtDegree2Pos( const QPoint &pole,
  116. double radius, double angle )
  117. {
  118. return qwtPolar2Pos( pole, radius, angle / 180.0 * M_PI );
  119. }
  120. inline QPointF qwtPolar2Pos( const QPointF &pole,
  121. double radius, double angle )
  122. {
  123. const double x = pole.x() + radius * qCos( angle );
  124. const double y = pole.y() - radius * qSin( angle );
  125. return QPointF( x, y);
  126. }
  127. inline QPointF qwtDegree2Pos( const QPointF &pole,
  128. double radius, double angle )
  129. {
  130. return qwtPolar2Pos( pole, radius, angle / 180.0 * M_PI );
  131. }
  132. inline QPointF qwtFastPolar2Pos( const QPointF &pole,
  133. double radius, double angle )
  134. {
  135. #if QT_VERSION < 0x040601
  136. const double x = pole.x() + radius * ::cos( angle );
  137. const double y = pole.y() - radius * ::sin( angle );
  138. #else
  139. const double x = pole.x() + radius * qFastCos( angle );
  140. const double y = pole.y() - radius * qFastSin( angle );
  141. #endif
  142. return QPointF( x, y);
  143. }
  144. inline QPointF qwtFastDegree2Pos( const QPointF &pole,
  145. double radius, double angle )
  146. {
  147. return qwtFastPolar2Pos( pole, radius, angle / 180.0 * M_PI );
  148. }
  149. inline QwtPointPolar qwtFastPos2Polar( const QPointF &pos )
  150. {
  151. return QwtPointPolar( qwtFastAtan2( pos.y(), pos.x() ),
  152. qSqrt( qwtSqr( pos.x() ) + qwtSqr( pos.y() ) ) );
  153. }
  154. #endif