class ElevationInterpolator {

    public double calculateElevationBasedOnTwoPoints(double lat, double lon, double lat0,
                                                     double lon0, double ele0, double lat1, double lon1, double ele1) {
        double dlat0 = lat0 - lat;
        double dlon0 = lon0 - lon;
        double dlat1 = lat1 - lat;
        double dlon1 = lon1 - lon;
        double l0 = Math.sqrt(dlon0 * dlon0 + dlat0 * dlat0);
        double l1 = Math.sqrt(dlon1 * dlon1 + dlat1 * dlat1);
        double l = l0 + l1;
        if (l < EPSILON) {
            // If points are too close to each other, return elevation of the
            // point which is closer;
            return l0 <= l1 ? ele0 : ele1;
        } else {
            // Otherwise do linear interpolation
            return round2(ele0 + (ele1 - ele0) * l0 / l);
        }
    }

}