Computer Science Canada

Trigonometry isn't working properly

Author:  goroyoshi [ Fri Oct 07, 2011 1:57 pm ]
Post subject:  Trigonometry isn't working properly

Why does my java code not work like my turing one does?
Java:

import hsa.Console;
import java.lang.Thread;
import java.awt.*;
import java.lang.Math;

public class Hook
{
    Console c;
    public Hook ()
    {
        c = new Console ();
    }


    double x, y;
    long x2, y2;
    public void trig ()
    {
        while (true)
        {
            for (int z = 0 ; z < 360 ; z++)
            {
                x = Math.sin (z);
                y = Math.cos (z);
                x2 = Math.round (Math.toDegrees (x));
                y2 = Math.round (Math.toDegrees (y));
                c.clear ();
                c.setColor (Color.green);
                c.fillOval (Math.round (x2) + 100, Math.round (y2) + 100, 10, 10);
                try
                {
                    Thread.sleep (100);
                }
                catch (InterruptedException e)
                {
                }
            }
        }
    }


    public static void main (String[] args)
    {
        Hook p = new Hook ();
        p.trig ();
    }
}

Turing:

var mx, my, mb : int
setscreen ("offscreenonly")
proc drawcircle (thickness, xradius, yradius, col : int)
    loop
        for x : 1 .. 360
            Mouse.Where (mx, my, mb)
            drawfilloval (floor (sind (x) * xradius) + mx, floor (cosd (x) * yradius) + my, thickness, thickness, col)
            delay (2)
            View.Update
            cls
        end for
    end loop
end drawcircle
drawcircle (5, 30, 30, blue)

Author:  Tony [ Fri Oct 07, 2011 3:18 pm ]
Post subject:  RE:Trigonometry isn\'t working properly

because one uses sin while the other uses sind.

I don't think Math.toDegrees does what you think it does.

Author:  goroyoshi [ Fri Oct 07, 2011 6:47 pm ]
Post subject:  RE:Trigonometry isn\'t working properly

so i should just multiply it by 57.3 ?
ok so that is what Math.toDegrees does

Author:  Tony [ Fri Oct 07, 2011 7:10 pm ]
Post subject:  RE:Trigonometry isn\'t working properly

What? Read the docs for the version for Java that you are using.

Author:  andrew. [ Sat Oct 08, 2011 10:10 am ]
Post subject:  RE:Trigonometry isn\'t working properly

I haven't read the Java docs but I'm assuming that sin and the other trig functions in Java take in radian angles. This means that you have to call toRadians on your angle to calculate the sine.

So basically:
Java:
x = Math.sin (Math.toRadians(z));

Author:  goroyoshi [ Sun Oct 09, 2011 10:24 am ]
Post subject:  RE:Trigonometry isn\'t working properly

i've fixed it already, ty for helping


: