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 | \note Invalid or null points will be returned as QPointF(0.0, 0.0)
|
---|
42 | \sa isValid(), isNull()
|
---|
43 | */
|
---|
44 | QPointF QwtPointPolar::toPoint() const
|
---|
45 | {
|
---|
46 | if ( d_radius <= 0.0 )
|
---|
47 | return QPointF( 0.0, 0.0 );
|
---|
48 |
|
---|
49 | const double x = d_radius * qCos( d_azimuth );
|
---|
50 | const double y = d_radius * qSin( d_azimuth );
|
---|
51 |
|
---|
52 | return QPointF( x, y );
|
---|
53 | }
|
---|
54 |
|
---|
55 | /*!
|
---|
56 | Returns true if point1 is equal to point2; otherwise returns false.
|
---|
57 |
|
---|
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 |
|
---|
62 | \sa normalized()
|
---|
63 | */
|
---|
64 | bool QwtPointPolar::operator==( const QwtPointPolar &other ) const
|
---|
65 | {
|
---|
66 | return d_radius == other.d_radius && d_azimuth == other.d_azimuth;
|
---|
67 | }
|
---|
68 |
|
---|
69 | /*!
|
---|
70 | Returns true if point1 is not equal to point2; otherwise returns false.
|
---|
71 |
|
---|
72 | Two points are equal to each other if radius and
|
---|
73 | azimuth-coordinates are the same. Points are not equal, when
|
---|
74 | the azimuth differs, but other.azimuth() == azimuth() % (2 * PI).
|
---|
75 |
|
---|
76 | \sa normalized()
|
---|
77 | */
|
---|
78 | bool QwtPointPolar::operator!=( const QwtPointPolar &other ) const
|
---|
79 | {
|
---|
80 | return d_radius != other.d_radius || d_azimuth != other.d_azimuth;
|
---|
81 | }
|
---|
82 |
|
---|
83 | /*!
|
---|
84 | Normalize radius and azimuth
|
---|
85 |
|
---|
86 | When the radius is < 0.0 it is set to 0.0. The azimuth is
|
---|
87 | a value >= 0.0 and < 2 * M_PI.
|
---|
88 | */
|
---|
89 | QwtPointPolar QwtPointPolar::normalized() const
|
---|
90 | {
|
---|
91 | const double radius = qMax( d_radius, 0.0 );
|
---|
92 |
|
---|
93 | double azimuth = d_azimuth;
|
---|
94 | if ( azimuth < -2.0 * M_PI || azimuth >= 2 * M_PI )
|
---|
95 | azimuth = ::fmod( d_azimuth, 2 * M_PI );
|
---|
96 |
|
---|
97 | if ( azimuth < 0.0 )
|
---|
98 | azimuth += 2 * M_PI;
|
---|
99 |
|
---|
100 | return QwtPointPolar( azimuth, radius );
|
---|
101 | }
|
---|
102 |
|
---|
103 | #ifndef QT_NO_DEBUG_STREAM
|
---|
104 |
|
---|
105 | QDebug operator<<( QDebug debug, const QwtPointPolar &point )
|
---|
106 | {
|
---|
107 | debug.nospace() << "QwtPointPolar("
|
---|
108 | << point.azimuth() << "," << point.radius() << ")";
|
---|
109 |
|
---|
110 | return debug.space();
|
---|
111 | }
|
---|
112 |
|
---|
113 | #endif
|
---|
114 |
|
---|