Example usage for java.lang Math atan2

List of usage examples for java.lang Math atan2

Introduction

In this page you can find the example usage for java.lang Math atan2.

Prototype

@HotSpotIntrinsicCandidate
public static double atan2(double y, double x) 

Source Link

Document

Returns the angle theta from the conversion of rectangular coordinates ( x ,  y ) to polar coordinates (r, theta).

Usage

From source file:org.esa.nest.dat.views.polarview.PolarCanvas.java

public double[] getRTheta(Point oP) {
    final Point p = new Point(oP);
    p.y = origin.y - p.y;//from w w  w .  j av a2s .  c o m
    p.x -= origin.x;
    if (Math.abs(p.y) > plotRadius || Math.abs(p.x) > plotRadius) {
        return null;
    } else {
        final int r = (int) Math.sqrt(p.x * p.x + p.y * p.y);
        final double rV = data.valueFromScreenPoint(r);
        return new double[] { rV, (360D - (Math.atan2(p.x, p.y) * 180D) / Constants.PI) % 360D };
    }
}

From source file:qupath.lib.algorithms.color.EstimateStainVectors.java

/**
 * /* w  w w.jav  a 2  s. co m*/
 * Check colors only currently applies to H&E.
 * 
 * @param rgbPacked
 * @param redOD
 * @param greenOD
 * @param blueOD
 * @param stainsOriginal
 * @param minStain
 * @param maxStain
 * @param ignorePercentage
 * @param checkColors
 * @return
 */
