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

public static void main(String[] args) {
    final Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());
    Composite comp = new Composite(shell, SWT.NONE);
    comp.setLayout(new FillLayout());
    GLData data = new GLData();
    data.doubleBuffer = true;/*from   w  w  w  .  j a v a 2s  .  co m*/
    final GLCanvas canvas = new GLCanvas(comp, SWT.NONE, data);

    canvas.setCurrent();
    final GLContext context = GLDrawableFactory.getFactory(GLProfile.getGL2GL3()).createExternalGLContext();
    ;

    canvas.addListener(SWT.Resize, new Listener() {
        @Override
        public void handleEvent(Event event) {
            Rectangle bounds = canvas.getBounds();
            float fAspect = (float) bounds.width / (float) bounds.height;
            canvas.setCurrent();
            context.makeCurrent();
            GL2 gl = (GL2) context.getGL();
            gl.glViewport(0, 0, bounds.width, bounds.height);
            gl.glMatrixMode(GL2.GL_PROJECTION);
            gl.glLoadIdentity();
            GLU glu = new GLU();
            glu.gluPerspective(45.0f, fAspect, 0.5f, 400.0f);
            gl.glMatrixMode(GL2.GL_MODELVIEW);
            gl.glLoadIdentity();
            context.release();
        }
    });

    context.makeCurrent();
    GL2 gl = (GL2) context.getGL();
    gl.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
    gl.glColor3f(1.0f, 0.0f, 0.0f);
    gl.glHint(GL2.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST);
    gl.glClearDepth(1.0);
    gl.glLineWidth(2);
    gl.glEnable(GL.GL_DEPTH_TEST);
    context.release();

    shell.setText("SWT/JOGL Example");
    shell.setSize(640, 480);
    shell.open();

    display.asyncExec(new Runnable() {
        int rot = 0;

        @Override
        public void run() {
            if (!canvas.isDisposed()) {
                canvas.setCurrent();
                context.makeCurrent();
                GL2 gl = (GL2) context.getGL();
                gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
                gl.glClearColor(.3f, .5f, .8f, 1.0f);
                gl.glLoadIdentity();
                gl.glTranslatef(0.0f, 0.0f, -10.0f);
                float frot = rot;
                gl.glRotatef(0.15f * rot, 2.0f * frot, 10.0f * frot, 1.0f);
                gl.glRotatef(0.3f * rot, 3.0f * frot, 1.0f * frot, 1.0f);
                rot++;
                gl.glPolygonMode(GL.GL_FRONT_AND_BACK, GL2.GL_LINE);
                gl.glColor3f(0.9f, 0.9f, 0.9f);
                drawTorus(gl, 1, 1.9f + ((float) Math.sin((0.004f * frot))), 15, 15);
                canvas.swapBuffers();
                context.release();
                display.asyncExec(this);
            }
        }
    });

    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

From source file:org.eclipse.swt.snippets.Snippet209.java

public static void main(String[] args) {
    final Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());
    Composite comp = new Composite(shell, SWT.NONE);
    comp.setLayout(new FillLayout());
    GLData data = new GLData();
    data.doubleBuffer = true;/*from  w  ww  .jav  a  2s.co m*/
    final GLCanvas canvas = new GLCanvas(comp, SWT.NONE, data);

    canvas.setCurrent();
    final GLContext context = GLDrawableFactory.getFactory().createExternalGLContext();

    canvas.addListener(SWT.Resize, new Listener() {
        public void handleEvent(Event event) {
            Rectangle bounds = canvas.getBounds();
            float fAspect = (float) bounds.width / (float) bounds.height;
            canvas.setCurrent();
            context.makeCurrent();
            GL gl = context.getGL();
            gl.glViewport(0, 0, bounds.width, bounds.height);
            gl.glMatrixMode(GL.GL_PROJECTION);
            gl.glLoadIdentity();
            GLU glu = new GLU();
            glu.gluPerspective(45.0f, fAspect, 0.5f, 400.0f);
            gl.glMatrixMode(GL.GL_MODELVIEW);
            gl.glLoadIdentity();
            context.release();
        }
    });

    context.makeCurrent();
    GL gl = context.getGL();
    gl.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
    gl.glColor3f(1.0f, 0.0f, 0.0f);
    gl.glHint(GL.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST);
    gl.glClearDepth(1.0);
    gl.glLineWidth(2);
    gl.glEnable(GL.GL_DEPTH_TEST);
    context.release();

    shell.setText("SWT/JOGL Example");
    shell.setSize(640, 480);
    shell.open();

    display.asyncExec(new Runnable() {
        int rot = 0;

        public void run() {
            if (!canvas.isDisposed()) {
                canvas.setCurrent();
                context.makeCurrent();
                GL gl = context.getGL();
                gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
                gl.glClearColor(.3f, .5f, .8f, 1.0f);
                gl.glLoadIdentity();
                gl.glTranslatef(0.0f, 0.0f, -10.0f);
                float frot = rot;
                gl.glRotatef(0.15f * rot, 2.0f * frot, 10.0f * frot, 1.0f);
                gl.glRotatef(0.3f * rot, 3.0f * frot, 1.0f * frot, 1.0f);
                rot++;
                gl.glPolygonMode(GL.GL_FRONT_AND_BACK, GL.GL_LINE);
                gl.glColor3f(0.9f, 0.9f, 0.9f);
                drawTorus(gl, 1, 1.9f + ((float) Math.sin((0.004f * frot))), 15, 15);
                canvas.swapBuffers();
                context.release();
                display.asyncExec(this);
            }
        }
    });

    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

From source file:org.eclipse.swt.snippets.Snippet195.java

public static void main(String[] args) {
    final Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());
    Composite comp = new Composite(shell, SWT.NONE);
    comp.setLayout(new FillLayout());
    GLData data = new GLData();
    data.doubleBuffer = true;/*from w w  w  .  j  av  a 2 s . c  om*/
    final GLCanvas canvas = new GLCanvas(comp, SWT.NONE, data);

    canvas.setCurrent();
    try {
        GLContext.useContext(canvas);
    } catch (LWJGLException e) {
        e.printStackTrace();
    }

    canvas.addListener(SWT.Resize, new Listener() {
        public void handleEvent(Event event) {
            Rectangle bounds = canvas.getBounds();
            float fAspect = (float) bounds.width / (float) bounds.height;
            canvas.setCurrent();
            try {
                GLContext.useContext(canvas);
            } catch (LWJGLException e) {
                e.printStackTrace();
            }
            GL11.glViewport(0, 0, bounds.width, bounds.height);
            GL11.glMatrixMode(GL11.GL_PROJECTION);
            GL11.glLoadIdentity();
            GLU.gluPerspective(45.0f, fAspect, 0.5f, 400.0f);
            GL11.glMatrixMode(GL11.GL_MODELVIEW);
            GL11.glLoadIdentity();
        }
    });

    GL11.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
    GL11.glColor3f(1.0f, 0.0f, 0.0f);
    GL11.glHint(GL11.GL_PERSPECTIVE_CORRECTION_HINT, GL11.GL_NICEST);
    GL11.glClearDepth(1.0);
    GL11.glLineWidth(2);
    GL11.glEnable(GL11.GL_DEPTH_TEST);

    shell.setText("SWT/LWJGL Example");
    shell.setSize(640, 480);
    shell.open();

    display.asyncExec(new Runnable() {
        int rot = 0;

        public void run() {
            if (!canvas.isDisposed()) {
                canvas.setCurrent();
                try {
                    GLContext.useContext(canvas);
                } catch (LWJGLException e) {
                    e.printStackTrace();
                }
                GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
                GL11.glClearColor(.3f, .5f, .8f, 1.0f);
                GL11.glLoadIdentity();
                GL11.glTranslatef(0.0f, 0.0f, -10.0f);
                float frot = rot;
                GL11.glRotatef(0.15f * rot, 2.0f * frot, 10.0f * frot, 1.0f);
                GL11.glRotatef(0.3f * rot, 3.0f * frot, 1.0f * frot, 1.0f);
                rot++;
                GL11.glPolygonMode(GL11.GL_FRONT_AND_BACK, GL11.GL_LINE);
                GL11.glColor3f(0.9f, 0.9f, 0.9f);
                drawTorus(1, 1.9f + ((float) Math.sin((0.004f * frot))), 15, 15);
                canvas.swapBuffers();
                display.asyncExec(this);
            }
        }
    });

    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

