dimanche 1 septembre 2019

Find second nearest value always return last nearest value

I want to find nearest value from my data set. The first value is returning right data, but second value is returning last nearest data. For example if i have 7, 11, 12, 15, 17, 20 and i have parameter values is 10 it should return first nearst value = 11 and second nearest value = 12. But when i try, the second value is not 12 but 20. First value will give right return but the second nearest value will always show the last nearest value, not second nearest.

Here's my code:

String closestPosition = null;

            ArrayList<Router> wifis = db.getFriendlyWifis(building);

            double min_distance = positionData.uDistance(positionsData.get(0), wifis);
            closestPosition = positionsData.get(0).getName();
            String res = "";
            res += closestPosition + "\n" + min_distance;
            res += "\n" + positionsData.get(0).toString();
            int j=0;
            for (int i = 1; i < positionsData.size(); i++) {
                double distance = positionData.uDistance(positionsData.get(i), wifis);
                res += "\n" + positionsData.get(i).getName() + "\n" + distance;
                res += "\n" + positionsData.get(i).toString();
                if (distance < min_distance) {
                    min_distance = distance;
                    closestPosition = positionsData.get(i).getName();
                    j=i;
                }

            }
            if (min_distance == PositionData.MAX_DISTANCE){
                closestPosition="OUT OF RANGE";
                Toast.makeText(this,"You are out of range of the selected building",Toast.LENGTH_LONG).show();

            }
            result.setText("Nearest point :  "+ closestPosition);
            res += "\nCurrent:\n" + positionData.toString();
            Log.v("Result",res);
            //////////////////////////////////////////////////

            min_distance = positionData.uDistance(positionsData.get(0), wifis);
            String closestPosition2 = null;

            closestPosition2 = positionsData.get(0).getName();
            res = "";
            res += closestPosition2 + "\n" + min_distance;
            res += "\n" + positionsData.get(0).toString();
            for (int i = 1; i < positionsData.size(); i=i+2) { if(j!=i){
                    double distance = positionData.uDistance(positionsData.get(i), wifis);
                    res += "\n" + positionsData.get(i).getName() + "\n" + distance;
                    res += "\n" + positionsData.get(i).toString();
                    closestPosition2 = positionsData.get(i).getName();//////////////////////////
                    if(closestPosition2.equals(closestPosition))
                        continue;
                    if (distance < min_distance) {
                        min_distance = distance;
                        closestPosition2 = positionsData.get(i).getName();

                    }
                }
            }

            if (min_distance == PositionData.MAX_DISTANCE){
                closestPosition2="OUT OF RANGE";
                Toast.makeText(this,"You are out of range of the selected building",Toast.LENGTH_LONG).show();

            }
            result2.setText("Nearest point :  "+ closestPosition2);

Here's the output:

V/closest1:: f
V/closest2:: e

here's the log:

 d
    36.0
    d(4.0,4.0)
    UGM-Secure : -63.0
    eduroam : -63.0
    UGM-Hotspot : -42.0

    a
    64.0
    a(1.0,1.0)
    UGM-Hotspot : -40.0

    f
    17.0
    f(6.0,6.0)
    UGM-Secure : -65.0
    eduroam : -66.0
    UGM-Hotspot : -46.0

    b
    25.0
    b(2.0,2.0)
    UGM-Secure : -60.0
    eduroam : -59.0
    UGM-Hotspot : -48.0

    c
    89.0
    c(3.0,3.0)
    UGM-Secure : -60.0
    eduroam : -59.0
    UGM-Hotspot : -40.0

    e
    94.0
    e(5.0,5.0)
    UGM-Secure : -65.0
    eduroam : -66.0
    UGM-Hotspot : -39.0

If the first nearest value is f with value 17, the second nearest value is whether b or d right? but why it return e that have farthest value? I'm also looking for 3rd nearest value actually, if there's also way to find 3rd nearest value it will be great.

Aucun commentaire:

Enregistrer un commentaire