1
/* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
3
* Copyright (C) 1997 Josef Wilgen
4
* Copyright (C) 2002 Uwe Rathmann
6
* This library is free software; you can redistribute it and/or
7
* modify it under the terms of the Qwt License, Version 1.0
8
*****************************************************************************/
10
#include "qwt_scale_map.h"
12
QT_STATIC_CONST_IMPL double QwtScaleMap::LogMin = 1.0e-150;
13
QT_STATIC_CONST_IMPL double QwtScaleMap::LogMax = 1.0e150;
15
//! Constructor for a linear transformation
16
QwtScaleTransformation::QwtScaleTransformation(Type type):
21
QwtScaleTransformation::~QwtScaleTransformation()
25
QwtScaleTransformation *QwtScaleTransformation::copy() const
27
return new QwtScaleTransformation(d_type);
31
\brief Transform a value between 2 linear intervals
33
\param x value related to the interval [x1, x2]
34
\param x1 first border of source interval
35
\param x2 first border of source interval
36
\param y1 first border of target interval
37
\param y2 first border of target interval
40
<dt>linear mapping:<dd>y1 + (y2 - y1) / (x2 - x1) * (x - x1)</dd>
43
<dt>log10 mapping: <dd>p1 + (p2 - p1) / log(s2 / s1) * log(x / s1)</dd>
47
double QwtScaleTransformation::xForm(
48
double s, double s1, double s2, double p1, double p2) const
50
if ( d_type == Log10 )
51
return p1 + (p2 - p1) / log(s2 / s1) * log(s / s1);
53
return p1 + (p2 - p1) / (s2 - s1) * (s - s1);
57
\brief Transform a value from a linear to a logarithmic interval
59
\param x value related to the linear interval [p1, p2]
60
\param p1 first border of linear interval
61
\param p2 first border of linear interval
62
\param s1 first border of logarithmic interval
63
\param s2 first border of logarithmic interval
66
<dt>exp((x - p1) / (p2 - p1) * log(s2 / s1)) * s1;
70
double QwtScaleTransformation::invXForm(double p, double p1, double p2,
71
double s1, double s2) const
73
if ( d_type == Log10 )
74
return exp((p - p1) / (p2 - p1) * log(s2 / s1)) * s1;
76
return s1 + (s2 - s1) / (p2 - p1) * (p - p1);
82
The scale and paint device intervals are both set to [0,1].
84
QwtScaleMap::QwtScaleMap():
91
d_transformation = new QwtScaleTransformation(
92
QwtScaleTransformation::Linear);
95
QwtScaleMap::QwtScaleMap(const QwtScaleMap& other):
102
d_transformation = other.d_transformation->copy();
108
QwtScaleMap::~QwtScaleMap()
110
delete d_transformation;
113
QwtScaleMap &QwtScaleMap::operator=(const QwtScaleMap &other)
121
delete d_transformation;
122
d_transformation = other.d_transformation->copy();
128
Initialize the map with a transformation
130
void QwtScaleMap::setTransformation(
131
QwtScaleTransformation *transformation)
133
if ( transformation == NULL )
136
delete d_transformation;
137
d_transformation = transformation;
138
setScaleInterval(d_s1, d_s2);
141
//! Get the transformation
142
const QwtScaleTransformation *QwtScaleMap::transformation() const
144
return d_transformation;
148
\brief Specify the borders of the scale interval
149
\param s1 first border
150
\param s2 second border
151
\warning logarithmic scales might be aligned to [LogMin, LogMax]
153
void QwtScaleMap::setScaleInterval(double s1, double s2)
155
if (d_transformation->type() == QwtScaleTransformation::Log10 )
159
else if (s1 > LogMax)
164
else if (s2 > LogMax)
171
if ( d_transformation->type() != QwtScaleTransformation::Other )
176
\brief Specify the borders of the paint device interval
177
\param p1 first border
178
\param p2 second border
180
void QwtScaleMap::setPaintInterval(int p1, int p2)
185
if ( d_transformation->type() != QwtScaleTransformation::Other )
190
\brief Specify the borders of the paint device interval
191
\param p1 first border
192
\param p2 second border
194
void QwtScaleMap::setPaintXInterval(double p1, double p2)
199
if ( d_transformation->type() != QwtScaleTransformation::Other )
204
\brief Re-calculate the conversion factor.
206
void QwtScaleMap::newFactor()
214
switch( d_transformation->type() )
216
case QwtScaleTransformation::Linear:
217
d_cnv = (d_p2 - d_p1) / (d_s2 - d_s1);
219
case QwtScaleTransformation::Log10:
220
d_cnv = (d_p2 - d_p1) / log(d_s2 / d_s1);