5
$\begingroup$

How do I rotate the following image?

CompleteGraph[30, DirectedEdges -> True, 
 EdgeStyle -> RGBColor[0, 0, 1], PlotRange -> 1.1*{{-1, 1}, {-1, 1}}, 
 EdgeShapeFunction -> GraphElementData["ShortCarvedArcArrow"], 
 ImageSize -> Floor[500/16]*16, Background -> White]

I want the rotation to not change the plot region, here's an example of what I do NOT want:

https://www.youtube.com/watch?v=Twz-SqQON4U

I want the object to stay centered while rotating. I don't know what the difference between ImageRotate and Rotate is...

Thanks!

$\endgroup$
2
  • $\begingroup$ Welcome to Mathematica.SE! I suggest that: 1) You take the introductory Tour now! 2) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! 3) As you receive help, try to give it too, by answering questions in your area of expertise. $\endgroup$
    – bbgodfrey
    May 12, 2015 at 16:51
  • $\begingroup$ What code are you using at the moment? $\endgroup$
    – Yves Klett
    May 12, 2015 at 18:07

3 Answers 3

9
$\begingroup$

You might find it easier to use Rotate inside of Graphics rather than outside. To do this, you will need to "convert" the Graph to Graphics (I use Show) and then use MapAt to apply Rotate inside of Graphics.

g = CompleteGraph[30, DirectedEdges -> True, 
   EdgeStyle -> RGBColor[0, 0, 1], 
   PlotRange -> 1.1*{{-1, 1}, {-1, 1}}, 
   EdgeShapeFunction -> GraphElementData["ShortCarvedArcArrow"], 
   ImageSize -> Floor[500/16]*16, Background -> White];

