[Therion] FW: Scalebar meatpost labelling errors

Bruce dangle at tomo.co.nz
Sat Feb 6 05:24:14 CET 2010


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




More information about the Therion mailing list