[metapost] precision of division
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/i of pt; (1)
(where pt 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:
Later I try to fill the different parts of the camembert by doing:
% calcul des parts
for $:=0 upto i - 1:
qt[$] := q[$] -- p -- q[$+1];
pt := pt cutbefore q[$] cutafter q[$+1];
show point infinity of qt[$];
show point infinity of (pt);
show point 0 of qt[$];
show point 0 of (pt);
qt[$] := buildcycle(qt[$], pt);
At the beginning of each iteration qt[$] is the path made of the two radius
and pt 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):
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,
More information about the metapost