2
* Copyright 2015 Canonical Ltd.
4
* This program is free software; you can redistribute it and/or modify
5
* it under the terms of the GNU Lesser General Public License as published by
6
* the Free Software Foundation; version 3.
8
* This program is distributed in the hope that it will be useful,
9
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
* GNU Lesser General Public License for more details.
13
* You should have received a copy of the GNU Lesser General Public License
14
* along with this program. If not, see <http://www.gnu.org/licenses/>.
18
#include "ucmathutils.h"
23
\inqmlmodule Ubuntu.Components
25
\brief Various mathematical utility functions.
28
UCMathUtils::UCMathUtils(QObject *parent) : QObject(parent)
34
\qmlmethod mathUtils::clamp(x, min, max)
35
Ensure the value x is between min and max
37
double UCMathUtils::clamp(double x, double min, double max)
40
return qBound(min, x, max);
42
// swap min/max if min > max
43
qWarning()<<"MathUtils.clamp, min value should not be bigger than the max value";
44
return qBound(max, x, min);
49
\qmlmethod mathUtils::lerp(delta, from, to)
50
Get the linear interpolation
52
double UCMathUtils::lerp(double delta, double from, double to)
54
return ((1.0 - delta) * from) + (delta * to);
58
\qmlmethod mathUtils::projectValue(x, xmin, xmax, ymin, ymax)
59
Linearly project a value x from [xmin, xmax] into [ymin, ymax]
61
double UCMathUtils::projectValue(double x, double xmin, double xmax, double ymin, double ymax)
63
return ((x - xmin) * ymax - (x - xmax) * ymin) / (xmax - xmin);
67
\qmlmethod mathUtils::clampAndProject(x, xmin, xmax, ymin, ymax)
68
Linearly project a value x, but in addition to projectValue it's clamped to xmin/xmax first
70
double UCMathUtils::clampAndProject(double x, double xmin, double xmax, double ymin, double ymax)
72
return projectValue(clamp(x, xmin, xmax), xmin, xmax, ymin, ymax);
75
QObject *UCMathUtils::qmlRegisterTypeCallback(QQmlEngine *engine, QJSEngine *scriptEngine)
78
Q_UNUSED(scriptEngine);
79
return new UCMathUtils;