Message Boards Message Boards

1
|
3080 Views
|
7 Replies
|
7 Total Likes
View groups...
Share
Share this post:

How to avoid recalculation of Graphics with moving object?

Posted 2 years ago

Often on has to draw some complex mostly static drawing with some animated object in it. Normally this is done by Manipulate which rebuilds the whole graphics for each step. I would like to have a mechanism which, for a refresh of the moving object, does not recalculate and redraw the hole graphics but only this object.

Neither Show nor Overlay do that. The only solution I found and used is not very elegant:

  • Build a Graphics from the static part alone. Rasterize it to get a Raster graphics primitive.
  • For each step: Draw the moving object only, use the Raster as background.

Can someone point me to a better solution?

POSTED BY: Werner Geiger
7 Replies

One point I would like to clear up that @Werner Geiger might be confused about is that Graphics does no computation - ever. It has no evaluation rules. Plot and other functions that produce a Graphics as part of their computation, but the resulting graphics object is inert.

POSTED BY: Jason Biggs

Jason,

excellent point - thank you! If I understand this correctly then the situation you are describing can be seen by looking at the FullForm, e.g. of my code above (there is still the locator which can be moved!):

Manipulate[
 FullForm@Show[background, 
   Graphics[{Translate[grgr, {xy}]}]], {{xy, {1, 1}}, Locator}]
POSTED BY: Henrik Schachner
Posted 2 years ago

Check out this. Maybe useful?

Manipulate[
    redcircle = Circle[center, radius];
    Graphics[{
        {Blue, Circle[]},
        {Red, redcircle},
        {Opacity@0.5, Rectangle[]}
    }],
    {center, Locator},
    {{radius, 0.5}, 0.1, 0.9, 0.1},
    {redcircle, None}
 ]
POSTED BY: Hans Milton

Werner,

one way could be to combine your graphics objects into a single object using GraphicsGroup and then applying Translate. Here an example:

grgr = GraphicsGroup[{Red, Disk[], Blue, Rectangle[{.5, .5}], Green, Sphere[{.5, .3}, 1.4]}];
Manipulate[
 Graphics[Translate[grgr, {xy}], PlotRange -> {{-4, 4}, {-4, 4}}, 
  Axes -> True], {{xy, {1, 1}}, Locator}]

Does that help? Regards -- Henrik

POSTED BY: Henrik Schachner
Posted 2 years ago

I am not shure, Henrik, if I understand you or you understood me. Where is that static Background in your example?

I added a randomly rotated LightBlue rectangle as such. Of course it is recalculated with each step, i.e. move of the locator:

grgr = GraphicsGroup[{Red, Disk[], Blue, Rectangle[{.5, .5}], Green, 
   Sphere[{.5, .3}, 1.4]}]; Manipulate[
 Graphics[{LightBlue, 
   GeometricTransformation[Rectangle[{-3, -3}, {3, 3}], 
    RotationTransform[RandomReal[{0, Pi/2}]]],
   Translate[grgr, {xy}]}, PlotRange -> {{-4, 4}, {-4, 4}}, 
  Axes -> True], {{xy, {1, 1}}, Locator}]
POSTED BY: Werner Geiger

And how about this approach:

grgr = GraphicsGroup[{Red, Disk[], Blue, Rectangle[{.5, .5}], Green, Sphere[{.5, .3}, 1.4]}];
background = Graphics[{LightBlue, GeometricTransformation[Rectangle[{-3, -3}, {3, 3}], 
     RotationTransform[RandomReal[{0, Pi/2}]]]}, PlotRange -> {{-4, 4}, {-4, 4}}, Axes -> True];

Manipulate[Show[background, Graphics[{Translate[grgr, {xy}]}]], {{xy, {1, 1}}, Locator}]
POSTED BY: Henrik Schachner
Posted 2 years ago

This actually works! At first I was surprised, because I thought the Show would be recalculated as a whole.

But now I understand it: xy is Dynamic, so only grgr is recalculated, background is not, because it does not depend on xy. Pretty simple and pretty stupid of me not to figure it out myself.

Thanks a lot!

Liebe Grüße and Glück auf!, Werner

POSTED BY: Werner Geiger
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract