Advertisement
Guest User

Untitled

a guest
Aug 31st, 2015
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 KB | None | 0 0
  1. (*Search the index of span [ui,ui+1)*)
  2. searchSpan[knots_, u0_] :=
  3. If[u0 != Max@knots,
  4. Ordering[UnitStep[u0 - knots], 1][[1]] - 2,
  5. Position[knots, u0][[1, 1]] - 2
  6. ]
  7. (*The definition of α coefficient*)
  8. α[{deg_, knots_}, {j_, k_}, u0_] /;
  9. knots[[j + deg + 2]] == knots[[j + k + 1]] := 0
  10. α[{deg_, knots_}, {j_, k_}, u0_] :=
  11. (u0 - knots[[j + k + 1]])/(knots[[j + deg + 2]] - knots[[j + k + 1]])
  12.  
  13. (*Implementation of de Boor algorithm*)
  14. deBoor[pts : {{_, _} ..}, {deg_, knots_}, u0_] :=
  15. Module[{calcNextGroup, idx = searchSpan[knots, u0]},
  16. calcNextGroup =
  17. Function[{points, k},
  18. Module[{coords, coeffs},
  19. coords = Partition[points, 2, 1];
  20. coeffs = {1 - #, #} & /@ (α[{deg, knots}, {#, k + 1}, u0] & /@
  21. Range[idx - deg, idx - k - 1]);
  22. {Plus @@@ MapThread[Times, {coords, coeffs}], k + 1}]
  23. ];
  24. Nest[calcNextGroup[Sequence @@ #] &,
  25. {pts[[idx - deg + 1 ;; idx + 1]], 0}, deg][[1, 1]]
  26. ]
  27.  
  28. points =
  29. {{1, 4}, {.5, 6}, {5, 4}, {3, 12}, {11, 14}, {8, 4}, {12, 3}, {11, 9}, {15, 10}, {17, 8}};
  30. (*here, I set the knots uniformly*)
  31. knots = {0, 0, 0, 0, 1/7, 2/7, 3/7, 4/7, 5/7, 6/7, 1, 1, 1, 1};
  32.  
  33. ParametricPlot[
  34. deBoor[points, {3, knots}, t], {t, 0, 1}, Axes -> False]
  35.  
  36. pointsCLOSE =
  37. {{1, 4}, {.5, 6}, {5, 4}, {3, 12}, {11, 14}, {8, 4}, {12, 3},
  38. {11, 9}, {15, 10}, {17, 8}, {1, 4}};
  39. (*here, I set the knots uniformly*)
  40. knotsCLOSE = {0, 0, 0, 0, 1/8, 2/8, 3/8, 4/8, 5/8, 6/8, 7/8, 1, 1, 1, 1};
  41. ParametricPlot[
  42. deBoor[pointsCLOSE, {3, knotsCLOSE}, t], {t, 0, 1}, Axes -> False]
  43.  
  44. Graphics[{BSplineCurve[points, SplineClosed -> True]}]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement