I wrote a function which takes Graphics
as input and outputs a Piecewise
parametric function by replacing the Head
. The list of implemented Graphics
types can easily be extended.
graphicsToParametricFunctions::grphtp="Unknown graphics type: `1`";
graphicsToParametricFunctions=Function[{graphics},Module[{line=Function[{pointPairs,t},With[{len=Length[pointPairs]},Piecewise[MapIndexed[{{((#2[[1]]-len t))#1[[1,1]]+(1+len t-#2[[1]])#1[[2,1]],(#2[[1]]-len t)#1[[1,2]]+(1+len t-#2[[1]])#1[[2,2]]},(#2[[1]]-1)/len<t<#2[[1]]/len}&,pointPairs]]]]},Module[{rules={Line[args__/;Depth[args]==3]:>Function[{t},line[Subsequences[args,{2}],t]],Line[args__/;Depth[args]==4]:>Function[{t},line[Flatten[Map[Subsequences[#,{2}]&,args],1],t]],Circle[{centerx__,centery__},radius__,{arc1__,arc2__}]:>Function[{t},{centerx+radius Cos[(1-t) arc1+t arc2],centery+radius Sin[(1-t) arc1+t arc2]}],BezierCurve[args__]:>Function[{t},Map[BernsteinBasis[Length[args]-1,#,t]&,Range[0,Length[args]-1]].args]}},Function[{t},Evaluate[PiecewiseExpand[Piecewise[MapIndexed[If[AnyTrue[Keys[rules],Function[{rule},MatchQ[#1,rule]]],{Replace[#1,rules][t-#2[[1]]+1],#2[[1]]-1<t<#2[[1]]},Message[graphicsToParametricFunctions::grphtp,#1]]&,Flatten[Level[graphics,1]]]]]]]]]];
Case 1
Two different types of Graphics
which are attached, shaped as a house.
houseGraphic=Graphics[{Line[{{-1,0},{-1,-1},{1,-1},{1,0}}],Circle[{0,0},1,{0,Pi}]}]
houseParametricFunction = graphicsToParametricFunctions[houseGraphic]
Manipulate[Show[ParametricPlot[houseParametricFunction[tt],{tt,0,t}],PlotRange->{{-1,1},{-1,1}},ImageSize->220,Axes->False],{t,0,2,0.1}]
Case 2
Two Line
s which are separate.
twoLinesGraphic=Graphics[{Line[{{{-1,1},{-1,-1},{1,-1},{1,1},{-1,1}},{{-1/2,1/2},{-1/2,-1/2},{1/2,-1/2},{1/2,1/2},{-1/2,1/2}}}]}]
twoLinesParametricFunction=graphicsToParametricFunctions[twoLinesGraphic]
Manipulate[Show[ParametricPlot[twoLinesParametricFunction[tt],{tt,0,t}],PlotRange->{{-1,1},{-1,1}},ImageSize->220,Axes->False],{t,0,1,0.05}]
Case 3
Random BezierCurve
s.
SeedRandom[1]
bezierGraphic=Graphics[Map[BezierCurve[#]&,RandomReal[1,{10,4,2}]]];
bezierParametricFunction=graphicsToParametricFunctions[bezierGraphic]
Manipulate[Show[ParametricPlot[bezierParametricFunction[tt],{tt,0,t}],PlotRange->{{0,1},{0,1}},ImageSize->220,Axes->False],{t,0,10,0.5}]
Case 4
Not implemented Graphics
type.
graphicsToParametricFunctions[Graphics[Disk[]]]
"AnimationRepetitions"->Infinity
as an option and it'll repeat indefinitely. $\endgroup$Export[..., Manipulate[..., AnimationDirection -> Forward], "AnimationRepetitions"->Infinity]
$\endgroup$