Maybe don't use all of the lines. Or maybe do.
[ipdf/code.git] / src / bezier.cpp
1 #include "bezier.h"
2
3 #include <unordered_map>
4 #include <cmath>
5
6 using namespace std;
7
8 namespace IPDF
9 {
10
11 /**
12  * Factorial
13  * Use dynamic programming / recursion
14  */
15 int Factorial(int n)
16 {
17         static unordered_map<int, int> dp;
18         static bool init = false;
19         if (!init)
20         {
21                 init = true;
22                 dp[0] = 1;
23         }
24         auto it = dp.find(n);
25         if (it != dp.end())
26                 return it->second;
27         int result = n*Factorial(n-1);
28         dp[n] = result;
29         return result;
30 }
31
32 /**
33  * Binomial coefficients
34  */
35 int BinomialCoeff(int n, int k)
36 {
37         return Factorial(n) / Factorial(k) / Factorial(n-k);
38 }
39
40 /**
41  * Bernstein Basis Polynomial
42  */
43 Real Bernstein(int k, int n, const Real & u)
44 {
45         return Real(BinomialCoeff(n, k)) * pow(u, k) * pow(Real(1.0) - u, n-k);
46 }
47
48 }

UCC git Repository :: git.ucc.asn.au