Example usage for java.lang Math sin

List of usage examples for java.lang Math sin

Introduction

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

Prototype

@HotSpotIntrinsicCandidate
public static double sin(double a) 

Source Link

Document

Returns the trigonometric sine of an angle.

Usage

From source file:Similaridade.GraficosSimilaridade.java

public static XYDataset newDataset2OndasComDeslocada(CapturaAtual onda1, CapturaAtual onda2, int deslocamento,
        double correlacao) {
    //metodo que cria um CategoryDataset para ser usado em grafico de linhas                
    float valOnda1[] = new float[Configuracoes.PONTOSONDA];
    float valOnda2[] = new float[Configuracoes.PONTOSONDA];
    float sen1[] = new float[Configuracoes.HARMONICAS];
    float cos1[] = new float[Configuracoes.HARMONICAS];
    float sen2[] = new float[Configuracoes.HARMONICAS];
    float cos2[] = new float[Configuracoes.HARMONICAS];
    int i;/*from  w  w  w  .  j  a  v a  2  s .co m*/

    float s1, s2, tempo[] = new float[Configuracoes.PONTOSONDA];
    XYSeriesCollection result = new XYSeriesCollection();

    for (i = 0; i < Configuracoes.HARMONICAS; i++) {
        sen1[i] = 0;
        cos1[i] = 0;
        sen2[i] = 0;
        cos2[i] = 0;
    }

    i = 0;
    for (HarmAtual oa : onda1.getHarmAtual()) {
        sen1[i] = oa.getSen();
        cos1[i] = oa.getCos();
        i++;
    }

    i = 0;
    for (HarmAtual oa : onda2.getHarmAtual()) {
        sen2[i] = oa.getSen();
        cos2[i] = oa.getCos();
        i++;
    }

    XYSeries series1 = new XYSeries(String.valueOf(onda1.getCodCaptura()));
    XYSeries series3 = new XYSeries("Shift " + String.valueOf(onda2.getCodCaptura()));

    tempo[0] = 0; //(float) 1 / 60;
    for (i = 0; i < Configuracoes.PONTOSONDA; i++) {
        if (TIRADOVM == 0) {
            s1 = onda1.getValorMedio() / 2;
            s2 = onda2.getValorMedio() / 2;
        } else {
            s1 = onda1.getValorMedio();
            s2 = onda2.getValorMedio();
        }

        for (int x = 0; x < Configuracoes.HARMONICAS; x++) {
            if (TIRADOVM == 0) {
                s1 += (float) (sen1[x] * Math.sin(2 * Math.PI * ((x + 1) * Configuracoes.FREQBASE) * tempo[i]))
                        + (cos1[x] * Math.cos(-2 * Math.PI * ((x + 1) * Configuracoes.FREQBASE) * tempo[i]));
                s2 += (float) (sen2[x] * Math.sin(2 * Math.PI * ((x + 1) * Configuracoes.FREQBASE) * tempo[i]))
                        + (cos2[x] * Math.cos(-2 * Math.PI * ((x + 1) * Configuracoes.FREQBASE) * tempo[i]));
            } else {
                s1 += (float) (sen1[x] * Math.sin(2 * Math.PI * ((x + 1) * Configuracoes.FREQBASE) * tempo[i]))
                        + (cos1[x] * Math.cos(2 * Math.PI * ((x + 1) * Configuracoes.FREQBASE) * tempo[i]));
                s2 += (float) (sen2[x] * Math.sin(2 * Math.PI * ((x + 1) * Configuracoes.FREQBASE) * tempo[i]))
                        + (cos2[x] * Math.cos(2 * Math.PI * ((x + 1) * Configuracoes.FREQBASE) * tempo[i]));
            }
        }
        if (TIRADOVM == 0) {
            valOnda1[i] = (int) ((s1 * (2.0)) / 256.0);
            valOnda1[i] = (valOnda1[i] - onda1.getOffset());
            valOnda2[i] = (int) ((s2 * (2.0)) / 256.0);
            valOnda2[i] = (valOnda2[i] - onda2.getOffset());

        } else {
            valOnda1[i] = (s1);
            valOnda2[i] = (s2);
        }
        valOnda1[i] = (valOnda1[i]) / onda1.getGain();
        valOnda2[i] = (valOnda2[i]) / onda2.getGain();

        series1.add(tempo[i] * 1000, valOnda1[i]);

        if (i == (Configuracoes.PONTOSONDA - 1)) {
            break;
        }
        tempo[i + 1] = (tempo[i] + (float) (1.0 / (60 * 256)));
    }

    int j = 0;
    if (correlacao < 0) {
        for (i = 0; i < Configuracoes.PONTOSONDA; i++) {
            if ((i + deslocamento) <= (Configuracoes.PONTOSONDA - 1)) {
                series3.add(tempo[i] * 1000, -1 * valOnda2[i + deslocamento]);
            } else {
                series3.add(tempo[i] * 1000, -1 * valOnda2[j++]);
            }
        }
    } else {
        for (i = 0; i < Configuracoes.PONTOSONDA; i++) {
            if ((i + deslocamento) <= (Configuracoes.PONTOSONDA - 1)) {
                series3.add(tempo[i] * 1000, valOnda2[i + deslocamento]);
            } else {
                series3.add(tempo[i] * 1000, valOnda2[j++]);
            }
        }
    }

    result.addSeries(series1);
    result.addSeries(series3);

    return result;
}

From source file:com.alvermont.terraj.planet.project.PetersProjection.java

/**
 * Carry out the projection/*from w  w w.j a v a2 s  . c  om*/
 */
public void project() {
    setcolours();

    final int width = getParameters().getProjectionParameters().getWidth();
    final int height = getParameters().getProjectionParameters().getHeight();

    final double lat = getParameters().getProjectionParameters().getLatitudeRadians();
    final double lon = getParameters().getProjectionParameters().getLongitudeRadians();

    final double scale = getParameters().getProjectionParameters().getScale();

    final double hgrid = getParameters().getProjectionParameters().getHgrid();
    final double vgrid = getParameters().getProjectionParameters().getVgrid();

    final boolean doShade = getParameters().getProjectionParameters().isDoShade();

    cacheParameters();

    colours = new short[width][height];
    shades = new short[width][height];

    depth = (3 * ((int) (log2(scale * height)))) + 6;

    double y;
    double cos2;
    double theta1;
    double scale1;
    int k;
    int i;
    int j;
    int water;
    int land;

    y = 2.0 * Math.sin(lat);
    k = (int) ((0.5 * y * width * scale) / Math.PI);

    water = 0;
    land = 0;

    progress.progressStart(height, "Generating Terrain");

    for (j = 0; j < height; ++j) {
        progress.progressStep(j);

        y = (0.5 * Math.PI * ((2.0 * (j - k)) - height)) / width / scale;

        if (Math.abs(y) > 1.0) {
            for (i = 0; i < width; ++i) {
                colours[i][j] = backgroundColour;

                if (doShade) {
                    shades[i][j] = 255;
                }
            }
        } else {
            cos2 = Math.sqrt(1.0 - (y * y));

            if (cos2 > 0.0) {
                scale1 = (scale * width) / height / cos2 / Math.PI;

                depth = (3 * ((int) (log2(scale1 * height)))) + 3;

                for (i = 0; i < width; ++i) {
                    theta1 = lon - (0.5 * Math.PI) + ((Math.PI * ((2.0 * i) - width)) / width / scale);

                    colours[i][j] = (short) planet0(Math.cos(theta1) * cos2, y, -Math.sin(theta1) * cos2);

                    if (doShade) {
                        shades[i][j] = shade;
                    }

                    if (colours[i][j] < colourLand0) {
                        ++water;
                    } else {
                        ++land;
                    }
                }
            }
        }
    }

    progress.progressComplete("Terrain Generated");

    log.debug("Water percentage: " + ((100 * water) / (water + land)));

    if (hgrid != 0.0) {
        /* draw horizontal gridlines */
        for (theta1 = 0.0; theta1 > -90.0; theta1 -= hgrid)
            ;

        for (theta1 = theta1; theta1 < 90.0; theta1 += hgrid) {
            y = 2.0 * Math.sin(Math.toRadians(theta1));
            j = (height / 2) + (int) ((0.5 * y * width * scale) / Math.PI) + k;

            if ((j >= 0) && (j < height)) {
                for (i = 0; i < width; ++i)
                    colours[i][j] = BLACK;
            }
        }
    }

    if (vgrid != 0.0) {
        /* draw vertical gridlines */
        for (theta1 = 0.0; theta1 > -360.0; theta1 -= vgrid)
            ;

        for (theta1 = theta1; theta1 < 360.0; theta1 += vgrid) {
            i = (int) (0.5 * width * (1.0 + ((scale * (Math.toRadians(theta1) - lon)) / Math.PI)));

            if ((i >= 0) && (i < width)) {
                for (j = Math.max(0, (height / 2) - (int) ((width * scale) / Math.PI) + k); j < Math.min(height,
                        (height / 2) + (int) ((width * scale) / Math.PI) + k); ++j)
                    colours[i][j] = BLACK;
            }
        }
    }

    if (doShade) {
        smoothshades();
    }

    doOutlining();
}

