qwt_point_polar.cpp
/******************************************************************************
* QwtPolar Widget Library
* Copyright (C) 2008 Uwe Rathmann
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the Qwt License, Version 1.0
*****************************************************************************/
#include "qwt_point_polar.h"
#include "qwt_math.h"
#if QT_VERSION >= 0x050200
static QwtPointPolar qwtPointToPolar( const QPointF& point )
{
return QwtPointPolar( point );
}
#endif
namespace
{
static const struct RegisterQwtPointPolar
{
inline RegisterQwtPointPolar()
{
qRegisterMetaType< QwtPointPolar >();
#if QT_VERSION >= 0x050200
QMetaType::registerConverter< QPointF, QwtPointPolar >( qwtPointToPolar );
QMetaType::registerConverter< QwtPointPolar, QPointF >( &QwtPointPolar::toPoint );
#endif
}
} qwtRegisterQwtPointPolar;
}
QwtPointPolar::QwtPointPolar( const QPointF& p )
{
m_radius = std::sqrt( qwtSqr( p.x() ) + qwtSqr( p.y() ) );
m_azimuth = std::atan2( p.y(), p.x() );
}
void QwtPointPolar::setPoint( const QPointF& p )
{
m_radius = std::sqrt( qwtSqr( p.x() ) + qwtSqr( p.y() ) );
m_azimuth = std::atan2( p.y(), p.x() );
}
QPointF QwtPointPolar::toPoint() const
{
if ( m_radius <= 0.0 )
return QPointF( 0.0, 0.0 );
const double x = m_radius * std::cos( m_azimuth );
const double y = m_radius * std::sin( m_azimuth );
return QPointF( x, y );
}
bool QwtPointPolar::operator==( const QwtPointPolar& other ) const
{
return m_radius == other.m_radius && m_azimuth == other.m_azimuth;
}
bool QwtPointPolar::operator!=( const QwtPointPolar& other ) const
{
return m_radius != other.m_radius || m_azimuth != other.m_azimuth;
}
QwtPointPolar QwtPointPolar::normalized() const
{
const double radius = qwtMaxF( m_radius, 0.0 );
double azimuth = m_azimuth;
if ( azimuth < -2.0 * M_PI || azimuth >= 2 * M_PI )
azimuth = std::fmod( m_azimuth, 2 * M_PI );
if ( azimuth < 0.0 )
azimuth += 2 * M_PI;
return QwtPointPolar( azimuth, radius );
}
#ifndef QT_NO_DEBUG_STREAM
#include <qdebug.h>
QDebug operator<<( QDebug debug, const QwtPointPolar& point )
{
debug.nospace() << "QwtPointPolar("
<< point.azimuth() << "," << point.radius() << ")";
return debug.space();
}
#endif
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