4

I want to read/write asymptote cyclic path definitions from/to files. For example, while running asymptote code, I want to build up an arbitrary cyclic path and write that path information to an external file for later inclusion in another asymptote run.

Is there already an easy solution for this?

If not, my thought is to loop thru the path nodes and use the point, precontrol, and postcontrol functions to write a string of the form...

path p = (0,0)..controls (0,100) and (100,100)..(100,0) and so on

to an external file and then include that file in the later asymptote run. Does anyone know of an easier way?

3
  • 1
    You can get the path string extracted using write(file, p); where p is the name of the path and file is the file you want (e.g. stdout works for a quick test). Preface it with writing the string 'path p = ' and append a ';' to get an includeable file. Jun 4, 2015 at 21:06
  • @CharlesStaats: That's perfect! I never saw in the manual that the write command would work for that. If you want to put that info in an answer I will certainly accept it.
    – James
    Jun 4, 2015 at 21:18
  • Maybe I'll turn it into an answer this weekend. (I'm concerned about whether the include statement is executed at runtime or compile time; if the latter, then any changes made to the file during the current asy run would be omitted.) Anyway, I suspect that this version of write was included mainly for debugging purposes (which is also how I came across it; I was debugging and wanted to check if the "lazy" code worked before writing something more elaborate). Jun 5, 2015 at 23:55

1 Answer 1

3

For whatever reason, the write function in Asymptote accepts a path argument that does what you want, although this does not appear to be mentioned in the manual. Note that there seems to be no corresponding function for directly converting a path to a string, so you can't write something like "path p = " + (string)p + ";" where p is a path.

Surprisingly enough, the following example works even on the first asy run, which means that in essence it contains self-writing code.

size(5cm);

path p = (0,0) .. (1,1) .. (2,0) .. cycle;

file f = output("pathcode.asy");
write(f,"path q = ");
write(f, p);
write(f, ';');
close(f);

include "pathcode.asy";

draw(q);
write(q);  // write to command line / terminal

The resulting terminal output:

(0,0).. controls (-0.0663123,0.549145) and (0.418883,1)
 ..(1,1).. controls (1.58112,1) and (2.06631,0.549145)
 ..(2,0).. controls (1.85727,-1.18201) and (0.142734,-1.18201)
 ..cycle
1
  • Very nice example. I now see in section 6.6 of the manual the following: write(string s="", T x, suffix suffix=endl ... T[]); where T is a built-in type.
    – James
    Jun 14, 2015 at 12:22

You must log in to answer this question.

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