Skip to content

Instantly share code, notes, and snippets.

@jg-you jg-you/bernstein.stan
Last active Oct 30, 2018

Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner Author

commented Oct 29, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.