[metapost] Drawing a urn

Hans Hagen pragma at wxs.nl
Thu Nov 29 21:05:25 CET 2007


Hartmut Henkel wrote:
> On Thu, 29 Nov 2007, Troy Henderson wrote:
> 
>>> yes, which leads to other implementations: put exactly as many black
>>> balls in as needed, but place them randomly (avoiding collisions),
>>> or e. g. put balls first in regularly and then stir them up randomly
>>> (e.g. repeatedly exchanging balls between two random positions.
>> Is that what your posted code does?
> 
> no, but Dan's does it, admirably (modulo typos :-)

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 ;


-----------------------------------------------------------------
                                           Hans Hagen | PRAGMA ADE
               Ridderstraat 27 | 8061 GH Hasselt | The Netherlands
      tel: 038 477 53 69 | fax: 038 477 53 74 | www.pragma-ade.com
                                              | www.pragma-pod.nl
-----------------------------------------------------------------


More information about the metapost mailing list