public static ColorDeconvolutionStains estimateStains(final int[] rgbPacked, final float[] redOD,
        final float[] greenOD, final float[] blueOD, final ColorDeconvolutionStains stainsOriginal,
        final double minStain, final double maxStain, final double ignorePercentage,
        final boolean checkColors) {

    double alpha = ignorePercentage / 100;

    int n = rgbPacked.length;
    if (redOD.length != n || greenOD.length != n || blueOD.length != n)
        throw new IllegalArgumentException("All pixel arrays must be the same length!");
    int[] rgb = Arrays.copyOf(rgbPacked, n);
    float[] red = Arrays.copyOf(redOD, n);
    float[] green = Arrays.copyOf(greenOD, n);
    float[] blue = Arrays.copyOf(blueOD, n);

    // Check if we do color sanity test
    boolean doColorTestForHE = checkColors && stainsOriginal.isH_E();
    //      boolean doColorTestForHDAB = checkColors && stainsOriginal.isH_DAB();
    boolean doGrayTest = checkColors && (stainsOriginal.isH_E() || stainsOriginal.isH_DAB());
    double sqrt3 = 1 / Math.sqrt(3);
    double grayThreshold = Math.cos(0.15);

    // Loop through and discard pixels that are too faintly or densely stained
    int keepCount = 0;
    double maxStainSq = maxStain * maxStain;
    for (int i = 0; i < rgb.length; i++) {
        float r = red[i];
        float g = green[i];
        float b = blue[i];
        double magSquared = r * r + g * g + b * b;
        if (magSquared > maxStainSq || r < minStain || g < minStain || b < minStain)
            continue;
        // Check for consistency with H&E staining, if required (i.e. only keep red/pink/purple/blue pixels and the like)
        if (doColorTestForHE && (r > g || b > g)) {
            continue;
        }
        //         // Check for consistency with H-DAB staining, if required (i.e. only keep red/pink/purple/blue pixels and the like)
        //         if (doColorTestForHDAB && (r > g)) {
        //            continue;
        //         }
        // Exclude very 'gray' pixels
        if (doGrayTest && (r * sqrt3 + g * sqrt3 + b * sqrt3) / Math.sqrt(magSquared) >= grayThreshold) {
            continue;
        }
        // Update the arrays
        red[keepCount] = r;
        green[keepCount] = g;
        blue[keepCount] = b;
        rgb[keepCount] = rgb[i];
        keepCount++;
    }
    if (keepCount <= 1)
        throw new IllegalArgumentException("Not enough pixels remain after applying stain thresholds!");

    // Trim the arrays
    if (keepCount < rgb.length) {
        red = Arrays.copyOf(red, keepCount);
        green = Arrays.copyOf(green, keepCount);
        blue = Arrays.copyOf(blue, keepCount);
        rgb = Arrays.copyOf(rgb, keepCount);
    }

    double[][] cov = new double[3][3];
    cov[0][0] = covariance(red, red);
    cov[1][1] = covariance(green, green);
    cov[2][2] = covariance(blue, blue);
    cov[0][1] = covariance(red, green);
    cov[0][2] = covariance(red, blue);
    cov[1][2] = covariance(green, blue);
    cov[2][1] = cov[1][2];
    cov[2][0] = cov[0][2];
    cov[1][0] = cov[0][1];

    RealMatrix mat = MatrixUtils.createRealMatrix(cov);
    logger.debug("Covariance matrix:\n {}", getMatrixAsString(mat.getData()));

    EigenDecomposition eigen = new EigenDecomposition(mat);

    double[] eigenValues = eigen.getRealEigenvalues();
    int[] eigenOrder = rank(eigenValues);
    double[] eigen1 = eigen.getEigenvector(eigenOrder[2]).toArray();
    double[] eigen2 = eigen.getEigenvector(eigenOrder[1]).toArray();
    logger.debug("First eigenvector: " + getVectorAsString(eigen1));
    logger.debug("Second eigenvector: " + getVectorAsString(eigen2));

    double[] phi = new double[keepCount];
    for (int i = 0; i < keepCount; i++) {
        double r = red[i];
        double g = green[i];
        double b = blue[i];
        phi[i] = Math.atan2(r * eigen1[0] + g * eigen1[1] + b * eigen1[2],
                r * eigen2[0] + g * eigen2[1] + b * eigen2[2]);
    }

    /*
     * Rather than projecting onto the plane (which might be a bit wrong),
     * select the vectors directly from the data.
     * This is effectively like a region selection, but where the region has
     * been chosen automatically.
     */
    int[] inds = rank(phi);
    int ind1 = inds[(int) (alpha * keepCount + .5)];
    int ind2 = inds[(int) ((1 - alpha) * keepCount + .5)];

    // Create new stain vectors
    StainVector s1 = new StainVector(stainsOriginal.getStain(1).getName(), red[ind1], green[ind1], blue[ind1]);
    StainVector s2 = new StainVector(stainsOriginal.getStain(2).getName(), red[ind2], green[ind2], blue[ind2]);

    // If working with H&E, we can use the simple heuristic of comparing the red values
    if (stainsOriginal.isH_E()) {
        // Need to check within the stain vectors (*not* original indexed values) because normalisation is important (I think... there were errors before)
        if (s1.getRed() < s2.getRed()) {
            s1 = new StainVector(stainsOriginal.getStain(1).getName(), red[ind2], green[ind2], blue[ind2]);
            s2 = new StainVector(stainsOriginal.getStain(2).getName(), red[ind1], green[ind1], blue[ind1]);
        }
    } else {
        // Check we've got the closest match - if not, switch the order
        double angle11 = StainVector.computeAngle(s1, stainsOriginal.getStain(1));
        double angle12 = StainVector.computeAngle(s1, stainsOriginal.getStain(2));
        double angle21 = StainVector.computeAngle(s2, stainsOriginal.getStain(1));
        double angle22 = StainVector.computeAngle(s2, stainsOriginal.getStain(2));
        if (Math.min(angle12, angle21) < Math.min(angle11, angle22)) {
            s1 = new StainVector(stainsOriginal.getStain(1).getName(), red[ind2], green[ind2], blue[ind2]);
            s2 = new StainVector(stainsOriginal.getStain(2).getName(), red[ind1], green[ind1], blue[ind1]);
        }
    }

    ColorDeconvolutionStains stains = new ColorDeconvolutionStains(stainsOriginal.getName(), s1, s2,
            stainsOriginal.getMaxRed(), stainsOriginal.getMaxGreen(), stainsOriginal.getMaxBlue());

    return stains;

}

