Last active
October 30, 2018 00:19
-
-
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
This file contains 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
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]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
De Casteljau's algorithm: https://en.m.wikipedia.org/wiki/De_Casteljau%27s_algorithm