From source file:chiliad.parser.pdf.extractor.vectorgraphics.operator.BeginInlineImage.java

/**
 * process : BI : begin inline image./* ww w. ja v  a  2 s.  co  m*/
 *
 * @param operator The operator that is being executed.
 * @param arguments List
 * @throws IOException If there is an error displaying the inline image.
 */
@Override
public void process(PDFOperator operator, List<COSBase> arguments) throws IOException {
    VectorGraphicsExtractor extractor = (VectorGraphicsExtractor) context;
    PDPage page = extractor.getPage();
    //begin inline image object
    ImageParameters params = operator.getImageParameters();
    PDInlinedImage image = new PDInlinedImage();
    image.setImageParameters(params);
    image.setImageData(operator.getImageData());
    if (params.isStencil()) {
        //TODO implement inline image stencil masks 
        LOG.warn("Stencil masks are not implemented, background may be incorrect");
    }
    BufferedImage awtImage = image.createImage(context.getColorSpaces());

    if (awtImage == null) {
        LOG.warn("BeginInlineImage.process(): createImage returned NULL");
        return;
    }
    int imageWidth = awtImage.getWidth();
    int imageHeight = awtImage.getHeight();
    double pageHeight = extractor.getPageSize().getHeight();

    Matrix ctm = extractor.getGraphicsState().getCurrentTransformationMatrix();
    int pageRotation = page.findRotation();

    AffineTransform ctmAT = ctm.createAffineTransform();
    ctmAT.scale(1f / imageWidth, 1f / imageHeight);
    Matrix rotationMatrix = new Matrix();
    rotationMatrix.setFromAffineTransform(ctmAT);
    // calculate the inverse rotation angle
    // scaleX = m00 = cos
    // shearX = m01 = -sin
    // tan = sin/cos
    double angle = Math.atan(ctmAT.getShearX() / ctmAT.getScaleX());
    Matrix translationMatrix = null;
    if (pageRotation == 0 || pageRotation == 180) {
        translationMatrix = Matrix.getTranslatingInstance((float) (Math.sin(angle) * ctm.getXScale()),
                (float) (pageHeight - 2 * ctm.getYPosition() - Math.cos(angle) * ctm.getYScale()));
    } else if (pageRotation == 90 || pageRotation == 270) {
        translationMatrix = Matrix.getTranslatingInstance((float) (Math.sin(angle) * ctm.getYScale()),
                (float) (pageHeight - 2 * ctm.getYPosition()));
    }
    rotationMatrix = rotationMatrix.multiply(translationMatrix);
    rotationMatrix.setValue(0, 1, (-1) * rotationMatrix.getValue(0, 1));
    rotationMatrix.setValue(1, 0, (-1) * rotationMatrix.getValue(1, 0));
    AffineTransform at = new AffineTransform(rotationMatrix.getValue(0, 0), rotationMatrix.getValue(0, 1),
            rotationMatrix.getValue(1, 0), rotationMatrix.getValue(1, 1), rotationMatrix.getValue(2, 0),
            rotationMatrix.getValue(2, 1));
    extractor.drawImage(awtImage, at);
}

From source file:de.thkwalter.et.betriebspunkt.Betriebspunkt.java

/**
 * Diese Methode berechnet auf Basis der Eingabewerte alle fehlenden Gren.
 *///  ww  w  .j  ava  2s.co m
private void rechnen() {
    // Die Scheinleistung (in kVA) wird berechnet.
    this.p_s = Math.sqrt(3.0) * this.getU_LL() * this.getI_L() / 1000.0;

    // Die Phasenverschiebung zwischen Strangstrom und Strangspannung wird berechnet.
    this.cosPhi = this.p_el / this.p_s;

    // Ein vom Schaltungstyp abhngiger Faktor wird berechnet.
    double faktor = this.schaltungstyp == Schaltungstyp.STERN ? 1.0 : 1.0 / Math.sqrt(3.0);

    // Der Realteil des Strangstroms (in A) wird berechnet.
    double i_s_re = faktor * this.i_L * this.cosPhi;

    // Der Imaginrteil des Strangstroms (in A) wird berechnet. Dabei ist zu beachten, dass fr elektrische Maschinen der
    // Phasenwinkel phi negativ ist.
    double i_s_im = faktor * this.i_L * Math.sin(-Math.acos(this.cosPhi));

    // Der komplexe Zeiger des Stnderstroms (in A) wird erzeugt.
    this.z_i_s = new Complex(i_s_re, i_s_im);
}

From source file:com.alvermont.terraj.planet.project.ConicalProjection.java

/**
 * Carry out the projection//w  ww.  j  av  a  2 s .c  om
 */
