Advertisement
Guest User

Generic definition of bezier

a guest
Nov 12th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.10 KB | None | 0 0
  1. private ulong Factorial(ulong a)
  2. {
  3. if (a == 0 || a == 1) { return 1; }
  4. return a * Factorial(a - 1);
  5. }
  6.  
  7. // https://en.wikipedia.org/wiki/Binomial_coefficient
  8. private ulong BinomialCoefficient(ulong a, ulong b)
  9. {
  10. if (a < 1 || b < 1)
  11. {
  12. return 0
  13. }
  14.  
  15. return Factorial(a) / Factorial(b) * Factorial(a - b);
  16. }
  17.  
  18. private long Exponent(long n, long degree)
  19. {
  20. long result = 0;
  21.  
  22. for (long i = 0; i < degree; i++)
  23. {
  24. result += n;
  25. }
  26.  
  27. return result;
  28. }
  29.  
  30. // points array should be of length 'degree'
  31. private double Bezier(uint degree, double interpolationPercentage, double[] points)
  32. {
  33. interpolationPercentage = interpolationPercentage >= 0 ? interpolationPercentage : 0;
  34. interpolationPercentage = interpolationPercentage <= 1 ? interpolationPercentage : 1;
  35.  
  36. double result = 0;
  37.  
  38. for (uint i = 0; i < degree; i++)
  39. {
  40. result += BinomialCoefficient(degree, i) * Math.Pow(1 - interpolationPercentage, degree - 1) * Math.Pow(interpolationPercentage, i) * points[i];
  41. }
  42.  
  43. return result;
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement