Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript. Tricky: Are all array values consistent? (ie: does a = [a,a,a,a]) for any size array

This is rather conceptual, and I'm hoping someone can help. I have a script, that when you insert coordinates (x,y, from 0 -> 800) in to it, it'll return lists of pre-plotted coordinates that are within 3 separate ranges.

ie: I enter 200,200. I receive a list for each of the following: Plots that are within a radius of 25, a radius of 60, a radius of 150.

The code if you're curious / for context:

        display(coords[n][1] + ", " + coords[n][2]);

    if(n==0){

    for(var i=0; i < data.length; i++) {
        var xs = 0;
        var xy = 0;
        xs = xPlot - data[i][0];
        ys = yPlot - data[i][1];
        xs = xs * xs;
        ys = ys * ys;

        distance = Math.sqrt(xs + ys);

        if (distance <= 25){
            display2(data[i][0] + ", " + data[i][1] + " - " + alliance);
        }
        else if(distance <= 60){
            display3(data[i][0] + ", " + data[i][1] + " - " + alliance);
        }
        else if(distance <= 150){
            display4(data[i][0] + ", " + data[i][1] + " - " + alliance);
        }
    }

Easy enough.

Now, when I input another set of coordinates another multidimensional array is created, I want to check for intersecting points within the two (well, 6) circles.

I thought of using an array for my checking. If 2 coords are entered, and a point is found within both inner (green) circles, a = [g,g]. If a point is found within an inner of one, but a mid (blue) of the other, set a = [g,b] You get the idea. Red being the outer circle.

When 3 coords are entered things get trickier. Let's say a point is within two inners, and one mid. Then a = [g,g,b]. The purpose of the 3 lists, is to organize the returned values in groups of best to worst. So in an example with 4 inputs, the lists are to be organised in the following way:

List 1 / List 2 / List 3
gggg / gggb / bbbr
------- / ggbb / bbrr
------- / gbbb / brrr
------- / bbbb / rrrr

How would I build check my array in a scalable way so if I had 5 inputs, I'd be able to place my results in the appropriate columns?

I've started with this sofar:

else if(n>0){

    for(var i=0; i < data.length; i++) {
        for(var j=0; j < coords.length; j++) {
            var xs = 0;
            var ys = 0;
            xs = coords[j][1] - data[i][0];
            ys = coords[j][2] - data[i][1];
            xs = xs * xs;
            ys = ys * ys;
            distance = Math.sqrt(xs + ys);

            if (distance <= 25){
                a[j] = [,[g]];
                }
            else if (distance <= 60){
                a[j] = [,[b]];
                }
            else if (distance <= 150){
                a[j] = [,[r]];
                }
        }
        if($.inArray('g', a) && (!($.inArray('b', a)))){
            display2(data[i][0] + ", " + data[i][1] + " - " + alliance);

}

Would this last line execute if a contained g, but NOT b?? (jquery) Am I on the right track?

like image 875
lunchtime Avatar asked Apr 13 '26 09:04

lunchtime


1 Answers

Instead of putting the occurance inside a circle as an item in an array, count the number of occurances in an array with the same length as the number of circles. I.e. instead of [g,g,g,g,b,b,r,r,r,r,r,r,r], use [4,2,7]. That way you don't have to keep the array sorted when adding items, you just increment the correct item.

That also makes it easy to sort the result on relevance. You can create a numeric value for the array by looking at it as a number with the base being the same as the number of data coordinates. I.e.:

var n = arr[0] * data.length * data.length + arr[1] * data.length + arr[2];

With 10 data coordinates, the array [4,2,7] would get the value 427, making it easy to compare it to the array [3,5,0] that would get the value 350.

You can calculate the arrays as:

for(var i=0; i < coords.length; i++) {
  var arr = [0, 0, 0];
  for(var j=0; j < data.length; j++) {
    var xs = coords[i][1] - data[j][0];
    var ys = coords[i][2] - data[j][1];
    distance = Math.sqrt(xs * xs + ys * ys);
    if (distance <= 25) {
      arr[0]++;
    } else if (distance <= 60) {
      arr[1]++;
    } else if (distance <= 150) {
      arr[2]++;
    }
  }
  coords[i].arr = arr;
  coords[i].value = arr[0] * data.length * data.length + arr[1] * data.length + arr[2];
}

Now you can sort the coordinates on the values:

coords.sort(function(a, b){
  return a.value - b.value;
});
like image 82
Guffa Avatar answered Apr 14 '26 23:04

Guffa



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!