Skip to main content
added 4 characters in body
Source Link
m_goldberg
  • 108.1k
  • 16
  • 104
  • 259

Your code constantly updates because nPts is local to a module enclosing the Manipulate. If the Manipulate enclosed the module, your code wouldn't work as you would like, but it wouldn't be constantly updating.

Here is a much simpler example showing the same behavior.

Module[{n = 1}, Manipulate[Row[{"n = ", n++}], {t, 0, 5}]]

constantly updates, but

Manipulate[Module[{n = 1}, Row[{"n = ", n++}]], {t, 0, 5}]

doesn't.

I would take a different approach. I wouldn't use locators to add more particles to the simulation; rather I would use a ClickPane. Nor would I use a slider to generate update events; rather I would use a Button

Manipulate[
  If[update, 
     update = False; pts = Plus @@@ Transpose[{pts, RandomReal[{-1, 1}, {Length@pts, 2}]}]];
  ClickPane[
    Framed@Graphics[{Blue, PointSize[0.02], Point[pts]}, PlotRange -> {{-5, 5}, {-5, 5}}], 
    AppendTo[pts, #] &],
  {{update, False}, ControlType -> None},
  {{pts, {{0, 0}}}, ControlType -> None},
  Button["Update", update = True]]

manipulate

This is much simpler than your code, but still performs the simulation you are trying to implement. Give it a try and see if it satisfies your needs.

Your code constantly updates because nPts is local to a module enclosing the Manipulate. If the Manipulate enclosed the module, your code wouldn't work as you would like, but it wouldn't be constantly updating.

Here is a much simpler example showing the same behavior.

Module[{n = 1}, Manipulate[Row[{"n = ", n++}], {t, 0, 5}]]

constantly updates, but

Manipulate[Module[{n = 1}, Row[{"n = ", n++}]], {t, 0, 5}]

doesn't.

I would take a different approach. I wouldn't use locators to add more particles to the simulation; rather I would use a ClickPane. Nor would I use a slider to generate update events; rather I would use a Button

Manipulate[
  If[update, 
     update = False; pts = Plus @@@ Transpose[{pts, RandomReal[{-1, 1}, {Length@pts, 2}]}]];
  ClickPane[
    Framed@Graphics[{Blue, PointSize[0.02], Point[pts]}, PlotRange -> {{-5, 5}, {-5, 5}}], 
    AppendTo[pts, #] &],
  {{update, False}, ControlType -> None},
  {{pts, {{0, 0}}}, ControlType -> None},
  Button["Update", update = True]]

manipulate

This is much simpler than your code, but still performs the simulation you trying to implement. Give it a try and see if it satisfies your needs.

Your code constantly updates because nPts is local to a module enclosing the Manipulate. If the Manipulate enclosed the module, your code wouldn't work as you would like, but it wouldn't be constantly updating.

Here is a much simpler example showing the same behavior.

Module[{n = 1}, Manipulate[Row[{"n = ", n++}], {t, 0, 5}]]

constantly updates, but

Manipulate[Module[{n = 1}, Row[{"n = ", n++}]], {t, 0, 5}]

doesn't.

I would take a different approach. I wouldn't use locators to add more particles to the simulation; rather I would use a ClickPane. Nor would I use a slider to generate update events; rather I would use a Button

Manipulate[
  If[update, 
     update = False; pts = Plus @@@ Transpose[{pts, RandomReal[{-1, 1}, {Length@pts, 2}]}]];
  ClickPane[
    Framed@Graphics[{Blue, PointSize[0.02], Point[pts]}, PlotRange -> {{-5, 5}, {-5, 5}}], 
    AppendTo[pts, #] &],
  {{update, False}, ControlType -> None},
  {{pts, {{0, 0}}}, ControlType -> None},
  Button["Update", update = True]]

manipulate

This is much simpler than your code, but still performs the simulation you are trying to implement. Give it a try and see if it satisfies your needs.

tried to explain the source of the OP problem
Source Link
m_goldberg
  • 108.1k
  • 16
  • 104
  • 259

Your code constantly updates because nPts is local to a module enclosing the Manipulate. If the Manipulate enclosed the module, your code wouldn't work as you would like, but it wouldn't be constantly updating.

Here is a much simpler example showing the same behavior.

Module[{n = 1}, Manipulate[Row[{"n = ", n++}], {t, 0, 5}]]

constantly updates, but

Manipulate[Module[{n = 1}, Row[{"n = ", n++}]], {t, 0, 5}]

doesn't.

I would take a different approach. I wouldn't use locators to add more particles to the simulation; rather I would use a ClickPane. Nor would I use a slider to generate update events; rather I would use a Button

Manipulate[
  If[update, 
     update = False; pts = Plus @@@ Transpose[{pts, RandomReal[{-1, 1}, {Length@pts, 2}]}]];
  ClickPane[
    Framed@Graphics[{Blue, PointSize[0.02], Point[pts]}, PlotRange -> {{-5, 5}, {-5, 5}}], 
    AppendTo[pts, #] &],
  {{update, False}, ControlType -> None},
  {{pts, {{0, 0}}}, ControlType -> None},
  Button["Update", update = True]]

manipulate

This is much simpler than your code, but still performs the simulation you trying to implement. Give it a try and see if it satisfies your needs.

I would take a different approach. I wouldn't use locators to add more particles to the simulation; rather I would use a ClickPane. Nor would I use a slider to generate update events; rather I would use a Button

Manipulate[
  If[update, 
     update = False; pts = Plus @@@ Transpose[{pts, RandomReal[{-1, 1}, {Length@pts, 2}]}]];
  ClickPane[
    Framed@Graphics[{Blue, PointSize[0.02], Point[pts]}, PlotRange -> {{-5, 5}, {-5, 5}}], 
    AppendTo[pts, #] &],
  {{update, False}, ControlType -> None},
  {{pts, {{0, 0}}}, ControlType -> None},
  Button["Update", update = True]]

manipulate

Give it a try and see if it satisfies your needs.

Your code constantly updates because nPts is local to a module enclosing the Manipulate. If the Manipulate enclosed the module, your code wouldn't work as you would like, but it wouldn't be constantly updating.

Here is a much simpler example showing the same behavior.

Module[{n = 1}, Manipulate[Row[{"n = ", n++}], {t, 0, 5}]]

constantly updates, but

Manipulate[Module[{n = 1}, Row[{"n = ", n++}]], {t, 0, 5}]

doesn't.

I would take a different approach. I wouldn't use locators to add more particles to the simulation; rather I would use a ClickPane. Nor would I use a slider to generate update events; rather I would use a Button

Manipulate[
  If[update, 
     update = False; pts = Plus @@@ Transpose[{pts, RandomReal[{-1, 1}, {Length@pts, 2}]}]];
  ClickPane[
    Framed@Graphics[{Blue, PointSize[0.02], Point[pts]}, PlotRange -> {{-5, 5}, {-5, 5}}], 
    AppendTo[pts, #] &],
  {{update, False}, ControlType -> None},
  {{pts, {{0, 0}}}, ControlType -> None},
  Button["Update", update = True]]

manipulate

This is much simpler than your code, but still performs the simulation you trying to implement. Give it a try and see if it satisfies your needs.

Post Undeleted by m_goldberg
More concise code
Source Link
m_goldberg
  • 108.1k
  • 16
  • 104
  • 259

I would take a different approach. I wouldn't use locators to add more particles to the simulation; rather I would use a ClickPane. Nor would I use a slider to generate update events; rather I would use a Button

Manipulate[
  If[update, 
     update = False; pts = updater[]];Plus @@@ Transpose[{pts, RandomReal[{-1, 1}, {Length@pts, 2}]}]];
  ClickPane[
    Framed@Graphics[{Blue, PointSize[0.02], Point[pts]}, PlotRange -> {{-5, 5}, {-5, 5}}], 
    AppendTo[pts, #] &],
  {{update, False}, ControlType -> None},
  {{pts, {{0, 0}}}, ControlType -> None},
  Button["Update", update = True],
  Initialization :> (
    vels[n_] := RandomReal[{-1, 1}, {n, 2}];
    updater[] := (pts = MapThread[(#1 + #2) &, {pts, vels[Length@pts]}])
  )]True]]

manipulate

Give it a try and see if it satisfies your needs.

I would take a different approach. I wouldn't use locators to add more particles to the simulation; rather I would use a ClickPane. Nor would I use a slider to generate update events; rather I would use a Button

Manipulate[
  If[update, update = False; pts = updater[]];
  ClickPane[
    Framed@Graphics[{Blue, PointSize[0.02], Point[pts]}, PlotRange -> {{-5, 5}, {-5, 5}}], 
    AppendTo[pts, #] &],
  {{update, False}, ControlType -> None},
  {{pts, {{0, 0}}}, ControlType -> None},
  Button["Update", update = True],
  Initialization :> (
    vels[n_] := RandomReal[{-1, 1}, {n, 2}];
    updater[] := (pts = MapThread[(#1 + #2) &, {pts, vels[Length@pts]}])
  )]

manipulate

Give it a try and see if it satisfies your needs.

I would take a different approach. I wouldn't use locators to add more particles to the simulation; rather I would use a ClickPane. Nor would I use a slider to generate update events; rather I would use a Button

Manipulate[
  If[update, 
     update = False; pts = Plus @@@ Transpose[{pts, RandomReal[{-1, 1}, {Length@pts, 2}]}]];
  ClickPane[
    Framed@Graphics[{Blue, PointSize[0.02], Point[pts]}, PlotRange -> {{-5, 5}, {-5, 5}}], 
    AppendTo[pts, #] &],
  {{update, False}, ControlType -> None},
  {{pts, {{0, 0}}}, ControlType -> None},
  Button["Update", update = True]]

manipulate

Give it a try and see if it satisfies your needs.

Post Deleted by m_goldberg
Source Link
m_goldberg
  • 108.1k
  • 16
  • 104
  • 259
Loading