Advertisement
HaLo2FrEeEk

N-Order Bezier

Aug 31st, 2021
1,620
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.98 KB | None | 0 0
  1. function lerp(start, end, bias) = (end * bias + start * (1 - bias));
  2.  
  3. function quad_p(sp, cp, ep, t) =
  4.   // Calculates a point for a quadratic bezier curve
  5.   lerp(lerp(sp, cp, t), lerp(cp, ep, t), t);
  6.  
  7. /*
  8. * The first and last point in points[] are used
  9. * as the start and end points, so give it at least
  10. * 3 points or it'll probably break. Don't set p.
  11. */
  12. function n_p(points=[], p=[], t=0) =
  13.   // Calculates a point for an n-order bezier curve
  14.   (len(p) == 1)
  15.   ? p[0]
  16.   : (len(p) == 0)
  17.     ? n_p(p=[ for(i=[1 : len(points) - 2]) quad_p(points[0], points[i], points[len(points) - 1], t) ], t=t)
  18.     : n_p(p=[ for(i=[0 : len(p) - 2]) lerp(p[i], p[i + 1], t) ], t=t);
  19.  
  20. function n_bezier(points, res=.1) = [
  21.   // Generates a n-order bezier curve
  22.   for(t=[0 : res : 1])
  23.     n_p(points, t=t)
  24.   ];
  25.  
  26. p1 = [
  27.   [0, 0],
  28.   [50, 500],
  29.   [100, 0],
  30.   [150, -500],
  31.   [250, 500],
  32.   [300, -1000],
  33.   [350, 500],
  34.   [400, 0]
  35. ];
  36.  
  37. for(p=n_bezier(p1, res=.01)) {
  38.   translate(p) sphere(1);
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement