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:org.eclipse.swt.snippets.Snippet209.java

static void drawTorus(GL gl, float r, float R, int nsides, int rings) {
    float ringDelta = 2.0f * (float) Math.PI / rings;
    float sideDelta = 2.0f * (float) Math.PI / nsides;
    float theta = 0.0f, cosTheta = 1.0f, sinTheta = 0.0f;
    for (int i = rings - 1; i >= 0; i--) {
        float theta1 = theta + ringDelta;
        float cosTheta1 = (float) Math.cos(theta1);
        float sinTheta1 = (float) Math.sin(theta1);
        gl.glBegin(GL.GL_QUAD_STRIP);//  w  w  w  . ja v a  2s. c om
        float phi = 0.0f;
        for (int j = nsides; j >= 0; j--) {
            phi += sideDelta;
            float cosPhi = (float) Math.cos(phi);
            float sinPhi = (float) Math.sin(phi);
            float dist = R + r * cosPhi;
            gl.glNormal3f(cosTheta1 * cosPhi, -sinTheta1 * cosPhi, sinPhi);
            gl.glVertex3f(cosTheta1 * dist, -sinTheta1 * dist, r * sinPhi);
            gl.glNormal3f(cosTheta * cosPhi, -sinTheta * cosPhi, sinPhi);
            gl.glVertex3f(cosTheta * dist, -sinTheta * dist, r * sinPhi);
        }
        gl.glEnd();
        theta = theta1;
        cosTheta = cosTheta1;
        sinTheta = sinTheta1;
    }
}

From source file:Main.java

/**
 * Gets the great circle distance in kilometers between two geographical points, using
 * the <a href="http://en.wikipedia.org/wiki/Haversine_formula">haversine formula</a>.
 *
 * @param latitude1 the latitude of the first point
 * @param longitude1 the longitude of the first point
 * @param latitude2 the latitude of the second point
 * @param longitude2 the longitude of the second point
 * @return the distance, in kilometers, between the two points
 *//*ww w. j a v  a 2s. co m*/
public static float getDistance(double latitude1, double longitude1, double latitude2, double longitude2) {
    double dLat = Math.toRadians(latitude2 - latitude1);
    double dLon = Math.toRadians(longitude2 - longitude1);
    double lat1 = Math.toRadians(latitude1);
    double lat2 = Math.toRadians(latitude2);
    double sqrtHaversineLat = Math.sin(dLat / 2);
    double sqrtHaversineLon = Math.sin(dLon / 2);
    double a = sqrtHaversineLat * sqrtHaversineLat
            + sqrtHaversineLon * sqrtHaversineLon * Math.cos(lat1) * Math.cos(lat2);
    double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));

    return (float) (EARTH_RADIUS_KM * c);
}

From source file:Main.java

private static double transformLat(double x, double y) {
    double ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * Math.sqrt(Math.abs(x));
    ret += (20.0 * Math.sin(6.0 * x * pi) + 20.0 * Math.sin(2.0 * x * pi)) * 2.0 / 3.0;
    ret += (20.0 * Math.sin(y * pi) + 40.0 * Math.sin(y / 3.0 * pi)) * 2.0 / 3.0;
    ret += (160.0 * Math.sin(y / 12.0 * pi) + 320 * Math.sin(y * pi / 30.0)) * 2.0 / 3.0;
    return ret;/*  ww  w .j a va2  s . c o m*/
}

From source file:eu.crisis_economics.abm.bank.strategies.TimeSeriesCatalogue.java

/** A sinusoidal time series with the given period. The sinusoid is
  * suspended in such a way that crests take the value maxY and troughs
  * take the value minY. *///from w  ww  .ja  va 2 s  .  c  o m
public static UnivariateFunction sinusoidalFunction(final double period, final double minY, final double maxY) {
    class SuspendedSinusoid implements UnivariateFunction {
        private final double __periodMult = __2Pi / period, __amplitude = (maxY - minY) / 2., __minY = minY;

        @Override
        public double value(double time) {
            return (Math.sin(time * __periodMult) + 1.) * __amplitude + __minY;
        }
    }
    ;
    return new SuspendedSinusoid();
}

From source file:SineFunctionPlotting.java

static int getNormalizedSine(int x, int halfY, int maxX) {
    double piDouble = 2 * Math.PI;
    double factor = piDouble / maxX;
    return (int) (Math.sin(x * factor) * halfY + halfY);
}

From source file:SineDraw.java

public void setCycles(int newCycles) {
    cycles = newCycles;//from   w w w .  ja  v a 2s.  c  om
    points = SCALEFACTOR * cycles * 2;
    sines = new double[points];
    for (int i = 0; i < points; i++) {
        double radians = (Math.PI / SCALEFACTOR) * i;
        sines[i] = Math.sin(radians);
    }
    repaint();
}

From source file:Main.java

/**
 * Gets straighten matrix for the given bounds and degrees.
 *//* w w w .j  a va  2 s  .  c  o  m*/
public static void getStraightenMatrix(RectF bounds, float degrees, Matrix matrix) {
    matrix.reset();
    if ((degrees != 0) && !bounds.isEmpty()) {
        float w = bounds.width() / 2;
        float h = bounds.height() / 2;
        float adjustAngle;
        if ((degrees < 0 && w > h) || (degrees > 0 && w <= h)) {
            // The top left point is the boundary.
            adjustAngle = (float) Math.atan(h / -w) + MATH_PI + degrees * DEGREES_TO_RADIAN;
        } else {
            // The top right point is the boundary.
            adjustAngle = (float) Math.atan(h / w) - MATH_PI + degrees * DEGREES_TO_RADIAN;
        }
        float radius = (float) Math.hypot(w, h);
        float scaleX = (float) Math.abs(radius * Math.cos(adjustAngle)) / w;
        float scaleY = (float) Math.abs(radius * Math.sin(adjustAngle)) / h;
        float scale = Math.max(scaleX, scaleY);

        postRotateMatrix(degrees, new RectF(bounds), matrix);
        matrix.postScale(scale, scale);
    }
}

From source file:Main.java

public static double[] findClosePointsForDrawingArc(int x1, int x2, int y1, int y2) {
    double[] xy = new double[2];
    double d = 5;
    double angle = 0;
    //        if ((x1 - x2) != 0) {
    // tan a = y2-y1/x2-x1
    //        System.out.println(" x1  = " + x1 + " y1  = " + y1 + " |  x2  = " + x2 + " |  y2  = " + y2);
    //        angle = Math.toDegrees(Math.atan2(Math.abs(y1 - y2), Math.abs(x1 - x2)));
    //        xy[0] = (Math.cos(angle) * d) + x2;
    //        xy[1] = (Math.sin(angle) * d) + y2;
    //        System.out.println("xt " + xy[0] + "  yt " + xy[1] + " angel would be : " + angle + " and Different number : " + d);

    angle = Math.toDegrees(Math.atan2(Math.abs(y1 - y2), Math.abs(x1 - x2)));
    System.out.println("----------------------------------vvvvv----------------------------------------");
    System.out.println(" ");
    System.out.println("x1 " + x1 + "  y1 " + y1 + " |  x2 " + x2 + " y2 " + y2);
    System.out.println("Math.abs(x1-x2)" + Math.abs(x1 - x2) + "  |  Math.abs(y1-y2) " + Math.abs(y1 - y2)
            + "  |   angel would be Math.toDegrees(Math.atan2(Math.abs(y1 - y2), Math.abs(x1 - x2))) : " + angle
            + " |   Different number : " + d);
    if (x1 > x2) {
        if (y1 > y2) {
            System.out.println(/*from  w  ww .  j a  v  a2s  . c  o  m*/
                    "x1>x2 && y1>y2 | x2= " + x2 + " | cos(angle) = " + Math.cos(angle) + " | d = " + d);
            xy[0] = x2 + (Math.abs(Math.cos(angle)) * d);
            System.out.println(
                    "x1>x2 && y1>y2 | x2= " + x2 + " | sin(angle) = " + Math.sin(angle) + " | d = " + d);
            xy[1] = y2 + (Math.abs(Math.sin(angle)) * d);
        } else if (y1 < y2) {
            System.out.println(
                    "x1>x2 && y1< y2 | x2= " + x2 + " | cos(angle) = " + Math.cos(angle) + " | d = " + d);
            xy[0] = x2 + (Math.abs(Math.cos(angle)) * d);
            System.out.println(
                    "x1>x2 && y1< y2 | x2= " + x2 + " | sin(angle) = " + Math.sin(angle) + " | d = " + d);
            xy[1] = y2 - (Math.abs(Math.sin(angle)) * d);
        } else {
            System.out.println(
                    "x1>x2 && y1 = y2 | x2= " + x2 + " | cos(angle) = " + Math.cos(angle) + " | d = " + d);
            xy[0] = x2 + (Math.abs(Math.cos(angle)) * d);
            System.out.println(
                    "x1>x2 && y1 = y2 | x2= " + x2 + " | sin(angle) = " + Math.sin(angle) + " | d = " + d);
            xy[1] = y2;
        }

    } else if (x1 < x2) {
        if (y1 > y2) {
            System.out.println(
                    "x1<x2 && y1>y2 | x2= " + x2 + " | cos(angle) = " + Math.cos(angle) + " | d = " + d);
            xy[0] = x2 - (Math.abs(Math.cos(angle)) * d);
            System.out.println(
                    "x1<x2 && y1>y2 | x2= " + x2 + " | sin(angle) = " + Math.sin(angle) + " | d = " + d);
            xy[1] = y2 + (Math.abs(Math.sin(angle)) * d);
        } else if (y1 < y2) {
            System.out.println(
                    "x1<x2 && y1< y2 | x2= " + x2 + " | cos(angle) = " + Math.cos(angle) + " | d = " + d);
            xy[0] = x2 - (Math.abs(Math.cos(angle)) * d);
            System.out.println(
                    "x1<x2 && y1< y2 | x2= " + x2 + " | sin(angle) = " + Math.sin(angle) + " | d = " + d);
            xy[1] = y2 - (Math.abs(Math.sin(angle)) * d);
        } else {
            System.out.println(
                    "x1<x2 && y1 =y2 | x2= " + x2 + " | cos(angle) = " + Math.cos(angle) + " | d = " + d);
            xy[0] = x2 - (Math.abs(Math.cos(angle)) * d);
            System.out.println(
                    "x1<x2 && y1 =y2 | x2= " + x2 + " | sin(angle) = " + Math.sin(angle) + " | d = " + d);
            xy[1] = y2;
        }
    } else {
        if (y1 > y2) {
            System.out.println(
                    "x1=x2 && y1>y2 | x2= " + x2 + " | cos(angle) = " + Math.cos(angle) + " | d = " + d);
            xy[0] = x2;
            System.out.println(
                    "x1=x2 && y1>y2 | x2= " + x2 + " | sin(angle) = " + Math.sin(angle) + " | d = " + d);
            xy[1] = y2 + (Math.abs(Math.sin(angle) * d));
        } else if (y1 < y2) {
            System.out.println(
                    "x1=x2 && y1< y2 | x2= " + x2 + " | cos(angle) = " + Math.cos(angle) + " | d = " + d);
            xy[0] = x2;
            System.out.println(
                    "x1=x2 && y1< y2 | x2= " + x2 + " | sin(angle) = " + Math.sin(angle) + " | d = " + d);
            xy[1] = y2 - (Math.abs(Math.sin(angle)) * d);
        } else {
            System.out.println(
                    "x1=x2 && y1 =y2 | x2= " + x2 + " | cos(angle) = " + Math.cos(angle) + " | d = " + d);
            xy[0] = x2;
            System.out.println(
                    "x1=x2 && y1 =y2 | x2= " + x2 + " | sin(angle) = " + Math.sin(angle) + " | d = " + d);
            xy[1] = y2;
        }
    }
    System.out.println(" X1 = " + x1 + " | X2 = " + x2 + " | Y1 = " + y1 + " | Y2 = " + y2 + " | X target = "
            + xy[0] + " | Y target = " + xy[1]);
    System.out.println(" ");
    System.out.println("--------------------------------^^^^----------------------------------------");
    //        } else {
    //            if (y2 > y1) {
    //                System.out.println("(x1 - x2) == 0 | X1 = " + x1 + " | X2 = " + x2 + " | Y1 = " + y1 + " | Y2 = " + y2 +   " | X target = " + x1 + " | Y target = " + (y2 + ((y2 - y1) / 12)));
    //                xy[0] = (x2);
    //                xy[1] = y2 + ((y2 - y1) / 12);
    //            } else {
    //                System.out.println("(x1 - x2) == 0 | X1 = " + x1 + " | X2 = " + x2 + " | Y1 = " + y1 + " | Y2 = " + y2  + " | X target = " + x1 + " | Y target = " + (y2 - ((y2 - y1) / 12)));
    //                xy[0] = (x2);
    //                xy[1] = y2 - ((y2 - y1 )/ 12);
    //            }
    //        }
    return xy;
}

From source file:com.bleedobsidian.datawave.utils.Sinewave.java

/**
 * Produce sound data from a sine wave./*from w w  w. j  a v a 2 s  .c o  m*/
 * 
 * @param sampleRate Sample rate.
 * @param frequency Frequency in Hertz.
 * @param duration Duration in seconds.
 * @param amplitude Volume scale.
 * 
 * @return Samples.
 */
public static double[] generateSound(double sampleRate, double frequency, double duration, double amplitude) {
    int n = (int) (sampleRate * duration);
    double[] a = new double[n + 1];

    for (int i = 0; i <= n; i++) {
        a[i] = amplitude * Math.sin(2 * Math.PI * i * frequency / sampleRate);
    }

    return a;
}

From source file:Main.java

public static double[] gcj02towgs84(double lng, double lat) {
    if (out_of_china(lng, lat)) {
        return new double[] { lng, lat };
    }//  w w w. j  a  v a  2 s  . c  om
    double dlat = transformlat(lng - 105.0, lat - 35.0);
    double dlng = transformlng(lng - 105.0, lat - 35.0);
    double radlat = lat / 180.0 * pi;
    double magic = Math.sin(radlat);
    magic = 1 - ee * magic * magic;
    double sqrtmagic = Math.sqrt(magic);
    dlat = (dlat * 180.0) / ((a * (1 - ee)) / (magic * sqrtmagic) * pi);
    dlng = (dlng * 180.0) / (a / sqrtmagic * Math.cos(radlat) * pi);
    double mglat = lat + dlat;
    double mglng = lng + dlng;
    return new double[] { lng * 2 - mglng, lat * 2 - mglat };
}