C 中的三角学

Trigonometry in C

我是 C 的初学者,遇到了这个问题:我应该制作一个应用程序,您可以在其中插入三角形顶点的坐标,然后它会打印有关其面积、周长和最有趣的细节,它应该打印它的角度。该代码应该使用 Heron 公式中的双切线方程编写。我试过使用 atan() 来做到这一点,但我想我应该添加 +n 以避免超出域范围。不知道如何。 Here's the equation。下面是我的代码:

#include <stdio.h>
#include <math.h>

#define PI  (4. * atan(1))

int main() {
    // Defining floats
    
    float xa, ya, xb, yb, xc, yc, s, P, a, b, c, x, y, z, alphadeg, alpharad, betadeg, betarad, gammadeg, gammarad;

    // Inserting coordinates of each points

    printf("Insert the first point's coordinates (a space should be between the X and Y coordinate):\n"); 
    scanf("%f %f", &xa, &ya);
    
    printf("Insert the second point's coordinates (a space should be between the X and Y coordinate):\n");
    scanf("%f %f", &xb, &yb);
    
    printf("Insert the third point's coordinates (a space should be between the X and Y coordinate):\n");
    scanf("%f %f", &xc, &yc);
    
    // Calculating and printing length of each side

    printf("Distance between point 1 and 2: %f\n", sqrt(pow(xa - xb, 2) + pow(ya - yb, 2)));
    printf("Distance between point 1 and 3: %f\n", sqrt(pow(xa - xc, 2) + pow(ya - yc, 2)));
    printf("Distance between point 2 and 3: %f\n", sqrt(pow(xb - xc, 2) + pow(yb - yc, 2)));
    
    // Defining each side

    a = sqrt(pow(xa - xb, 2) + pow(ya - yb, 2));
    b = sqrt(pow(xa - xc, 2) + pow(ya - yc, 2));
    c = sqrt(pow(xb - xc, 2) + pow(yb - yc, 2));

    // Defining s as the parameter from Heron's formula

    s = ((a + b + c) / 2);

    // Defining P as the area from Heron's formula

    P = sqrt(s * (s - a) * (s - b) * (s - c));

    // Printing the area and perimeter of the triangle

    printf("The area of your triangle is %f\n", P);
    printf("The perimeter of your triangle is %f\n", a + b + c);
 

    // Angles
    
    /*
    tan(alpha/2)=sqrt(((P-b)*(P-c))/(P*(P-a)));
    tan(beta/2)=sqrt(((P-a)*(P-c))/(P*(P-b)));
    tan(gamma/2)=sqrt(((P-a)*(P-a))/(P*(P-c))); 
    
    Let 
    x = tan(alpha/2)
    y = tan(beta/2)
    z = tan(gamma/2)
    */
   
    x = sqrt(((P - b) * (P - c)) / (P * (P - a)));
    y = sqrt(((P - a) * (P - c)) / (P * (P - b)));
    z = sqrt(((P - a) * (P - b)) / (P * (P - c)));
    alphadeg = (atan(x)) * 360 / PI;
    betadeg = (atan(y)) * 360 / PI;
    gammadeg = (atan(z)) * 360 / PI;
    alpharad = 2 * (atan(x));
    betarad = 2 * (atan(y));
    gammarad = 2 * (atan(z));

    printf("The value of the alpha angle is %0.3f\n", alphadeg);
    printf("The value of the beta angle is %0.3f\n", betadeg);
    printf("The value of the gamma angle is %0.3f\n", gammadeg);

    printf("%f = %f", PI, (alpharad + betarad + gammarad));
    
    return 0;
}

您可以考虑像下面这样添加 epsilon:

float EPSILON = 0.1
x = sqrt(((P-b)*(P-c))/(EPSILON+P*(P-a)));

这个问题是由于对数学公式的误解造成的。

// That's the formula to calculate the semi perimeter of a triangle
// given the length of its sides a, b and c.
s = ((a + b + c) / 2);

// Defining P as the area from Heron's formula
P = sqrt(s * (s - a) * (s - b) * (s - c));

此处的名称 P 具有误导性(实际上 s 也是),不仅因为它们是单字母变量名。在以下公式中,OP 使用 P 而不是半周长:

x = sqrt(((P - b) * (P - c)) / (P * (P - a)));
//         ^         ^          ^    ^           You should use 's' instead.