PHP在基数曲线上插值点

PHP在基数曲线上插值点,php,math,bezier,interpolation,Php,Math,Bezier,Interpolation,我有一组x,y点,希望在php中的基数曲线上插入一组更详细的点 我无法理解如何将web上的数学公式转换为php函数或类 有人能帮忙吗?这是我提出的解决方案,以防其他人也面临同样的问题 function caculate_cardinal_points($points,$tension=0.5,$steps=20) { $return_points = array(); $tangents = array(); // calculate tangents $pr

我有一组x,y点,希望在php中的基数曲线上插入一组更详细的点

我无法理解如何将web上的数学公式转换为php函数或类


有人能帮忙吗?

这是我提出的解决方案,以防其他人也面临同样的问题

function caculate_cardinal_points($points,$tension=0.5,$steps=20) {

    $return_points = array();
    $tangents = array();

    // calculate tangents
    $previous_point = false;
    for($i=0;$i<count($points);$i++) {
        $px = $points[$i][0];
        $py = $points[$i][1];
        if (isset($points[$i+1]) && isset($points[$i-1])) {
            $tx = ($tension * (($points[$i+1][0]-$px) - ($points[$i-1][0]-$px)));
            $ty = ($tension * (($points[$i+1][1]-$py) - ($points[$i-1][1]-$py))); 
        } elseif (isset($points[$i+1])) {
            $tx = ($tension * (($points[$i+1][0]-$px) - ($points[$i][0]-$px)));
            $ty = ($tension * (($points[$i+1][1]-$py) - ($points[$i][1]-$py)));
        } elseif (isset($points[$i-1])) {
            $tx = ($tension * (($points[$i][0]-$px) - ($points[$i-1][0]-$px)));
            $ty = ($tension * (($points[$i][1]-$py) - ($points[$i-1][1]-$py)));
        }
        $tangents[] = array($tx,$ty);
        $previous_x = $px;
        $previous_y = $py;
    }

    // interpolate
    for($i=0;$i<count($tangents)-1;$i++) {
        $p0x = $points[$i][0];
        $p0y = $points[$i][1];
        $p1x = $points[$i+1][0];
        $p1y = $points[$i+1][1];
        $t0x = $tangents[$i][0];
        $t0y = $tangents[$i][1];
        $t1x = $tangents[$i+1][0];
        $t1y = $tangents[$i+1][1];
        $previous_x = $p0x;
        $previous_y = $p0y;
        $return_points[] = array($p0x,$p0y);
        for ($t=0; $t < $steps; $t++) {
            $s = $t / $steps;    // scale s to go from 0 to 1
            $h1 = 2*pow($s,3) - 3*pow($s,2) + 1;
            $h2 = pow($s,3) - 2*pow($s,2) + $s;
            $h3 = -2*pow($s,3) + 3*pow($s,2);
            $h4 = pow($s,3) - pow($s,2);
            $x = $h1*$p0x+$h2*$t0x+$h3*$p1x+$h4*$t1x;
            $y = $h1*$p0y+$h2*$t0y+$h3*$p1y+$h4*$t1y;
            $return_points[] = array($x,$y);
            $previous_x = $x;
            $previous_y = $y;
        }
        $return_points[] = array($p1x,$p1y);
    }
    return $return_points;
}
函数计算基数点($points,$tension=0.5,$steps=20){
$return_points=array();
$切线=数组();
//计算切线
$previous_point=false;

对于($i=0;$i),我提出了一个解决方案,以防其他人面临同样的问题

function caculate_cardinal_points($points,$tension=0.5,$steps=20) {

    $return_points = array();
    $tangents = array();

    // calculate tangents
    $previous_point = false;
    for($i=0;$i<count($points);$i++) {
        $px = $points[$i][0];
        $py = $points[$i][1];
        if (isset($points[$i+1]) && isset($points[$i-1])) {
            $tx = ($tension * (($points[$i+1][0]-$px) - ($points[$i-1][0]-$px)));
            $ty = ($tension * (($points[$i+1][1]-$py) - ($points[$i-1][1]-$py))); 
        } elseif (isset($points[$i+1])) {
            $tx = ($tension * (($points[$i+1][0]-$px) - ($points[$i][0]-$px)));
            $ty = ($tension * (($points[$i+1][1]-$py) - ($points[$i][1]-$py)));
        } elseif (isset($points[$i-1])) {
            $tx = ($tension * (($points[$i][0]-$px) - ($points[$i-1][0]-$px)));
            $ty = ($tension * (($points[$i][1]-$py) - ($points[$i-1][1]-$py)));
        }
        $tangents[] = array($tx,$ty);
        $previous_x = $px;
        $previous_y = $py;
    }

    // interpolate
    for($i=0;$i<count($tangents)-1;$i++) {
        $p0x = $points[$i][0];
        $p0y = $points[$i][1];
        $p1x = $points[$i+1][0];
        $p1y = $points[$i+1][1];
        $t0x = $tangents[$i][0];
        $t0y = $tangents[$i][1];
        $t1x = $tangents[$i+1][0];
        $t1y = $tangents[$i+1][1];
        $previous_x = $p0x;
        $previous_y = $p0y;
        $return_points[] = array($p0x,$p0y);
        for ($t=0; $t < $steps; $t++) {
            $s = $t / $steps;    // scale s to go from 0 to 1
            $h1 = 2*pow($s,3) - 3*pow($s,2) + 1;
            $h2 = pow($s,3) - 2*pow($s,2) + $s;
            $h3 = -2*pow($s,3) + 3*pow($s,2);
            $h4 = pow($s,3) - pow($s,2);
            $x = $h1*$p0x+$h2*$t0x+$h3*$p1x+$h4*$t1x;
            $y = $h1*$p0y+$h2*$t0y+$h3*$p1y+$h4*$t1y;
            $return_points[] = array($x,$y);
            $previous_x = $x;
            $previous_y = $y;
        }
        $return_points[] = array($p1x,$p1y);
    }
    return $return_points;
}
函数计算基数点($points,$tension=0.5,$steps=20){
$return_points=array();
$切线=数组();
//计算切线
$previous_point=false;
对于($i=0;$i)