I am coding a calculater that find the coordinates of a rotated point. (inspired from math tutorial on youtube http://www.youtube.com/watch?v=NQduSvpTyhs)
the part where im stuck is the return of ansx and ansy. instead of 4.0 and 3.6 it returns incorect answer. (Nouvelles coordoner : -3.0853888289108733,-4.413657867849748)
I cant understand what is happening wrong in those 2 lines ...
static void tourner()
{
Scanner tournsc = new Scanner(System.in);
double coordx, coordy, roattiondeg, ansx, ansy, angle, triangleh, newangle;
//Test input : X 5, Y 2, rotation deg 20
System.out.println("Coord X :");
coordx = tournsc.nextInt();
System.out.println("Coord Y :");
coordy = tournsc.nextInt();
System.out.println("Degre : ");
roattiondeg = tournsc.nextInt();
triangleh = ((Math.pow(coordx, 2)) + (Math.pow(coordy, 2)));
angle = Math.toDegrees(Math.atan(coordy/coordx));
newangle = roattiondeg + angle;
ansx = Math.sqrt(triangleh)*Math.cos(newangle);
ansy = Math.sqrt(triangleh)*Math.sin(newangle);
System.out.println("Nouvelles coordoner : " + ansx + "," + ansy);
}
Question : why ansx and ansy give good answer on my real life calculater but wrong in the program?
thx for helps.
The library functions sin
and cos
expect their input to be in radians but you passed them degrees instead.
Just keep it all in radians from the input onwards and you shouldn't have a problem:
roattiondeg = Math.toRadians(tournsc.nextInt());
triangleh = ((Math.pow(coordx, 2)) + (Math.pow(coordy, 2)));
angle = Math.atan(coordy/coordx));
newangle = roattiondeg + angle;
ansx = Math.sqrt(triangleh)*Math.cos(newangle);
ansy = Math.sqrt(triangleh)*Math.sin(newangle);
System.out.println("Nouvelles coordoner : " + ansx + "," + ansy);