简体   繁体   中英

Calculating the 3rd side of triangle

I am creating a program in C# and I am not able to figure out what the heck is going on and why the 3rd side is not calculating correctly

This is the what I am using to calculate it http://www.mathsisfun.com/algebra/trig-solving-sas-triangles.html

This is what I have

private void button1_Click(object sender, EventArgs e)
    {
        const double TRIANGLE_DEGREES = 180.0;

        string userEntry = string.Empty;
        // Get angle 1 and assign to variable
        userEntry = tbAngle3.Text;
        int angle3 = int.Parse(userEntry);

        // Get side 1 and assign to variable
        userEntry = tbSide1.Text;
        int side1 = int.Parse(userEntry);

        // Get side 2 and assign to variable
        userEntry = tbSide2.Text;
        int side2 = int.Parse(userEntry);

        // Figure out side 3 and assign to a variable
        double side3 = Math.Sqrt(side1 * side1 + side2 * side2 - 2 *  side1 * side2 * Math.Cos(angle3));

        // Display side 3
        textSide3.Text = string.Format("{0}", side3);
    }

It's pretty simple actually, and not coming from your algorithm. Math.Cos takes an angle in radians , while you're working in degrees. Make the conversion (angle * Pi/180) and it will work:

double side3 = Math.Sqrt(side1 * side1 + side2 * side2 - 2 *  side1 * side2 * Math.Cos(angle3 * (Math.PI / 180)));

The modified part is only the Math.Cos , which became Math.Cos(angle3 * (Math.PI / 180)))

It's pretty easy to miss, but as stated by the doc (emphasis mine):

(double d):double
Returns the cosine of the specified angle
d: An angle, measured in radians

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM