Message Boards Message Boards

GROUPS:

DynamicModule scoping of local variables with delayed set

Posted 8 years ago
3908 Views
|
2 Replies
|
1 Total Likes
|

I'm confused why the first one doesn't work, but the second one does. Can someone help me understand?

Append to the local variable "disks"

DynamicModule[{pt = {0, 0}, size = 0.001, addDisk, disks},
 addDisk[pt_, size_] := AppendTo[disks, Disk[pt, size]];
 disks = {Disk[pt, size]};
 ClickPane[
  Dynamic@
   Graphics[
    Join[Flatten@Table[Text["X", {a, b}], {a, -2, 2}, {b, -2, 2}], 
     disks]], (addDisk[disks, #, .2]) &]]

Append, but return to a "Set" evaluation within the "ClickPane" argument

DynamicModule[{pt = {0, 0}, size = 0.001, addDisk},
 addDisk[disks_, pt_, size_] := Append[disks, Disk[pt, size]];
 disks = {Disk[pt, size]};
 ClickPane[
  Dynamic@
   Graphics[
    Join[Flatten@Table[Text["X", {a, b}], {a, -2, 2}, {b, -2, 2}], 
     disks]], (disks = addDisk[disks, #, .2]) &]]
POSTED BY: Eric Smith
2 Replies
Anonymous User
Anonymous User
Posted 8 years ago

Thanks Kuba, that was a typo when trying to simplify the issue for the forum. I added "disks_" as an argument later. I'm still having problems with the code

DynamicModule[{pt = {0, 0}, size = 0.001, addDisk},
 disks = {Disk[pt, size]};
 addDisk[pt_, size_] := 
  If[Length@disks > 25, {disks[[1]]}, Append[disks, Disk[pt, size]]];
 ClickPane[
  Dynamic@
   Graphics[
    Join[Flatten@Table[Text["X", {a, b}], {a, -2, 2}, {b, -2, 2}], 
     disks], PlotRange -> {{-3, 3}, {-2.5, 2.5}}, 
    ImageSize -> 400], (addDisk[Round /@ #, .2]) &]]
POSTED BY: Anonymous User

It is not working because you've defined addDisk as a function of two arguments addDisk[pt_, size_] :=... and when click happens you are providing there three: (addDisk[disks, #, .2]) &.

Drop the first and it will work.

POSTED BY: Kuba Podkalicki
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract