SplineCurve.cs 737 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. using UnityEngine;
  2. namespace Assets.Plugins.SmartLevelsMap.Scripts
  3. {
  4. public class SplineCurve
  5. {
  6. private readonly Vector2[] _points;
  7. public SplineCurve()
  8. {
  9. _points = new Vector2[4];
  10. }
  11. public void ApplyPoints(Vector2 p1, Vector2 p2, Vector2 p3, Vector2 p4)
  12. {
  13. _points[0] = p1;
  14. _points[1] = p2;
  15. _points[2] = p3;
  16. _points[3] = p4;
  17. }
  18. public Vector2 GetPoint(float t)
  19. {
  20. return GetPoint(_points[0], _points[1], _points[2], _points[3], t);
  21. }
  22. public static Vector2 GetPoint(Vector2 a, Vector2 b, Vector2 c, Vector2 d, float t)
  23. {
  24. //t==0, ret b
  25. //t==1, ret c
  26. return .5f*(
  27. (-a + 3f*b - 3f*c + d)*(t*t*t)
  28. + (2f*a - 5f*b + 4f*c - d)*(t*t)
  29. + (-a + c)*t
  30. + 2f*b
  31. );
  32. }
  33. }
  34. }