summaryrefslogtreecommitdiff
path: root/src/libpcp_qwt/src/qwt_point_polar.cpp
blob: 83224eebc5c7ec10adb39c2e2cbfe0444ba0952a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
 * 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 < 0x040601
#define qAtan2(y, x) ::atan2(y, x)
#endif

/*!
   Convert and assign values from a point in Cartesian coordinates

   \param p Point in Cartesian coordinates
   \sa setPoint(), toPoint()
*/
QwtPointPolar::QwtPointPolar( const QPointF &p )
{
    d_radius = qSqrt( qwtSqr( p.x() ) + qwtSqr( p.y() ) );
    d_azimuth = qAtan2( p.y(), p.x() );
}

/*!
   Convert and assign values from a point in Cartesian coordinates
   \param p Point in Cartesian coordinates
*/
void QwtPointPolar::setPoint( const QPointF &p )
{
    d_radius = qSqrt( qwtSqr( p.x() ) + qwtSqr( p.y() ) );
    d_azimuth = qAtan2( p.y(), p.x() );
}

/*!
   Convert and return values in Cartesian coordinates

   \note Invalid or null points will be returned as QPointF(0.0, 0.0)
   \sa isValid(), isNull()
*/
QPointF QwtPointPolar::toPoint() const
{
    if ( d_radius <= 0.0 )
        return QPointF( 0.0, 0.0 );

    const double x = d_radius * qCos( d_azimuth );
    const double y = d_radius * qSin( d_azimuth );

    return QPointF( x, y );
}

/*!
    Returns true if point1 is equal to point2; otherwise returns false.

    Two points are equal to each other if radius and
    azimuth-coordinates are the same. Points are not equal, when
    the azimuth differs, but other.azimuth() == azimuth() % (2 * PI).

    \sa normalized()
*/
bool QwtPointPolar::operator==( const QwtPointPolar &other ) const
{
    return d_radius == other.d_radius && d_azimuth == other.d_azimuth;
}

/*!
    Returns true if point1 is not equal to point2; otherwise returns false.

    Two points are equal to each other if radius and
    azimuth-coordinates are the same. Points are not equal, when
    the azimuth differs, but other.azimuth() == azimuth() % (2 * PI).

    \sa normalized()
*/
bool QwtPointPolar::operator!=( const QwtPointPolar &other ) const
{
    return d_radius != other.d_radius || d_azimuth != other.d_azimuth;
}

/*!
   Normalize radius and azimuth

   When the radius is < 0.0 it is set to 0.0. The azimuth is
   a value >= 0.0 and < 2 * M_PI.
*/
QwtPointPolar QwtPointPolar::normalized() const
{
    const double radius = qMax( d_radius, 0.0 );

    double azimuth = d_azimuth;
    if ( azimuth < -2.0 * M_PI || azimuth >= 2 * M_PI )
        azimuth = ::fmod( d_azimuth, 2 * M_PI );

    if ( azimuth < 0.0 )
        azimuth += 2 * M_PI;

    return QwtPointPolar( azimuth, radius );
}

#ifndef QT_NO_DEBUG_STREAM

QDebug operator<<( QDebug debug, const QwtPointPolar &point )
{
    debug.nospace() << "QwtPointPolar(" 
        << point.azimuth() << "," << point.radius() << ")";

    return debug.space();
}

#endif