\edef\r{\luaexpr{1/3.8}} % Radius of smaller circle (larger circle has radius 1)
\edef\N{10} % Number of frames in animation per hypocycloid "branch"

\edef\K{1}\doloop{\ctxlua{commands.doifelse(math.abs(\K*\r - math.floor(\K*\r+0.5))<0.000001)}{\exitloop}{\edef\K{\luaexpr{\K+1}}}}
\startMPinclusions[+]
	r:=\r; % Radius of smaller circle (larger circle has radius 1)
	u:=70; % Scaling factor for the graphics
	K:=\K; % Number of "branches" per period (computed automatically)
	M:=8; % Number of points in the Bezier curve for each "branch"
	N:=\N; % Number of frames in animation per hypocycloid "bran
	path p; % p is the "first branch" of the hypocycloid
	p:=(1,0) for m=1 upto (M-1): .. (r*cosd(360*(r-1)/(M-1)*m)-(r-1)*cosd(360*r/(M-1)*m),r*sind(360*(r-1)/(M-1)*m)-(r-1)*sind(360*r/(M-1)*m)) endfor;
	W:=ceiling(u/32*65); % Width of each figure's bounding box
	H:=W; % Height of each figure's bounding box
	path q;
\stopMPinclusions
\starttext
	\dorecurse{\luaexpr{\K*\N+1}}{
		\startMPpage
			m:=floor((\recurselevel-1)/N);
			n:=\recurselevel-m*N-1;
			numeric s[];
			t:=n/N;
			q:=(((1-r)*cosd(360*r*t),(1-r)*sind(360*r*t))--((1+r)*cosd(360*(r-1)*t)-(r-1)*cosd(360*r*t),(1+r)*sind(360*(r-1)*t)-(r-1)*sind(360*r*t)));
			(s1,s2) = p intersectiontimes reverse q;
			for j=0 upto (m-1): draw p scaled u rotated (360*r*j) withcolor red; endfor;
			draw subpath (0,s1) of p scaled u rotated (360*r*m) withcolor red;
			draw fullcircle scaled (2*u);
			draw fullcircle scaled (2*r*u) shifted ((1-r)*u*cosd(360*r*t),(1-r)*u*sind(360*r*t)) rotated (360*r*m);
			draw subpath (s2,length q) of (reverse q) scaled u rotated (360*r*m);
			setbounds currentpicture to (-W/2,-H/2)--(W/2,-H/2)--(W/2,H/2)--(-W/2,H/2)--cycle;
		\stopMPpage
	}
\stoptext
