Area of a triangle

#include<bits/stdc++.h>
#define ll long long

// input: length of 2 sides and the angle (unit: degree) between those 2 sides of a triangle
// output: area of the triangle
double area_triangle_trigonometry (double side1, double side2, double angle) {
    // formula: area = b*c*sinA / 2
    // PI = 2 * acos(0);
    
    double area = side1 * side2 * sin(angle / 180 * PI) / 2.;
    return area;
}

// input: 3 sides
// output: area of the triangle
double area_triangle_3_sides(double side1, double side2, double side3) {
    double p = (side1 + side2 + side3) / 2.;
    double area = sqrt(p*(p-side1)*(p-side2)*(p-side3));
    return area;
}

// input: 3 vertices
// output: area of the triangle
double area_triangle_3_points(double x1, double y1, double x2, double y2, double x3, double y3) {
    double side1 = sqrt( (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2) );
    double side2 = sqrt( (x1-x3)*(x1-x3) + (y1-y3)*(y1-y3) );
    double side3 = sqrt( (x3-x2)*(x3-x2) + (y3-y2)*(y3-y2) );
    return area_triangle_3_sides(side1, side2, side3);
}

// input: 2 angles (unit: degree) and 1 side
// output: area of the triangle
double area_triangle_2_angles_1_side(double angle1, double angle2, double side) {
    double angle3 = 180. - angle1 - angle2;
    double area = side*side * sin(angle1) * sin(angle2) / (2. * sin(angle3));
    return area;
}

Leave a Reply