1

Is there a way to convert a path to a string [save it to a file, read it] and convert it back to a path? Something like this:

path p,q;
p := (0,0)..(1cm,1cm)..(3/2cm, -1cm);
write "q := " & pathToString(p) & ";" to "somepath.mp";
write EOF to "somepath.mp";
input "somepath.mp";
draw q;

It's certainly possible to write a function which would reconstruct a path from all the precontrols, postcontrols and stuff, but maybe there's an easier way or a ready solution?

2
  • What about string S[]; path P[]; S[1] := "(0,0)..(1cm,1cm)..(3/2cm, -1cm)"; P[1] := scantokens(S[1]); draw P[1]; or something along these lines? Is it too cumbersome to write it like that?
    – user226564
    Jan 23, 2021 at 21:24
  • Yeah, but I need to start with a path. It's actually related to this question tex.stackexchange.com/questions/580204/… . If I get some text outlines, i want to be able to save them and to open and manipulate them in MetaPost afterwards. Jan 23, 2021 at 21:45

1 Answer 1

1

For the time being, here's my function to do this:

vardef pathToString (expr p) =
save outputString, endpoint, i;
    string outputString;
    outputString :=
        "(" & decimal(xpart(point 0 of p)) & ", " & decimal(ypart(point 0 of p)) & ")"
        & ".. controls (" & decimal(xpart(postcontrol 0 of p)) & ", " & decimal(ypart(postcontrol 0 of p)) & ") and "
        & "(" & decimal(xpart(precontrol 1 of p)) & ", " & decimal(ypart(precontrol 1 of p)) & ")";
    if (cycle p):
        endpoint := length(p) - 1;
    else:
        endpoint := length(p);
    fi;
    for i := 1 step 1 until endpoint:
        outputString := outputString
            & ".. (" & decimal(xpart(point i of p)) & ", " & decimal(ypart(point i of p)) & ")";
        if ((i < length(p)) or (cycle p)):
            outputString := outputString
                & ".. controls (" & decimal(xpart(postcontrol i of p)) & ", " & decimal(ypart(postcontrol i of p)) & ") and "
                & "(" & decimal(xpart(precontrol i + 1 of p)) & ", " & decimal(ypart(precontrol i + 1 of p)) & ")";
        fi;
    endfor;
    if (cycle p):
        outputString := outputString & " .. cycle";
    fi;
    outputString
enddef;

It seems to work ok.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .