| 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_polar_fitter.h"
|
|---|
| 10 | #include <qpolygon.h>
|
|---|
| 11 | #include <qpainterpath.h>
|
|---|
| 12 |
|
|---|
| 13 | class QwtPolarFitter::PrivateData
|
|---|
| 14 | {
|
|---|
| 15 | public:
|
|---|
| 16 | PrivateData()
|
|---|
| 17 | : stepCount( 5 )
|
|---|
| 18 | {
|
|---|
| 19 | }
|
|---|
| 20 |
|
|---|
| 21 | int stepCount;
|
|---|
| 22 | };
|
|---|
| 23 |
|
|---|
| 24 | /*!
|
|---|
| 25 | Constructor
|
|---|
| 26 |
|
|---|
| 27 | \param stepCount Number of points, that will be inserted between 2 points
|
|---|
| 28 | \sa setStepCount()
|
|---|
| 29 | */
|
|---|
| 30 | QwtPolarFitter::QwtPolarFitter( int stepCount )
|
|---|
| 31 | : QwtCurveFitter( QwtPolarFitter::Polygon )
|
|---|
| 32 | {
|
|---|
| 33 | m_data = new PrivateData;
|
|---|
| 34 | m_data->stepCount = stepCount;
|
|---|
| 35 | }
|
|---|
| 36 |
|
|---|
| 37 | //! Destructor
|
|---|
| 38 | QwtPolarFitter::~QwtPolarFitter()
|
|---|
| 39 | {
|
|---|
| 40 | delete m_data;
|
|---|
| 41 | }
|
|---|
| 42 |
|
|---|
| 43 | /*!
|
|---|
| 44 | Assign the number of points, that will be inserted between 2 points
|
|---|
| 45 | The default value is 5.
|
|---|
| 46 |
|
|---|
| 47 | \param stepCount Number of steps
|
|---|
| 48 |
|
|---|
| 49 | \sa stepCount()
|
|---|
| 50 | */
|
|---|
| 51 | void QwtPolarFitter::setStepCount( int stepCount )
|
|---|
| 52 | {
|
|---|
| 53 | m_data->stepCount = qMax( stepCount, 0 );
|
|---|
| 54 | }
|
|---|
| 55 |
|
|---|
| 56 | /*!
|
|---|
| 57 | \return Number of points, that will be inserted between 2 points
|
|---|
| 58 | \sa setStepCount()
|
|---|
| 59 | */
|
|---|
| 60 | int QwtPolarFitter::stepCount() const
|
|---|
| 61 | {
|
|---|
| 62 | return m_data->stepCount;
|
|---|
| 63 | }
|
|---|
| 64 |
|
|---|
| 65 | /*!
|
|---|
| 66 | Insert stepCount() number of additional points between 2 elements
|
|---|
| 67 | of points.
|
|---|
| 68 |
|
|---|
| 69 | \param points Array of points
|
|---|
| 70 | \return Array of points including the additional points
|
|---|
| 71 | */
|
|---|
| 72 | QPolygonF QwtPolarFitter::fitCurve( const QPolygonF& points ) const
|
|---|
| 73 | {
|
|---|
| 74 | if ( m_data->stepCount <= 0 || points.size() <= 1 )
|
|---|
| 75 | return points;
|
|---|
| 76 |
|
|---|
| 77 | QPolygonF fittedPoints;
|
|---|
| 78 |
|
|---|
| 79 | int numPoints = points.size() + ( points.size() - 1 ) * m_data->stepCount;
|
|---|
| 80 |
|
|---|
| 81 | fittedPoints.resize( numPoints );
|
|---|
| 82 |
|
|---|
| 83 | int index = 0;
|
|---|
| 84 | fittedPoints[index++] = points[0];
|
|---|
| 85 | for ( int i = 1; i < points.size(); i++ )
|
|---|
| 86 | {
|
|---|
| 87 | const QPointF& p1 = points[i - 1];
|
|---|
| 88 | const QPointF& p2 = points[i];
|
|---|
| 89 |
|
|---|
| 90 | const double dx = ( p2.x() - p1.x() ) / m_data->stepCount;
|
|---|
| 91 | const double dy = ( p2.y() - p1.y() ) / m_data->stepCount;
|
|---|
| 92 | for ( int j = 1; j <= m_data->stepCount; j++ )
|
|---|
| 93 | {
|
|---|
| 94 | const double x = p1.x() + j * dx;
|
|---|
| 95 | const double y = p1.y() + j * dy;
|
|---|
| 96 |
|
|---|
| 97 | fittedPoints[index++] = QPointF( x, y );
|
|---|
| 98 | }
|
|---|
| 99 | }
|
|---|
| 100 | fittedPoints.resize( index );
|
|---|
| 101 |
|
|---|
| 102 | return fittedPoints;
|
|---|
| 103 | }
|
|---|
| 104 |
|
|---|
| 105 | /*!
|
|---|
| 106 | \param points Series of data points
|
|---|
| 107 | \return Curve path
|
|---|
| 108 | \sa fitCurve()
|
|---|
| 109 | */
|
|---|
| 110 | QPainterPath QwtPolarFitter::fitCurvePath( const QPolygonF& points ) const
|
|---|
| 111 | {
|
|---|
| 112 | QPainterPath path;
|
|---|
| 113 | path.addPolygon( fitCurve( points ) );
|
|---|
| 114 | return path;
|
|---|
| 115 | }
|
|---|