5
$\begingroup$

I am writing a code to display longitudinal waves. I started with the following, filling the slots using the Range function.

    l = {Blue, Disk[{3 Sin[(5) t + 0.1 #] + 1.4 #, 1}, 0.5]} & /@ 
   Range[25];
Animate[Evaluate[Graphics[l, PlotRange -> {{-4, 44}, All}]], {t, 0, 
  20. \[Pi]}, AnimationRate -> .3]

The slots control the x-position of the disks, as a result I get 25 disks beside each others. However, I want to obtain this single line of disks multiple times, each line under the other, as shown in the figure below. enter image description here I tried to insert a new slot in the y-position of Disk function, but I do not know how to separate this new slot from the other two in the x-position, and assign it a new Range function. Can anyone help me in this please?

$\endgroup$

2 Answers 2

4
$\begingroup$
Clear["Global`*"]

nRow = 10;

nCol = 25;

l = {Blue, 
     Disk[{3 Sin[5 t + 0.1 #[[1]]] + 1.4 #[[1]], 1.5 #[[2]]}, 
      0.5]} & /@ Flatten[Array[{#1, #2} &, {nCol, nRow}], 1];

Animate[Evaluate[Graphics[l, PlotRange -> {{-4, 44}, All}]], {t, 0, 
  20. π}, AnimationRate -> .3]

enter image description here

$\endgroup$
1
  • $\begingroup$ Thank you Bob! You are a Legend! $\endgroup$
    – haith
    Commented Oct 14, 2019 at 17:33
3
$\begingroup$

Array:

ClearAll[disks]
disks[t_, nc_, nr_] := Array[Disk[{3 Sin[5 t + 0.1 #] + 1.4 #, 1.5 #2}, 0.5] &, {nc, nr}];

Animate[Graphics[{Blue, disks[t, 25, 10]}, PlotRange -> {{-4, 44}, All}], {t, 0, 20.  π}, 
 AnimationRate -> .3]

enter image description here

Outer:

ClearAll[disks2]
disks2[t_, nc_, nr_] := Outer[Disk[{3 Sin[5 t + 0.1 #] + 1.4 #, 1.5 #2}, 0.5] &, 
 Range @ nc, Range @ nr];

Animate[Graphics[{Blue, disks2[t, 25, 10]}, PlotRange -> {{-4, 44}, All}], {t, 0, 20.  π}, 
 AnimationRate -> .3]

same picture

Tuples:

ClearAll[disks3]
disks3[t_, nc_, nr_] := Disk[{3 Sin[5 t + 0.1 #] + 1.4 #, 1.5 #2}, 0.5] & @@@ 
  Tuples[Range /@ {nc, nr}];

Animate[Graphics[{Blue, disks3[t, 25, 10]}, PlotRange -> {{-4, 44}, All}], {t, 0, 20. π}, 
 AnimationRate -> .3]

same picture

ClearAll[disks4]
disks4[t_, nc_, nr_] := Disk[#, .5] & /@ 
  Tuples[{3 Sin[5 t + 0.1 Range[nc]] + 1.4 Range[nc], 1.5 Range[nr]}]

Animate[Graphics[{Blue, disks4[t, 25, 10]}, PlotRange -> {{-4, 44}, All}], {t, 0, 20. π}, 
 AnimationRate -> .3]

same picture

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.