Skip to content

Instantly share code, notes, and snippets.

@andrewrcollins
Last active December 11, 2015 06:58
Show Gist options
  • Save andrewrcollins/4562952 to your computer and use it in GitHub Desktop.
Save andrewrcollins/4562952 to your computer and use it in GitHub Desktop.
<?php
// t : [0, 1]
// p0, p1, p2 : n-dimensional point
// B(t) = (1-t)^2 * p0 + 2*(1-t)*t * p1 + t^2 * p2
function QuadraticBezierCurve($t, $p0, $p1, $p2)
{
$u = 1 - $t;
$uu = $u * $u;
$tt = $t * $t;
// first term
// $t1 = $uu * $p0;
$function = function($i) use ($uu) {
return $uu * $i;
};
$t1 = array_map($function, $p0);
// second term
// $t2 = 2 * $u * $t * $p1;
$function = function($i) use ($u, $t) {
return 2 * $u * $t * $i;
};
$t2 = array_map($function, $p1);
// third term
// $t3 = $tt * $p2;
$function = function($i) use ($tt) {
return $tt * $i;
};
$t3 = array_map($function, $p2);
// sum of terms
// $p = $t1 + $t2 + $t3;
$function = function($a, $b, $c) {
return $a + $b + $c;
};
$p = array_map($function, $t1, $t2, $t3);
return $p;
}
$p0 = array(0, 50);
$p1 = array(50, 100);
$p2 = array(200, 200);
for($t = 0.0; $t < 1.0; $t += 0.1)
{
$p = QuadraticBezierCurve($t, $p0, $p1, $p2);
echo $t . "\t" . implode(",", $p) . "\n";
}
$p0 = array(0, 0, 0);
$p1 = array(64, 32, 255);
$p2 = array(128, 255, 16);
for($t = 0.0; $t < 1.0; $t += 0.1)
{
$p = QuadraticBezierCurve($t, $p0, $p1, $p2);
$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