public void project() {
    setcolours();

    final int width = getParameters().getProjectionParameters().getWidth();
    final int height = getParameters().getProjectionParameters().getHeight();

    final double lat = getParameters().getProjectionParameters().getLatitudeRadians();
    final double lon = getParameters().getProjectionParameters().getLongitudeRadians();

    final double scale = getParameters().getProjectionParameters().getScale();

    final double hgrid = getParameters().getProjectionParameters().getHgrid();
    final double vgrid = getParameters().getProjectionParameters().getVgrid();

    final boolean doShade = getParameters().getProjectionParameters().isDoShade();

    cacheParameters();

    colours = new short[width][height];
    shades = new short[width][height];

    final double sla = Math.sin(lat);
    final double cla = Math.cos(lat);
    final double slo = Math.sin(lon);
    final double clo = Math.cos(lon);

    depth = (3 * ((int) (log2(scale * height)))) + 6;

    double k1;
    double c;
    double y2;
    double x;
    double y;
    double zz;
    double x1;
    double y1;
    double z1;
    double theta1;
    double theta2;
    double ymin;
    double ymax;
    double cos2;
    int i;
    int j;

    ymin = 2.0;
    ymax = -2.0;

    if (lat > 0) {
        k1 = 1.0 / Math.sin(lat);
        c = k1 * k1;

        y2 = Math.sqrt((c * (1.0 - Math.sin(lat / k1))) / (1.0 + Math.sin(lat / k1)));

        progress.progressStart(height, "Generating Terrain");

        for (j = 0; j < height; ++j) {
            progress.progressStep(j);

            for (i = 0; i < width; ++i) {
                x = ((2.0 * i) - width) / height / scale;
                y = (((2.0 * j) - height) / height / scale) + y2;
                zz = (x * x) + (y * y);

                if (zz == 0.0) {
                    theta1 = 0.0;
                } else {
                    theta1 = k1 * Math.atan2(x, y);
                }

                if ((theta1 < -Math.PI) || (theta1 > Math.PI)) {
                    colours[i][j] = backgroundColour;

                    if (doShade) {
                        shades[i][j] = 255;
                    }
                } else {
                    /* theta1 is longitude */
                    theta1 += (lon - (0.5 * Math.PI));
                    theta2 = k1 * Math.asin((zz - c) / (zz + c));

                    /* theta2 is latitude */
                    if ((theta2 > (0.5 * Math.PI)) || (theta2 < (-0.5 * Math.PI))) {
                        colours[i][j] = backgroundColour;

                        if (doShade) {
                            shades[i][j] = 255;
                        }
                    } else {
                        cos2 = Math.cos(theta2);
                        y = Math.sin(theta2);

                        if (y < ymin) {
                            ymin = y;
                        }

                        if (y > ymax) {
                            ymax = y;
                        }

                        colours[i][j] = (short) planet0(Math.cos(theta1) * cos2, y, -Math.sin(theta1) * cos2);

                        if (doShade) {
                            shades[i][j] = shade;
                        }
                    }
                }
            }
        }

        progress.progressComplete("Terrain Generated");

        if (hgrid != 0.0) {
            /* draw horizontal gridlines */
            for (theta1 = 0.0; theta1 > -90.0; theta1 -= hgrid)
                ;

            for (theta1 = theta1; theta1 < 90.0; theta1 += hgrid) {
                y = Math.sin(Math.toRadians(theta1));

                if ((ymin <= y) && (y <= ymax)) {
                    zz = Math.sqrt((c * (1.0 + Math.sin(Math.toRadians(theta1) / k1)))
                            / (1.0 - Math.sin(Math.toRadians(theta1) / k1)));

                    for (theta2 = -Math.PI + lon; theta2 < (Math.PI + lon); theta2 += (0.5 / width / scale)) {
                        z1 = theta2 - lon;
                        x1 = zz * Math.sin(z1 / k1);
                        y1 = zz * Math.cos(z1 / k1);

                        i = (int) (0.5 * ((height * scale * x1) + width));
                        j = (int) (0.5 * ((height * scale * (y1 - y2)) + height));

                        if ((0 <= i) && (i < width) && (0 <= j) && (j < height)) {
                            colours[i][j] = BLACK;
                        }
                    }
                }
            }
        }

        if (vgrid != 0.0) {
            /* draw vertical gridlines */
            for (theta1 = -0.5 * Math.PI; theta1 < (0.5 * Math.PI); theta1 += (0.5 / width / scale)) {
                y = Math.sin(theta1);

                if ((ymin <= y) && (y <= ymax)) {
                    zz = Math.sqrt((c * (1.0 + Math.sin(theta1 / k1))) / (1.0 - Math.sin(theta1 / k1)));

                    for (theta2 = 0.0; theta2 > (-180.0 + Math.toDegrees(lon)); theta2 -= vgrid)
                        ;

                    for (theta2 = theta2; theta2 < (180.0 + Math.toDegrees(lon)); theta2 += vgrid) {
                        z1 = Math.toRadians(theta2) - lon;
                        x1 = zz * Math.sin(z1 / k1);
                        y1 = zz * Math.cos(z1 / k1);

                        i = (int) (0.5 * ((height * scale * x1) + width));
                        j = (int) (0.5 * ((height * scale * (y1 - y2)) + height));

                        if ((0 <= i) && (i < width) && (0 <= j) && (j < height)) {
                            colours[i][j] = BLACK;
                        }
                    }
                }
            }
        }
    } else {
        k1 = 1.0 / Math.sin(lat);
        c = k1 * k1;
        y2 = Math.sqrt((c * (1.0 - Math.sin(lat / k1))) / (1.0 + Math.sin(lat / k1)));

        progress.progressStart(height, "Generating Terrain");

        for (j = 0; j < height; ++j) {
            progress.progressStep(j);

            for (i = 0; i < width; ++i) {
                x = ((2.0 * i) - width) / height / scale;
                y = (((2.0 * j) - height) / height / scale) - y2;
                zz = (x * x) + (y * y);

                if (zz == 0.0) {
                    theta1 = 0.0;
                } else {
                    theta1 = -k1 * Math.atan2(x, -y);
                }

                if ((theta1 < -Math.PI) || (theta1 > Math.PI)) {
                    colours[i][j] = backgroundColour;

                    if (doShade) {
                        shades[i][j] = 255;
                    }
                } else {
                    /* theta1 is longitude */
                    theta1 += (lon - (0.5 * Math.PI));
                    theta2 = k1 * Math.asin((zz - c) / (zz + c));

                    /* theta2 is latitude */
                    if ((theta2 > (0.5 * Math.PI)) || (theta2 < (-0.5 * Math.PI))) {
                        colours[i][j] = backgroundColour;

                        if (doShade) {
                            shades[i][j] = 255;
                        }
                    } else {
                        cos2 = Math.cos(theta2);
                        y = Math.sin(theta2);

                        if (y < ymin) {
                            ymin = y;
                        }

                        if (y > ymax) {
                            ymax = y;
                        }

                        colours[i][j] = (short) planet0(Math.cos(theta1) * cos2, y, -Math.sin(theta1) * cos2);

                        if (doShade) {
                            shades[i][j] = shade;
                        }
                    }
                }
            }
        }

        progress.progressComplete("Terrain Generated");

        if (hgrid != 0.0) {
            /* draw horizontal gridlines */
            for (theta1 = 0.0; theta1 > -90.0; theta1 -= hgrid)
                ;

            for (theta1 = theta1; theta1 < 90.0; theta1 += hgrid) {
                y = Math.sin(Math.toRadians(theta1));

                if ((ymin <= y) && (y <= ymax)) {
                    zz = Math.sqrt((c * (1.0 + Math.sin(Math.toRadians(theta1) / k1)))
                            / (1.0 - Math.sin(Math.toRadians(theta1) / k1)));

                    for (theta2 = -Math.PI + lon; theta2 < (Math.PI + lon); theta2 += (0.5 / width / scale)) {
                        z1 = theta2 - lon;
                        x1 = -zz * Math.sin(z1 / k1);
                        y1 = -zz * Math.cos(z1 / k1);

                        i = (int) (0.5 * ((height * scale * x1) + width));
                        j = (int) (0.5 * ((height * scale * (y1 + y2)) + height));

                        if ((0 <= i) && (i < width) && (0 <= j) && (j < height)) {
                            colours[i][j] = BLACK;
                        }
                    }
                }
            }
        }

        if (vgrid != 0.0) {
            /* draw vertical gridlines */
            for (theta1 = -0.5 * Math.PI; theta1 < (0.5 * Math.PI); theta1 += (0.5 / width / scale)) {
                y = Math.sin(theta1);

                if ((ymin <= y) && (y <= ymax)) {
                    zz = Math.sqrt((c * (1.0 + Math.sin(theta1 / k1))) / (1.0 - Math.sin(theta1 / k1)));

                    for (theta2 = 0.0; theta2 > (-180.0 + Math.toDegrees(lon)); theta2 -= vgrid)
                        ;

                    for (theta2 = theta2; theta2 < (180.0 + Math.toDegrees(lon)); theta2 += vgrid) {
                        z1 = Math.toRadians(theta2) - lon;
                        x1 = -zz * Math.sin(z1 / k1);
                        y1 = -zz * Math.cos(z1 / k1);

                        i = (int) (0.5 * ((height * scale * x1) + width));
                        j = (int) (0.5 * ((height * scale * (y1 + y2)) + height));

                        if ((0 <= i) && (i < width) && (0 <= j) && (j < height)) {
                            colours[i][j] = BLACK;
                        }
                    }
                }
            }
        }
    }

    if (doShade) {
        smoothshades();
    }

    doOutlining();
}

