Math question, with arrays and stuff

I have a series of variables created that store the distances between two points. I need to find the shortest distance between the two points so I’m storing the results in an array, then sorting the array to show me the smallest number which is the shortest distance.

But the problem I have is that I don’t need the number I need the variable in the calculation that relates to that number. Make any sense?

So if the equation (herexPos - column3Begin) produces the smallest number I need to be able to reference column3Begin after I’ve sorted the array.

Any ideas on how to do this or how to approach this? Help!

var SecondPointOne:Number = (Math.abs(herexPos - column4xBegin));
var SecondPointTwo:Number = (Math.abs(herexPos - column3xBegin));
var SecondPointThree:Number = (Math.abs(herexPos - column2xBegin));
var SecondPointFour:Number = (Math.abs(herexPos - column1xBegin));
var SecondPointFive:Number = (Math.abs(hereyPos - column4yEnd));
var SecondPointSix:Number = (Math.abs(hereyPos - column3yEnd));
var SecondPointSeven:Number = (Math.abs(hereyPos - column2yEnd));
var SecondPointEight:Number = (Math.abs(hereyPos - column1yEnd));
var SecondPointNine:Number = (Math.abs(hereyPos - column1yBegin));
//trace(SecondPointFour);

var my_array:Array = new Array(SecondPointThree, SecondPointTwo, SecondPointOne, SecondPointFour, SecondPointFive, SecondPointSix, SecondPointSeven, SecondPointEight, SecondPointNine);

//trace(my_array);
//my_array.sort(Array.DESCENDING | 16);
trace(my_array);
my_array.sort(Array | 16);
trace(my_array[0]);
var SecondPointOne:Object = {num:Math.abs(herexPos - column4xBegin), obj:"column4xBegin"};
var SecondPointTwo:Object = {num:Math.abs(herexPos - column3xBegin), obj:"column3xBegin"};
var SecondPointThree:Object = {num:Math.abs(herexPos - column2xBegin), obj:"column2xBegin"};
var SecondPointFour:Object = {num:Math.abs(herexPos - column1xBegin), obj:"column1xBegin"};
var SecondPointFive:Object = {num:Math.abs(hereyPos - column4yEnd), obj:"column4yEnd"};
var SecondPointSix:Object = {num:Math.abs(hereyPos - column3yEnd), obj:"column3yEnd"};
var SecondPointSeven:Object = {num:Math.abs(hereyPos - column2yEnd), obj:"column2yEnd"};
var SecondPointEight:Object = {num:Math.abs(hereyPos - column1yEnd), obj:"column1yEnd"};
var SecondPointNine:Object = {num:Math.abs(hereyPos - column1yBegin), obj:"column1yBegin"};
var my_array:Array = new Array(SecondPointThree, SecondPointTwo, SecondPointOne, SecondPointFour, SecondPointFive, SecondPointSix, SecondPointSeven, SecondPointEight, SecondPointNine);
my_array.sortOn("num", Array.NUMERIC);
trace(my_array[0].obj);

TheCanadian! That is a HUGE help! Thank you ever so much. :thumb:

I’m one small step from wrapping this thing up. This last part isn’t working and I’m not sure why.

I need to take the value that I’m getting in my_array[0].obj and store it in a variable, in this example the variable is called MidX, it’s the midpoint of the line I’m drawing that connects two locations. So I have an array that’s using variables to define the 4 points of this line, as you can see in the code below in bold. For some reason the drawing array isn’t recognizing the variable MidX. But when I substitute the contents of the variable, for example column1xBegin, it works. And when I trace MidX it shows that it’s contents is column1xBegin. So for some reason it isn’t being passed to the drawing array.

Any idea why?


my_array.sortOn("num", Array.NUMERIC);

var MidX:Number;
MidX = my_array[0].obj;
trace(MidX);
                      
createEmptyMovieClip('linepath',0);
linepath.time = 0;
linepath.speed = .020;
linepath.style = [3, 0xff0000, 100];
linepath.anchors = [
  ** [herexPos,hereyPos],
   [MidX,hereyPos],
   [MidX,crosshairY],
   [crosshairX,crosshairY]**
];

Thanks again for your help!

Okay, I see the problem just not the solution.

Doing this:

var SecondPointNine:Object = {num:Math.abs(hereyPos - column1yBegin), obj:"column1yBegin"};

creates an object called column1yBegin. So in the drawing Array it isn’t see the variable I created called column1yBegin which is being subtracted from hereYPos, it is seeing the new Object with the same name.

