POV-Ray : Newsgroups : povray.general : Help with triangle dilema. Server Time
16 Nov 2024 15:16:45 EST (-0500)
  Help with triangle dilema. (Message 1 to 5 of 5)  
From: Nathan O'Brien
Subject: Help with triangle dilema.
Date: 8 Jun 1998 08:23:16
Message: <357BD7B4.6FDE6EC0@no13.net>
Hi all.

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.

-- 
regards

Nathan O'Brien
no1### [at] no13net
http://www.no13.net


Post a reply to this message

From: Jamis Buck
Subject: Re: Help with triangle dilema.
Date: 8 Jun 1998 09:37:32
Message: <357BE91C.8808AD7F@cs.byu.edu>
Hmmm.  This isn't a solution really, but maybe something to lead you to
one?  Since any triangle may be divided into two right-triangles, maybe
you could do that.  With a right triangle, its a simple matter of
trigonometry to figure how far a point is from the vertical "stem" given
the hieght along the stem, ie:
    
                  /|
                 / |
  this point->  /  | <- at this height
               /   |
              /    |
             /_____|

Then you could position the sphere randomly between the "stem" and the
hypotenuse.

Hmm.  A thought, anyway.

- Jamis Buck
  buc### [at] csbyuedu

Nathan O'Brien wrote:
> 
> Hi all.
> 
> 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.
> 
> --
> regards
> 
> Nathan O'Brien
> no1### [at] no13net
> http://www.no13.net


Post a reply to this message

From: Michael Lundahl
Subject: Re: Help with triangle dilema.
Date: 10 Jun 1998 08:53:17
Message: <6llvjt$lr1$1@oz.aussie.org>
In article <357BD7B4.6FDE6EC0@no13.net>,
	Nathan O'Brien <no1### [at] no13net> writes:
>Hi all.
>
>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.

This snippet of code should do it. Because of a bug in POV, I can't use
the .v property of 2D - vectors so the code is a bit longer then 
should be needed. Let me know how this works. 

#declare P1x = 0  //First corner of the triangle
#declare P1y = 0
#declare P2x = 1  //Second corner of the triangle
#declare P2y = 0
#declare P3x = 0  //Third corner of the triangle
#declare P3y = 1

#declare E1x = P2x-P1x
#declare E1y = P2y-P1y
#declare E2x = P3x-P2x
#declare E2y = P3y-P2y
#declare E3x = P1x-P3x
#declare E3y = P1y-P3y

#declare n = 0
#while (n < 1) //change 1 to how many spheres you want...
  
  #declare Pxx = 0.25 // add random function for spheres center, x
  #declare Pxy = 0.25 // add random function for spheres center, y

  #declare ydec1 = -1*E1y*(Pxx-P1x)+ E1x*(Pxy-P1y)
  #declare ydec2 = -1*E2y*(Pxx-P2x)+ E2x*(Pxy-P2y)
  #declare ydec3 = -1*E3y*(Pxx-P3x)+ E3x*(Pxy-P3y)
  
  #if(((ydec1 >= 0) + (ydec2 >= 0) + (ydec3 >= 0)) = 3)
    //sphere definition here
    #debug "Within bounds\n"
  #else
    #debug "Not within bounds\n"
  #end
  #declare n = n + 1
#end


Best of luck :)

/Michael


Post a reply to this message

From: Steve Vogel
Subject: Re: Help with triangle dilema.
Date: 11 Jun 1998 02:26:54
Message: <357F78AE.370@iconn.net>
Here is a file that I wrote that does it a little bit differently.  It
calculates the three vectors for the sides of the triangle.  Then it
picks a point inside the triangle by multiplyng one edge vector by a
random number between 0 and 1, finding the vector to the opposite
corner, and then multiplying that by a random number between 0 and 1. 
This always yields a point inside the triangle.  Then it calculates the
perpendicular distances to the three edges and uses the smallest
distance for the radius.

camera{
     location <4,4,-10>
     look_at <0,0,0>
     }

light_source{
     <10,7,-20>
     color rgb <1,1,1>
     }

#declare P1 = <-3,-3,0>   \\ put your three points here
#declare P2 = <0,4,0>
#declare P3 = <3,0,0>

#declare A=seed(3)

#declare vec1 = P2-P1
#declare vec2 = P3-P2
#declare vec3 = P1-P3

#declare counter = 0
#while (counter < 300)
     #declare some_center = P1 + rand(A)*((vec2 * rand(A))+vec1)
     #declare dist_vec1 = vlength(
vcross(vec1,(some_center-P2)))/vlength(vec1)
     #declare dist_vec2 = vlength(
vcross(vec2,(some_center-P3)))/vlength(vec2)
     #declare dist_vec3 = vlength(
vcross(vec3,(some_center-P1)))/vlength(vec3)
     
     sphere{
          some_center, min(dist_vec1, min(dist_vec2,dist_vec3) )
                   \\ draw a circle that touches the edge of the
triangle
          pigment{ color rgb <0,1,1>}
     }
     
     #declare counter = counter + 1
#end

triangle{
     P1, P2, P3
     pigment { color rgb <1,1,1> }
     }


Post a reply to this message

From: Chris Colefax
Subject: Re: Help with triangle dilema.
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.