Tree Never Stops Rendering - Too Broad Recursion

If you're having trouble using Context Free or don't understand the language, ask for help here.

Moderators: MtnViewJohn, chris, mtnviewmark

Post Reply
flembobs
Posts: 47
Joined: Tue Oct 09, 2012 3:00 pm

Tree Never Stops Rendering - Too Broad Recursion

Post by flembobs »

Here is the code I have written:

Code: Select all

startshape TREE

shape TREE
rule 
{
	CIRCLE[s 0.1 b 0.5 sat 1]
	TREE[s 0.99 y 0.05 h .7]
}
rule 3%
{
	BRANCH[]
	TREE[s 0.99 y 0.05 h .7]
}

shape BRANCH
{
	BRANCH_LEFT [ ]
	BRANCH_LEFT[ flip 90]
}
shape BRANCH_LEFT
rule {
    TREE [ s 0.9 rotate 15 ]
}
rule {
    TREE [ rotate 15 ]
}
rule {
    TREE [ rotate 20 ]
}
It produces some nice looking trees:
Image

The problem is that it never stops!

I think the cause of this is the fact that each TREE can spawn many branches, each of which is a TREE that can spawn more branches. So the tree gets very wide before it gets tall and finishes.

How can I reduce the number of branches that are spawned? I've tried messing around with the weights etc. but that produces very ugly trees.

User avatar
MtnViewJohn
Site Admin
Posts: 882
Joined: Fri May 06, 2005 2:26 pm
Location: Mountain View, California
Contact:

Re: Tree Never Stops Rendering - Too Broad Recursion

Post by MtnViewJohn »

I see two reasons why it never stops. One is easy and you have already seen it: too much branching in your recursive shapes. You can tweak the branching rule weight to fix that.

The other cause is more subtle. Your design specifies infinite recursion yet Context Free manages to stop at some point (usually). This is because Context Free silently stop recursing when shapes get too small to draw. But you are fooling this algorithm by making all of your terminal shapes (CIRCLE) be one tenth the size of your non-terminal shapes (TREE, BRANCH, etc). The pruning algorithm is based on the non-terminal shape size. So Context Free is spending enormous amounts of time evaluating TREE and BRANCH rules when the resulting CIRCLE shapes cannot possibly affect the output.

In general it is a good idea for recursive designs to have at least some terminal shapes that are close to the same size as the non-terminal shapes that are their parents.:

Code: Select all

shape TREE
rule 
{
   CIRCLE[s 1 b 0.5 sat 1]     // size changed by factor of ten
   TREE[s 0.99 y 0.5 h .7]     // displacement changed by factor of ten
}
rule 2%                        // tweak rule weight
{
   CIRCLE[s 1 b 0.5 sat 1]     // you forgot this one
   BRANCH[]
   TREE[s 0.99 y 0.5 h .7]     // displacement changed by factor of ten
}
You can also use the two-valued form of hue if you want the branch tips to not go past green to blue.

flembobs
Posts: 47
Joined: Tue Oct 09, 2012 3:00 pm

Re: Tree Never Stops Rendering - Too Broad Recursion

Post by flembobs »

Thanks for your reply!

I've tweaked the code a bit and now my trees finally stop rendering!

The leaves at the top don't look quite as dense unfortunately, but there's probably a better way to draw trees out there anyway.

Post Reply