I have an Y position defined in an XML document for column1yBegin and that’s what I’m trying to reference.

So in the Drawing Array(linepath.anchors) how can I use the Object column1yBegin to reference the variable of the same name.

I didnt go trough all posts but

var SecondPointNine:Object = {num:Math.abs(hereyPos - column1yBegin), obj:"column1yBegin"};

does NOT create an object called column1yBegin.

It creates and object called SecondPointNine with an attribute obj which contains string “column1yBegin”.

That means that trace(column1yBegin) would output nothing but trace(SecondPointNine.obj) will output column1yBegin and trace(SecondPointNine.num) will output some number.

Hmm. . .okay. I see your point.

I have a variable that I’ve already created called column1yBegin that is being subtracted from herexPos. Then I’m sorting the differences between these equations in the array to find the smallest number with my_array[0].obj. This returns column1yBegin, the attribute of the object. But what I need is to convert the attribute columny1yBegin to the variable column1yBegin so I can reference this variable in my drawing array.

Does that make any sense?

I think I know what you mean… you got a string and you need to use it as a reference to a variable… then try out this[my_array[0].obj]

is that different than my_array[0].obj? does putting it in brackets give it a different meaning?

By the way, thanks so much for the feedback.

What I need is for the linepath anchors below:

linepath.anchors = [
   [herexPos,hereyPos],
  ** [my_array[0].obj,hereyPos],
   [my_array[0].obj,crosshairY],**
   [crosshairX,crosshairY]
];

If I put my_array[0].obj inside that linepath array then it is referencing the object attribute column1yBegin and NOT the variable column1yBegin. The variable column1yBegin references a Y position in the XML, that’s what I need.

OK… if you try trace(typeof my_array[0].obj) Im sure it will output “string”… and there is a difference between string and variable… Let me put together a little example where I could explain it.

// we have a variable called aaa
aaa = "Hello";
// and a variable called bbb
bbb = "aaa";
// we want to use bbb to reveal us contents of aaa
// if we look into aaa straight it works
trace(aaa); // outputs: Hello
// but this doesnt
trace(bbb); // outputs: aaa
// we need to use content of bbb as a name of a variable
// just like this
trace(this[bbb])
// hooray we now have "Hello" in the output panel

Do you see where Im going?

my_array[0].obj contains a string just like bbb and that string is same as a name of a variable which you are trying to reach to use its value… then this[my_array[0].obj] should give you what you want…

I dont know the name of the this[something] or _root[something] … I think they are referencial arrays… it is to replace a deprecated function eval() and you can use it to get a reference to any object.

Actually it outputs “object”. Is your example still applicable? I’m not sure. Trying to understand it and implement it. . .

Thats strange… I get string… Do you use TheCanadians script? If you do, tehn it souldnt act like that… oh I got it… use his script and simply remove those quotes… intead of

var SecondPointOne:Object = {num:Math.abs(herexPos - column4xBegin), obj:"column4xBegin"};

use

var SecondPointOne:Object = {num:Math.abs(herexPos - column4xBegin), obj:column4xBegin};

now you can use my_array[0].obj as it is… I think :slight_smile:

Aha! I think I’ve got it. I need to play with it some more but my eyes are killing me so I think I’m going to call it a day.

Thanks so much for your help! I appreciate it(as does my boss). :thumb:

I didn’t realize that you wanted the value of the variable instead of the name. Matthew’s code from post 22 will do what you want I think.

And for the record, eval is not deprecated - array referencing is just another way to, well, reference objects.

It’s called Subscript Notation, and is just another notation :stuck_out_tongue: hehe

I’d be interested in seeing how you implement these objects because honestly, I don’t see the point of having the objects (unless you are using them over and over again)… because if you think about it… subtraction is one of the quickest operators, and Math.abs(); is very fast as well. The way that abs is implemented in programming is simple… all numbers are represented, eventually, on a binary level… where signed numbers (Actionscript’s implementation of the Number) is a binary number who’s left most bits represent the mathematical sign, when a call to abs is made you are simply setting those bits to 0, and we all know that modifying bits is something the computer can do very quickly.

Now all of this efficiency is killed when you starting setting these values to objects. Is it possible that you could manipulate these values directly within the function?

I am not reading the entire post as I should, I just saw the implementation and figured it’d be a good chance to bring this up so that others may know it.

TakeCare
_Michael