2
* Copyright 2012-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/>.
19
\inqmlmodule Ubuntu.Components 1.3
21
\brief Various mathematical utility functions.
27
\qmlmethod mathUtils::clamp(x, min, max)
28
Ensure the value x is between min and max
30
function clamp(x, min, max) {
32
return Math.max(min, Math.min(x, max));
34
// swap min/max if min > max
35
return clamp(x, max, min);
40
\qmlmethod mathUtils::lerp(delta, from, to)
41
Get the linear interpolation
43
function lerp(delta, from, to) {
44
return ((1.0 - delta) * from) + (delta * to);
48
\qmlmethod mathUtils::projectValue(x, xmin, xmax, ymin, ymax)
49
Linearly project a value x from [xmin, xmax] into [ymin, ymax]
51
function projectValue(x, xmin, xmax, ymin, ymax) {
52
return ((x - xmin) * ymax - (x - xmax) * ymin) / (xmax - xmin)
56
\qmlmethod mathUtils::clampAndProject(x, xmin, xmax, ymin, ymax)
57
Linearly project a value x, but in addition to projectValue it's clamped to xmin/xmax first
59
function clampAndProject(x, xmin, xmax, ymin, ymax) {
60
return projectValue(clamp(x, xmin, xmax), xmin, xmax, ymin, ymax)