Qwt User's Guide 6.3.0

Loading...
Searching...
No Matches
qwt_point_polar.cpp
1/******************************************************************************
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 >= 0x050200
13
14 static QwtPointPolar qwtPointToPolar( const QPointF& point )
15{
16 return QwtPointPolar( point );
17}
18
19#endif
20
21 namespace
22{
23 static const struct RegisterQwtPointPolar
24 {
25 inline RegisterQwtPointPolar()
26 {
27 qRegisterMetaType< QwtPointPolar >();
28
29#if QT_VERSION >= 0x050200
30 QMetaType::registerConverter< QPointF, QwtPointPolar >( qwtPointToPolar );
31 QMetaType::registerConverter< QwtPointPolar, QPointF >( &QwtPointPolar::toPoint );
32#endif
33 }
34
35 } qwtRegisterQwtPointPolar;
36}
37
44 QwtPointPolar::QwtPointPolar( const QPointF& p )
45{
46 m_radius = std::sqrt( qwtSqr( p.x() ) + qwtSqr( p.y() ) );
47 m_azimuth = std::atan2( p.y(), p.x() );
48}
49
54 void QwtPointPolar::setPoint( const QPointF& p )
55{
56 m_radius = std::sqrt( qwtSqr( p.x() ) + qwtSqr( p.y() ) );
57 m_azimuth = std::atan2( p.y(), p.x() );
58}
59
68QPointF QwtPointPolar::toPoint() const
69{
70 if ( m_radius <= 0.0 )
71 return QPointF( 0.0, 0.0 );
72
73 const double x = m_radius * std::cos( m_azimuth );
74 const double y = m_radius * std::sin( m_azimuth );
75
76 return QPointF( x, y );
77}
78
90 bool QwtPointPolar::operator==( const QwtPointPolar& other ) const
91{
92 return m_radius == other.m_radius && m_azimuth == other.m_azimuth;
93}
94
105 bool QwtPointPolar::operator!=( const QwtPointPolar& other ) const
106{
107 return m_radius != other.m_radius || m_azimuth != other.m_azimuth;
108}
109
118 QwtPointPolar QwtPointPolar::normalized() const
119{
120 const double radius = qwtMaxF( m_radius, 0.0 );
121
122 double azimuth = m_azimuth;
123 if ( azimuth < -2.0 * M_PI || azimuth >= 2 * M_PI )
124 azimuth = std::fmod( m_azimuth, 2 * M_PI );
125
126 if ( azimuth < 0.0 )
127 azimuth += 2 * M_PI;
128
129 return QwtPointPolar( azimuth, radius );
130}
131
132#ifndef QT_NO_DEBUG_STREAM
133
134#include <qdebug.h>
135
136QDebug operator<<( QDebug debug, const QwtPointPolar& point )
137{
138 debug.nospace() << "QwtPointPolar("
139 << point.azimuth() << "," << point.radius() << ")";
140
141 return debug.space();
142}
143
144#endif
145
QwtPointPolar
A point in polar coordinates.
Definition qwt_point_polar.h:29
QwtPointPolar::radius
double radius() const
Returns the radius.
Definition qwt_point_polar.h:102
QwtPointPolar::QwtPointPolar
QwtPointPolar()
Definition qwt_point_polar.h:71
QwtPointPolar::azimuth
double azimuth() const
Returns the azimuth.
Definition qwt_point_polar.h:108
QwtPointPolar::toPoint
QPointF toPoint() const
Definition qwt_point_polar.cpp:68
QwtPointPolar::setPoint
void setPoint(const QPointF &)
Definition qwt_point_polar.cpp:54
QwtPointPolar::operator!=
bool operator!=(const QwtPointPolar &) const
Definition qwt_point_polar.cpp:105
QwtPointPolar::normalized
QwtPointPolar normalized() const
Definition qwt_point_polar.cpp:118
QwtPointPolar::operator==
bool operator==(const QwtPointPolar &) const
Compare 2 points.
Definition qwt_point_polar.cpp:90