Home > Back-end >  Unusual formatting of Vincenty Formulae in Java
Unusual formatting of Vincenty Formulae in Java

Time:09-27

I am in the process of taking over a project and noticed that, when the vincenty formulae is used, it has been written in a unusual way. This is how it's written:

    String direct(double distance, double initialBearing, double positionlat, double positionlong)  {
        // if (this.height != 0) throw new RangeError('point must be on the surface of
        // the ellipsoid');

        double φ1 = this.toRad(positionlat)/* .toRadians() */, λ1 = this.toRad(positionlong)/* .toRadians() */;
        double α1 = this.toRad(initialBearing);
        double s = distance;

        // allow alternative ellipsoid to be specified
        // double ellipsoid = /*this.datum ? this.datum.ellipsoid :*/
        // LatLonEllipsoidal.ellipsoids.WGS84;
        // const {a, b, f} = ellipsoid;
        double a = 6378137;
        double b = 6356752.314245;
        double f = 1 / 298.257223563;
        // double a = ellipsoid;
        // double b = ellipsoid;
        // double f = ellipsoid;

        double sinα1 = Math.sin(α1);
        double cosα1 = Math.cos(α1);

        double tanU1 = (1 - f) * Math.tan(φ1), cosU1 = 1 / Math.sqrt((1   tanU1 * tanU1)), sinU1 = tanU1 * cosU1;
        double σ1 = Math.atan2(tanU1, cosα1); // σ1 = angular distance on the sphere from the equator to P1
        double sinα = cosU1 * sinα1; // α = azimuth of the geodesic at the equator
        double cosSqα = 1 - sinα * sinα;
        double uSq = cosSqα * (a * a - b * b) / (b * b);
        double A = 1   uSq / 16384 * (4096   uSq * (-768   uSq * (320 - 175 * uSq)));
        double B = uSq / 1024 * (256   uSq * (-128   uSq * (74 - 47 * uSq)));

        double σ = s / (b * A);
        Double sinσ = null, cosσ = null, Δσ = null; // σ = angular distance P� P₂ on the sphere
        Double cos2σₘ = null; // σₘ = angular distance on the sphere from the equator to the midpoint of the
                                // line

        Double σʹ = null, iterations = 0d;
        do {
            cos2σₘ = Math.cos(2 * σ1   σ);
            sinσ = Math.sin(σ);
            cosσ = Math.cos(σ);
            Δσ = B * sinσ * (cos2σₘ   B / 4 * (cosσ * (-1   2 * cos2σₘ * cos2σₘ)
                    - B / 6 * cos2σₘ * (-3   4 * sinσ * sinσ) * (-3   4 * cos2σₘ * cos2σₘ)));
            σʹ = σ;
            σ = s / (b * A)   Δσ;
        } while (Math.abs(σ - σʹ) > 1e-12 &&   iterations < 100);
        if (iterations >= 100) {
            //throw new Exception("Vincenty formula failed to converge"); // not possible?
            System.err.println("Warning: Vincenty formula failed to converge!");
        }
        

        double x = sinU1 * sinσ - cosU1 * cosσ * cosα1;
        double φ2 = Math.atan2(sinU1 * cosσ   cosU1 * sinσ * cosα1, (1 - f) * Math.sqrt(sinα * sinα   x * x));
        double λ = Math.atan2(sinσ * sinα1, cosU1 * cosσ - sinU1 * sinσ * cosα1);
        double C = f / 16 * cosSqα * (4   f * (4 - 3 * cosSqα));
        double L = λ - (1 - C) * f * sinα * (σ   C * sinσ * (cos2σₘ   C * cosσ * (-1   2 * cos2σₘ * cos2σₘ)));
        double λ2 = λ1   L;

        double α2 = Math.atan2(sinα, -x);

        // const destinationPoint = new
        // LatLonEllipsoidal_Vincenty(this.toDeg(φ2)/*.toDegrees()*/,
        // this.toDeg(λ2)/*.toDegrees()*/, 0, undefined);

        return this.toDeg(φ2)   ";"   this.toDeg(λ2);/*
                                                         * { lat: this.toDeg(φ2), lng: this.toDeg(λ2)
                                                         */
        /*
         * destinationPoint.g point: destinationPoint, finalBearing:
         * Dms.wrap360(this.toDeg(α2)/*.toDegrees()
         *//*
             * ), iterations: iterations,
             */
        // };
    }

Now the IDE (eclipse) is responding, that it can't handle variable names such as e.g. φ1. Is there a elegant solution to fixing this or do I have to re-write it?

CodePudding user response:

Most probably, the source code was written in the UTF-8 encoding, and your Eclipse is configured to interpret the sources as ANSI, ISO-8859-1, cp1252 or similar.

Then, what you see as φ is in fact the two-byte UTF-8 representation of the greek character phi (φ), interpreted according to ANSI.

Configure Eclipse to expect the UTF-8 encoding (Preferences / General / Workspace / Text file encoding), then it should be able to compile.

This example shows why it is a bad idea even today to use characters outside ASCII in source code.

  • Related