如何"顺利"一个带整数的数组

时间:2016-03-15 22:13:29

标签: javascript arrays

我有一个数组:

array = [10, 10, 10, 10, 10]

我想遍历这个数组,以便得到一个"斜率"基于整数。因此,如果整数为2,则结果为:

array = [0, 5, 10, 5, 0]

如果我有一个更大的阵列:

bigArray = [50, 50, 50, 50, 50, 50, 50, 50, 50, 50]

然后如果整数仍为2,则结果为:

bigArray = [0, 25, 50, 50, 50, 50, 50, 50, 25, 0]

整数的值并不重要,只要数组具有" smooth"如上所示结束它。整数只指定受影响的索引数。

1 个答案:

答案 0 :(得分:3)

你可以简单地计算两端。



function smooth(array, part) {
    var i;
    array = array.slice();
    for (i = 0; i < part; i++) {
        array[i] = array[i] * i / part;
        array[array.length - 1 - i] = array[array.length - 1 - i] * i / part ;
    }
    return array;
}

document.write('<pre>' + JSON.stringify(smooth([10, 10, 10, 10, 10], 2), 0, 4) + '</pre>');
document.write('<pre>' + JSON.stringify(smooth([50, 50, 50, 50, 50, 50, 50, 50, 50, 50], 2), 0, 4) + '</pre>');
&#13;
&#13;
&#13;

正弦

的一个例子

&#13;
&#13;
function smooth(array, parts) {
    function f(x) {
        // return x; // linear
        return Math.sin(x * Math.PI / 2); // sine
    }

    var i;
    array = array.slice();
    for (i = 0; i < parts; i++) {
        array[i] = f(i / parts) * array[i] | 0;
        array[array.length - 1 - i] = f(i / parts) * array[array.length - 1 - i] | 0;
    }
    return array;
}

document.write('<pre>' + JSON.stringify(smooth([20, 20, 20, 20, 20, 20, 20], 3), 0, 4) + '</pre>');
document.write('<pre>' + JSON.stringify(smooth([50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50], 5), 0, 4) + '</pre>');
&#13;
&#13;
&#13;

奖励:使用平滑

的回调动态生成数组

&#13;
&#13;
function smooth(length, value, parts, fn) {
    fn = fn || function (x) { return x; };
    return Array.apply(null, { length: length }).map(function (_, i) {
        if (i < parts) {
            return value * fn(i / parts) | 0;
        }
        if (i >= length - parts) {
            return value * fn((length - i - 1) / parts) | 0;
        }
        return value;
    });
}

document.write('<pre>' + JSON.stringify(smooth(5, 10, 2), 0, 4) + '</pre>');
document.write('<pre>' + JSON.stringify(smooth(10, 50, 4), 0, 4) + '</pre>');
document.write('<pre>' + JSON.stringify(smooth(5, 10, 2, function f(x) { return Math.sin(x * Math.PI / 2); }), 0, 4) + '</pre>');
document.write('<pre>' + JSON.stringify(smooth(10, 50, 4, function f(x) { return Math.sin(x * Math.PI / 2); }), 0, 4) + '</pre>');
&#13;
&#13;
&#13;

编辑:此代码适用于http://easings.net/中的公式。如需进一步阅读,我建议您访问What is an easing function?

&#13;
&#13;
// formula     http://easings.net/
// description https://stackoverflow.com/questions/8316882/what-is-an-easing-function
// x: percent
// t: current time,
// b: begInnIng value,
// c: change In value,
// d: duration
function easeInOutQuad(x, t, b, c, d) {
    if ((t /= d / 2) < 1) {
        return c / 2 * t * t + b;
    } else {
        return -c / 2 * ((--t) * (t - 2) - 1) + b;
    }
}

function smooth(array, parts) {
    function fn(f, x, v) {
        return f(x, 1000 * x, 0, v, 1000)
    }

    var i;
    array = array.slice();
    for (i = 0; i < parts; i++) {
        array[i] = fn(easeInOutQuad, i / parts, array[i]) | 0;
        array[array.length - 1 - i] = fn(easeInOutQuad, i / parts, array[array.length - 1 - i]) | 0;
    }
    return array;
}

document.write('<pre>' + JSON.stringify(smooth([20, 20, 20, 20, 20, 20, 20], 3), 0, 4) + '</pre>');
document.write('<pre>' + JSON.stringify(smooth([50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50], 4), 0, 4) + '</pre>');
document.write('<pre>' + JSON.stringify(smooth([50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50], 5), 0, 4) + '</pre>');
&#13;
&#13;
&#13;