From source file:org.eclipse.swt.snippets.Snippet341.java

public static void main(String[] args) {
    final Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new FormLayout());
    GLData data = new GLData();
    data.doubleBuffer = true;/* w  w w  .ja v  a 2 s .c  o  m*/
    final GLCanvas canvas = new GLCanvas(shell, SWT.NONE, data);

    canvas.setCurrent();
    GL.createCapabilities();

    canvas.addListener(SWT.Resize, event -> {
        Rectangle bounds = canvas.getBounds();
        float fAspect = (float) bounds.width / (float) bounds.height;
        canvas.setCurrent();
        GL.createCapabilities();
        GL11.glViewport(0, 0, bounds.width, bounds.height);
        GL11.glMatrixMode(GL11.GL_PROJECTION);
        GL11.glLoadIdentity();
        float near = 0.5f;
        float bottom = -near * (float) Math.tan(45.f / 2);
        float left = fAspect * bottom;
        GL11.glFrustum(left, -left, bottom, -bottom, near, 400.f);
        GL11.glMatrixMode(GL11.GL_MODELVIEW);
        GL11.glLoadIdentity();
    });

    GL11.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
    GL11.glColor3f(1.0f, 0.0f, 0.0f);
    GL11.glHint(GL11.GL_PERSPECTIVE_CORRECTION_HINT, GL11.GL_NICEST);
    GL11.glClearDepth(1.0);
    GL11.glLineWidth(2);
    GL11.glEnable(GL11.GL_DEPTH_TEST);

    Button button = new Button(shell, SWT.PUSH);
    button.setText("Capture");
    button.addListener(SWT.Selection, event -> capture(canvas));

    FormData formData = new FormData(640, 480);
    formData.top = new FormAttachment(0, 0);
    formData.left = new FormAttachment(0, 0);
    formData.bottom = new FormAttachment(button, 0);
    formData.right = new FormAttachment(100, 0);
    canvas.setLayoutData(formData);
    formData = new FormData();
    formData.left = new FormAttachment(0, 0);
    formData.bottom = new FormAttachment(100, 0);
    formData.right = new FormAttachment(100, 0);
    button.setLayoutData(formData);

    shell.pack();
    shell.open();

    display.asyncExec(new Runnable() {
        int rot = 0;

        @Override
        public void run() {
            if (!canvas.isDisposed()) {
                canvas.setCurrent();
                GL.createCapabilities();
                GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
                GL11.glClearColor(.3f, .5f, .8f, 1.0f);
                GL11.glLoadIdentity();
                GL11.glTranslatef(0.0f, 0.0f, -10.0f);
                float frot = rot;
                GL11.glRotatef(0.15f * rot, 2.0f * frot, 10.0f * frot, 1.0f);
                GL11.glRotatef(0.3f * rot, 3.0f * frot, 1.0f * frot, 1.0f);
                rot++;
                GL11.glPolygonMode(GL11.GL_FRONT_AND_BACK, GL11.GL_LINE);
                GL11.glColor3f(0.9f, 0.9f, 0.9f);
                drawTorus(1, 1.9f + ((float) Math.sin((0.004f * frot))), 15, 15);
                canvas.swapBuffers();
                display.asyncExec(this);
            }
        }
    });

    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

From source file:msi.gaml.operators.Maths.java

