Blue Bridge Cup - Knowing three points of a triangle to find the perimeter, area, outer center and center of gravity of the triangle

The gist of the title: Find the perimeter, area, circumcenter, and center of gravity of a triangle given three points, and keep the result to two decimal places

Topic analysis: The perimeter is easy to find, just add the lengths of the three line segments. The area calculation method has a special formula, which is relatively fast.

The circumcenter is the intersection of the perpendiculars on the three line segments of the triangle. The method is to find the two perpendiculars, and then find their intersection.

The center of gravity is the intersection of the three median lines, and only the mean of the three points is required. ((x1+x2+x3)/3, (y1+y2+y3)/3)


Code display:

#include <iostream>
#include <iomanip>
#include <algorithm>
#include <math.h>
using namespace std;

struct Point{
    int x,y;
};

double len(Point p1,Point p2){
    return sqrt(pow(p1.x-p2.x,2)+pow(p1.y-p2.y,2));
}

int main(){
    Point p[3];
    for(int i=0;i<3;i++){
        cin>>p[i].x>>p[i].y;
    }
    double perimeter = 0;
    for(int i=0;i<3;i++){
        perimeter += len(p[i],p[(i+1)%3]);
    }
    cout<<fixed<<setprecision(2)<<perimeter<<endl;
    double a = len(p[0],p[1]);
    double b = len(p[1],p[2]);
    double c = len(p[2],p[0]);
    double s = 0.5*(a+b+c);
    double area = sqrt(s*(s-a)*(s-b)*(s-c));
    cout<<fixed<<setprecision(2)<<area<<endl;
    double a1 = p[1].x - p[0].x;
    double b1 = p[1].y - p[0].y;
    double c1 = (p[0].y * p[0].y - p[1].y * p[1].y + p[0].x * p[0].x - p[1].x * p[1].x) * 0.5000 ;
    double a2 = p[1].x - p[2].x ;
    double b2 = p[1].y - p[2].y ;
    double c2 = (p[2].y * p[2].y - p[1].y * p[1].y + p[2].x * p[2].x - p[1].x * p[1].x) * 0.5000 ;
    double X = (c1 * b2 - b1 * c2) / (b1 * a2 - a1 * b2) ;
    double Y = (a1 * c2 - c1 * a2) / (b1 * a2 - a1 * b2) ;
    cout<<fixed<<setprecision(2)<<X<<" ";
    cout<<fixed<<setprecision(2)<<Y<<endl;
    double weightx = (p[0].x+p[1].x+p[2].x)*1.0/3;
    double weighty = (p[0].y+p[1].y+p[2].y)*1.0/3;
    cout<<fixed<<setprecision(2)<<weightx<<" ";
    cout<<fixed<<setprecision(2)<<weighty<<endl;
    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325990582&siteId=291194637