سؤال

I'm trying to convert the variable tri2 to degress by wrapping it in the toDegree() and setting it to a new variable called triDegree. But when I cast the variable and run the application I get the following in my logcat: http://pastebin.com/HKDSAuQK

The application was running fine and calculating up to the point where I converted it to degrees. Can anyone see what is wrong with my implementation here?

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 triDegree;

        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;
        triDegree = Math.toDegrees(tri2);
        marking2 = off3 / Math.tan(triDegree);

        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!");
    }
}
هل كانت مفيدة؟

المحلول

The error happens here:

tri2 = (180 - tri1) / 2;

At this point, you treat tri1 as degrees, before the conversion. If you want to subtract an angle from the value of the "straight" 180° angle, use π instead of 180, the measure of the straight angle in radians:

tri2 = (Math.PI - tri1) / 2;

This call is also incorrect

triDegree = Math.toDegrees(tri2);
marking2 = off3 / Math.tan(triDegree);

because triDegree is expressed in degrees, while tan is expecting a value expressed in radians.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top