POV-Ray : Newsgroups : povray.general : Help with triangle dilema. : Re: Help with triangle dilema. Server Time
13 Aug 2024 21:17:54 EDT (-0400)
  Re: Help with triangle dilema.  
From: Chris Colefax
Date: 10 Jun 1998 11:19:53
Message: <357EA419.4E8F7644@geocities.com>
Nathan O'Brien wrote:
> 
> I'm looking for a way to distribute spheres randomly on the surface of a
> triangle so that no sphere centre lies outside the confines of the
> triangle. The triangle may be any 3 points. The next bit is that I would
> like to do this using only POV 3.0 code. Any help is greatly
> appreciated.

You could try something like this:

   #declare _RO_tempver = version #version 3.0
   #ifndef (random_seed) #declare _RO_rand = seed(0) #else #declare _RO_rand =
seed(random_seed) #end
   #ifndef (object_size_turb) #declare object_size_turb = .5 #end
   #ifndef (object_count) #declare object_count = 10 #end

   union {
   #declare _RO_count = 0 #while (_RO_count < object_count)
      #switch (rand(_RO_rand))
      #range (0, .333)
         #declare _RO_RP1 = P1 + (rand(_RO_rand) * (P2 - P1))
         #declare _RO_RP2 = P1 + (rand(_RO_rand) * (P3 - P1)) #break
      #range (.333, .667)
         #declare _RO_RP1 = P2 + (rand(_RO_rand) * (P1 - P2))
         #declare _RO_RP2 = P2 + (rand(_RO_rand) * (P3 - P2)) #break
      #else
         #declare _RO_RP1 = P3 + (rand(_RO_rand) * (P1 - P3))
         #declare _RO_RP2 = P3 + (rand(_RO_rand) * (P2 - P3))
      #end

   object {random_object
      scale 1 + ((rand(_RO_rand) - .5) * object_size_turb * 2)
      translate _RO_RP1 + (rand(_RO_rand) * (_RO_RP2 - _RO_RP1))}

   #declare _RO_count = _RO_count + 1 #end }
   #version _RO_tempver

For example (when saved as RandObj):

   camera {location <1, 2, -5> look_at <0, 0, 0>}
   light_source {<-40, 80, -100> rgb 1}

   #declare P1 = <-2, -1, -1>
   #declare P2 = <3, 0, 0>
   #declare P3 = <1, 3, 1>

   #declare random_object = sphere {<0, 0, 0>, .3}
   #declare object_count = 100
   #declare object_size_turb = .3

   object {#include "RandObj" pigment {wrinkles
      color_map {[0 rgb <1, 0, 0>] [1 rgb <1, 1, 0>]}}
      finish {phong .5 phong_size 15}}

Of course, this is a very simple example.  You could easily add support
to automatically orient the random_object to the normal of the triangle,
and to make the number of objects used for each triangle dependant on
the surface area of the triangle.


Post a reply to this message

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