From source file:org.csa.rstb.gpf.decompositions.Touzi.java

/**
 * Perform decomposition for given tile.
 *
 * @param targetTiles     The current tiles to be computed for each target band.
 * @param targetRectangle The area in pixel coordinates to be computed.
 * @param op              the polarimetric decomposition operator
 * @throws org.esa.beam.framework.gpf.OperatorException If an error occurs during computation of the filtered value.
 *///from w w w.  j  a v  a  2 s.co m
public void computeTile(final Map<Band, Tile> targetTiles, final Rectangle targetRectangle, final Operator op) {

    final int x0 = targetRectangle.x;
    final int y0 = targetRectangle.y;
    final int w = targetRectangle.width;
    final int h = targetRectangle.height;
    final int maxY = y0 + h;
    final int maxX = x0 + w;
    //System.out.println("x0 = " + x0 + ", y0 = " + y0 + ", w = " + w + ", h = " + h);

    final TileIndex trgIndex = new TileIndex(targetTiles.get(op.getTargetProduct().getBandAt(0)));

    final double[][] Tr = new double[3][3];
    final double[][] Ti = new double[3][3];
    final double[][] EigenVectRe = new double[3][3];
    final double[][] EigenVectIm = new double[3][3];
    final double[] EigenVal = new double[3];

    final double[] psi = new double[3];
    final double[] tau = new double[3];
    final double[] alpha = new double[3];
    final double[] phi = new double[3];
    final double[] vr = new double[3];
    final double[] vi = new double[3];
    double p1, p2, p3, psiMean, tauMean, alphaMean, phiMean;
    double phase, c, s, tmp1r, tmp1i, tmp2r, tmp2i;

    for (final PolBandUtils.PolSourceBand bandList : srcBandList) {

        final Tile[] sourceTiles = new Tile[bandList.srcBands.length];
        final ProductData[] dataBuffers = new ProductData[bandList.srcBands.length];
        final Rectangle sourceRectangle = getSourceRectangle(x0, y0, w, h);
        for (int i = 0; i < bandList.srcBands.length; ++i) {
            sourceTiles[i] = op.getSourceTile(bandList.srcBands[i], sourceRectangle);
            dataBuffers[i] = sourceTiles[i].getDataBuffer();
        }
        final TileIndex srcIndex = new TileIndex(sourceTiles[0]);

        for (int y = y0; y < maxY; ++y) {
            trgIndex.calculateStride(y);
            for (int x = x0; x < maxX; ++x) {
                final int idx = trgIndex.getIndex(x);

                PolOpUtils.getMeanCoherencyMatrix(x, y, halfWindowSizeX, halfWindowSizeY, sourceImageWidth,
                        sourceImageHeight, sourceProductType, srcIndex, dataBuffers, Tr, Ti);

                PolOpUtils.eigenDecomposition(3, Tr, Ti, EigenVectRe, EigenVectIm, EigenVal);

                for (int k = 0; k < 3; ++k) {
                    for (int l = 0; l < 3; ++l) {
                        vr[l] = EigenVectRe[l][k];
                        vi[l] = EigenVectIm[l][k];
                    }

                    phase = Math.atan2(vi[0], vr[0] + PolOpUtils.EPS);
                    c = FastMath.cos(phase);
                    s = FastMath.sin(phase);
                    for (int l = 0; l < 3; ++l) {
                        tmp1r = vr[l];
                        tmp1i = vi[l];
                        vr[l] = tmp1r * c + tmp1i * s;
                        vi[l] = tmp1i * c - tmp1r * s;
                    }

                    psi[k] = 0.5 * Math.atan2(vr[2], vr[1] + PolOpUtils.EPS);

                    tmp1r = vr[1];
                    tmp1i = vi[1];
                    tmp2r = vr[2];
                    tmp2i = vi[2];
                    c = FastMath.cos(2.0 * psi[k]);
                    s = FastMath.sin(2.0 * psi[k]);
                    vr[1] = tmp1r * c + tmp2r * s;
                    vi[1] = tmp1i * c + tmp2i * s;
                    vr[2] = -tmp1r * s + tmp2r * c;
                    vi[2] = -tmp1i * s + tmp2i * c;

                    tau[k] = 0.5 * Math.atan2(-vi[2], vr[0] + PolOpUtils.EPS);

                    phi[k] = Math.atan2(vi[1], vr[1] + PolOpUtils.EPS);

                    alpha[k] = Math
                            .atan(Math.sqrt((vr[1] * vr[1] + vi[1] * vi[1]) / (vr[0] * vr[0] + vi[2] * vi[2])));

                    if ((psi[k] < -Math.PI / 4.0) || (psi[k] > Math.PI / 4.0)) {
                        tau[k] = -tau[k];
                        phi[k] = -phi[k];
                    }

                }

                final double sum = EigenVal[0] + EigenVal[1] + EigenVal[2];
                p1 = EigenVal[0] / sum;
                p2 = EigenVal[1] / sum;
                p3 = EigenVal[2] / sum;

                psiMean = p1 * psi[0] + p2 * psi[1] + p3 * psi[2];
                tauMean = p1 * tau[0] + p2 * tau[1] + p3 * tau[2];
                alphaMean = p1 * alpha[0] + p2 * alpha[1] + p3 * alpha[2];
                phiMean = p1 * phi[0] + p2 * phi[1] + p3 * phi[2];

                for (final Band band : bandList.targetBands) {
                    final String targetBandName = band.getName();
                    final ProductData dataBuffer = targetTiles.get(band).getDataBuffer();
                    if (outputTouziParamSet0) {
                        if (targetBandName.equals("Psi") || targetBandName.contains("Psi_"))
                            dataBuffer.setElemFloatAt(idx, (float) psiMean);
                        else if (targetBandName.equals("Tau") || targetBandName.contains("Tau_"))
                            dataBuffer.setElemFloatAt(idx, (float) tauMean);
                        else if (targetBandName.equals("Alpha") || targetBandName.contains("Alpha_"))
                            dataBuffer.setElemFloatAt(idx, (float) alphaMean);
                        else if (targetBandName.equals("Phi") || targetBandName.contains("Phi_"))
                            dataBuffer.setElemFloatAt(idx, (float) phiMean);
                    }
                    if (outputTouziParamSet1) {
                        if (targetBandName.contains("Psi1"))
                            dataBuffer.setElemFloatAt(idx, (float) psi[0]);
                        else if (targetBandName.contains("Tau1"))
                            dataBuffer.setElemFloatAt(idx, (float) tau[0]);
                        else if (targetBandName.contains("Alpha1"))
                            dataBuffer.setElemFloatAt(idx, (float) alpha[0]);
                        else if (targetBandName.contains("Phi1"))
                            dataBuffer.setElemFloatAt(idx, (float) phi[0]);
                    }
                    if (outputTouziParamSet2) {
                        if (targetBandName.contains("Psi2"))
                            dataBuffer.setElemFloatAt(idx, (float) psi[1]);
                        else if (targetBandName.contains("Tau2"))
                            dataBuffer.setElemFloatAt(idx, (float) tau[1]);
                        else if (targetBandName.contains("Alpha2"))
                            dataBuffer.setElemFloatAt(idx, (float) alpha[1]);
                        else if (targetBandName.contains("Phi2"))
                            dataBuffer.setElemFloatAt(idx, (float) phi[1]);
                    }
                    if (outputTouziParamSet3) {
                        if (targetBandName.contains("Psi3"))
                            dataBuffer.setElemFloatAt(idx, (float) psi[2]);
                        else if (targetBandName.contains("Tau3"))
                            dataBuffer.setElemFloatAt(idx, (float) tau[2]);
                        else if (targetBandName.contains("Alpha3"))
                            dataBuffer.setElemFloatAt(idx, (float) alpha[2]);
                        else if (targetBandName.contains("Phi3"))
                            dataBuffer.setElemFloatAt(idx, (float) phi[2]);
                    }
                }

            }
        }
    }
}

