Skip to content

Instantly share code, notes, and snippets.

@jg-you
Last active October 30, 2018 00:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jg-you/ff23cbd7b7f515d7e36621c59cb5d376 to your computer and use it in GitHub Desktop.
Save jg-you/ff23cbd7b7f515d7e36621c59cb5d376 to your computer and use it in GitHub Desktop.
Evaluate a Bézier curve in Bernstein form at a point, using De Casteljau's algorithm, in the STAN language
functions {
/**
* Computes the value of a Bernstein polynomial of degree N at point t,
* using De Casteljau's algorithm.
*
* @param t Point in [0, 1] where the the polynomial will be evaluated.
* @param beta Vector of the real N + 1 coefficients of the polynomial.
* @param N Degree of the polynomial.
*
* @return Value of the polynomial at point t.
*/
real bernstein(real t,
vector beta,
int N)
{
vector[N + 1] curr_beta;
vector[N + 1] next_beta;
for (i in 1:N + 1) {
curr_beta[i] = beta[i];
}
for (j in 1:N + 1) {
for (i in 1:N + 1 - j) {
next_beta[i] = curr_beta[i] * (1 - t) + curr_beta[i + 1] * t;
}
curr_beta = next_beta;
}
return next_beta[1];
}
}
@jg-you
Copy link
Author

jg-you commented Oct 29, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment