1
$\begingroup$

Can someone help me to create a function. The truth I'm new in Mathematica and I don't understand well the way functions work in I. I need a function that animate some rectangles according to a curve that I introduce as a vector depending on time. I already did this, but I don't want to do for every curve. The animation that I have is the next:

{Animate[Graphics[  {{Red, 
     Polygon[{{0 , 0}, {1, 0}, {Flatten[a[t]][[1]] + 1, 
        1}, {Flatten[a[t]][[1]], 1}}]}, {Blue, 
     Polygon[{{Flatten[a[t]][[1]] + 1, 1}, {Flatten[a[t]][[1]], 
        1}, {Flatten[a[t]][[2]], 2}, {Flatten[a[t]][[2]] + 1, 2}}]},
    {Green, 
     Polygon[{{Flatten[a[t]][[2]] + 1, 2}, {Flatten[a[t]][[2]], 
        2}, {Flatten[a[t]][[3]], 3}, {Flatten[a[t]][[3]] + 1, 3}}]},
    {Black, 
     Polygon[{{Flatten[a[t]][[3]] + 1, 3}, {Flatten[a[t]][[3]], 
        3}, {Flatten[a[t]][[4]], 4}, {Flatten[a[t]][[4]] + 1, 4}}]},
    {Orange, 
     Polygon[{{Flatten[a[t]][[4]] + 1, 4}, {Flatten[a[t]][[4]], 
        4}, {Flatten[a[t]][[5]], 5}, {Flatten[a[t]][[5]] + 1, 5}}]},
    {Blue, 
     Polygon[{{Flatten[a[t]][[5]] + 1, 5}, {Flatten[a[t]][[5]], 
        5}, {Flatten[a[t]][[6]], 6}, {Flatten[a[t]][[6]] + 1, 6}}]},
    {Pink, 
     Polygon[{{Flatten[a[t]][[6]], 6}, {Flatten[a[t]][[6]] + 1, 
        6}, {Flatten[a[t]][[7]] + 1, 7}, {Flatten[a[t]][[7]], 7}}]}}, 
   PlotRange -> {{-2, 2}, {0, 8}}]   , {t , 0, 2 Pi} , 
  AnimationRunning -> False]}

I was wondering if there is a way to transform this code into a function so I just call the function with a different vector and get the animation?

$\endgroup$
  • 2
    $\begingroup$ Your code won't work without some definition for a[t]. $\endgroup$ – bill s Nov 13 '14 at 15:19
  • $\begingroup$ Is there a way to solve this?, in other languages I could use a block like this and then call the function with the vector that I want to use. $\endgroup$ – user22107 Nov 13 '14 at 15:22
0
$\begingroup$

Perhaps something like

colors = {Red, Blue, Green, Black, Orange, Blue, Pink};
aA = Sin[t] RandomInteger[{1, 7}, {7}];

table = Table[{{aA[[k]] + 1, k}, {aA[[k]], k},
               {aA[[k + 1]], k + 1}, {aA[[k + 1]] + 1, k + 1}}, {k, 1, 6}];
polygons = Polygon /@ Prepend[table, Join[{{0, 0}, {1, 0}}, table[[1, ;; 2]]]];

Animate[Evaluate@ Graphics[Thread[{colors, polygons}], PlotRange -> {{-8, 8}, {0, 8}}], 
       {t, 0, 2 Pi}, AnimationRunning -> False]

enter image description here

Change aC to

aC = Cos[t^2/2] RandomInteger[{1, 7}, {7}];

and {t, 0, 2 Pi} to {t, -2 Pi, 2 Pi} to get enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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