I'm trying to make a calculator for some calc homework, but the information is given in degrees and the answer must be in degrees. I tried using the Math.toDegrees and Math.toRadians but the answer isn't correct.
public static double crossProduct() {
Scanner in = new Scanner(System.in);
System.out.println("enter angle in degrees");
double angle = in.nextDouble();
System.out.println("x1 = ");
double x1 = in.nextDouble();
System.out.println("y1 = ");
double y1 = in.nextDouble();
System.out.println("z1 = ");
double z1 = in.nextDouble();
System.out.println("");
System.out.println("x2 = ");
double x2 = in.nextDouble();
System.out.println("y2 = ");
double y2 = in.nextDouble();
System.out.println("z2 = ");
double z2 = in.nextDouble();
double mag1 = Math.sqrt((x1 * x1) + (y1 * y1) + (z1 * z1));
double mag2 = Math.sqrt((x2 * x2) + (y2 * y2) + (z2 * z2));
double res = mag1 * mag2 * Math.toDegrees(Math.sin(Math.toRadians(angle)));
System.out.println("cross product = " + res);
return res;
}
inputting <0,2,0> and <0,5,0> and angle = 90 degrees should give the answer as 10. instead it returns:
572.9577951308232
Any ideas?
EDIT: Also, if anyone has a better/shorter way for inputting the components, that would be amazing.
Crossproduct is a number that is not expressed in degrees. You need to convert the angle to radians before passing it to Math.sin
, but converting the result back to degrees is incorrect.
double res = mag1 * mag2 * Math.sin(Math.toRadians(angle));