On 12/17/2023 11:22 PM, Gavin via ntg-context wrote:
Hello ConTeXters,
I wrote a little ConTeXt module for drawing globes. I would love some advice on how to improve it and share it. The code is quite short (about 250 lines). Most of the work is done by Lua, which reads the data files and calculates paths. These paths are passed to MetaFun, which draws the globe.
Just some quiks remarks ... - you use the right strategy (tex/lua/mp) - in mp you can do this vardef theglobe(expr lat, lon) = for i = 1 upto lua.mp.makeglobe(lat, lon): (lua.mp.getglobepath(i)) &&&& endfor cycle enddef ; and then: \startMPpage GlobeDiameter = 10cm ; fill fullcircle scaled GlobeDiameter withcolor .9white ; path p ; p := theglobe(23, 0) scaled .5GlobeDiameter ; draw p withpen pencircle scaled .2mm withcolor blue withtransparency (1,.5) ; fill p withcolor red withtransparency (1,.5) ; draw fullcircle scaled GlobeDiameter withcolor black ; \stopMPpage i use transparency to demo that a single path has advantages; you'll also notice that the runtime is actually at the mp end. - in your lua code do this local cosd = math.cosd local sind = math.sind local sqrt = math.sqrt - also, use "MP.makeglobe" in the mp code and at the lua end function MP.makeglobe"" as that is the user namespace. I'll mail you the lua file with some suggestions. When all is stable I can spend a few hours on optimizing if needed.
I am a novice at both Lua and MetaPost. I’m also new to Git and have never shared anything of substance with the ConTeXt community. (This barely counts as substantive, but I figure it’s best to start small.) I’m sure many of you could find opportunities for improvement with even a quick glance at the code. I welcome anything, from advice on performance to suggestions about the license. My most pressing questions are these:
1. How do I avoid redrawing diagrams with every typeset? The globe above takes about 0.7s, which is not bad, but it adds up in a book with many diagrams.
see previous mail
2. How do I organize this according to TDS for sharing? I know what TDS is and why it’s important, but that’s about it!
3. Should I be creating a namespace for this module, or launching a separate MetaFun instance? I have a general sense of what “namespace” and “instance” mean is this context, but I don’t know the consequences or the how-to.
I’d like to share this module, even though the potential demand is tiny, at best. I’m going through the Module Writing Guidelines (https://wiki.contextgarden.net/Modules#Module_writing_guidelines), but there is a lot that I don’t understand in those instructions. Questions 2 and 3 above relate to the instructions that are most mysterious to me. I think I can figure most of the others out.
I have been using ConTeXt for several years to write a high school physics textbook (along with the problem sets, tests, equations sheets, etc.). I wrote this module because I needed globes in some diagrams. I found an old MetaPost tool, mp-geo, that seemed to have the right ingredients, but I couldn’t get it to work, so I wrote my own tool using the data files from mp-geo. Hans and others on the list gave me valuable advice for these globes a couple years ago (and gave other valuable advice on all sorts of things before and since).
Many diagrams in my physics book use TikZ and pgfplots. I'd like to convert everything to MetaFun. I think the best way will be writing a few more modules for things like graphs (including polar and 3D plots), simple circuits, simple Feynman diagrams, etc. I’m hoping that these would be useful to the ConTeXt community. The luageo module has the basic design I’d like to use for the others: Lua for data handling and calculations, producing paths that are drawn by MetaFun. With some mentoring from the generous ConTeXt community, I’m hoping we can provide MetaFun alternatives to some of the TikZ libraries.
next year Mikael S and I are going to look into 3D (and projections) as we can only work on math when we have a parallel metafun pet project.
I have an alpha version of luagraph by Alan Braslau, which was helpful in designing luageo. Working on luagraph is my next project.
Gavin
___________________________________________________________________________________ If your question is of interest to others as well, please add an entry to the Wiki!
maillist : ntg-context@ntg.nl / https://mailman.ntg.nl/mailman3/lists/ntg-context.ntg.nl webpage : https://www.pragma-ade.nl / https://context.aanhet.net (mirror) archive : https://github.com/contextgarden/context wiki : https://wiki.contextgarden.net ___________________________________________________________________________________
-- ----------------------------------------------------------------- Hans Hagen | PRAGMA ADE Ridderstraat 27 | 8061 GH Hasselt | The Netherlands tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl -----------------------------------------------------------------