Some Graphics in Mathematica could also be described by functions, in particular by ParametricPlots. For example, take this figure of a 'house' with a round 'roof'.


Simple Graphics

Turning this manually into parametric functions gives:


Moving ParametricPlot

Is it possible to generate a (possibly Piecewise) parametric function which may be drawn using ParametricPlot automatically based on Graphics as input?

Background: imagine using Mathematica to export G-code for CNC laser engraving or cutting while giving you control over the order, direction and speed of each segment, and allowing you to verify the behaviour of your CNC before printing. Note that exporting an SVG will not give you control over direction nor speed.

  • $\begingroup$ Per the GIF, you can export it with "AnimationRepetitions"->Infinity as an option and it'll repeat indefinitely. $\endgroup$ – b3m2a1 May 22 '19 at 17:23
  • $\begingroup$ Going to i.stack.imgur.com/QbMbE.gif shows me a GIF which loops indefinitely. As does the GIF I created locally. I think it's a browser thing. Though I could be wrong. Can't be bothered generating it again ;) thanks though. $\endgroup$ – LBogaardt May 22 '19 at 17:29
  • 1
    $\begingroup$ the imgur one only loops twice. That’s Mathematica’s default. $\endgroup$ – b3m2a1 May 22 '19 at 17:34
  • $\begingroup$ Ah, indeed. Thanks! Any idea how to generate GIFs which only run Forward, and then return to the start? $\endgroup$ – LBogaardt May 22 '19 at 19:57
  • $\begingroup$ Try Export[..., Manipulate[..., AnimationDirection -> Forward], "AnimationRepetitions"->Infinity] $\endgroup$ – b3m2a1 May 22 '19 at 20:49
toParametricLines[mcm_: .1][g_Graphics] := Module[{bsfs = 
    MeshPrimitives[DiscretizeGraphics[g, MaxCellMeasure -> {"Length" -> mcm}], 1] /. 
      Line -> (BSplineFunction[#, SplineDegree -> 1] &)}, 
  ParametricPlot[Through@bsfs@t, {t, 0, 1}]]


g1 = Graphics[{Line[{{-1, 0}, {-1, -1}, {1, -1}, {1, 0}}], Circle[{0, 0}, 1, {0, Pi}]}];

enter image description here

g2 = Graphics[{RandomColor[], BezierCurve @ #} & /@ RandomReal[1, {10, 4, 2}]]

enter image description here

Show[toParametricLines[][g2], Axes -> False]

enter image description here

Update: For the case of a single closed curve, we can get a single BSplineFunction (as opposed to one function for each Line object returned by MeshPrimitives[...]) using

toParametricLines2[mcm_: .1][g_Graphics] := Module[{bsf = 
     MeshPrimitives[DiscretizeGraphics[g, MaxCellMeasure -> {"Length" -> mcm}], 1] /. 

      l : {__Line} :> BSplineFunction[Join @@ l[[All, 1]], SplineDegree -> 1] }, 
    ParametricPlot[bsf@t, {t, 0, 1}]]


same as the first picture above

  • 1
    $\begingroup$ Presumably there is only one cutting tool, so there should only be one BSplineFunction. $\endgroup$ – Carl Woll May 22 '19 at 15:33
  • $\begingroup$ @Carl, right; good point. To get a single BSplineFunction for all the Line primitives returned by MeshPrimitives[...] we need an additional step to combine the coordinates in the correct order. $\endgroup$ – kglr May 22 '19 at 15:43
  • $\begingroup$ You're basically approximating the original Graphic with a set of points and then interpolating those points. Cool idea. And definitely appropriate for any CNC task, as long as the approximation-resolution is higher than the CNC machine's resolution. $\endgroup$ – LBogaardt May 22 '19 at 17:23
  • $\begingroup$ Nonetheless, I would have hoped there was a more fundamental way to 'explode' a Graphic and adapt the internal Mathematica code to a parametric function. For example, a Line[{{a,b},{c,d}}] must refer to some internal plot-function which takes a,b,c,d as input arguments (I assume). $\endgroup$ – LBogaardt May 22 '19 at 17:27
  • 1
    $\begingroup$ I now see InputForm[ParametricPlot[{Cos[t], Sin[t]}, {t, 0, Pi}]] yields a Graphic with a Line with many points, similar to your approximation. So to Mathematica, a Plot internally is a Graphic and a Graphic is not internally a Plot, as I first assumed (see reference.wolfram.com/language/tutorial/…) $\endgroup$ – LBogaardt May 23 '19 at 11:32

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.


Graphic of a house

houseParametricFunction = graphicsToParametricFunctions[houseGraphic]

Piecewise of a house


GIF of a house

Case 2

Two Lines which are separate.


Graphic of two squares


Piecewise of two squares


GIF of two squares

Case 3

Random BezierCurves.


Graphic of random Beziers


Piecewise of random Beziers


GIF of random Beziers

Case 4

Not implemented Graphics type.


Screenshot of an error


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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