Animation of moving rectangles in 2D from data file

I am not very familiar with mathematica but I know the basics so I thought I give it a try. For a project I have to animate rectangles moving around in 2D (later also 3D). The data come from a C simulation I wrote so I can change the output as I want, but for now I have N files each looking like this:

t       x-coordinate      y-coordinate     length
1           1                 3              10
2           5                 3              10
3           10                3              10
...         ...               ...            ...


the thickness of the rectangles should be const over time. How would you approach this? I found the help on importing data and making animations rather confusing so far.

Please help me.

-

2 Answers

fakedata = Flatten /@ Transpose@{Range[10], RandomInteger[100, {10, 3}]}
(* {{1, 59, 34, 6}, {2, 9, 17, 66}, {3, 22, 50, 16}, {4, 91, 2, 40},
{5, 8, 11, 93}, {6, 99, 18, 23}, {7, 45, 48, 81}, {8, 71, 47, 19},
{9,  7, 20, 34}, {10, 73, 1, 51}}  *)
height = 20;
rectCoords = {{#1, #2}, {#1 + #3, #2 + height}} & @@@ fakedata[[All, 2 ;;]];
colors = ColorData[1, "ColorList"][[;; 10]];

Animate[Graphics[{colors[[t]], Rectangle @@ (rectCoords[[t]])},
PlotRange -> {{0, 200}, {0, 120}}], {t, Range[Length@rectCoords]}]

-
Thank you a lot! I started playing around with openGL but I think there will be no need for that now.. Can I also combine about 100 files into one animation or would it be easier to just give one file as a output? Do I have to write a super long line with 100 Rectangle commands ore is there some more convenient way? But thanks a lot anyways. –  user3683367 May 30 at 21:56
@user3683367, you can use the answers in this Q/A to import your files into an array testlist where testlist[[i]] contains the data from the ith file. Then you can map a function like rcF={{#1, #2}, {#1 + #3, #2 + height}} & @@@ #[[All,2;;]]& on testlist -- i.e., coords=rcF/@testfile gets you the list of rectangle coordinates for all your data. To animate the rectangles from file i just use coords[[i]][[t]] instead of rectCoords[[[t]] inside Animate. –  kguler May 30 at 22:42

So now my code looks like this (2 files in notebook directory):

files = FileNames[NotebookDirectory[] <> "*.dat"];

data = Import[#, "Table"] & /@ files
(*Initiate coord matrices*)

coords = {{#1, #2}, {#1 + #3, #2 + height}} & @@@ #[[All, 2 ;;]] & /@ data;

height = 2;
time = {{#1}} &@data[[All, 1]];

Animate[Graphics[{Red, Rectangle @@ (coords[[1]][[t]]), Blue,
Rectangle @@ (coords[[2]][[t]])},
PlotRange -> {{-200, 200}, {0, 20}}], {t, Range[0, 1000]}]


Can I somehow shorten the part in Graphics? Now I would just write the coords function for all rectangles... which would not look that nice and the number of rectangles is strictly given by the number of files. Thanks in advance.

-
With a predefined color list, say colors={Red,Blue}, try Sequence @@ {colors[[#]], Rectangle @@ (coords[[#]][[t]])} & /@ {1, 2} as the first argument of Graphics. –  kguler May 31 at 17:03
ok that worked nicely. In that way I can even import a color array and color them filaments according to their characteristics. –  user3683367 May 31 at 19:03