Jordan King
Jordan King

Reputation: 11

C# visual studio finding last 2 angles using sin and cosine

I've written a program for my class that asks me to write a GUI program to calculate Side C when the user inputs side a side b and angle c which is opposite of side C. The program I have written correctly does that.

My question is to the extra credit portion of this assignment which asks me to take all that information and find the other two angles. I'm having a hard time even getting started writing it. We are learning methods so I have to write a new method to calculate the other 2 angles using the length of all sides that I now have and the size of angle c.

Any advice on how to start or go about writing that method? I can post a little of my source code if that will help at all. I have this code so far but I don't know where my mistake is on my method. I cannot get the right number to display

static double CalcAngleB(double side1, double side2, double side3)
{
    const double COS = 2; const double RADIANS = 180;

    //calculate sideOne by muliplying side1 by side1
    double sideA = side1 * side1;

    //calculate sideTwo by multiplying side2 by side2
    double sideB = side2 * side2;

    //calculate sideThree by muliplying side3 by side3
    double sideC = side3 * side3;

    //Begin cosine functions to find Angle B.
    double sumOfThreeSides = sideA + sideC - sideB;
    double RadiansAngleB = sumOfThreeSides / (COS * side1 * side3);

    //convert back to degrees degrees = radians * (180/Math.PI)
    //double angleB = RadiansAngleB * (RADIANS / Math.PI);
    double angleB = Math.Cosh(RadiansAngleB);
    //double angleBDegress = angleB * (RADIANS / Math.PI);

    //return the answer
    return angleB;
}

Upvotes: 0

Views: 1049

Answers (1)

dyatchenko
dyatchenko

Reputation: 2343

You can easily use law of cosines. Code:

//    /\
//   /  \
// a/    \b
// /\     \
//---\------
//    c
public double CalcAngleB(double a, double b, double c)
{
    return Math.Acos((a * a + c * c - b * b) / (2 * a * c));
}

//    /\
//   /  \
// a/    \b
// /     /\
//------/---
//    c
public double CalcAngleA(double a, double b, double c)
{
    return Math.Acos((b * b + c * c - a * a) / (2 * b * c));
}

//    /\
//   /--\
// a/    \b
// /      \
//----------
//    c
public double CalcAngleC(double a, double b, double c)
{
    return Math.Acos((b * b + a * a - c * c) / (2 * b * a));
}

Your code is also correct, but you should use Math.Acos instead of Math.Cosh. Hope this helps.

Upvotes: 1

Related Questions