frames = Table[
   MapAt[Rotate[#, q, {0, 0}] &, Show[g], {1, All}], {q, 0, 
    2 Pi - Pi/8, Pi/8}];

Export[FileNameJoin[{"myPath", "rotate.gif"}], frames, "GIF", Background -> None]

slightly smaller image

$\endgroup$
5
  • 1
    $\begingroup$ Uhh, cannot... stop... watching... $\endgroup$
    – Yves Klett
    May 12, 2015 at 19:02
  • 3
    $\begingroup$ ....you will send chuy nonconsecutive $20 bills... $\endgroup$
    – chuy
    May 12, 2015 at 19:08
  • 1
    $\begingroup$ Thanks chuy! Exactly what I needed!!! $\endgroup$
    – atat
    May 12, 2015 at 21:16
  • $\begingroup$ I apologize for not coding the code right! Oops, I'm supposed to avoid comments that just say thanks. So I will add that I changed the code slightly as I am trying to export to a MOV: code g=CompleteGraph[30,DirectedEdges->True,EdgeStyle-RGBColor[0,0,1],PlotRange->1.1*{{-1,1},{-1,1}},EdgeShapeFunction-GraphElementData["ShortCarvedArcArrow"],ImageSize-Floor[500/16]*16,Background->White]; frames=ParallelTable[MapAt[Rotate[#,q Degree,{0,0}]&,Show[g],{1,All}],{q,0,360-360/16.,360/16.}]; Export["test18-1.mov",frames] $\endgroup$
    – atat
    May 12, 2015 at 21:42
  • $\begingroup$ Now I want it to change hue when rotating, so the hue will be a function of the rotation angle. How do I do this? $\endgroup$
    – atat
    May 22, 2015 at 16:44
3
$\begingroup$

I am assuming that it would be acceptable to turn the graph into an image first, then rotate / animate that image. This can be done at arbitrary resolution through appropriate options for Image. Here I am just using the image size you originally specified for the graph object.

An option to ImageRotate comes in handy here: the size of its output can be set to All, which in this context means "the smallest square to accommodate the rotated image for any rotation angle" (see docs).

(* I am using a 5-order graph, adjust to desiderd order *) 
CompleteGraph[5,
  DirectedEdges -> True, EdgeStyle -> RGBColor[0, 0, 1], 
  PlotRange -> 1.1*{{-1, 1}, {-1, 1}}, 
  EdgeShapeFunction -> GraphElementData["ShortCarvedArcArrow"], 
  ImageSize -> Floor[500/16]*16, Background -> White
];

(* Convert graph to an image of the same size as the one generated above *)
Image[%, AbsoluteOptions[%, ImageSize]];

(* Rotate the image *)
Animate[
 ImageRotate[%, theta, All, Background -> None],
 {theta, 0, 2 Pi}
]

Animated rotating graph


Update - changing colors during rotation:

chuy's code above can be more readily adapted to what you want to do. I will borrow his code to generate the rotated graphs, and modify it to include a changing edge color for the graphs. This is accomplished after graph generation, by digging into the structure of the Graphics object generated by Show and replacing the value of the EdgeStyle directive with color values from one of the built-in gradient functions defined in ColorData["Gradients"]. In the example below I used the 'CMYKColors` gradient function.

A few more adaptations are needed to work with the angle of rotation. The gradient functions take an input in the $(0,1)$ range. Additionally, in order to have smooth color transitions between successive animation cycles, I want to run through the gradient function twice, once forward and once backwards, so that the last point of the animation cycle takes the color immediately preceding the first point in the next cycle. This gives pleasingly smooth color transitions. This is accomplished by taking the value of the angle parameter and transforming it so it ramps linearly up to $0.5$ at mid-cycle, then ramps back down linearly to $0-stepvalue$ at the end of the cycle. I use 1 - Abs[2 (q/Pi) - 1] to do so.

graph =
  CompleteGraph[
    30, DirectedEdges -> True,
    EdgeStyle -> RGBColor[0, 0, 1], 
    EdgeShapeFunction -> GraphElementData["ShortCarvedArcArrow"], 
    PlotRange -> 1.1*{{-1, 1}, {-1, 1}}, ImageSize -> Floor[500/16]*16,
     Background -> White
   ];

frames =
  Table[
    MapAt[
      Rotate[#, q, {0, 0}] & , 
      ReplaceAll[
        Show[graph], RGBColor[0, 0, 1] -> ColorData["CMYKColors"][1 - Abs[2 (q/Pi) - 1]]
      ],
     {1, All}
    ],
    {q, 0, Pi - Pi/16, Pi/16}
  ];

Export["animatedgraph.gif", frames, "GIF"]

rotating colored graph

$\endgroup$
4
  • $\begingroup$ How can I make the hue change when it rotates? $\endgroup$
    – atat
    May 23, 2015 at 5:41
  • $\begingroup$ @atat I updated my answer to achieve color change during rotation. Take a look. $\endgroup$
    – MarcoB
    May 23, 2015 at 21:58
  • $\begingroup$ Thank you so much! Actually I managed to figure it out! I used a really different method but yours may make the animation I am rendering go faster...there are about 1500 frames so any edge counts! $\endgroup$
    – atat
    May 24, 2015 at 3:04
  • $\begingroup$ @atat glad it might help $\endgroup$
    – MarcoB
    May 24, 2015 at 3:09
2
$\begingroup$

We can extract the coordinates of the graph's vertices and rotate those. To wit:

n = 5; (* I used a smaller example for demonstration; change if needed *)
vlist = VertexCoordinates /.
        AbsoluteOptions[CompleteGraph[n], VertexCoordinates];

Prepare the frames of the animation (this might take a fair bit of time for large n):

With[{frames = 30}, 
     gl = Table[Show[CompleteGraph[n,
                Background -> White, DirectedEdges -> True,
                EdgeShapeFunction -> GraphElementData["ShortCarvedArcArrow"], 
                EdgeStyle -> Hue[t/(2 π)], ImageSize -> 16 Quotient[500, 16], 
                PlotRange -> 1.1 {{-1, 1}, {-1, 1}}, 
                VertexCoordinates -> (RotationTransform[t] /@ vlist)]],
                {t, 0, 2 π - 2 π/frames, 2 π/frames}]];

ListAnimate[gl]

rotating complete graph

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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