Plotting an epicycloid

I recreated the animation on Wikipedia:

enter image description here

Here is the Manipulate version:

R = 3; r = 1;

fx[θ_, a_: 1] := (R + r) Cos[θ] - a r Cos[(R + r) θ/r];
fy[θ_, a_: 1] := (R + r) Sin[θ] - a r Sin[(R + r) θ/r];

gridlines = Table[{x, GrayLevel[0.9]}, {x, -6, 6, 0.5}];

plot[max_] := ParametricPlot[
  {fx[θ], fy[θ]}, {θ, 0, max},
  PlotStyle -> {Red, Thick},
  Epilog -> {
    Thick,
    Blue, Circle[{0, 0}, R],
    Black, Circle[{fx[max, 0], fy[max, 0]}, r],
    Red, PointSize[Large], Point[{fx[max], fy[max]}],
    Black, Line[{{fx[max, 0], fy[max, 0]}, {fx[max], fy[max]}}]
    },
  GridLines -> {gridlines, gridlines},
  PlotRange -> {-6, 6}
  ]
Manipulate[plot[max], {max, 0.01, 2 Pi}]

It is easiest to use ParametricPlot and RotationTransform.

ParametricPlot[
  RotationTransform[a][{1, 0}] + RotationTransform[4 a][{1/4, 0}],
  {a, 0, 2 Pi}, Evaluated -> True]

enter image description here

Evaluated -> True is not strictly necessary; it's there just to construct the equation once instead of for every point in the graph separately (... which is slow).

EDIT: Improved per comment from Mr.Wizard.


Epicycloids (and other roulette curves) are fun to play with using Manipulate. Here is one of many possible implementations.

Manipulate[
  ParametricPlot[{
    (1 + r)  Cos[theta] + a Cos[(1 + r) theta], 
    (1 + r) Sin[theta] + a Sin[(1 + r) theta]}, 
    {theta, 0, 2 Pi}],
  {{r, 1}, 1, 20, 1, Appearance -> "Labeled"},
  {{a, 1}, -10, 10, .2, Appearance -> "Labeled"}]

epicycloid.png


Edit

Although the question only calls for epicycloids, it is very easy to make interactive panel that allows the user to play with hypocycloids as well. Because there is a simple relationship between epicycloids and hypocycloids, doing so adds very little code.

Manipulate[
  ParametricPlot[{
      type r Cos[theta] + a Cos[type r theta], 
      type r Sin[theta] + a Sin[type  r theta]},
    {theta, 0, 2 Pi},
    PlotStyle -> {Red, Thick},
    ImageSize -> {400, 400}],
  {{type, 1}, {1 -> "epicycloid", -1 -> "hypocycloid"}},
  {{r, 1}, 1, 20, 1, Appearance -> "Labeled"},
  {{a, 1}, -10, 10, .1, Appearance -> "Labeled"}]

roulettes.png