POV-Ray : Newsgroups : povray.text.scene-files : Random object placement Server Time
28 Jul 2024 14:26:56 EDT (-0400)
  Random object placement (Message 1 to 1 of 1)  
From: Alf Peake
Subject: Random object placement
Date: 1 Jan 2000 17:56:41
Message: <386e8629@news.povray.org>
/*
  Demos placement of random balls without overlap.
  In response to a mushy query in pov.general by Ryan Twitchell

Alf

http://www.peake42.freeserve.co.uk
http://ourworld.compuserve.com/homepages/Alf_Peake/
*/

#version 3.1;
global_settings { assumed_gamma 1 }
#include "colors.inc"

camera{ location y*40 angle 20 look_at 0 }
light_source {<20, 100, -20> color White }

#declare R2=seed(1999);
#declare Wanted=40;  // Number of balls
#declare Store=array[Wanted]  // Stores ball positions
#declare Rad=0.5; // Size of ball
plane{ y,-Rad pigment{ checker rgb 0.3, rgb 0.7 } hollow }
// For randon sizes, #declare Kiss=(radius1+radius2) later
#declare Kiss = 2*Rad; file://Minimum separation
#declare BallPark=5; // Put 'em here
#declare Quit = 0;  // Escape route
#declare Status = Green; // Will be a green or red torus on exit

#declare Count=0;
#while (Count<Wanted)
  #declare RandAngl = rand(R2)*pi*2;  // Random angle from origin
  #declare RandDist=rand(R2)*BallPark-Rad; file://and distance
  #declare New = <sin(RandAngl)*RandDist, 0, cos(RandAngl)*RandDist>;
  #declare OK = true;
  #if (Count>0) #declare Count2=0;

     #while (Count2<Count)
        #if (abs( vlength( Store[Count2]-New)) < Kiss )
           #declare OK=false;
        #end
        #declare Count2=Count2+1;
     #end

  #end
  #if (OK=true) // Use location now, or use the array later
    sphere{ New, Rad pigment{ Yellow }
            finish{ phong 1 phong_size 40 } } // Good 'un
    #declare Store[Count]=New;   // Keep it
    #declare Count=Count+1;
  #end
  #declare Quit=Quit+1;
  #if (Quit>1500)   // Bails out if stuck
    #debug concat("Quit at Ball ", str(Count, 4, 1),"\n")
    #declare Count=Wanted; #declare Status = Red;
  #end
#end
// Shows valid area and status
torus{ BallPark+0.05,0.1 pigment{ Status } }


Post a reply to this message

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