From source file:com.google.publicalerts.cap.validator.MapVisualizer.java

/**
 * Computes the end point of an arc of a great circle given a starting point
 * and arc length./*  www .  j  a  v a2s  . co m*/
 * See http://williams.best.vwh.net/avform.htm#LL
 *
 * @param heading the direction of the arc, in radians
 * @param arcLength the length of the arc, in kilometers
 * @param latitude latitude of the starting point of the arc
 * @param longitude longitude of the starting point of the arc
 * @return the end point
 */
private JSONArray getRadialEndpoint(double heading, double arcLength, double latitude, double longitude)
        throws JSONException {
    // the angle of the great circle arc
    double earthAngle = arcLength / EARTH_RADIUS_KM;
    // convert decimal degrees to radians for the calculation
    double lat = latitude * DEG_TO_RAD;
    double lng = Math.abs(longitude) * DEG_TO_RAD;
    double asinArg = Math.sin(lat) * Math.cos(earthAngle)
            + Math.cos(lat) * Math.sin(earthAngle) * Math.cos(heading);

    double destLat = Math.asin(asinArg);
    double y = Math.sin(heading) * Math.sin(earthAngle) * Math.cos(lat);
    double x = Math.cos(earthAngle) - Math.sin(lat) * Math.sin(destLat);
    double destLng = (lng - Math.atan2(y, x) + Math.PI) % (2 * Math.PI) - Math.PI;

    return toJsPoint(destLat * RAD_TO_DEG, destLng * RAD_TO_DEG * (longitude < 0 ? -1 : 1));
}

