Home > OS >  Trigonometry in C
Trigonometry in C

Time:10-15

I'm a beginner in C and have this problem: I'm supposed to make an app where you insert the coordinates of a triangle's vertices, and then it prints details about its area, perimeter and most interesting of all, it's supposed to print its angles. The code is supposed to be written using the double tangent equation from Heron's formula. I've tried doing it using atan(), but I guess I should add n to avoid going out of the domain. Don't know how though. Here's the equation. And below is my code:

#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;
}

CodePudding user response:

You can consider to add epsilon like below:

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

CodePudding user response:

The issue is caused by a misunderstanding of the mathematical formulas.

// 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));

The name P, here, is misleading (s too, actually) and not only because they are one-letter variable names. In the following formulas, the OP uses P instead of the semi-perimeter:

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