POV-Ray : Newsgroups : povray.newusers : help with macro : Re: help with macro Server Time
29 Jul 2024 06:19:56 EDT (-0400)
  Re: help with macro  
From: Warp
Date: 15 May 2006 09:42:50
Message: <4468855a@news.povray.org>
mine <nomail@nomail> wrote:
> I've made a basic macro (with the help of a tutorial)that randomly places
> objects (in this case spheres) in a given space. The trouble is the
> tutorial didn't tell me how to stop the spheres from touching. Can anyone
> help?

  This is not the fastest possible algorithm for a large number of
spheres, but it gets the job done. Note that it can end in an infinite
loop if the defined box runs out of space for any more spheres.

camera { location -z*25 look_at 0 angle 35 }
light_source { <100, 200, -300>, 1 }
light_source { <-200, 100, -100>, <.5,.3,.1> }

#declare Objects = 300;
#declare MinCoords = <-5, -5, -5>;
#declare MaxCoords = <5, 5, 5>;
#declare SphereDiameter = 1;
#declare S = seed(0);

#declare Centers = array[Objects];
#declare CoordInd = 0;
#while(CoordInd < Objects)
  #declare CoordOk = false;
  #while(!CoordOk)
    #declare Coord =
      MinCoords+(MaxCoords-MinCoords)*<rand(S), rand(S), rand(S)>;
    #declare CoordOk = true;
    #declare Ind = CoordInd-1;
    #while(Ind >= 0)
      #if(vlength(Centers[Ind]-Coord) < SphereDiameter)
        #declare CoordOk = false;
        #declare Ind = -1;
      #end
      #declare Ind = Ind-1;
    #end // #while(Ind >= 0)
  #end // #while(!CoordOk)
  #declare Centers[CoordInd] = Coord;
  #declare CoordInd = CoordInd+1;
#end // #while(CoordInd < Objects)

#declare Ind = 0;
#while(Ind < Objects)
  sphere
  { Centers[Ind], SphereDiameter/2
    pigment { rgb x }
    finish { specular .5 }
  }
  #declare Ind = Ind+1;
#end


-- 
                                                          - Warp


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.