From source file:org.csa.rstb.polarimetric.gpf.decompositions.Touzi.java

/**
 * Perform decomposition for given tile.
 *
 * @param targetTiles     The current tiles to be computed for each target band.
 * @param targetRectangle The area in pixel coordinates to be computed.
 * @param op              the polarimetric decomposition operator
 * @throws OperatorException If an error occurs during computation of the filtered value.
 *//*from  ww  w .  j av  a  2s.co m*/
public void computeTile(final Map<Band, Tile> targetTiles, final Rectangle targetRectangle, final Operator op) {

    final int x0 = targetRectangle.x;
    final int y0 = targetRectangle.y;
    final int w = targetRectangle.width;
    final int h = targetRectangle.height;
    final int maxY = y0 + h;
    final int maxX = x0 + w;
    //System.out.println("x0 = " + x0 + ", y0 = " + y0 + ", w = " + w + ", h = " + h);

    final TileIndex trgIndex = new TileIndex(targetTiles.get(op.getTargetProduct().getBandAt(0)));

    final double[][] Tr = new double[3][3];
    final double[][] Ti = new double[3][3];
    final double[][] EigenVectRe = new double[3][3];
    final double[][] EigenVectIm = new double[3][3];
    final double[] EigenVal = new double[3];

    final double[] psi = new double[3];
    final double[] tau = new double[3];
    final double[] alpha = new double[3];
    final double[] phi = new double[3];
    final double[] vr = new double[3];
    final double[] vi = new double[3];
    double p1, p2, p3, psiMean, tauMean, alphaMean, phiMean;
    double phase, c, s, tmp1r, tmp1i, tmp2r, tmp2i;

    for (final PolBandUtils.PolSourceBand bandList : srcBandList) {

        final Tile[] sourceTiles = new Tile[bandList.srcBands.length];
        final ProductData[] dataBuffers = new ProductData[bandList.srcBands.length];
        final Rectangle sourceRectangle = getSourceRectangle(x0, y0, w, h);
        for (int i = 0; i < bandList.srcBands.length; ++i) {
            sourceTiles[i] = op.getSourceTile(bandList.srcBands[i], sourceRectangle);
            dataBuffers[i] = sourceTiles[i].getDataBuffer();
        }
        final TileIndex srcIndex = new TileIndex(sourceTiles[0]);

        for (int y = y0; y < maxY; ++y) {
            trgIndex.calculateStride(y);
            for (int x = x0; x < maxX; ++x) {
                final int idx = trgIndex.getIndex(x);

                PolOpUtils.getMeanCoherencyMatrix(x, y, halfWindowSizeX, halfWindowSizeY, sourceImageWidth,
                        sourceImageHeight, sourceProductType, srcIndex, dataBuffers, Tr, Ti);

                PolOpUtils.eigenDecomposition(3, Tr, Ti, EigenVectRe, EigenVectIm, EigenVal);

                for (int k = 0; k < 3; ++k) {
                    for (int l = 0; l < 3; ++l) {
                        vr[l] = EigenVectRe[l][k];
                        vi[l] = EigenVectIm[l][k];
                    }

                    phase = Math.atan2(vi[0], vr[0] + PolOpUtils.EPS);
                    c = FastMath.cos(phase);
                    s = FastMath.sin(phase);
                    for (int l = 0; l < 3; ++l) {
                        tmp1r = vr[l];
                        tmp1i = vi[l];
                        vr[l] = tmp1r * c + tmp1i * s;
                        vi[l] = tmp1i * c - tmp1r * s;
                    }

                    psi[k] = 0.5 * Math.atan2(vr[2], vr[1] + PolOpUtils.EPS);

                    tmp1r = vr[1];
                    tmp1i = vi[1];
                    tmp2r = vr[2];
                    tmp2i = vi[2];
                    c = FastMath.cos(2.0 * psi[k]);
                    s = FastMath.sin(2.0 * psi[k]);
                    vr[1] = tmp1r * c + tmp2r * s;
                    vi[1] = tmp1i * c + tmp2i * s;
                    vr[2] = -tmp1r * s + tmp2r * c;
                    vi[2] = -tmp1i * s + tmp2i * c;

                    tau[k] = 0.5 * Math.atan2(-vi[2], vr[0] + PolOpUtils.EPS);

                    phi[k] = Math.atan2(vi[1], vr[1] + PolOpUtils.EPS);

                    alpha[k] = Math
                            .atan(Math.sqrt((vr[1] * vr[1] + vi[1] * vi[1]) / (vr[0] * vr[0] + vi[2] * vi[2])));

                    if ((psi[k] < -Constants.PI / 4.0) || (psi[k] > Constants.PI / 4.0)) {
                        tau[k] = -tau[k];
                        phi[k] = -phi[k];
                    }

                }

                final double sum = EigenVal[0] + EigenVal[1] + EigenVal[2];
                p1 = EigenVal[0] / sum;
                p2 = EigenVal[1] / sum;
                p3 = EigenVal[2] / sum;

                psiMean = p1 * psi[0] + p2 * psi[1] + p3 * psi[2];
                tauMean = p1 * tau[0] + p2 * tau[1] + p3 * tau[2];
                alphaMean = p1 * alpha[0] + p2 * alpha[1] + p3 * alpha[2];
                phiMean = p1 * phi[0] + p2 * phi[1] + p3 * phi[2];

                for (final Band band : bandList.targetBands) {
                    final String targetBandName = band.getName();
                    final ProductData dataBuffer = targetTiles.get(band).getDataBuffer();
                    if (outputTouziParamSet0) {
                        if (targetBandName.equals("Psi") || targetBandName.contains("Psi_"))
                            dataBuffer.setElemFloatAt(idx, (float) psiMean);
                        else if (targetBandName.equals("Tau") || targetBandName.contains("Tau_"))
                            dataBuffer.setElemFloatAt(idx, (float) tauMean);
                        else if (targetBandName.equals("Alpha") || targetBandName.contains("Alpha_"))
                            dataBuffer.setElemFloatAt(idx, (float) alphaMean);
                        else if (targetBandName.equals("Phi") || targetBandName.contains("Phi_"))
                            dataBuffer.setElemFloatAt(idx, (float) phiMean);
                    }
                    if (outputTouziParamSet1) {
                        if (targetBandName.contains("Psi1"))
                            dataBuffer.setElemFloatAt(idx, (float) psi[0]);
                        else if (targetBandName.contains("Tau1"))
                            dataBuffer.setElemFloatAt(idx, (float) tau[0]);
                        else if (targetBandName.contains("Alpha1"))
                            dataBuffer.setElemFloatAt(idx, (float) alpha[0]);
                        else if (targetBandName.contains("Phi1"))
                            dataBuffer.setElemFloatAt(idx, (float) phi[0]);
                    }
                    if (outputTouziParamSet2) {
                        if (targetBandName.contains("Psi2"))
                            dataBuffer.setElemFloatAt(idx, (float) psi[1]);
                        else if (targetBandName.contains("Tau2"))
                            dataBuffer.setElemFloatAt(idx, (float) tau[1]);
                        else if (targetBandName.contains("Alpha2"))
                            dataBuffer.setElemFloatAt(idx, (float) alpha[1]);
                        else if (targetBandName.contains("Phi2"))
                            dataBuffer.setElemFloatAt(idx, (float) phi[1]);
                    }
                    if (outputTouziParamSet3) {
                        if (targetBandName.contains("Psi3"))
                            dataBuffer.setElemFloatAt(idx, (float) psi[2]);
                        else if (targetBandName.contains("Tau3"))
                            dataBuffer.setElemFloatAt(idx, (float) tau[2]);
                        else if (targetBandName.contains("Alpha3"))
                            dataBuffer.setElemFloatAt(idx, (float) alpha[2]);
                        else if (targetBandName.contains("Phi3"))
                            dataBuffer.setElemFloatAt(idx, (float) phi[2]);
                    }
                }

            }
        }
    }
}

