Skip to content
Advertisement

Java finding minimum value from for loop

I was having some problem when trying to find the minimum distance and from the minimum distance, I then plot two circles onto the map.

                    double distance = 0;
                    double minDistance;
                    convertedHotSpotGeomList = RetrieveHotSpotAsyncTask.convertedHotSpotGeom;
                    for(int i = 0; i < convertedHotSpotGeomList.size(); i++){
                        LatLng point1 = new LatLng(convertedHotSpotGeomList.get(i).getY(), convertedHotSpotGeomList.get(i).getX());                                 
                        LatLng point2 = new LatLng(convertedHotSpotGeomList.get(++i).getY(), convertedHotSpotGeomList.get(++i).getX());
                        distance = calculateHotSpot(point1, point2);
                        Log.i("DIST", String.valueOf(distance));
                        minDistance = distance;
                        if(minDistance < distance){
                            minDistance = distance;
                            Log.i("MIN", String.valueOf(minDistance));
                             CircleOptions circleOptions = new CircleOptions()
                                .center(point1)
                                .radius(1000)
                                .fillColor(Color.argb(95, 178, 30, 37))
                                .strokeColor(Color.TRANSPARENT);

                                googleBasemap.addCircle(circleOptions);

                                CircleOptions circleOptions1= new CircleOptions()
                                .center(point2)
                                .radius(1000)
                                .fillColor(Color.argb(95, 88, 130, 37))
                                .strokeColor(Color.TRANSPARENT);

                                googleBasemap.addCircle(circleOptions1);

                        }
                    }

Currently I am getting 15, 3, 15, 14 as DIST but I could not get the minDist as it is not printed out. Any ideas?

Thanks in advance.

Advertisement

Answer

You probably want:

    distance = calculateHotSpot(point1, point2);
    ....
    if(distance < minDistance){  
        minDistance = distance;

and initialize minDistance to Double.MAX_VALUE: double minDistance = Double.MAX_VALUE

The original few lines does not make much sense, because minDistance < distance would never evaluates to true since you just set minDistance = distance;

    distance = calculateHotSpot(point1, point2);
    ....
    minDistance = distance;
    if(minDistance < distance){ // will never happen
        minDistance = distance;

Updated:

More complete solution as the point above is not the only issue:

double minDistance = Double.MAX_VALUE;
convertedHotSpotGeomList = RetrieveHotSpotAsyncTask.convertedHotSpotGeom;
LatLng[] minPoints = new LatLng[2]; // save the minimum points
for(int i = 0; i < convertedHotSpotGeomList.size(); i++){
    LatLng point1 = new LatLng(convertedHotSpotGeomList.get(i).getY(),   convertedHotSpotGeomList.get(i).getX());                                 
    LatLng point2 = new LatLng(convertedHotSpotGeomList.get(++i).getY(), convertedHotSpotGeomList.get(++i).getX());
    distance = calculateHotSpot(point1, point2);
    Log.i("DIST", String.valueOf(distance));
    if(distance < minDistance){
        minDistance = distance;
        Log.i("MIN", String.valueOf(minDistance));
        minPoints[0] = point1;
        minPoints[1] = point2;
    }
}
// we finish all the comparison, so we draw the circles now 
if(minPoints[0]!=null && minPoints[1] !=null){
    CircleOptions circleOptions = new CircleOptions()
        .center(minPoints[0])
        .radius(1000)
        .fillColor(Color.argb(95, 178, 30, 37))
        .strokeColor(Color.TRANSPARENT);
    CircleOptions circleOptions1= new CircleOptions()
        .center(minPoints[1])
        .radius(1000)
        .fillColor(Color.argb(95, 88, 130, 37))
        .strokeColor(Color.TRANSPARENT);

    googleBasemap.addCircle(circleOptions);
    googleBasemap.addCircle(circleOptions1);
}
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement