当前位置:网站首页>[2018 Blue Bridge Cup final] a-triangle area

[2018 Blue Bridge Cup final] a-triangle area

2020-11-10 15:35:09 Fool_one

Answer key

   Method 1 : Helen's formula

        Middle school mathematics , The area of a triangle can be solved by knowing its three sides , And the formula is Helen's formula .

       p = (a + b + c) / 2,s = sqrt(p * (p - a) * (p - b) * (p - c)).

#include <iostream>
#include <cmath>
using namespace std;

double x2, y2, x3, y3, x4, y4;

int main()
{
    x2 = 2.3, y2 = 2.5;
    x3 = 6.4, y3 = 3.1;
    x4 = 5.1, y4 = 7.2;
    //  Helen's formula 
    // p = (a + b + c) / 2
    // s = sqrt(p * (p - a) * (p - b) * (p - c))
    double a = sqrt((x2 - x3) * (x2 - x3) + (y2 - y3) * (y2 - y3));
    double b = sqrt((x2 - x4) * (x2 - x4) + (y2 - y4) * (y2 - y4));
    double c = sqrt((x3 - x4) * (x3 - x4) + (y3 - y4) * (y3 - y4));
    double p = (a + b + c) / 2.0;
    double s = sqrt(p * (p - a) * (p - b) * (p - c));
    printf("%.3f\n", s);
    return 0;
}

 

   Method 2 : The cross product of a vector

        Middle school vector , The cross product of a vector can be used to solve the area of a triangle .

       s = 1 / 2 * (a * b),a = (x2 - x1, y2 - y1),b = (x3 - x1, y3 - y1).

       x1 = x2 - x1,y1 = y2 - y1.

       x2 = x3 - x1,y2 = y3 - y1.

       s = 1 / 2 * (x1 * y2 - x2 * y1).

#include <iostream>
using namespace std;

double x2, y2, x3, y3, x4, y4;

int main()
{
    x2 = 2.3, y2 = 2.5;
    x3 = 6.4, y3 = 3.1;
    x4 = 5.1, y4 = 7.2;
    //  The cross product of a vector 
    // s = 1/2 * a * b
    // a = (x3 - x2, y3 - y2) = (x5, y5)
    // b = (x4 - x2, y4 - y2) = (x6, y6)
    double x5 = x3 - x2, y5 = y3 - y2;
    double x6 = x4 - x2, y6 = y4 - y2;
    double s = 0.50 * (x5 * y6 - x6 * y5);
    printf("%.3f\n", s); 
    return 0;
}

 

   Method 3 : Triangle area formula solution

        May adopt s = 1 / 2 *a * b * sin<a, b> solve .

        The final result is :

       s = 1 / 2 * | x1 * y2 + x2 * y3 + x3 * y1 - x1 * y3 - x3 * y2 - x2 * y1 | .

        The specific derivation can be seen in my CSDN A blog post on :https://blog.csdn.net/weixin_44095278/article/details/98034114

#include <iostream>
#include <cmath>
using namespace std;

double x2, y2, x3, y3, x4, y4;

int main()
{
    x2 = 5.1, y2 = 7.2;
    x3 = 2.3, y3 = 2.5;
    x4 = 6.4, y4 = 3.1;
    double s = 0.50 * fabs(x2 * y3 + x3 * y4 + x4 * y2 - x2 * y4 - x4 * y3 - x3 * y2);
    printf("%.3f\n", s);
    return 0;
}

 

  

  

 

     

版权声明
本文为[Fool_one]所创,转载请带上原文链接,感谢