简体   繁体   English

使用System.Math类(C#)中的方法计算三角形的面积

[英]Calculating the area of a triangle using methods in the System.Math class (C#)

I am studying C# and found this practice problem: 我正在研究C#,发现此实践问题:

Write a program that calculates the area of a triangle with the following given: the lengths of two sides and the angle between them (hint: side-angle-side) 编写一个程序,计算三角形的面积,并给出以下信息:两侧的长度和它们之间的角度(提示:side-angle-side)

I know how to find the area of a triangle in C# if I have the base and the height, and I know that there's a way to use the .Cos method in the .Math class in order to get the cosine that I need for my problem. 我知道如何在C#中找到三角形的面积(如果有底数和高度),而且我知道有一种方法可以在.Math类中使用.Cos方法来获取我需要的余弦值问题。 However, my program does not seem to like the syntax that I am using. 但是,我的程序似乎不喜欢我使用的语法。 May I have any advice for how to implement methods in the .Math class in order to solve a geometry problem like this that takes user input for the side, the angle, and the other side? 对于如何在.Math类中实现方法,以便解决这样的几何问题(需要用户输入侧面,角度和另一侧),我是否可以提出任何建议?

I know that the formula is c^2 = a^2 + b^2 - 2ab * cos(y) //where y = the degree of the angle 我知道公式是c ^ 2 = a ^ 2 + b ^ 2-2ab * cos(y)//其中y =角度

Here is what I have so far, which I think will get across what I am trying to do: 到目前为止,这是我想做的事情:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace day_of_the_week
{

    class Program

    {

        static void Main(string[] args)

        {

            double side1;
            double side2;
            double angle;

            Console.WriteLine("Enter one side length.");
            side1 = Convert.ToDouble(Console.ReadLine());

            Console.WriteLine("Enter the other side's height.");
            side2 = Convert.ToDouble(Console.ReadLine());

            Console.WriteLine("Enter the value of the angle.");
            angle = Convert.ToDouble(Console.ReadLine());

            double thirdside = Program.thirdside(side1, side2);
            Console.WriteLine(thirdside);
            Console.ReadLine();
            }
      public static double thirdside(double side1, double side2, double angle)
        {
            return (side1*side1 +  side2*side2 - 2*side1*side2.Cos(angle));
        }
        }
    }

Implement your function with calls to Math.Cos and Math.Sqrt , like this: 通过调用Math.CosMath.Sqrt实现函数,如下所示:

public static double rad(double deg)
{
    return deg * Math.PI / 180;  
}

public static double thirdside(double side1, double side2, double angleDeg)
{
    double angleRad = rad(angleDeg);
    return Math.Sqrt(side1*side1 + side2*side2 - 2*side1*side2*Math.Cos(angleRad));
}

Bear in mind that it is likely that you will want to input the angle in degrees. 请记住,您可能需要输入以度为单位的角度。 But Math.Cos accepts the angle in radians, hence the conversion. 但是Math.Cos接受以弧度表示的角度,因此进行转换。

And the code in the question neglected to take the square root of the expression. 问题中的代码忽略了表达式的平方根。 The code in this answer does so. 此答案中的代码就是这样做的。

Need to specify class. 需要指定类。 so that is Math.Cos(angle) . 这样就是Math.Cos(angle) Also use Math.Pow(side1, 2) to square numbers. 也可以使用Math.Pow(side1, 2)来平方数。

Note that the area of the triangle is 请注意,三角形的面积为

0.5*side1*side2*sin(toRadians(angle))

if angle is the angle between side1 and side2 . 如果angle是之间的角度side1side2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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