Created
          January 18, 2013 07:58 
        
      - 
      
- 
        Save andrewrcollins/4563045 to your computer and use it in GitHub Desktop. 
    Cubic Bezier Curve
http://en.wikipedia.org/wiki/B%C3%A9zier_curve#Cubic_B.C3.A9zier_curves
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | <?php | |
| // http://en.wikipedia.org/wiki/B%C3%A9zier_curve#Cubic_B.C3.A9zier_curves | |
| // t : [0, 1] | |
| // p0, p1, p2, p3 : n-dimensional point | |
| // B(t) = (1-t)^3 * p0 + 3*(1-t)^2*t * p1 + 3*(1-t)*t^2 * p2 + t^3 * p3 | |
| function CubicBezierCurve($t, $p0, $p1, $p2, $p3) | |
| { | |
| $u = 1 - $t; | |
| $uu = $u * $u; | |
| $uuu = $uu * $u; | |
| $tt = $t * $t; | |
| $ttt = $tt * $t; | |
| // first term | |
| // $t1 = $uuu * $p0; | |
| $function = function($i) use ($uuu) { | |
| return $uuu * $i; | |
| }; | |
| $t1 = array_map($function, $p0); | |
| // second term | |
| // $t2 = 3 * $uu * $t * $p1; | |
| $function = function($i) use ($uu, $t) { | |
| return 3 * $uu * $t * $i; | |
| }; | |
| $t2 = array_map($function, $p1); | |
| // third term | |
| // $t3 = 3 * $u * $tt * $p2; | |
| $function = function($i) use ($u, $tt) { | |
| return 3 * $u * $tt * $i; | |
| }; | |
| $t3 = array_map($function, $p2); | |
| // fourth term | |
| // $t4 = $ttt * $p3; | |
| $function = function($i) use ($ttt) { | |
| return $ttt * $i; | |
| }; | |
| $t4 = array_map($function, $p3); | |
| // sum of terms | |
| // $p = $t1 + $t2 + $t3 + $t4; | |
| $function = function($a, $b, $c, $d) { | |
| return $a + $b + $c + $d; | |
| }; | |
| $p = array_map($function, $t1, $t2, $t3, $t4); | |
| return $p; | |
| } | |
| $p0 = array(0, 50); | |
| $p1 = array(50, 100); | |
| $p2 = array(200, 200); | |
| $p3 = array(100, 200); | |
| for($t = 0.0; $t < 1.0; $t += 0.1) | |
| { | |
| $p = CubicBezierCurve($t, $p0, $p1, $p2, $p3); | |
| echo $t . "\t" . implode(",", $p) . "\n"; | |
| } | |
| $p0 = array(0, 0, 0); | |
| $p1 = array(64, 32, 255); | |
| $p2 = array(128, 255, 16); | |
| $p3 = array(255, 16, 16); | |
| for($t = 0.0; $t < 1.0; $t += 0.1) | |
| { | |
| $p = CubicBezierCurve($t, $p0, $p1, $p2, $p3); | |
| $p = array_map("round", $p); | |
| echo $t . "\t" . implode(",", $p) . "\n"; | |
| } | |
| ?> | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment