Guest User

Untitled

a guest
Feb 25th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.76 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3.  
  4. // Class that implements the deCasteljau algorithm for
  5. // computing the value of a Bezier curve of arbitrary
  6. // degree
  7. public class BezierCurve
  8. {
  9. // Calculate value of Bezier curve
  10. public static Vector3 DeCasteljau(int degree, float u, List<Vector3> controlPoints)
  11. {
  12. Vector3 f = new Vector3();
  13. // Check if order matches the degree of the curve
  14. if (controlPoints.Count != (degree + 1))
  15. {
  16. Debug.Log("The number of control points has to be equal with the degree of the curve plus 1!!!");
  17. return f;
  18. }
  19. // Check degree
  20. if (degree < 1)
  21. {
  22. Debug.Log("Bezier curve has to be at least of degree 1!!!");
  23. return f;
  24. }
  25.  
  26. List<Vector3> bezierPoints = new List<Vector3>();
  27. for (int level = degree; level >= 0; level--)
  28. {
  29. // Top level of the DeCasteljau pyramid
  30. if (level == degree)
  31. {
  32. for (int i = 0; i <= degree; i++)
  33. {
  34. bezierPoints.Add(controlPoints[i]);
  35. }
  36. continue;
  37. }
  38.  
  39. // All the other levels are constructed using their
  40. // immediate above level
  41. int lastIdx = bezierPoints.Count;
  42. int levelIdx = level + 2;
  43. int idx = lastIdx - levelIdx;
  44. for (int i = 0; i <= level; i++)
  45. {
  46. Vector3 pi = (1 - u) * bezierPoints[idx] + u * bezierPoints[idx + 1];
  47. bezierPoints.Add(pi);
  48. ++idx;
  49. }
  50. }
  51.  
  52. // Return the last element of the pyramid
  53. int lastElmnt = bezierPoints.Count - 1;
  54. return bezierPoints[lastElmnt];
  55. }
  56. }
Add Comment
Please, Sign In to add comment