public static void main(final String[] args) throws ParseException {
    java.lang.System.out.println("Various format tests");
    java.lang.System.out.println("NumberFormat.parse1e1 = " + NumberFormat.getInstance(Locale.US).parse("1e1"));
    java.lang.System.out.println("Double.parse 1e1 = " + Double.parseDouble("1e1"));
    java.lang.System.out/*  w  w w .j  a  v a 2 s  . c  om*/
            .println("NumberFormat.parse 1E1 = " + NumberFormat.getInstance(Locale.US).parse("1E1"));
    java.lang.System.out.println("Double.parse 1E1 = " + Double.parseDouble("1E1"));
    java.lang.System.out
            .println("NumberFormat.parse 1.0e1 = " + NumberFormat.getInstance(Locale.US).parse("1.0e1"));
    java.lang.System.out.println("Double.parse 1.0e1 = " + Double.parseDouble("1.0e1"));
    java.lang.System.out.println(
            "NumberFormat.parse 0.001E+10 = " + NumberFormat.getInstance(Locale.US).parse("0.001E+10"));
    java.lang.System.out.println("Double.parse 0.001E+10 = " + Double.parseDouble("0.001E+10"));
    java.lang.System.out.println(
            "NumberFormat.parse 0.001E-10 = " + NumberFormat.getInstance(Locale.US).parse("0.001E-10"));
    java.lang.System.out.println("Double.parse 0.001E-10 = " + Double.parseDouble("0.001E-10"));
    java.lang.System.out
            .println("NumberFormat.parse 0.001e-10 =" + NumberFormat.getInstance(Locale.US).parse("0.001e-10"));
    java.lang.System.out.println("Double.parse 0.001e-10 = " + Double.parseDouble("0.001e-10"));
    java.lang.System.out.println("Various arithmetic tests");
    java.lang.System.out.println("cos(PI) = " + Maths.cos_rad(PI));
    java.lang.System.out.println("sin_rad(0.0) = " + sin_rad(0.0));
    java.lang.System.out.println("tan_rad(0.0) = " + tan_rad(0.0));
    java.lang.System.out.println("sin(360) = " + sin(360));
    java.lang.System.out.println("sin(-720) = " + sin(-720));
    java.lang.System.out.println("sin(360.0) = " + sin(360.0));
    java.lang.System.out.println("sin(-720.0) = " + sin(-720.0));
    java.lang.System.out.println("sin(90) = " + sin(90));
    java.lang.System.out.println("sin(45) = " + sin(45));
    java.lang.System.out.println("sin(0) = " + sin(0));
    java.lang.System.out.println("sin(135) = " + sin(135));

    java.lang.System.out.println("Math.sin(360.0) = " + Math.sin(2 * Math.PI));
    // java.lang.System.out.println("3.0 = 3" + (3d == 3));
    // java.lang.System.out.println("3.0 != 3" + (3d != 3));
    java.lang.System.out.println("floor and ceil 2.7 " + floor(2.7) + " and " + ceil(2.7));
    java.lang.System.out.println("floor and ceil -2.7 " + floor(-2.7) + " and " + ceil(-2.7));
    java.lang.System.out.println("floor and ceil -2 " + floor(-2) + " and " + ceil(-2));
    java.lang.System.out.println("floor and ceil 3 " + floor(3) + " and " + ceil(3));
    double atan2diff = 0;
    double atan2diff2 = 0;
    Random rand = new Random();
    long s1 = 0;
    long t1 = 0;
    long t2 = 0;
    long t3 = 0;
    // for ( int i = 0; i < 10000000; i++ ) {
    // double x = rand.nextDouble();
    // double y = rand.nextDouble();
    // s1 = java.lang.System.currentTimeMillis();
    // double a1 = Math.atan2(x, y);
    // t1 += java.lang.System.currentTimeMillis() - s1;
    // s1 = java.lang.System.currentTimeMillis();
    // double a2 = FastMath.atan2(x, y);
    // t2 += java.lang.System.currentTimeMillis() - s1;
    // s1 = java.lang.System.currentTimeMillis();
    // double a3 = Maths.atan2Opt2(x, y);
    // t3 += java.lang.System.currentTimeMillis() - s1;
    //
    // atan2diff += Math.abs(a1 - a2);
    // atan2diff2 += Math.abs(a1 - a3);
    // }
    // java.lang.System.out.println("atan2diff : " + atan2diff + "  atan2diff2 : " + atan2diff2 + " t1 : " + t1 +
    // " t2 : " + t2 + " t3 : " + t3);

    long t4 = 0;
    long t5 = 0;
    long t6 = 0;
    double distDiff1 = 0;
    double distDiff2 = 0;
    for (int i = 0; i < 1000000; i++) {
        double x1 = rand.nextDouble();
        double y1 = rand.nextDouble();
        double x2 = rand.nextDouble();
        double y2 = rand.nextDouble();
        Coordinate c1 = new Coordinate(x1, y1);
        Coordinate c2 = new Coordinate(x2, y2);

        s1 = java.lang.System.currentTimeMillis();
        double a1 = Math.hypot(x2 - x1, y2 - y1);
        t4 += java.lang.System.currentTimeMillis() - s1;
        s1 = java.lang.System.currentTimeMillis();
        double a2 = FastMath.hypot(x2 - x1, y2 - y1);
        t5 += java.lang.System.currentTimeMillis() - s1;
        s1 = java.lang.System.currentTimeMillis();
        double a3 = c1.distance(c2);
        t6 += java.lang.System.currentTimeMillis() - s1;
        distDiff1 += Math.abs(a1 - a2);
        distDiff2 += Math.abs(a1 - a3);
    }
    java.lang.System.out.println("distDiff1 : " + distDiff1 + "  distDiff2 : " + distDiff2 + " t4 : " + t4
            + " t5 : " + t5 + " t6 : " + t6);

    long t7 = 0;
    long t8 = 0;
    distDiff1 = 0;

    for (int i = 0; i < 1000000; i++) {
        double a1, a2;
        double x1 = rand.nextDouble();
        double x2 = rand.nextDouble();
        double y1 = rand.nextDouble();
        double y2 = rand.nextDouble();
        double z1 = 0.0;
        double z2 = 0.0;
        GamaPoint c2 = new GamaPoint(x2, y2, z2);

        s1 = java.lang.System.currentTimeMillis();
        if (z1 == 0d && c2.getZ() == 0d) {
            a1 = hypot(x1, x2, y1, y2);
        } else {
            a1 = hypot(x1, x2, y1, y2, z1, z2);
        }
        t7 += java.lang.System.currentTimeMillis() - s1;
        s1 = java.lang.System.currentTimeMillis();
        a2 = hypot(x1, x2, y1, y2, z1, c2.getZ());
        t8 += java.lang.System.currentTimeMillis() - s1;
        distDiff1 += Math.abs(a1 - a2);
    }
    java.lang.System.out.println(
            "with 0.0 check : " + t7 + "  with direct 3 parameters call : " + t8 + " distance : " + distDiff1);
    // java.lang.System.out.println("Infinity to int:" + (int) Double.POSITIVE_INFINITY);
    // java.lang.System.out.println("NaN to int:" + (int) Double.NaN);
    // GuiUtils.debug("(int) (1.0/0.0):" + (int) (1.0 / 0.0));
    // GuiUtils.debug("(int) (1.0/0):" + (int) (1.0 / 0));
    // GuiUtils.debug("(int) (1.0/0d):" + (int) (1 / 0d));
    // GuiUtils.debug("(int) (1/0):" + 1 / 0);
}

From source file:Main.java

private static double sin(double d) {
    return Math.sin(d);
}

From source file:Main.java

public static float sin(double degree) {
    return (float) Math.sin(Math.toRadians(degree));
}

From source file:Main.java

private static double getSinY(long time) {
    return 0.5f * Math.sin(3 * time / 1000.0f) + 0.5;
}

From source file:Main.java

public static int getStopY(float angle, int distance) {
    return Double.valueOf(Math.rint(-1 * distance * Math.sin(Math.toRadians(angle)))).intValue();
}

From source file:Main.java

public static double[] zRotate(double angle, double[] gVector) {
    double[][] matrix = { { Math.cos(angle), Math.sin(angle), 0, 0 },
            { -Math.sin(angle), Math.cos(angle), 0, 0 }, { 0, 0, 1, 0 }, { 0, 0, 0, 1 } };
    double[] tempDot = { gVector[0], gVector[1], gVector[2], gVector[3], };
    for (int j = 0; j < tempDot.length; j++) {
        gVector[j] = (tempDot[0] * matrix[0][j] + tempDot[1] * matrix[1][j] + tempDot[2] * matrix[2][j]
                + tempDot[3] * matrix[3][j]);
    }//from ww w.j a  v  a 2  s.com
    return gVector;
}