# Recursive Functions! AAAAAAAH!

``````function pointInsideCircle() {
ranY = random(Stage.width), ranX = random(Stage.width);
cy = _ymouse, cx = _xmouse, r = Stage.width/2;
xdist = Math.round(cx - ranX), ydist = Math.round(cy - ranY);
dis = Math.round(Math.sqrt((xdist*xdist) + (ydist*ydist)));
trace("Radius: "+r+" & Distance: "+dis);
if (r < dis) {
pointInsideCircle();
} else {
xy = [ranX, ranY];
return xy;
}
}
b = pointInsideCircle();
trace(b);
``````

This function picks two random coordinates inside the Stage, it then creates an invisible line (a radius) from the cursor, half the stage width. Using pythag’s theory it checks to see if the random coords are inside the circle. We don’t need a whole circle, we just need the radius (which is all we have). If the distance between the cursor and the point is longer than the radius then it’s outside the circle. If this is the case it calls itself, repeating itself over and over until it gets some coords inside the circle.

This is all fine and dandy…well it isn’t, hence the forum post for help.

If the function get’s a point inside the circle first time then it returns:

``````Radius: 325 & Distance: 310
338,23
``````

Which is what we want, however if it takes more than one iteration, we have problems. It still finishes when it should do but xy is undefined:

``````Radius: 325 & Distance: 637
Radius: 325 & Distance: 491
Radius: 325 & Distance: 595
Radius: 325 & Distance: 259
undefined
``````

And I don’t know why it’s doing this!!!