FW: Scalebar meatpost labelling errors
Hi Thomas
that's strange, it should work at any scale. What is error cp-02 ?
Cp-02 from therion, actually the metapost complains about the tmpx in the loop that calculates the position of the intermediate text labels. It is some sort of numeric overflow. As from the wiki it only runs for me if scale = 1:1000. If I constrain the loop to cycle only twice it runs for scale =1:500, 1:1000, 1:2000, 1:5000, but crashes similarly for 1:10000. If I play around with the values that are plotted it seems that there is quite a bit of rounding error - maybe we are putting the wrong type of number into the variables, or the intermediate calculation exceeds the variable range. I agree though, on the face of it the maths looks like it should work at any scale. In a few days I could cut and paste examples of what is produced, to illustrate, if necessary (I'm supposed to be drawing cave maps). Here is the code annotated a little, you can see how I have curtailed the loop somewhat to get it to work most of the time ... Bruce layout LayoutScalebar1 # Scalebar by Thomas Holder code metapost def s_scalebar (expr l, units, txt) = % l = value of scale-bar length % units = ?? % txt = string representing units begingroup interim warningcheck:=0; tmpl:=l / Scale * cm * units / 2; % tmpl = half plotted length of scale bar from central top insertion point tmpx:=l / Scale * cm * units / 5; tmph:=5bp; % bar height endgroup; pickup PenC; draw (-tmpl,0)--(tmpl,0)--(tmpl,-tmph)--(-tmpl,-tmph)--cycle; p:=(0,0)--(tmpx,0)--(tmpx,-tmph)--(0,-tmph)--cycle; for i:=-2.5 step 2 until 2: fill p shifted (i * tmpx,0); endfor; begingroup interim labeloffset:=3.5bp; for i:=0 step (l/5) until 0.3*l: % (l-1): % 0.9*l: alternative exit criteria tmpx:=(tmpl * i * 2 / l) - tmpl; % problem is here typically on third run thru this loop, unless scale = 1:1000 % tmpx:=(tmpl * i * 2 / l) - tmpl; % added () to original line label.top(thTEX(decimal (i)),(tmpx,0)); endfor; label.top(thTEX(decimal (l) & "\thinspace" & txt),(tmpl,0)); label.bot(thTEX( "Original Scale 1 : " & decimal (Scale*100)),(0,-tmph)); endgroup; enddef; endcode endlayout LayoutScalebar1
Hi Bruce,
that's strange, it should work at any scale. What is error cp-02 ?
Cp-02 from therion, actually the metapost complains about the tmpx in the loop that calculates the position of the intermediate text labels. It is some sort of numeric overflow. As from the wiki it only runs for me if scale = 1:1000. If I constrain the loop to cycle only twice it runs for scale =1:500, 1:1000, 1:2000, 1:5000, but crashes similarly for 1:10000.
change the line to tmpx:=tmpl * (i * 2 / l - 1); works for me up to 1:30000 I'll change it in the wiki. Cheers, Thomas
Hi Thomas
change the line to
tmpx:=tmpl * (i * 2 / l - 1);
works for me up to 1:30000
Thanks! Same formula rearranged does the trick! This line of metapost does work better. The scale and scale-bar statements within the layouts work together. If the physical length of the plotted scale bar is too short or too long at the chosen scale then an error is produced (Therion reports as CP-02). With scale bar statements between 100m and 1000m in length it will work with scales approx 1:150 to 1:30000. That's plotted scalebars on the output between 35mm and 650mm long. Seems a reasonable tolerance. Bruce
participants (2)
-
Bruce -
Thomas Holder