From source file:fr.amap.viewer3d.object.camera.TrackballCamera.java

@Override
public void rotateAroundPoint(Vec3F axis, Vec3F pivot, float angle) {

    if (axis.x != 0) {

        //this.angleX = angle;
        this.angleX = this.angleX + angle;

        float r = (float) Math.sqrt(Math.pow(pivot.z - location.z, 2) + Math.pow(pivot.y - location.y, 2));

        float z = (float) (pivot.z + r * Math.cos(this.angleX));
        float y = (float) (pivot.y + r * Math.sin(this.angleX));

        location.z = z;/*from   w  w  w.  j  a v a 2s  . com*/
        location.y = y;

    } else if (axis.y != 0) {

        //this.angleY = angle;
        this.angleY = this.angleY + angle;

        /*
        if(Math.toDegrees(angleY)>360){
                
        angleY = (float) Math.toRadians(Math.toDegrees(angleY) - 360);
        }else if(Math.toDegrees(angleY)<0){
        angleY = (float) Math.toRadians(360 - Math.toDegrees(-angleY));
        }
        */

        float r = (float) Math.sqrt(Math.pow(pivot.x - location.x, 2) + Math.pow(pivot.z - location.z, 2));

        float x = (float) (pivot.x + r * Math.cos(this.angleY));
        float z = (float) (pivot.z + r * Math.sin(this.angleY));

        location.x = x;
        location.z = z;

    } else if (axis.z != 0) {

        //this.angleZ = angle;
        this.angleZ = this.angleZ + angle;

        float r = (float) Math.sqrt(Math.pow(pivot.x - location.x, 2) + Math.pow(pivot.y - location.y, 2));

        float x = (float) (pivot.x + r * Math.cos(this.angleZ));
        float y = (float) (pivot.y + r * Math.sin(this.angleZ));

        location.x = x;
        location.y = y;
    }

    target = pivot;

    updateViewMatrix();
}