From source file:weatherwebscraper.GoogleMapsQuery.java

/**
 * Uses Haversine Formula to return distance between two points on the globe as the crow flies.
 * //  w  w  w . ja  va 2  s. c  om
 * @param lat1 
 * @param lng1
 * @param lat2
 * @param lng2
 * @return 
 */
private static double distanceBetweenLatLongs(double lat1, double lng1, double lat2, double lng2) {
    double rlat1 = degToRad(lat1);
    double rlat2 = degToRad(lat2);
    double drlat = degToRad(lat2 - lat1);
    double drlng = degToRad(lng2 - lng1);
    double R = 6371000.0; //Earth's radius

    double a = Math.sin(drlat / 2.0) * Math.sin(drlat / 2.0)
            + Math.cos(rlat1) * Math.cos(rlat2) * Math.sin(drlng / 2.0) * Math.sin(drlng / 2.0);
    double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1.0 - a));
    double d = R * c;

    return d;
}

From source file:org.alder.fotobuchconvert.scribus.ScribusWriter.java

public ScribusLine addLine(double x1, double y1, double x2, double y2) {
    ScribusLine si = new ScribusLine();
    double w = x2 - x1, h = y2 - y1;
    si.setPosition(x1, y1, Math.sqrt(w * w + h * h), 1, Math.atan2(h, w) / Math.PI * 180);
    si.setBorder(0, Color.BLACK);
    return si;/*from w ww  .  ja v a2  s .  co m*/
}

