Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Bezier{
- function cubicBezierwithControls(array $c1,array $c2,array $c3,array $c4){
- $points=array();
- $b1=function ($t) {
- return ($t * $t * $t) ;
- };
- $b2= function ($t) {
- return (3 * $t * $t * (1-$t)) ;
- };
- $b3= function ($t) {
- return (3 * $t * (1-$t) * (1-$t)) ;
- };
- $b4= function ($t) {
- return ((1-$t) * (1-$t) * (1-$t)) ;};
- $getPoint = function ($t) use($b1,$b2,$b3,$b4, $c1,$c2,$c3,$c4) {
- $x= ($c1[0]* $b1($t)) + ($c2[0]* $b2($t)) + ($c3[0]* $b3($t)) + ($c4[0]* $b4($t));
- $y= ($c1[1]* $b1($t)) + ($c2[1]* $b2($t)) + ($c3[1]* $b3($t)) + ($c4[1]* $b4($t));
- return array('x' =>$x , 'y'=>$y);
- };
- for($i=0; $i<1024; $i++){
- $point=$getPoint($i/1024);
- if($point['x']>0)
- $points[$point['x']]=round($point['y']);
- }
- return $points;
- }
- function cubicBezierwithPoints(array $xy0 , array $xy4, array $xy5, array $xy3){
- $solvexy=function($a,$b,$c,$d,$e,$f){
- $j=($c- $a/$d * $f)/($b - $a * $e / $d);
- $i=($c - ($b * $j))/$a;
- return array(round($i),round($j));
- };
- $b0=function($t) { return pow((1-$t), 3); };
- $b1=function($t) { return 3 * $t * pow((1-$t),2); };
- $b2=function($t) { return 3 * pow($t , 2) * (1-$t); };
- $b3=function($t) { return pow($t,3); };
- $c1=sqrt(pow(($xy4[0]-$xy0[0]),2) + pow(($xy4[1]-$xy0[1]),2));
- $c2=sqrt(pow(($xy5[0]-$xy4[0]),2) + pow(($xy5[1]-$xy4[1]),2));
- $c3=sqrt(pow(($xy3[0]-$xy5[0]),2) + pow(($xy3[1]-$xy5[1]),2));
- $t1=$c1/($c1+$c2+$c3);
- $t2=($c1+$c2)/($c1+$c2+$c3);
- $x12=$solvexy($b1($t1), $b2($t1), $xy4[0]-($xy0[0]*$b0($t1))-($xy3[0] * $b3($t1)), $b1($t2), $b2($t2), $xy5[0]-($xy0[0] * $b0($t2))-($xy3[0] * $b3($t2)));
- $y12=$solvexy($b1($t1), $b2($t1), $xy4[1]-($xy0[1]*$b0($t1))-($xy3[1] * $b3($t1)), $b1($t2), $b2($t2), $xy5[1]-($xy0[1]* $b0($t2))-($xy3[1] * $b3($t2)));
- return array($x12,$y12);
- }
- function equalizeCurve(array $s, array $c1, array $c2, array $e){
- $result=$this->cubicBezierwithControls($s, $c1, $c2, $e);
- if(!isset($result[0]))
- $result[0]=$s[1];
- for($i=0; $i<256; $i++){
- if(!isset($result[$i]))
- $result[$i]=$result[$i-1];
- }
- ksort($result);
- return $result;
- //print_r( $result );
- }
- }
- /*$bez=new Bezier();
- $s=[0,0]; $p1=[20, 32]; $p2=[200,220]; $e=[255,255];
- $xy=$bez->cubicBezierwithPoints($s, $p1, $p2, $e);
- print_r($xy);
- $c1=array($xy[0][0],$xy[1][0]);
- $c2=array($xy[0][1],$xy[1][1]);
- $bez->equalizeCurve($s,$c1,$c2,$e);
- */
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement