在 MetaPost 中将路径转换为字符串/保存到文件的路径

在 MetaPost 中将路径转换为字符串/保存到文件的路径

有没有办法将路径转换为字符串 [将其保存到文件,读取] 并将其转换回路径?像这样:

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;

当然可以编写一个函数来从所有预控制、后控制和其他内容中重建一条路径,但也许有更简单的方法或现成的解决方案?

答案1

目前,这是我执行此操作的功能:

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;

看起来运行正常。

相关内容