From source file:com.wattzap.model.GPXReader.java

/**
 * Calculate distance between two points in latitude and longitude taking
 * into account height difference. If you are not interested in height
 * difference pass 0.0. Uses Haversine method as its base.
 * /*from w  ww .  ja v a  2s.c o m*/
 * lat1, lon1 Start point lat2, lon2 End point el1 Start altitude in meters
 * el2 End altitude in meters
 * 
 * @returns Distance in Meters
 */
public static double distance(double lat1, double lat2, double lon1, double lon2, double el1, double el2) {

    final int R = 6371; // Radius of the earth

    Double latDistance = Math.toRadians(lat2 - lat1);
    Double lonDistance = Math.toRadians(lon2 - lon1);
    Double a = Math.sin(latDistance / 2) * Math.sin(latDistance / 2) + Math.cos(Math.toRadians(lat1))
            * Math.cos(Math.toRadians(lat2)) * Math.sin(lonDistance / 2) * Math.sin(lonDistance / 2);
    Double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
    double distance = R * c * 1000; // convert to meters

    double height = el1 - el2;

    distance = (distance * distance) + (height * height);

    return Math.sqrt(distance);
}

From source file:jat.core.cm.TwoBodyAPL.java

public double ta_from_t(double t) {

    double M = meanAnomaly(t);
    double ea = solveKepler(M, this.e);

    double sinE = Math.sin(ea);
    double cosE = Math.cos(ea);
    double den = 1.0 - this.e * cosE;
    double sqrome2 = Math.sqrt(1.0 - this.e * this.e);
    double sinv = (sqrome2 * sinE) / den;
    double cosv = (cosE - this.e) / den;

    double ta = Math.atan2(sinv, cosv);
    if (this.ta < 0.0) {
        this.ta = this.ta + 2.0 * Constants.pi;
    }//from w w w .  ja  v a 2 s  .c  om

    return ta;
}

From source file:org.jcurl.core.base.CurveTransformedTest.java

/**
 * Test the transformation from a Rock Coordinates (rc) System at wc(3,3.5)
 * with positive y axis along wc(2,4.2) into World Coordinates (wc). Uses a
 * Point rc(5,1.3) = wc(8,2.5).//  w  w  w  .ja va2  s .c o m
 * 
 * @see CurveTransformed#createRc2Wc(AffineTransform, Point2D, Point2D)
 */
public void testCreateRc2Wc() {
    final Point2D p0_wc = new Point2D.Double(3, 3.5);
    final Rock v0_wc = new RockDouble(2, 4.2, 0.3);
    final AffineTransform at = CurveTransformed.createRc2Wc(null, p0_wc, v0_wc);
    assertEquals(AffineTransform.TYPE_GENERAL_ROTATION + AffineTransform.TYPE_TRANSLATION, at.getType());
    assertEquals(1.0, at.getDeterminant());
    assertEquals(0.9028605188239303, at.getScaleX());
    assertEquals(at.getScaleX(), at.getScaleY());
    assertEquals(0.42993358039234775, at.getShearX());
    assertEquals(-at.getShearX(), at.getShearY());
    assertEquals(p0_wc.getX(), at.getTranslateX());
    assertEquals(p0_wc.getY(), at.getTranslateY());

    final Point2D rc = new Point2D.Double(5, 1.3);
    final Point2D wc = at.transform(rc, null);
    assertEquals("Point2D.Double[8.073216248629704, 2.524050772509371]", wc.toString());

    // angle in rc:
    double ang = Math.atan2(rc.getY(), rc.getX());
    assertEquals(14.574216198038739, rad2deg(ang));

    // wc rotation:
    ang = Math.atan2(at.getShearY(), at.getScaleY());
    assertEquals(-25.463345061871614, rad2deg(ang));
    final double[] d = new double[6];
    at.getMatrix(d);
    ang = Math.atan2(-d[2], d[3]);
    assertEquals(-25.463345061871614, rad2deg(ang));

    // angle in wc:
    ang = Math.atan2(wc.getY(), wc.getX());
    assertEquals(17.36159358309492, rad2deg(ang));
}