Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Simple Math Problem acos / asin?

Status
Not open for further replies.

danielhai

Programmer
Oct 23, 2001
25
US
Using, Flash MX, creating a randomly generated tree. Seems like my math is just funky. it's creating most of the tree fine, but on some degrees, the branches don't get created right. You can view the fla file here: .

You'll see that some of the branches get created by the default branch. Any help here? I'm terrible @ math.

here's the code (all in first frame):

stop();
this.onEnterFrame = null;
Math.toRadians = function (theta) {
return theta * Math.PI / 180;
}

function createBranch(x, y, offsetrot, level) {
defBranch.duplicateMovieClip('branch' + numBranches, numBranches)
obj = this['branch' + numBranches]
obj._x = x;
obj._y = y;
obj._xscale = 5;
obj._yscale = 5;
obj.steps = 3 + random(5);
obj._rotation = offsetrot + (random(50) - 25);

this['branch' + numBranches].onEnterFrame = function() {
if (this.steps > 0) {
var grownum = random(4) + 1;
this._xscale += grownum;
this._yscale += grownum;
this.steps--;
} else if (this.steps == 0 && level < 12) {
var height = Math.acos(Math.toRadians(this._rotation)) * this._yscale;
var width = Math.asin(Math.toRadians(this._rotation)) * this._xscale;

this.steps--;
if (random(4) > 2)
createBranch(this._x+width,this._y - this._height, this._rotation, level+1);

createBranch(this._x+width,this._y - this._height, this._rotation, level+1);
}
}

numBranches++;
}

var numBranches = 0;

for (var count=0; count<5; count++) {
createBranch(200 + (count * 50),250,0,0);
}
 
The problem is that some of the values being produced by the trigonometrical expressions are evaluating out as 'NaN'. This means that the x parameters being fed to the createBranches function are effectively zero and the branch is being positioned at the zero point (beside the default branch graphic).

You could add an extra condition that traps NaN values and sidesteps the branch function or creates a new valid numerical value.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top