当前位置 : 主页 > 网络编程 > PHP >

php的缓动算法

来源:互联网 收集:自由互联 发布时间:2021-06-28
tween.php function($t,$b,$c,$d) { return $c*$t/$d + $b; }, 1 = function($t,$b,$c,$d) { return $b - $c*$t/$d; }, 2 = function($t,$b,$c,$d) { return $c*($t/=$d)*$t + $b; }, 3 = function($t,$b,$c,$d) { return -$c *($t/=$d)*($t-2) + $b; }, 4 =
tween.php
  function($t,$b,$c,$d) {
        return $c*$t/$d + $b;
    },
    1 => function($t,$b,$c,$d) {
        return $b - $c*$t/$d;
    },
    2 => function($t,$b,$c,$d) {
        return $c*($t/=$d)*$t + $b;
    },
    3 => function($t,$b,$c,$d) {
        return -$c *($t/=$d)*($t-2) + $b;
    },
    4 => function($t,$b,$c,$d) {
        return $c*($t/=$d)*$t*$t + $b;
    },
    5 => function($t,$b,$c,$d) {
        return $c*(($t=$t/$d-1)*$t*$t + 1) + $b;
    },
    6 => function($t,$b,$c,$d) {
        return $c*($t/=$d)*$t*$t*$t + $b;
    },
    7 => function($t,$b,$c,$d) {
        return -$c * (($t=$t/$d-1)*$t*$t*$t - 1) + $b;
    },
    8 => function($t,$b,$c,$d) {
        return $c*($t/=$d)*$t*$t*$t*$t + $b;
    },
    9 => function($t,$b,$c,$d) {
        return $c*(($t=$t/$d-1)*$t*$t*$t*$t + 1) + $b;
    },
    10 => function($t,$b,$c,$d) {
        return ($t==0) ? $b : $c * pow(2, 10 * ($t/$d - 1)) + $b;
    },
    11 => function($t,$b,$c,$d) {
        return ($t==$d) ? $b+$c : $c * (-pow(2, -10 * $t/$d) + 1) + $b;
    }
            
];
$t = time();//最后的时间 修改更改
$b = 10000 + rand(0,20);
$rt = [];
shuffle($tween);
$key = rand(0,11);//添加其他后再增加
$func = $tween[$key];
//$func = $tween[11];
$d = 30;
for($i;$i < $d; $i++) {
    $j = $t + $i;
    $v = $func($i,$b, $i, $d);
    $rt[$j] = sprintf("%.2f", $v);
}
var_export($rt);
优化后的算法
  function($num) {
        return pow($num, 2);
    },
    1 => function($num) {
        return -(pow(($num - 1), 2) - 1);
    },
    2 => function($num) {
        if (($num /= 0.5) < 1)
            return 0.5 * pow($num, 2);
        return -0.5 * (($num -= 2) * $num - 2);
    },
    3 => function($num) {
        return pow($num, 3);
    },
    4 => function($num) {
        return (pow(($num - 1), 3) + 1);
    },
    5 => function($num) {
        if (($num /= 0.5) < 1)
            return 0.5 * pow($num, 3);
        return 0.5 * (pow(($num - 2), 3) + 2);
    },
    6 => function($num) {
        return pow($num, 4);
    },
    7 => function($num) {
        return -(pow(($num - 1), 4) - 1);
    },
    8 => function($num) {
        if (($num /= 0.5) < 1)
            return 0.5 * pow($num, 4);
        return -0.5 * (($num -= 2) * pow($num, 3) - 2);
    },
    9 => function($num) {
        return pow($num, 5);
    },
    10 => function($num) {
        return (pow(($num - 1), 5) + 1);
    },
    11 => function($num) {
        if (($num /= 0.5) < 1)
            return 0.5 * pow($num, 5);
        return 0.5 * (pow(($num - 2), 5) + 2);
    },
    12 => function($num) {
        return -cos($num * (pi() / 2)) + 1;
    },
    13 => function($num) {
        return (-.5 * (cos(pi() * $num) - 1));
    },
    14 => function($num) {
        return ($num == 0) ? 0 : pow(2, 10 * ($num - 1));
    },
    15 => function($num) {
        return ($num == 1) ? 1 : -pow(2, -10 * $num) + 1;
    },
    16 => function($num) {
        if ($num == 0)
            return 0;
        if ($num == 1)
            return 1;
        if (($num /= 0.5) < 1)
            return 0.5 * pow(2, 10 * ($num - 1));
        return 0.5 * (-pow(2, -10 * --$num) + 2);
    },
    17 => function($num) {
        return -(tan(1 - ($num * $num)) - 1);
    },
    18 => function($num) {
        return tan(1 - pow(($num - 1), 2));
    },
    19 => function($num) {
        if (($num /= 0.5) < 1)
            return -0.5 * (tan(1 - $num * $num) - 1);
        return 0.5 * (tan(1 - ($num -= 2) * $num) + 1);
    },
    20 => function($num) {
        if (($num) < (1 / 2.75)) {
            return (7.5625 * $num * $num);
        } else if ($num < (2 / 2.75)) {
            return (7.5625 * ($num -= (1.5 / 2.75)) * $num + .75);
        } else if ($num < (2.5 / 2.75)) {
            return (7.5625 * ($num -= (2.25 / 2.75)) * $num + .9375);
        } else {
            return (7.5625 * ($num -= (2.625 / 2.75)) * $num + .984375);
        }
    },
    21 => function($num) {
        $s = 1.70158;
        return ($num) * $num * (($s + 1) * $num - $s);
    },
    22 => function($num) {
        $s = 1.70158;
        return ($num = $num - 1) * $num * ((s + 1) * $num + $s) + 1;
    },
    23 => function($num) {
        $s = 1.70158;
        if (($num /= 0.5) < 1)
            return 0.5 * ($num * $num * ((($s *= (1.525)) + 1) * $num - $s));
        return 0.5 * (($num -= 2) * $num * ((($s *= (1.525)) + 1) * $num + $s) + 2);
    },
    24 => function($num) {
        return -1 * pow(4, -8 * $num) * sin(($num * 6 - 1) * (2 * pi()) / 2) + 1;
    },
    25 => function($num) {
        $s = 1.70158;
        return (($num /= 0.5) < 1) ? 0.5 * ($num * $num * ((($s *= (1.525)) + 1) * $num - $s)) :
                0.5 * (($num -= 2) * $num * ((($s *= (1.525)) + 1) * $num + $s) + 2);
    },
    26 => function($num) {
        $s = 1.70158;
        return $num * $num * (($s + 1) * $num - $s);
    },
    27 => function($num) {
        $s = 1.70158;
        return ($num -= 1) * $num * (($s + 1) * $num + $s) + 1;
    },
    28 => function($num) {
        if ($num < (1 / 2.75)) {
            return (7.5625 * $num * $num);
        } else if ($num < (2 / 2.75)) {
            return (7.5625 * ($num -= (1.5 / 2.75)) * $num + .75);
        } else if ($num < (2.5 / 2.75)) {
            return (7.5625 * ($num -= (2.25 / 2.75)) * $num + .9375);
        } else {
            return (7.5625 * ($num -= (2.625 / 2.75)) * $num + .984375);
        }
    },
    29 => function($num) {
        if ($num < (1 / 2.75)) {
            return (7.5625 * $num * $num);
        } else if ($num < (2 / 2.75)) {
            return 2 - (7.5625 * ($num -= (1.5 / 2.75)) * $num + .75);
        } else if ($num < (2.5 / 2.75)) {
            return 2 - (7.5625 * ($num -= (2.25 / 2.75)) * $num + .9375);
        } else {
            return 2 - (7.5625 * ($num -= (2.625 / 2.75)) * $num + .984375);
        }
    },
    30 => function($num) {
        if (($num /= 0.5) < 1)
            return 0.5 * pow($num, 4);
        return -0.5 * (($num -= 2) * pow($num, 3) - 2);
    },
    31 => function($num) {
        return pow($num, 4);
    },
    32 => function($num) {
        return pow($num, 0.25);
    },
    33 => function($num) {
        return $num;
    },
    34 => function($num) {
        return (-cos($num * pi()) / 2) + 0.5;
    },
    35 => function($num) {
        return 1 - $num;
    },
    36 => function($num) {
        return sin($num * (pi() / 2));
    },
    37 => function($num) {
        $num = $num + (rand() - 0.5) / 5;
        return (-cos(($num < 0 ? 0 : $num > 1 ? 1 : $num) * pi()) / 2) + 0.5;
    },
    38 => function($num) {
        return (-cos($num * pi() * (9 * $num)) / 2) + 0.5;
    },
    39 => function($num) {
        return 1 - (cos($num * 4.5 * pi()) * exp(-$num * 6));
    },
];

shuffle($tween);
$key = rand(0, 39); //添加其他后再增加
$func = $tween[$key];
$t = time(); //最后的时间
$rt = [];
$d = 30;
$b = 10000 + rand(-21, 21);
for ($i = 0; $i < $d; $i++) {
    $v = $func($i/$d)*$d;
    if($v > $d) $v = $d;
    if($v < 0 && $v < -$d) $v = -$d;
    $j = $t + $i;
    $rt[$j] = sprintf("%.2f", $b + $v);
}
var_export($rt);
exit();
网友评论