[metapost] straight lines vs. curves (SVG)

Taco Hoekwater taco at elvenkind.com
Sun Apr 26 09:09:51 CEST 2009


Mojca Miklavec wrote:
> Hello Taco,
> 
> I was playing with SVG output in metapost and came accross two
> questions (both with low priority):
> 
> 1.) Simple code
>    fill unitsquare xscaled 120 yscaled 20;
> generates
>    <path d="M0 20L120 20L120 0L0 0Z" style="fill:
> rgb(0%,0%,0%);stroke: none;"></path>
> but
>     fill unitsquare xscaled 1200 yscaled 200;
> generates
>   <path d="M0 200C399.9939 200,800.0061 200,1200 200C1200
> 133.33435,1200 66.66565,1200 0C800.0061 0,399.9939 0,0 0C0 66.66565,0
> 133.33435,0 200Z" style="fill: rgb(0%,0%,0%);stroke: none;"></path>
> 
> What am I missing? Is there some cure to it?

This is a faithful rendering of the internal paths:

(0,0)..controls (39.99939,0) and (80.00061,0)
  ..(120,0)..controls (120,6.66656) and (120,13.33344)
  ..(120,20)..controls (80.00061,20) and (39.99939,20)
  ..(0,20)..controls (0,13.33344) and (0,6.66656)
  ..cycle

and

(0,0)..controls (399.9939,0) and (800.0061,0)
  ..(1200,0)..controls (1200,66.66565) and (1200,133.33435)
  ..(1200,200)..controls (800.0061,200) and (399.9939,200)
  ..(0,200)..controls (0,133.33435) and (0,66.66565)
  ..cycle

the scale factor has brought the inaccuracies in the visible
range, but they were present already. Remember that
unitsquare is scaled to 1 pt, so the original path looked
like this:

(0,0)..controls (0.33333,0) and (0.66667,0)
  ..(1,0)..controls (1,0.33333) and (1,0.66667)
  ..(1,1)..controls (0.66667,1) and (0.33333,1)
  ..(0,1)..controls (0,0.66667) and (0,0.33333)
  ..cycle

where 0.3333 is actually 21845/65536, and 0.6667  43961/66536.

This is one of the areas where megapost will solve the problem
eventually, but until then, the solution is to use paths that
have a better original size compared to the required result, or
to use an explicit right-sized path instead of xscaled/yscaled.

> 2.) Since lower bits are wrong anyway: is there a simple way to limit
> the number of digits that are written to file and thus reduce file
> size?

No. I'll consider adding something for that, but the assumption that
the lesser digits are always wrong is not necessarily true so it would
have to be used with extreme care.

Best wishes,
Taco




More information about the metapost mailing list