Reset Button for Manipulate and freezing Sliders

I work on a Bezier curve subdivision project. I managed to implement it, but I faced some problems. (The code of my program is presented below.) Firstly, if I change parameter n after changing other manipulators, then my program crashes. (But I can change it in the very beginning.) Secondly, if I change position of 2D-sliders or parameter t after changing positions of locators(red and blue points), then the resulting parts will not be Bezier curves, that's why sliders and parameter t should not be able to move after any change of locators.

That is why I decided to make a Reset Button for Manipulate (or for Dynamic Module?) which will return everything to initial state, from which we can change parameter n without crash. But how can I do that?

Also I want to make 2D-sliders and parameter t motionless(to freeze them) if any change of positions of locators is made. It turned out to be too difficult for me. Please, help. Here is the code of my program:

DynamicModule[
{ pts, pts0,  n, s, (*listoflocs, listoflocsinit,*) part1init,
part2init, divpoint},

listoflocsinit = {};
listoflocsinit = part1init~Join~part2init;

Manipulate[

part1init = {};
part2init = {};

b[t_, i_, r_] := (1 - t) b[t, i, r - 1] + t b[t, i + 1, r - 1];

For[i = 0, i < n - 1, i++,
b[t_, i, 1] = (1 - t) pts0[[i + 1]] + t pts0[[i + 2]] ];

For[i = 1, i < n - 1, i++,
part1init = Join[part1init, {b[s, 0, i]}]  ];
For[i = 1, i < n - 1, i++,
part2init = Join[part2init, {b[s, i, n - 1 - i]} ]   ];

pts = pts0;

divpoint = b[s, 0, n - 1];

Show[
{

Graphics[ {{PointSize[Large], Purple, Point[pts], Blue,
Point[ Join[
Join[{pts[[1]]}, listoflocs[[1 ;; n - 2]] ], {divpoint}][[
2 ;; n - 1]]],
Black, Point[divpoint],

Red,
Point[ Join[
Join[{divpoint},
listoflocs[[n - 1 ;; 2 n - 4]]], {pts[[n]]}][[
2 ;; n - 1]]]}, {PointSize[0.024], Black, Point[divpoint]} }
],
Graphics[{
Green, AbsoluteThickness[2],
BezierCurve[pts, SplineDegree -> Length[pts] - 1],

Blue,
BezierCurve[
Join[Join[{pts[[1]]}, listoflocs[[1 ;; n - 2]] ], {divpoint}],
SplineDegree ->
Length[Join[
Join[{pts[[1]]},
listoflocs[[1 ;; n - 2]] ], {divpoint}]] - 1],

Red,
BezierCurve[
Join[Join[{divpoint},
listoflocs[[n - 1 ;; 2 n - 4]]], {pts[[n]]}],
SplineDegree ->
Length[Join[
Join[{divpoint},
listoflocs[[n - 1 ;; 2 n - 4]]], {pts[[n]]}]] - 1]
}]

},
PlotRange -> {{0, 15}, {-2, 15}}, Axes -> True, ImageSize -> 6 72,
Background -> GrayLevel[.98]],

{n, 4, 20, 1, Appearance -> "Labeled"},

{{s, 0.5, "t"}, 0.01, 1, Appearance -> "Labeled"},

Dynamic[

pts0 = Table[{j - 1/j + 2, j + 1/j}, {j, n}];
Dynamic[pts0];
Table[
With[{i = i},
Slider2D[Dynamic[ pts0[[i]] ], {{0, -2}, {15, 15}}] ], {i, n}]
],

{{listoflocs, listoflocsinit }, Locator,
LocatorAutoCreate ->  True }

]

]

• Could you break down your problem into smaller portions? Also, where does your code fail at the moment? Dec 13, 2022 at 19:19
• Do not use For in Mathematica. Dec 14, 2022 at 0:37
• My code fails when I change n after any other manipulator (slider, locator). There is problem with locators and that is why I want to make a refresh Button and to add a freezing option. Dec 14, 2022 at 16:44