From source file:com.nanosheep.bikeroute.parser.GoogleElevationParser.java

/**
 * Calculate the distance between two points of the earth using
 * the haversine formula./*from w ww.j a va2s  . com*/
 * @param lat Starting latitude.
 * @param lng Starting longitude.
 * @param latA End latitude,
 * @param lngA End longitude.
 * @return The distance between the two points in m.
 */

private double pointDiff(final double lat, final double lng, final double latA, final double lngA) {
    final double dLat = Math.toRadians(latA - lat);
    final double dLon = Math.toRadians(lngA - lng);
    final double a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.cos(Math.toRadians(lat))
            * Math.cos(Math.toRadians(latA)) * Math.sin(dLon / 2) * Math.sin(dLon / 2);
    final double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
    return BikeRouteConsts.EARTH_RADIUS * c * 1000;
}

From source file:io.github.malapert.jwcs.coordsystem.Utility.java

/**
 * Calculates the matrix that represents a 3d rotation around the Z axis.
 *     /*from  w ww . j a  v  a  2  s. c  om*/
 * @param angle Rotation angle in degrees
 * @return A 3x3 matrix representing the rotation about angle around Z axis.
 */
public final static RealMatrix rotZ(double angle) {
    double angleRadians = Math.toRadians(angle);
    double[][] array = { { Math.cos(angleRadians), Math.sin(angleRadians), 0.d },
            { -1 * Math.sin(angleRadians), Math.cos(angleRadians), 0.d }, { 0.d, 0.d, 1.d } };
    return MatrixUtils.createRealMatrix(array);
}

From source file:com.alvermont.terraj.planet.project.AzimuthProjection.java

/**
 * Carry out the projection/*from   ww  w .j a v a2  s .  c om*/
 */
