首页 > 编程知识 正文

三角形的周长已知求三角形面积

时间:2023-05-06 07:21:58 阅读:208974 作者:586

题目大意:已知三点求三角形周长、面积、外心、重心,将结果保留两位小数

题目分析:周长很好求,只要将三条线段的长度相加即可。面积的求法有专门的公式,比较快捷。

外心是三角形三条线段上中垂线交点,求法是求出两条中垂线,然后求它们的交点即可。

重心是三条中线的交点,只要求出三点的均值即可。((x1+x2+x3)/3,(y1+y2+y3)/3)


代码展示:

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

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。