Advertisement
khAldr0g0

Bezier.php

Jan 9th, 2014
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.43 KB | None | 0 0
  1. <?php
  2. class Bezier{
  3. function cubicBezierwithControls(array $c1,array $c2,array $c3,array $c4){
  4.         $points=array();
  5.         $b1=function ($t) {
  6.             return ($t * $t * $t) ;
  7.         };
  8.  
  9.         $b2= function ($t) {
  10.             return (3 * $t * $t * (1-$t)) ;
  11.         };
  12.  
  13.         $b3= function ($t) {
  14.             return (3 * $t * (1-$t) * (1-$t)) ;
  15.         };
  16.        
  17.         $b4= function ($t) {
  18.             return ((1-$t) * (1-$t) * (1-$t)) ;};
  19.  
  20.         $getPoint = function ($t) use($b1,$b2,$b3,$b4, $c1,$c2,$c3,$c4) {
  21.             $x= ($c1[0]* $b1($t)) + ($c2[0]* $b2($t)) + ($c3[0]* $b3($t)) + ($c4[0]* $b4($t));
  22.             $y= ($c1[1]* $b1($t)) + ($c2[1]* $b2($t)) + ($c3[1]* $b3($t)) + ($c4[1]* $b4($t));
  23.              return array('x' =>$x , 'y'=>$y);
  24.         };
  25.        
  26.         for($i=0; $i<1024; $i++){
  27.             $point=$getPoint($i/1024);
  28.             if($point['x']>0)
  29.                 $points[$point['x']]=round($point['y']);
  30.         }
  31.         return $points;
  32.     }
  33.  
  34. function cubicBezierwithPoints(array $xy0 , array $xy4, array $xy5, array $xy3){
  35.  
  36. $solvexy=function($a,$b,$c,$d,$e,$f){
  37. $j=($c- $a/$d * $f)/($b - $a * $e / $d);
  38. $i=($c - ($b * $j))/$a;
  39. return array(round($i),round($j));
  40. };
  41.  
  42. $b0=function($t) { return pow((1-$t), 3); };
  43. $b1=function($t) { return 3 * $t * pow((1-$t),2); };
  44. $b2=function($t) { return 3 * pow($t , 2) * (1-$t); };
  45. $b3=function($t) { return pow($t,3); };
  46.  
  47. $c1=sqrt(pow(($xy4[0]-$xy0[0]),2) + pow(($xy4[1]-$xy0[1]),2));
  48. $c2=sqrt(pow(($xy5[0]-$xy4[0]),2) + pow(($xy5[1]-$xy4[1]),2));
  49. $c3=sqrt(pow(($xy3[0]-$xy5[0]),2) + pow(($xy3[1]-$xy5[1]),2));
  50.  
  51. $t1=$c1/($c1+$c2+$c3);
  52. $t2=($c1+$c2)/($c1+$c2+$c3);
  53.  
  54.  
  55. $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)));
  56.  
  57. $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)));
  58.  
  59. return array($x12,$y12);
  60. }
  61.  
  62.  
  63.  
  64.  
  65.     function equalizeCurve(array $s, array $c1, array $c2, array $e){      
  66.  
  67.         $result=$this->cubicBezierwithControls($s, $c1, $c2, $e);
  68.  
  69.         if(!isset($result[0]))
  70.             $result[0]=$s[1];
  71.  
  72.         for($i=0; $i<256; $i++){
  73.             if(!isset($result[$i]))
  74.                 $result[$i]=$result[$i-1];
  75.         }
  76.         ksort($result);
  77.         return $result;
  78.         //print_r( $result );
  79.        
  80.     }
  81. }
  82.  
  83.  
  84. /*$bez=new Bezier();
  85. $s=[0,0]; $p1=[20, 32]; $p2=[200,220]; $e=[255,255];
  86. $xy=$bez->cubicBezierwithPoints($s, $p1, $p2, $e);
  87.  
  88. print_r($xy);
  89. $c1=array($xy[0][0],$xy[1][0]);
  90. $c2=array($xy[0][1],$xy[1][1]);
  91.  
  92. $bez->equalizeCurve($s,$c1,$c2,$e);
  93.  */
  94. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement