# Importing and animating images

I have three image files that make up a system similar to a mechanical arm.

I wonder if it's possible to be created an assembly that I can import these images and use them in a way to generate an animation.

It may seem strange to my question, I know that there are better tools for this purpose , talking about the animation.

However , I do not believe there is a tool that in addition to running an animation can provide me mathematical results related to this animation.

Below I show an animation made by me using a software called SolidWorks software, but this software just gives me an animation and I can not get mathematical data such as position, velocity and acceleration:

The initial position of the mounted images is shown in the figure below :

• It would be better to create the elements in Mathematica, then animate them based on your equations of motion ... take a look at this - wolfram.com/mathematica/new-in-9/… Commented Jul 7, 2016 at 0:17
• I also agree that it is very interesting to do through the graph function. But I created this question to see the possibilities with this idea. Commented Jul 7, 2016 at 0:35
• Perhaps you'll like this: Animation of double pendulum
– Jens
Commented Jul 7, 2016 at 17:20
• @Jens This is really cool. It will really help with my studies. Commented Jul 7, 2016 at 20:00

The trick is to create rectangles that cover the bounding boxes of each component image and use the images as textures, then we can use Rotate and Translate to animate the robot arm the way we want.

To that end, we may use this code:

t1 = Import["~/Downloads/ElementosPNG/1.png"];

width[texture_] := ImageDimensions[texture][[1]]
height[texture_] := ImageDimensions[texture][[2]]

getComponent[texture_, transform_] := Graphics[{
Texture[texture],
transform@Polygon[{{0, 0},
{width[texture], 0},
{width[texture], height[texture]},
{0, height[texture]}},
VertexTextureCoordinates -> {{0, 0}, {1, 0}, {1, 1}, {0, 1}}]
}]


Now we can do, e.g.,

getComponent[t1, Identity]


and similarly for the two other components. The second argument of getComponent is a transform function that can be used to translate and rotate the component. Right click on the image of the first component and choose "get coordinates" to get the coordinate of the center of the hole, then plot the second component and find out the coordinates for its axis. I found that the coordinates were {135, 179} and {52, 60}. Testing the values, we can see that this is approximately right:

Show[
getComponent[t2, Translate[#, {135, 179} - {52, 60}] &],
getComponent[t1, Identity]
]


We can add rotation as well:

renderComponent[] := getComponent[t1, Identity]
renderComponent[theta_] := getComponent[
t2,
Composition[
Rotate[#, theta, {135, 175}] &,
Translate[#, {135, 179} - {52, 60}] &
]
]


Henceforth I'm going to be using renderComponent for all three of the components. No arguments means it it will plot the first component, one argument is the second component and two arguments is the third component.

In order to figure out the transform for the third component, plot the second argument and find out the distances between the axes. Also plot the third component and find out the coordinates for its axis. This is what I got:

renderComponent[theta_, phi_] := getComponent[
t3,
Composition[
Rotate[#, phi, {135, 179} - {52, 60} + 1025 {Cos[theta], Sin[theta]} + {80, 80}] &,
Translate[#, {135, 179} - {52, 60} + 1025 {Cos[theta], Sin[theta]}] &
]
]


Plotting this, it seems to be approximately right:

Manipulate[Show[
renderComponent[theta],
renderComponent[theta, phi],
renderComponent[],
PlotRange -> {{-2000, 2000}, {0, 2000}}
], {theta, 0, Pi}, {phi, 0, 2 Pi}]


The precision with which you got the coordinates off of the components will determine how precisely the components fit together.

• The purpose of import and animation was completed. It is now my responsibility to create control functions. Thanks. Commented Jul 7, 2016 at 9:50
• If you have an interest in correcting your GIF because I realized a small failure, I would be grateful, because your answer was fantastic. Commented Jul 7, 2016 at 17:03

My answer is nothing more than a complement of learned with @C. E.:

t1 = Import["~/Downloads/ElementosPNG/1.png"];


I added some points for me to locate:

p1={136,173.4};p2={54.8,54.2};p3={1009.2,54.2};
p4={81.3,80.5};

width[texture_]:=ImageDimensions[texture][[1]]
height[texture_]:=ImageDimensions[texture][[2]]
getComponent[texture_,transform_]:=Graphics[{Texture[texture],transform@Polygon[{{0,0},{width[texture],0},{width[texture],height[texture]},{0,height[texture]}},VertexTextureCoordinates->{{0,0},{1,0},{1,1},{0,1}}]}]


I have tried to add these commands to find the coordinates:

getComponent[t1,Identity];
getComponent[t2,Identity];
getComponent[t3,Identity];


I have tried to add these commands to find the positions and the path that will be followed:

g=Graphics[{Dashed,Red,Thickness[0.003],Circle[p1,1064],
Line[{{0,p1[[2]]},{1200,p1[[2]]}}],
Line[{{p1[[1]]+p3[[1]]+p2[[1]],0},{p1[[1]]+p3[[1]]+p2[[1]],400}}],
Line[{{p1[[1]],0},{p1[[1]],400}}]}];

Show[
getComponent[t2,Translate[#,p1-p2]&],
getComponent[t1,Identity],
getComponent[t3,Translate[#,p1-{-983,80.5}]&],
g,
PlotRange->{{0,2000},{0,400}},
ImageSize->700,Axes->True]


I changed some values to relate with the points P1, P2, P3 and P4:

renderComponent[]:=getComponent[t1,Identity]
renderComponent[theta_]:=getComponent[t2,Composition[Rotate[#,theta,p1]&,Translate[#,p1-p2]&]]
renderComponent[theta_,phi_]:=getComponent[t3,Composition[Rotate[#,phi,p1-p4+(p2[[1]]+p3[[1]]){Cos[theta],Sin[theta]}+p4]&,Translate[#,p1-p4+(p2[[1]]+p3[[1]]){Cos[theta],Sin[theta]}]&]]

Manipulate[
Show[
renderComponent[theta],
renderComponent[theta,phi],
renderComponent[],
g,PlotRange->{{-1000,2000},{0,1500}},ImageSize->700,Axes->True],{theta,0,Pi},{phi,0,2 Pi}]

• In the response that I presented, the codigo getComponent[t3, Translate[#, p1 - {-983, 80.5}] &] I introduced the values manually to achieve the expected result. What these values indicate? I managed to get the result, but I didn't. Commented Jul 7, 2016 at 20:50