7

I have tried to convert a calculation from an app I made using MIT AppInventor which uses Kawa to Android using Java.The problem I'm facing is that the trigonometric parts of the calculation in Kawa are using degress.My question is how do I translate this calculation to Java and get the same output?

This is how I do the calculation is Kawa,all variables are of type double:

 Tri 1=atan(Offset Depth/Offset Length)
 Mark 1=sqrt(Offset Length^2+Offset Depth^2)
 Tri 2=(180-Tri1)/2
 Mark 2=Duct Depth/(tan(Tri 2))

Then I did my best to translate it to Java code,the variables are double also as above,depth,length and duct depth are user input values.

 tri1 = Math.atan(offsetDepth / offsetLength);
 marking1 = Math.sqrt(Math.pow(offsetLength,2) + Math.pow(offsetDepth,2));  
 tri2 = (180 - tri1) / 2;
 marking2 = ductDepth / Math.tan(tri2);

Screenshot of what the inputs and outputs look like:

enter image description here

Brian Var
  • 6,029
  • 25
  • 114
  • 212

2 Answers2

26

You can use Math.toRadians() to convert degrees to radians.

Keppil
  • 45,603
  • 8
  • 97
  • 119
  • So when I convert all the calculation from degress to radians,do I convert back to degrees when outputting or else the calculation won't be the same as Kawa calculation? Also when using `toRadians` how do I implement it in the calculation? – Brian Var Dec 15 '13 at 12:57
  • For the methods that expect radians, you first calculate the arguments the same way as before, and then converts them before passing them to the methods. For the methods returning radians, yes, you will have to convert that back to degrees. You can use Math#toDegrees() for this. – Keppil Dec 15 '13 at 13:23
11

You can convert the angles to radians yourself.

As we know:

180 degrees = PI radians

So:

1 degree = PI / 180 radians

So wherever you have X degrees,
they are equal to (X * PI / 180) radians.

In Java you have

Math.PI

which defines the value of the PI number.

Just change your Java code to this:

tri11 = Math.atan(1.0 * offsetDepth / offsetLength); // tri11 is radians
tri1 = tri11 * 180.0 / Math.PI; // tri1 is degrees
marking1 = Math.sqrt(Math.pow(1.0 * offsetLength,2) + Math.pow(1.0 * offsetDepth,2));  
tri2 = (180.0 - tri1) / 2.0; // tri2 is degrees
tri22 = tri2 * Math.PI / 180.0; // tri22 is radians
marking2 = 1.0 * ductDepth / Math.tan(tri22);
// output whatever you like now
peter.petrov
  • 38,363
  • 16
  • 94
  • 159
  • I'm wondering how would I convert the whole calculation to match the output of the Kawa calculation.This converting to radians is confusing.So `180 * PI / 180 radians will fix tri2` so how would I correct the rest of the calculation? These questions probably sound easy to you but I can't get my head around it. – Brian Var Dec 15 '13 at 12:54
  • I think I'm starting to get a clearer picture now.What I'm understanding is you calculate the `tri11` in radians then convert to degress,so that part is correct,then `tri22` is radians? so `marking2` is doing calculation in radians.That means I will still have to convert the result of `marking2` to degrees because its in radians? thanks for the help so far :) – Brian Var Dec 15 '13 at 13:07
  • @BrianJ Which of your variables represent angles? Each variable which represents an angle value you should convert (as I did). The marking2 does not represent an angle in my opinion. I think it represents length of some segment and not the size of some angle. – peter.petrov Dec 15 '13 at 13:20
  • Okay,the first mark1 is the hypotenuse as in the diagram,and mark2 is distance,so I guess you were right? I'll test this anyways and check if it works.Appreciate the help. – Brian Var Dec 15 '13 at 14:00
  • I just tested and the marking 1 is off by nearly 10mm,its more accurate than it was but still not the same calculation as the original.Do you have any idea why this would be? – Brian Var Dec 15 '13 at 14:18