POV-Ray : Newsgroups : povray.general : I am having a lot of problems writting a recursive function Server Time
6 Nov 2024 04:17:15 EST (-0500)
  I am having a lot of problems writting a recursive function (Message 1 to 6 of 6)  
From: Jay D  Patteson
Subject: I am having a lot of problems writting a recursive function
Date: 24 Aug 2004 21:40:00
Message: <web.412bed2c6402a89bb46190930@news.povray.org>
I have been working on a fractal tree.  I know that there is probably tons
and tons of source code to pick from, but I wanted to figure it out myself.
 I have "successfully done it"  But I couldn't figure out the recursive
fuctions.

I wound up having to have a macro call the second macro which called the
third, etc until 16 or so.  So I wrote 16 macros, when I bet I could have
done it with a simple recursive macro.  :(

Could anyone help me figure out how to change this:

#version 3.5;
#include "colors.inc"
#include "math.inc"

#declare R1 = seed(0);

global_settings {
  assumed_gamma 1.0
}

camera {
  location  <-40.0, 10, -0.0>

  look_at   <0.0, 10,  0.0>
}
sky_sphere {
  pigment {
    gradient y
    color_map {
      [0.0 rgb <0.6,0.7,1.0>]
      [0.7 rgb <0.0,0.1,0.8>]
    }
  }
}
light_source {
  <-50, 100, -100>
  color rgb <1, 1, 1>

}



#macro leave(aty,rotz, scl)

cylinder { <0, 0, 0>, <0, 10, 0>, 1

pigment{ rgb <0,1,0>}

scale <scl*4,scl*2,scl/3>
rotate <0,0,rotz>
translate <0,aty,0>

}
#end

#macro tree(aty,rotz, scl)
union {
cylinder { <0, 0, 0>, <0, 8, 0>, 1 }
sphere {<0,8,0>,1.2}

texture {
  pigment {
    wood
    color_map {
      [0.1 color red .5]
      [0.5 color red .2]
    }
    turbulence 0.5
    scale <1,3,1>
  }
  normal {
    wood 0.3
    turbulence 0.5
    scale <1,3,1>
  }
  finish {

    specular 0.6
  }
}


scale <scl,scl,scl>
rotate <0,0,rotz>
translate <0,aty,0>

}
#end

#macro tree2(aty,rotz, scl)
union {
tree(0,0,1)
leave(8,atan2d(4,3),.75)
leave(7,-atan2d(3,4),.75)
scale <scl,scl,scl>
rotate <0,72,rotz>
translate <0,aty,0>
}
#end

#macro tree3(aty,rotz, scl)
union {
tree(0,0,1)
tree2(8,atan2d(4,3),.75)
tree2(7,-atan2d(3,4),.75)
scale <scl,scl,scl>
rotate <0,72,rotz>
translate <0,aty,0>
}
#end

#macro tree4(aty,rotz, scl)
union {
tree(0,0,1)
tree3(8,atan2d(4,3),.75)
tree3(7,-atan2d(3,4),.75)
scale <scl,scl,scl>
rotate <0,72,rotz>
translate <0,aty,0>
}
#end

#macro tree5(aty,rotz, scl)
union {
tree(0,0,1)
tree4(8,atan2d(4,3),.75)
tree4(7,-atan2d(3,4),.75)
scale <scl,scl,scl>
rotate <0,72,rotz>
translate <0,aty,0>
}
#end

#macro tree6(aty,rotz, scl)
union {
tree(0,0,1)
tree5(8,atan2d(4,3),.75)
tree5(7,-atan2d(3,4),.75)
scale <scl,scl,scl>
rotate <0,72,rotz>
translate <0,aty,0>
}
#end

#macro tree7(aty,rotz, scl)
union {
tree(0,0,1)
tree6(8,atan2d(4,3),.75)
tree6(7,-atan2d(3,4),.75)
scale <scl,scl,scl>
rotate <0,72,rotz>
translate <0,aty,0>
}
#end

#macro tree8(aty,rotz, scl)
union {
tree(0,0,1)
tree7(8,atan2d(4,3),.75)
tree7(7,-atan2d(3,4),.75)
scale <scl,scl,scl>
rotate <0,72,rotz>
translate <0,aty,0>
}
#end
#macro tree9(aty,rotz, scl)
union {
tree(0,0,1)
tree8(8,atan2d(4,3),.75)
tree8(7,-atan2d(3,4),.75)
scale <scl,scl,scl>
rotate <0,72,rotz>
translate <0,aty,0>
}
#end
#macro tree10(aty,rotz, scl)
union {
tree(0,0,1)
tree9(8,atan2d(4,3),.75)
tree9(7,-atan2d(3,4),.75)
scale <scl,scl,scl>
rotate <0,72,rotz>
translate <0,aty,0>
}
#end
#macro tree11(aty,rotz, scl)
union {
tree(0,0,1)
tree10(8,atan2d(4,3),.75)
tree10(7,-atan2d(3,4),.75)
scale <scl,scl,scl>
rotate <0,72,rotz>
translate <0,aty,0>
}
#end
#macro tree12(aty,rotz, scl)
union {
tree(0,0,1)
tree11(8,atan2d(4,3),.75)
tree11(7,-atan2d(3,4),.75)
scale <scl,scl,scl>
rotate <0,72,rotz>
translate <0,aty,0>
}
#end
#macro tree13(aty,rotz, scl)
union {
tree(0,0,1)
tree12(8,atan2d(4,3),.75)
tree12(7,-atan2d(3,4),.75)
scale <scl,scl,scl>
rotate <0,72,rotz>
translate <0,aty,0>
}
#end
#macro tree14(aty,rotz, scl)
union {
tree(0,0,1)
tree13(8,atan2d(4,3),.75)
tree13(7,-atan2d(3,4),.75)
scale <scl,scl,scl>
rotate <0,72,rotz>
translate <0,aty,0>
}
#end
#macro tree15(aty,rotz, scl)
union {
tree(0,0,1)
tree14(8,atan2d(4,3),.75)
tree14(7,-atan2d(3,4),.75)
scale <scl,scl,scl>
rotate <0,72,rotz>
translate <0,aty,0>
}
#end
#macro tree16(aty,rotz, scl)
union {
tree(0,0,1)
tree15(8,atan2d(4,3),.75)
tree15(7,-atan2d(3,4),.75)
scale <scl,scl,scl>
rotate <0,72,rotz>
translate <0,aty,0>
}
#end



// call the tree here!

tree16(0,0,1)





plane {
  y, 0
texture
{
  pigment
  {
    dents
    color_map {[0.1 color green .7] [0.5 color green .5]}
    turbulence 0.5
    scale <5,1,5>
  }

  normal
  {
    marble
    turbulence 1
    scale <1,1,1>
  }
}
}


Post a reply to this message

From: Slime
Subject: Re: I am having a lot of problems writting a recursive function
Date: 24 Aug 2004 22:28:00
Message: <412bf930$1@news.povray.org>
> I wound up having to have a macro call the second macro which called the
> third, etc until 16 or so.  So I wrote 16 macros, when I bet I could have
> done it with a simple recursive macro.  :(


Eek!

It looks like all of your macros are pretty much identical. All you need to
do is keep track of how many calls "deep" you are. You can do that with an
extra parameter, like this:

(I renamed "treeX" to "buildtree" so that it doesn't conflict with your
"tree" macro which actually makes an individual branch.)

#macro buildtree(aty,rotz, scl,  depth)
 union {
 tree(0,0,1)
 #if (depth = 15)
  leave(8,atan2d(4,3),.75)
  leave(7,-atan2d(3,4),.75)
 #else
  buildtree(8,atan2d(4,3),.75, depth+1) // depth increases with each call
  buildtree(7,-atan2d(3,4),.75, depth+1)
 #end
 scale <scl,scl,scl>
 rotate <0,72,rotz>
 translate <0,aty,0>
}

buildtree(0,0,1, 0 /* initial depth of zero */)

By the way, "leave" should probably be "leaf"; the v is only used for the
plural ("leaves").

 - Slime
 [ http://www.slimeland.com/ ]


Post a reply to this message

From: Jay D  Patteson
Subject: Re: I am having a lot of problems writting a recursive function
Date: 24 Aug 2004 22:40:00
Message: <web.412bfb3778ba3b00b46190930@news.povray.org>
Thanks man, I knew I needed and extra exit parameter, but I was having a
terrible time working it all out.   Right now I am working on the river in
front of the tree, so I will try that recursive thing in a little bit!
Thank you so much!  It's been many years since I took a programming class.
lol

BTW, the last interation is called leave, and will remain so, because it's a
pun on leaving, since it's the last iteration.  I was trying to make a pun
on leafs and leave.  ;)  (i guess it wasn't as funny as i had hoped.  )

-Jay


Post a reply to this message

From: Rafal 'Raf256' Maj
Subject: Re: I am having a lot of problems writting a recursive function
Date: 26 Aug 2004 08:28:47
Message: <Xns955193AFF722Draf256com@203.29.75.35>
ipi### [at] maccom news:web.412bed2c6402a89bb46190930@news.povray.org

> I have been working on a fractal tree.  I know that there is probably
> tons and tons of source code to pick from, but I wanted to figure it
> out myself. 
>  I have "successfully done it"  But I couldn't figure out the
>  recursive 
> fuctions.

A very simple but quite nice example from my SCC3 entry,
http://www.raf256.com/3d/image/a00119/

there where also few simmilar entries AFAIR, You might want to look through 
http://astronomy.swin.edu.au/~pbourke/povray/scc3/


-- 
http://www.raf256.com/3d/
Rafal Maj 'Raf256', home page - http://www.raf256.com/me/
Computer Graphics


Post a reply to this message

From: Jay D  Patteson
Subject: Re: I am having a lot of problems writting a recursive function
Date: 26 Aug 2004 22:50:00
Message: <web.412ea08078ba3b0085917ae90@news.povray.org>
> A very simple but quite nice example from my SCC3 entry,
> http://www.raf256.com/3d/image/a00119/
>
> there where also few simmilar entries AFAIR, You might want to look through
> http://astronomy.swin.edu.au/~pbourke/povray/scc3/

Thanks Rafal, but I am still very much a newbie at POVRay... and I don't
have a clue what your source code is doing. (even though I am rendering it
as we speak) I got it all worked out with the last guy's help.  Thanks,
anyways.

I do have an additional question though:

I was a math major in college for many years, which is where I first learned
about fractal trees and such.  Granted that the fractal tree that I
generated has two limbs rotated about the axis of the last limb at 72
degrees, and one limb at arctan(4/3) and the other at arctan(3/4), 1 unit
higher, it is beautiful.  But it looks very mathematical.  So I tried to
induce some randomness in to it.  I have played with random rotations,
random branch heights, and random branch angles.  Nothing looks as real as
the generic one with nothing random at all.  Freakish!.. I don't get it.  I
thought that a true tree would be much more random.  Or perhaps, it is the
simmetry of the regular fractal that appeals to me...  has anyone else
noticed this?

Thanks for your time,
-Jay


Post a reply to this message

From: Nicolas Alvarez
Subject: Re: I am having a lot of problems writting a recursive function
Date: 1 Sep 2004 20:06:14
Message: <413663f6@news.povray.org>

news:412bf930$1@news.povray.org...
>
> #macro buildtree(aty,rotz, scl,  depth)
>  union {
>  tree(0,0,1)
>  #if (depth = 15)
>   leave(8,atan2d(4,3),.75)
>   leave(7,-atan2d(3,4),.75)
>  #else
>   buildtree(8,atan2d(4,3),.75, depth+1) // depth increases with each call
>   buildtree(7,-atan2d(3,4),.75, depth+1)
>  #end
>  scale <scl,scl,scl>
>  rotate <0,72,rotz>
>  translate <0,aty,0>
> }
>
> buildtree(0,0,1, 0 /* initial depth of zero */)
>

There is a better way to do it:

 #macro buildtree(aty,rotz, scl,  depth)
  union {
  tree(0,0,1)
  #if (depth = 16)
   cylinder { <0, 0, 0>, <0, 10, 0>, 1
    pigment{ rgb <0,1,0>}

    scale <scl*4,scl*2,scl/3>
    rotate <0,0,rotz>
    translate <0,aty,0>
   }
  #else
   buildtree(8,atan2d(4,3),.75, depth+1) // depth increases with each call
   buildtree(7,-atan2d(3,4),.75, depth+1)
  #end
  scale <scl,scl,scl>
  rotate <0,72,rotz>
  translate <0,aty,0>
 }

--
- Nicolas Alvarez
nicoalvar0 [at] hotmail [dot] com


Post a reply to this message

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