PDA

View Full Version : [FMX] Array Math, graphing functions



xcleetusx
December 2nd, 2003, 09:43 AM
I want to make a graph. Specifically, a graph of a normal population distribution.

I think (please correct me if I'm wrong) that the best way for me to do this is to use two arrays for coordinates and use the lineTo function to connect the dots:
- An Index array up to about 300 as the "x-axis"
- A Second array to represent the "y-axis" created using the formula for normal population applied to each entry of the index array

So I need to create these two arrays in FMX. the first array i think should be created with a For loop:
for(i=0;i<a.length;i++){}

The second array somehow applying my formula to each entry in array 1.

I believe my methods are correct, but I am having a great deal of trouble getting the AS down properly and I can't find many good sources on it. Help??

Voetsjoeba
December 2nd, 2003, 01:31 PM
Using two arrays only complicate things, use one array that contains objects with the x and y values of the points:



yourArray = [{x:250,y:150},{x:150,y:250},{x:360,y:203},and so on ...]


Then you can loop through that array and use lineTo to draw a line to the x and y values of the object at the specific arraynumber:



for(var i=0;i<yourArray.length;i++){
drawingMC.lineTo(yourArray[i].x,yourArray[i].y);
}

:)

xcleetusx
December 2nd, 2003, 02:06 PM
Thanks for the help. I was sort of working out the last kinks just as I was reading this, but for anyone else here is the AS I used, applied to an empty MC of any name:

onClipEvent (load) {

this.createEmptyMovieClip ("line_mc", 1);
indexArray = new Array ();
yAxisArray = new Array ();

for(i=-241; i<439; i++)
{
indexArray[i] = i;
yAxisArray[i] = -15000 * Math.exp ( ( -1 * Math.pow ( (indexArray[i] - 100), 2 ) ) / ( 2 * Math.pow (50,2) ) ) / ( 50 * Math.sqrt ( (2 * Math.PI) ) );
with (line_mc)
{
lineStyle (1, 0x000000, 100);
lineTo (indexArray[i], yAxisArray[i]);
}
// trace (yAxisArray[i]);
}
}

Voetsjoeba
December 2nd, 2003, 02:08 PM
Some tricky Math there ;) Good you got it working :)

pom
December 3rd, 2003, 10:31 AM
Just as a side note, it might be faster for Flash to do some of the calculations outside the loop:onClipEvent (load) {
this.createEmptyMovieClip ("line_mc", 1);
indexArray = new Array ();
yAxisArray = new Array ();

var mult = -15000 / ( 50 * Math.sqrt ( (2 * Math.PI) ) ) ;
var div = 2 * Math.pow (50,2) ;
for(i=-241; i<439; i++)
{
indexArray[i] = i;
yAxisArray[i] = mult * Math.exp ( ( -1 * Math.pow ( (indexArray[i] - 100), 2 ) ) / div );
with (line_mc)
{
lineStyle (1, 0x000000, 100);
lineTo (indexArray[i], yAxisArray[i]);
}
// trace (yAxisArray[i]);
}
}pom :)