Forums > MaxMSP

Recursive function in MAX ?

February 8, 2012 | 3:32 pm

Hi, I’m trying to translate this Processing code in Max but I can’t understand how create recursive function in Max.
I tried to create an abstract that call itself but I got a Max error "That is not permitted".
Can someone show me the right way?
Do I have to use jit.linden ?
Thanks

Processing code to translate:

float theta;

void setup() {
size(640, 360);
}

void draw() {
background(0);
frameRate(30);
stroke(255);
// Let’s pick an angle 0 to 90 degrees based on the mouse position
float a = (mouseX / (float) width) * 90f;
// Convert it to radians
theta = radians(a);
// Start the tree from the bottom of the screen
translate(width/2,height);
// Draw a line 120 pixels
line(0,0,0,-120);
// Move to the end of that line
translate(0,-120);
// Start the recursive branching!
branch(120);

}

void branch(float h) {
// Each branch will be 2/3rds the size of the previous one
h *= 0.66;

// All recursive functions must have an exit condition!!!!
// Here, ours is when the length of the branch is 2 pixels or less
if (h > 2) {
pushMatrix(); // Save the current state of transformation (i.e. where are we now)
rotate(theta); // Rotate by theta
line(0, 0, 0, -h); // Draw the branch
translate(0, -h); // Move to the end of the branch
branch(h); // Ok, now call myself to draw two new branches!!
popMatrix(); // Whenever we get back here, we "pop" in order to restore the previous matrix state

// Repeat the same thing, only branch off to the "left" this time!
pushMatrix();
rotate(-theta);
line(0, 0, 0, -h);
translate(0, -h);
branch(h);
popMatrix();
}
}

Attachments:
  1. branch.maxpat

February 10, 2012 | 9:21 am

I try to simplify the question:
this is the recursive function I’m tryig to translate in Max, the problem is the recursion.

void branch(float h) {
h *= 0.66;

if (h > 2) {
pushMatrix();
rotate(theta);
line(0, 0, 0, -h);
translate(0, -h);
branch(h);//////////////////////HERE!!!!
popMatrix();

pushMatrix();
rotate(-theta);
line(0, 0, 0, -h);
translate(0, -h);
branch(h);//////////////////////HERE!!!!
popMatrix();
}

You can see that inside branch() there are two call to itself branch().


February 10, 2012 | 11:09 am

I dont think max really lends itself to recursive structures, but I think there is a principle in computer science which says that any recursive function can also be expressed as a simple iteration (ie a for-loop), so you could possible rewrite the function non-recursively (in pseudocode), then it would be easier to translate into a max patch.

You might be able to implement it using recursion with Mattijs Kneppers’ oo objects http://cycling74.com/toolbox/kneppers-oo-objects/

but having never used them myself, you’d need to check it out to see if it’s actually possible.

you’re may better off overall trying to implement it using java within max

hth


February 10, 2012 | 12:05 pm

A recursive function can be expressed as a simple iteration only if the recursive call is the last operation of the function ("tail recursion"). Otherwise you need an additional stack for handling the local data. This can be done in Max for example with [zl stack] but seems rather limited and inconvenient.


February 10, 2012 | 1:22 pm

This can be very easy to do using java inside Mxj. Just learn a bit of the java API for interfacing with max and then you can do as many resursions you want !!


February 10, 2012 | 2:54 pm

recursion — lisp!

http://music.columbia.edu/~brad/maxlispj/

brad

http://music.columbia.edu/~brad/


February 10, 2012 | 3:23 pm

And obligatory Lisp joke:

http://xkcd.com/297/


February 10, 2012 | 3:55 pm

I love it!

brad

http://music.columbia.edu/~brad/


February 11, 2012 | 1:41 am

one way to do a "while" loop. interesting to experiment with [defer] and [deferlow] too…

– Pasted Max Patch, click to expand. –

February 11, 2012 | 11:38 am

Thanks to all maybe mxj or js is the faster way for tree generation.


February 13, 2012 | 12:52 am

actually you can do this kind of recursion with native max objects/messaging.
The only difference to the processing code is, that you have to carry over all 4 parameters (x,y,h,theta) to the next recursion.

– Pasted Max Patch, click to expand. –

February 13, 2012 | 7:44 am

awesome.


Viewing 12 posts - 1 through 12 (of 12 total)