如何将弧度计算更改为输出度?

时间:2013-10-08 20:50:52

标签: java parsing math

我使用以下计算来计算测量,但问题是它以弧度而非度数输出计算。我的问题是如何将计算转换为输出degress而不是。我知道Math.toDegress方法,但我在这种情况下遇到麻烦。这是我的onCreate计算完成的地方:

public void onClick(View v) {
        // TODO Auto-generated method stub

        try {

            String getoffsetlength = data.offsetLength.getText().toString(); 
            String getoffsetdepth = data.offsetDepth.getText().toString(); 
            String getductdepth = data.ductDepth.getText().toString(); 

            double tri1,tri2;
            double marking1,marking2;


            double off1 = Double.parseDouble(getoffsetlength);//length
            double off2 = Double.parseDouble(getoffsetdepth);//depth
            double off3 = Double.parseDouble(getductdepth);//duct depth


            marking1 = Math.sqrt(Math.pow(off1,2) + Math.pow(off2,2));
            tri1 = Math.atan(off2 / off1);

            tri2 = (180 - tri1) / 2;
            marking2 = off3 / Math.tan(tri2);



            Intent myIntent = new Intent(MainActivity.this, CalcResult.class);
            myIntent.putExtra("number1", marking1);
            myIntent.putExtra("number2", marking2);
            startActivity(myIntent);
            //make a toast 
            Toast.makeText(getBaseContext(), "Calculating!", Toast.LENGTH_SHORT).show(); 



        } catch (NumberFormatException e) {
            // TODO: handle exception
            System.out.println("Must enter a numeric value!");


        }

    }


}

2 个答案:

答案 0 :(得分:3)

不要重新发明方向盘,请使用:Math.toDegrees(double angrad)

来自Doc:

以度为单位返回所提供的弧度角度。 结果是angrad * 180 / pi

答案 1 :(得分:0)

弧度到度转换只是乘以常数因子, 如果您有任何理由,请使用Math.toDegrees()或自己定义:

public static final double TO_DEGREES = 180.0 / Math.PI;
public static final double TO_RADIANS = Math.PI / 180.0;

但您的代码看起来不对:

Math.toDegrees(Math.sqrt(Math.pow(off1,2) + Math.pow(off2,2)));

这可以简化为

Math.toDegrees(Math.sqrt(off1 * off1 + off2 * off2));

然后更容易识别: 使用Math.sqrt计算距离(off1 * off1 + off2 * off2) 但请将此值用作传递给toDegrees的角度。

我怀疑这是对的。

回答你的评论: 使用

将值“angleRadians”转换为度数
Double angleDegrees = Math.toRadians(angleRadians);