# Help creating a simple function

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?

• Your code won't work without some definition for a[t]. Nov 13, 2014 at 15:19
• 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. Nov 13, 2014 at 15:22

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]


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