public void project() {
    setcolours();

    final int width = getParameters().getProjectionParameters().getWidth();
    final int height = getParameters().getProjectionParameters().getHeight();

    final double lat = getParameters().getProjectionParameters().getLatitudeRadians();
    final double lon = getParameters().getProjectionParameters().getLongitudeRadians();

    final double scale = getParameters().getProjectionParameters().getScale();

    final double hgrid = getParameters().getProjectionParameters().getHgrid();
    final double vgrid = getParameters().getProjectionParameters().getVgrid();

    cacheParameters();

    final double sla = Math.sin(lat);
    final double cla = Math.cos(lat);
    final double slo = Math.sin(lon);
    final double clo = Math.cos(lon);

    colours = new short[width][height];
    shades = new short[width][height];

    depth = (3 * ((int) (log2(scale * height)))) + 6;

    double x;
    double y;
    double z;
    double x1;
    double y1;
    double z1;
    double zz;
    double theta1;
    double theta2;
    double ymin;
    double ymax;
    int i;
    int j;

    ymin = 2.0;
    ymax = -2.0;

    final boolean doShade = getParameters().getProjectionParameters().isDoShade();

    log.debug("Azimuth projection starting with seed " + getParameters().getPlanetParameters().getSeed());

    progress.progressStart(height, "Generating Terrain");

    for (j = 0; j < height; ++j) {
        progress.progressStep(j);

        for (i = 0; i < width; ++i) {
            x = ((2.0 * i) - width) / height / scale;
            y = ((2.0 * j) - height) / height / scale;
            zz = (x * x) + (y * y);
            z = 1.0 - (0.5 * zz);

            if (z < -1.0) {
                colours[i][j] = backgroundColour;

                if (doShade) {
                    shades[i][j] = 255;
                }
            } else {
                zz = Math.sqrt(1.0 - (0.25 * zz));
                x = x * zz;
                y = y * zz;

                x1 = (clo * x) + (slo * sla * y) + (slo * cla * z);
                y1 = (cla * y) - (sla * z);
                z1 = (-slo * x) + (clo * sla * y) + (clo * cla * z);

                if (y1 < ymin) {
                    ymin = y1;
                }

                if (y1 > ymax) {
                    ymax = y1;
                }

                colours[i][j] = (short) planet0(x1, y1, z1);

                if (doShade) {
                    shades[i][j] = shade;
                }
            }
        }
    }

    progress.progressComplete("Terrain Generated");

    if (hgrid != 0.0) {
        /* draw horizontal gridlines */
        for (theta1 = 0.0; theta1 > -90.0; theta1 -= hgrid)
            ;

        for (theta1 = theta1; theta1 < 90.0; theta1 += hgrid) {
            y = Math.sin(Math.toRadians(theta1));

            if ((ymin <= y) && (y <= ymax)) {
                zz = Math.sqrt(1 - (y * y));

                for (theta2 = -Math.PI; theta2 < Math.PI; theta2 += (0.5 / width / scale)) {
                    x = Math.sin(theta2) * zz;
                    z = Math.cos(theta2) * zz;

                    x1 = (clo * x) - (slo * z);
                    y1 = (slo * sla * x) + (cla * y) + (clo * sla * z);
                    z1 = (slo * cla * x) - (sla * y) + (clo * cla * z);

                    if (z1 != -1.0) {
                        i = (int) (0.5 * (((height * scale * x1) / Math.sqrt(0.5 + (0.5 * z1))) + width));
                        j = (int) (0.5 * (((height * scale * y1) / Math.sqrt(0.5 + (0.5 * z1))) + height));

                        if ((0 <= i) && (i < width) && (0 <= j) && (j < height)) {
                            colours[i][j] = BLACK;
                        }
                    }
                }
            }
        }
    }

    if (vgrid != 0.0) {
        /* draw vertical gridlines */
        for (theta2 = -Math.PI; theta2 < Math.PI; theta2 += (0.5 / width / scale)) {
            y = Math.sin(theta2);

            if ((ymin <= y) && (y <= ymax)) {
                for (theta1 = 0.0; theta1 < 360.0; theta1 += vgrid) {
                    x = Math.sin(Math.toRadians(theta1)) * Math.cos(theta2);
                    z = Math.cos(Math.toRadians(theta1)) * Math.cos(theta2);

                    x1 = (clo * x) - (slo * z);
                    y1 = (slo * sla * x) + (cla * y) + (clo * sla * z);
                    z1 = (slo * cla * x) - (sla * y) + (clo * cla * z);

                    if (z1 != -1.0) {
                        i = (int) (0.5 * (((height * scale * x1) / Math.sqrt(0.5 + (0.5 * z1))) + width));
                        j = (int) (0.5 * (((height * scale * y1) / Math.sqrt(0.5 + (0.5 * z1))) + height));

                        if ((0 <= i) && (i < width) && (0 <= j) && (j < height)) {
                            colours[i][j] = BLACK;
                        }
                    }
                }
            }
        }
    }

    if (doShade) {
        smoothshades();
    }

    doOutlining();
}

From source file:com.alvermont.terraj.planet.project.OrthographicProjection.java

/**
 * Carry out the projection// w  ww.j av  a 2 s  . co m
 */
