12-1 三角形

题目
 为二维空间中的点设计一个结构体,在此基础上为三角形设计一个结构体。分别设计独立的函数计算三角形的周长、面积、中心和重心。输入三个点,输出这三个点构成的三角形的周长、面积、外心和重心。结果保留小数点后2位数字。

思路
这是数学题

面积用海伦公式:a=AB,b=BC,c=AC,p=(a+b+c)/2,
三角形面积s=sqrt(p*(p-a)(p-b)(p-c));

image
重心:x=(x1+x2+x3)/3 ; y=(y1+y2+y3)/3

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include<stdio.h>
#include<math.h>

typedef struct{
double x,y;
}Point;

typedef struct{
Point a,b,c;
}Triangle;

double abso(double n){
if(n<0) return -n;
else return n;
}

//周长
void cir(Triangle tri){
double c1=sqrt((tri.a.x-tri.b.x)*(tri.a.x-tri.b.x)+(tri.a.y-tri.b.y)*(tri.a.y-tri.b.y));//ab
double c2=sqrt((tri.a.x-tri.c.x)*(tri.a.x-tri.c.x)+(tri.a.y-tri.c.y)*(tri.a.y-tri.c.y));//ac
double c3=sqrt((tri.b.x-tri.c.x)*(tri.b.x-tri.c.x)+(tri.b.y-tri.c.y)*(tri.b.y-tri.c.y));//bc
printf("%.2lf\n",c1+c2+c3);
}

//面积
void area(Triangle tri){
//海伦公式
double a=sqrt((tri.a.x-tri.b.x)*(tri.a.x-tri.b.x)+(tri.a.y-tri.b.y)*(tri.a.y-tri.b.y));
double b=sqrt((tri.b.x-tri.c.x)*(tri.b.x-tri.c.x)+(tri.b.y-tri.c.y)*(tri.b.y-tri.c.y));
double c=sqrt((tri.a.x-tri.c.x)*(tri.a.x-tri.c.x)+(tri.a.y-tri.c.y)*(tri.a.y-tri.c.y));
double p=(a+b+c)/2;
double s=sqrt(p*(p-a)*(p-b)*(p-c));
printf("%.2lf\n",s);
}

//外心
void outside(Triangle tri){
Point out;
double a1=2*(tri.b.x-tri.a.x);
double b1=2*(tri.b.y-tri.a.y);
double c1=(tri.b.x)*(tri.b.x)+tri.b.y*tri.b.y-tri.a.x*tri.a.x-tri.a.y*tri.a.y;
double a2=2*(tri.c.x-tri.b.x);
double b2=2*(tri.c.y-tri.b.y);
double c2=tri.c.x*tri.c.x+tri.c.y*tri.c.y-tri.b.x*tri.b.x-tri.b.y*tri.b.y;
out.x=(c1*b2-c2*b1)/(a1*b2-a2*b1);
out.y=(a1*c2-a2*c1)/(a1*b2-a2*b1);
printf("%.2lf %.2lf\n",out.x,out.y);

}

//重心
void weight(Triangle tri){
Point wei;
wei.x=(tri.a.x+tri.b.x+tri.c.x)/3;
wei.y=(tri.a.y+tri.b.y+tri.c.y)/3;
printf("%.2lf %.2lf\n",wei.x,wei.y);
}

int main(){
Triangle tri;
scanf("%lf%lf",&tri.a.x,&tri.a.y);
scanf("%lf%lf",&tri.b.x,&tri.b.y);
scanf("%lf%lf",&tri.c.x,&tri.c.y);
cir(tri);
area(tri);
outside(tri);
weight(tri);
return 0;
}