qwt_point_polar.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
  2. * QwtPolar Widget Library
  3. * Copyright (C) 2008 Uwe Rathmann
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the Qwt License, Version 1.0
  7. *****************************************************************************/
  8. #include "qwt_point_polar.h"
  9. #include "qwt_math.h"
  10. #if QT_VERSION < 0x040601
  11. #define qAtan2(y, x) ::atan2(y, x)
  12. #endif
  13. /*!
  14. Convert and assign values from a point in Cartesian coordinates
  15. \param p Point in Cartesian coordinates
  16. \sa setPoint(), toPoint()
  17. */
  18. QwtPointPolar::QwtPointPolar( const QPointF &p )
  19. {
  20. d_radius = qSqrt( qwtSqr( p.x() ) + qwtSqr( p.y() ) );
  21. d_azimuth = qAtan2( p.y(), p.x() );
  22. }
  23. /*!
  24. Convert and assign values from a point in Cartesian coordinates
  25. \param p Point in Cartesian coordinates
  26. */
  27. void QwtPointPolar::setPoint( const QPointF &p )
  28. {
  29. d_radius = qSqrt( qwtSqr( p.x() ) + qwtSqr( p.y() ) );
  30. d_azimuth = qAtan2( p.y(), p.x() );
  31. }
  32. /*!
  33. Convert and return values in Cartesian coordinates
  34. \note Invalid or null points will be returned as QPointF(0.0, 0.0)
  35. \sa isValid(), isNull()
  36. */
  37. QPointF QwtPointPolar::toPoint() const
  38. {
  39. if ( d_radius <= 0.0 )
  40. return QPointF( 0.0, 0.0 );
  41. const double x = d_radius * qCos( d_azimuth );
  42. const double y = d_radius * qSin( d_azimuth );
  43. return QPointF( x, y );
  44. }
  45. /*!
  46. Returns true if point1 is equal to point2; otherwise returns false.
  47. Two points are equal to each other if radius and
  48. azimuth-coordinates are the same. Points are not equal, when
  49. the azimuth differs, but other.azimuth() == azimuth() % (2 * PI).
  50. \sa normalized()
  51. */
  52. bool QwtPointPolar::operator==( const QwtPointPolar &other ) const
  53. {
  54. return d_radius == other.d_radius && d_azimuth == other.d_azimuth;
  55. }
  56. /*!
  57. Returns true if point1 is not equal to point2; otherwise returns false.
  58. Two points are equal to each other if radius and
  59. azimuth-coordinates are the same. Points are not equal, when
  60. the azimuth differs, but other.azimuth() == azimuth() % (2 * PI).
  61. \sa normalized()
  62. */
  63. bool QwtPointPolar::operator!=( const QwtPointPolar &other ) const
  64. {
  65. return d_radius != other.d_radius || d_azimuth != other.d_azimuth;
  66. }
  67. /*!
  68. Normalize radius and azimuth
  69. When the radius is < 0.0 it is set to 0.0. The azimuth is
  70. a value >= 0.0 and < 2 * M_PI.
  71. */
  72. QwtPointPolar QwtPointPolar::normalized() const
  73. {
  74. const double radius = qMax( d_radius, 0.0 );
  75. double azimuth = d_azimuth;
  76. if ( azimuth < -2.0 * M_PI || azimuth >= 2 * M_PI )
  77. azimuth = ::fmod( d_azimuth, 2 * M_PI );
  78. if ( azimuth < 0.0 )
  79. azimuth += 2 * M_PI;
  80. return QwtPointPolar( azimuth, radius );
  81. }
  82. #ifndef QT_NO_DEBUG_STREAM
  83. QDebug operator<<( QDebug debug, const QwtPointPolar &point )
  84. {
  85. debug.nospace() << "QwtPointPolar("
  86. << point.azimuth() << "," << point.radius() << ")";
  87. return debug.space();
  88. }
  89. #endif