public void project() {
    setcolours();

    final int width = getParameters().getProjectionParameters().getWidth();
    final int height = getParameters().getProjectionParameters().getHeight();

    final double lat = getParameters().getProjectionParameters().getLatitudeRadians();
    final double lon = getParameters().getProjectionParameters().getLongitudeRadians();

    final double scale = getParameters().getProjectionParameters().getScale();

    final double hgrid = getParameters().getProjectionParameters().getHgrid();
    final double vgrid = getParameters().getProjectionParameters().getVgrid();

    final boolean doShade = getParameters().getProjectionParameters().isDoShade();

    colours = new short[width][height];
    shades = new short[width][height];

    double x;
    double y;
    double z;
    double x1;
    double y1;
    double z1;
    double ymin;
    double ymax;
    double theta1;
    double theta2;
    double zz;
    int i;
    int j;

    depth = (3 * ((int) (log2(scale * height)))) + 6;

    final double sla = Math.sin(lat);
    final double cla = Math.cos(lat);
    final double slo = Math.sin(lon);
    final double clo = Math.cos(lon);

    cacheParameters();

    ymin = 2.0;
    ymax = -2.0;

    progress.progressStart(height, "Generating Terrain");

    for (j = 0; j < height; ++j) {
        progress.progressStep(j);

        for (i = 0; i < width; ++i) {
            x = ((2.0 * i) - width) / height / scale;
            y = ((2.0 * j) - height) / height / scale;

            if (((x * x) + (y * y)) > 1.0) {
                colours[i][j] = backgroundColour;

                if (doShade) {
                    shades[i][j] = 255;
                }
            } else {
                z = Math.sqrt(1.0 - (x * x) - (y * y));
                x1 = (clo * x) + (slo * sla * y) + (slo * cla * z);
                y1 = (cla * y) - (sla * z);
                z1 = (-slo * x) + (clo * sla * y) + (clo * cla * z);

                if (y1 < ymin) {
                    ymin = y1;
                }

                if (y1 > ymax) {
                    ymax = y1;
                }

                colours[i][j] = (short) planet0(x1, y1, z1);

                if (doShade) {
                    shades[i][j] = shade;
                }
            }
        }
    }

    progress.progressComplete("Terrain Generated");

    if (hgrid != 0.0) {
        /* draw horizontal gridlines */
        for (theta1 = 0.0; theta1 > -90.0; theta1 -= hgrid)
            ;

        for (theta1 = theta1; theta1 < 90.0; theta1 += hgrid) {
            y = Math.sin(Math.toRadians(theta1));

            if ((ymin <= y) && (y <= ymax)) {
                zz = Math.sqrt(1 - (y * y));

                for (theta2 = -Math.PI; theta2 < Math.PI; theta2 += (0.5 / width / scale)) {
                    x = Math.sin(theta2) * zz;
                    z = Math.cos(theta2) * zz;

                    x1 = (clo * x) + (slo * z);
                    y1 = ((slo * sla * x) + (cla * y)) - (clo * sla * z);
                    z1 = (-slo * cla * x) + (sla * y) + (clo * cla * z);

                    if (0.0 >= z1) {
                        i = (int) (0.5 * ((height * scale * x1) + width));
                        j = (int) (0.5 * ((height * scale * y1) + height));

                        if ((0 <= i) && (i < width) && (0 <= j) && (j < height)) {
                            colours[i][j] = BLACK;
                        }
                    }
                }
            }
        }
    }

    if (vgrid != 0.0) {
        /* draw vertical gridlines */
        for (theta2 = -Math.PI; theta2 < Math.PI; theta2 += (0.5 / width / scale)) {
            y = Math.sin(theta2);

            if ((ymin <= y) && (y <= ymax)) {
                for (theta1 = 0.0; theta1 < 360.0; theta1 += vgrid) {
                    x = Math.sin(Math.toRadians(theta1)) * Math.cos(theta2);
                    z = Math.cos(Math.toRadians(theta1)) * Math.cos(theta2);

                    x1 = (clo * x) + (slo * z);
                    y1 = ((slo * sla * x) + (cla * y)) - (clo * sla * z);
                    z1 = (-slo * cla * x) + (sla * y) + (clo * cla * z);

                    if (0.0 >= z1) {
                        i = (int) (0.5 * ((height * scale * x1) + width));
                        j = (int) (0.5 * ((height * scale * y1) + height));

                        if ((0 <= i) && (i < width) && (0 <= j) && (j < height)) {
                            colours[i][j] = BLACK;
                        }
                    }
                }
            }
        }
    }

    if (doShade) {
        smoothshades();
    }

    doOutlining();
}