[metapost] precision of division

Clément Hurlin Clement.Hurlin at sophia.inria.fr
Tue Oct 10 17:15:12 CEST 2006

Dear users of metapost,

  I'm not an expert but I have used metapost for some drawings and, recently, 
I've faced a problem when I try to build cycles involving points on a circle. 
You can see it on the following pdf file:

I've divided the quarter circle from p0 to p2 into six equal paths: q0 -- q1, 
q1 -- q2, ..., by doing:

  pair q[]; % calcul des points d'approximation
  for $:=0 upto i:
    q[$] = point $ * length pt[1]/i of pt[1];           (1)
(where pt[1] is the quarter circle and i is the number of parts of the 
"camembert" (in this example 6). You can see the complete, small source at:
http://www-sop.inria.fr/everest/Clement.Hurlin/figure.mp )

Later I try to fill the different parts of the camembert by doing:
  % calcul des parts
  for $:=0 upto i - 1:
    show $;
    qt[$] := q[$] -- p[3] -- q[$+1]; 
    pt[3] := pt[1] cutbefore q[$] cutafter q[$+1]; 
    show point infinity of qt[$];
    show point infinity of (pt[3]);

    show point 0 of  qt[$];
    show point 0 of (pt[3]);
    qt[$] := buildcycle(qt[$], pt[3]); 

At the beginning of each iteration qt[$] is the path made of the two radius 
and pt[3] is the subpath of the quarter-cycle that is relevant at this 
After calling buildcyle, qt[$] is a part of the camember.

My whole figure is parameterized by i (number of parts of camembert) and k 
(number of parts which I would like to fill) (in the sample pdf file, i and k 
are 6, which means I want to fill every part).

The problem is that, sometimes the buildcycle method fails to do the job (as 
you can see on the sample for the second part, which isn't filled) because, I 
think, of insufficient precision in the division (1).
I think so because the points where paths should intersect exhibit a small 
difference when it fails: (you can see values outputted by the multiple show 
commands of the last loop mentionned, when compiling the sample .mp file):

>> 0
>> (-136.87585,-36.78265)
>> (-136.87585,-36.78265)
>> (-141.73225,0)
>> (-141.73225,0)
>> 1
>> (-122.79346,-70.77658)
>> (-122.79332,-70.77682)
>> (-136.87585,-36.78265)
>> (-136.87585,-36.78265)
>> 2

You can see that the first buildcycle succeed because extremities of the two 
paths are the same.
Yet for the second one it fails, because the first coordinate of the first 
extremity are not the same: -122.79346 != -122.79332
Plus by changing i and k and the ratio of my figure, it leads to hazardous 
results (sometimes works fine, sometimes many parts of the camembert are not 
filled despite they should be).

Thus, my questions are: does it make sense and how can I fix it ?

Thanks in advance,
Clément Hurlin

More information about the metapost mailing list