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
10 /*! \file */
11 #ifndef _QWT_POINT_POLAR_H_
12 #define _QWT_POINT_POLAR_H_ 1
13
14 #include "qwt_global.h"
15 #include "qwt_math.h"
16 #include <qpoint.h>
17 #ifndef QT_NO_DEBUG_STREAM
18 #include <qdebug.h>
19 #endif
20
21 /*!
22 \brief A point in polar coordinates
23
24 In polar coordinates a point is determined by an angle and a distance.
25 See http://en.wikipedia.org/wiki/Polar_coordinate_system
26 */
27
28 class QWT_EXPORT QwtPointPolar
29 {
30 public:
31 QwtPointPolar();
32 QwtPointPolar( double azimuth, double radius );
33 QwtPointPolar( const QPointF & );
34
35 void setPoint( const QPointF & );
36 QPointF toPoint() const;
37
38 bool isValid() const;
39 bool isNull() const;
40
41 double radius() const;
42 double azimuth() const;
43
44 double &rRadius();
45 double &rAzimuth();
46
47 void setRadius( double );
48 void setAzimuth( double );
49
50 bool operator==( const QwtPointPolar & ) const;
51 bool operator!=( const QwtPointPolar & ) const;
52
53 QwtPointPolar normalized() const;
54
55 private:
56 double d_azimuth;
57 double d_radius;
58 };
59
60 /*!
61 Constructs a null point, with a radius and azimuth set to 0.0.
62 \sa QPointF::isNull()
63 */
QwtPointPolar()64 inline QwtPointPolar::QwtPointPolar():
65 d_azimuth( 0.0 ),
66 d_radius( 0.0 )
67 {
68 }
69
70 /*!
71 Constructs a point with coordinates specified by radius and azimuth.
72
73 \param azimuth Azimuth
74 \param radius Radius
75 */
QwtPointPolar(double azimuth,double radius)76 inline QwtPointPolar::QwtPointPolar( double azimuth, double radius ):
77 d_azimuth( azimuth ),
78 d_radius( radius )
79 {
80 }
81
82 //! Returns true if radius() >= 0.0
isValid()83 inline bool QwtPointPolar::isValid() const
84 {
85 return d_radius >= 0.0;
86 }
87
88 //! Returns true if radius() >= 0.0
isNull()89 inline bool QwtPointPolar::isNull() const
90 {
91 return d_radius == 0.0;
92 }
93
94 //! Returns the radius.
radius()95 inline double QwtPointPolar::radius() const
96 {
97 return d_radius;
98 }
99
100 //! Returns the azimuth.
azimuth()101 inline double QwtPointPolar::azimuth() const
102 {
103 return d_azimuth;
104 }
105
106 //! Returns the radius.
rRadius()107 inline double &QwtPointPolar::rRadius()
108 {
109 return d_radius;
110 }
111
112 //! Returns the azimuth.
rAzimuth()113 inline double &QwtPointPolar::rAzimuth()
114 {
115 return d_azimuth;
116 }
117
118 //! Sets the radius to radius.
setRadius(double radius)119 inline void QwtPointPolar::setRadius( double radius )
120 {
121 d_radius = radius;
122 }
123
124 //! Sets the atimuth to atimuth.
setAzimuth(double azimuth)125 inline void QwtPointPolar::setAzimuth( double azimuth )
126 {
127 d_azimuth = azimuth;
128 }
129
130 #ifndef QT_NO_DEBUG_STREAM
131 QWT_EXPORT QDebug operator<<( QDebug, const QwtPointPolar & );
132 #endif
133
qwtPolar2Pos(const QPoint & pole,double radius,double angle)134 inline QPoint qwtPolar2Pos( const QPoint &pole,
135 double radius, double angle )
136 {
137 const double x = pole.x() + radius * qCos( angle );
138 const double y = pole.y() - radius * qSin( angle );
139
140 return QPoint( qRound( x ), qRound( y ) );
141 }
142
qwtDegree2Pos(const QPoint & pole,double radius,double angle)143 inline QPoint qwtDegree2Pos( const QPoint &pole,
144 double radius, double angle )
145 {
146 return qwtPolar2Pos( pole, radius, angle / 180.0 * M_PI );
147 }
148
qwtPolar2Pos(const QPointF & pole,double radius,double angle)149 inline QPointF qwtPolar2Pos( const QPointF &pole,
150 double radius, double angle )
151 {
152 const double x = pole.x() + radius * qCos( angle );
153 const double y = pole.y() - radius * qSin( angle );
154
155 return QPointF( x, y);
156 }
157
qwtDegree2Pos(const QPointF & pole,double radius,double angle)158 inline QPointF qwtDegree2Pos( const QPointF &pole,
159 double radius, double angle )
160 {
161 return qwtPolar2Pos( pole, radius, angle / 180.0 * M_PI );
162 }
163
qwtFastPolar2Pos(const QPointF & pole,double radius,double angle)164 inline QPointF qwtFastPolar2Pos( const QPointF &pole,
165 double radius, double angle )
166 {
167 #if QT_VERSION < 0x040601
168 const double x = pole.x() + radius * ::cos( angle );
169 const double y = pole.y() - radius * ::sin( angle );
170 #else
171 const double x = pole.x() + radius * qFastCos( angle );
172 const double y = pole.y() - radius * qFastSin( angle );
173 #endif
174
175 return QPointF( x, y);
176 }
177
qwtFastDegree2Pos(const QPointF & pole,double radius,double angle)178 inline QPointF qwtFastDegree2Pos( const QPointF &pole,
179 double radius, double angle )
180 {
181 return qwtFastPolar2Pos( pole, radius, angle / 180.0 * M_PI );
182 }
183
qwtFastPos2Polar(const QPointF & pos)184 inline QwtPointPolar qwtFastPos2Polar( const QPointF &pos )
185 {
186 return QwtPointPolar( qwtFastAtan2( pos.y(), pos.x() ),
187 qSqrt( qwtSqr( pos.x() ) + qwtSqr( pos.y() ) ) );
188 }
189
190 #endif
191