[metapost] Drawing a urn
Hartmut Henkel
hartmut_henkel at gmx.de
Thu Nov 29 22:29:16 CET 2007
On Thu, 29 Nov 2007, Hans Hagen wrote:
> you can avoid using u (units) here; also, dots are a bit more efficient (but
> the loop isn't -)
>
> vardef balls(expr nx, ny, n, l) =
> image (
> save done, m, i, j ;
> boolean done[][] ; numeric m, i, j ;
> m := round(nx * ny * n) ;
> forever :
> i := ceiling(uniformdeviate(nx)) ;
> j := ceiling(uniformdeviate(ny)) ;
> if not known done[i][j] :
> done[i][j] := true ; m := m - 1 ; exitif m = 0 ;
> fi ;
> endfor ;
> for i=1 upto nx : for j=1 upto ny :
> drawdot(i,j)
> withcolor if known done[i][j] : .5white else : .7 white fi
> withpen pencircle scaled l ;
> endfor ; endfor ;
> draw (0,ny+l) -- (0,0) -- (nx+1,0) -- (nx+1,ny+l)
> withcolor .3 white ;
> )
> enddef ;
>
> draw balls(20,10,0.57,.4) scaled 10 ;
that works nice!
only, to get rid of the "(end occurred when if on line xx was
incomplete)" warning just move the exitif behind the enclosing if. And a
"draw origin shifted (i,j) ... withpen pencircle scaled 2l;" appears to
be more efficient than the "drawdot(i,j)" as it uses PostScript's own
circles (no curveto at all in the eps file).
Regards, Hartmut
More information about the metapost
mailing list