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 |
|
---|
9 | #include "qwt_point_polar.h"
|
---|
10 | #include "qwt_math.h"
|
---|
11 |
|
---|
12 | #if QT_VERSION < 0x040601
|
---|
13 | #define qAtan2(y, x) ::atan2(y, x)
|
---|
14 | #endif
|
---|
15 |
|
---|
16 | /*!
|
---|
17 | Convert and assign values from a point in Cartesian coordinates
|
---|
18 |
|
---|
19 | \param p Point in Cartesian coordinates
|
---|
20 | \sa setPoint(), toPoint()
|
---|
21 | */
|
---|
22 | QwtPointPolar::QwtPointPolar( const QPointF &p )
|
---|
23 | {
|
---|
24 | d_radius = qSqrt( qwtSqr( p.x() ) + qwtSqr( p.y() ) );
|
---|
25 | d_azimuth = qAtan2( p.y(), p.x() );
|
---|
26 | }
|
---|
27 |
|
---|
28 | /*!
|
---|
29 | Convert and assign values from a point in Cartesian coordinates
|
---|
30 | \param p Point in Cartesian coordinates
|
---|
31 | */
|
---|
32 | void QwtPointPolar::setPoint( const QPointF &p )
|
---|
33 | {
|
---|
34 | d_radius = qSqrt( qwtSqr( p.x() ) + qwtSqr( p.y() ) );
|
---|
35 | d_azimuth = qAtan2( p.y(), p.x() );
|
---|
36 | }
|
---|
37 |
|
---|
38 | /*!
|
---|
39 | Convert and return values in Cartesian coordinates
|
---|
40 |
|
---|
41 | \return Converted point in Cartesian coordinates
|
---|
42 |
|
---|
43 | \note Invalid or null points will be returned as QPointF(0.0, 0.0)
|
---|
44 | \sa isValid(), isNull()
|
---|
45 | */
|
---|
46 | QPointF QwtPointPolar::toPoint() const
|
---|
47 | {
|
---|
48 | if ( d_radius <= 0.0 )
|
---|
49 | return QPointF( 0.0, 0.0 );
|
---|
50 |
|
---|
51 | const double x = d_radius * qCos( d_azimuth );
|
---|
52 | const double y = d_radius * qSin( d_azimuth );
|
---|
53 |
|
---|
54 | return QPointF( x, y );
|
---|
55 | }
|
---|
56 |
|
---|
57 | /*!
|
---|
58 | \brief Compare 2 points
|
---|
59 |
|
---|
60 | Two points are equal to each other if radius and
|
---|
61 | azimuth-coordinates are the same. Points are not equal, when
|
---|
62 | the azimuth differs, but other.azimuth() == azimuth() % (2 * PI).
|
---|
63 |
|
---|
64 | \return True if the point is equal to other; otherwise return false.
|
---|
65 |
|
---|
66 | \sa normalized()
|
---|
67 | */
|
---|
68 | bool QwtPointPolar::operator==( const QwtPointPolar &other ) const
|
---|
69 | {
|
---|
70 | return d_radius == other.d_radius && d_azimuth == other.d_azimuth;
|
---|
71 | }
|
---|
72 |
|
---|
73 | /*!
|
---|
74 | Compare 2 points
|
---|
75 |
|
---|
76 | Two points are equal to each other if radius and
|
---|
77 | azimuth-coordinates are the same. Points are not equal, when
|
---|
78 | the azimuth differs, but other.azimuth() == azimuth() % (2 * PI).
|
---|
79 |
|
---|
80 | \return True if the point is not equal to other; otherwise return false.
|
---|
81 | \sa normalized()
|
---|
82 | */
|
---|
83 | bool QwtPointPolar::operator!=( const QwtPointPolar &other ) const
|
---|
84 | {
|
---|
85 | return d_radius != other.d_radius || d_azimuth != other.d_azimuth;
|
---|
86 | }
|
---|
87 |
|
---|
88 | /*!
|
---|
89 | Normalize radius and azimuth
|
---|
90 |
|
---|
91 | When the radius is < 0.0 it is set to 0.0. The azimuth is
|
---|
92 | a value >= 0.0 and < 2 * M_PI.
|
---|
93 |
|
---|
94 | \return Normalized point
|
---|
95 | */
|
---|
96 | QwtPointPolar QwtPointPolar::normalized() const
|
---|
97 | {
|
---|
98 | const double radius = qMax( d_radius, 0.0 );
|
---|
99 |
|
---|
100 | double azimuth = d_azimuth;
|
---|
101 | if ( azimuth < -2.0 * M_PI || azimuth >= 2 * M_PI )
|
---|
102 | azimuth = ::fmod( d_azimuth, 2 * M_PI );
|
---|
103 |
|
---|
104 | if ( azimuth < 0.0 )
|
---|
105 | azimuth += 2 * M_PI;
|
---|
106 |
|
---|
107 | return QwtPointPolar( azimuth, radius );
|
---|
108 | }
|
---|
109 |
|
---|
110 | #ifndef QT_NO_DEBUG_STREAM
|
---|
111 |
|
---|
112 | QDebug operator<<( QDebug debug, const QwtPointPolar &point )
|
---|
113 | {
|
---|
114 | debug.nospace() << "QwtPointPolar("
|
---|
115 | << point.azimuth() << "," << point.radius() << ")";
|
---|
116 |
|
---|
117 | return debug.space();
|
---|
118 | }
|
---|
119 |
|
---|
120 | #endif
|
---|
121 |
|
---|