C已知三角形顶点 a(xa,ya),b(xb,yb),c(xc,yc) 。用调用函数的形式求这个三角面积
时间: 2024-05-06 12:17:39 浏览: 9
可以定义一个函数来计算三角形面积,函数的参数为三角形三个顶点的坐标。函数内部可以使用海龙公式计算三角形面积。
海龙公式:s = sqrt(p * (p - a) * (p - b) * (p - c))
其中,p = (a + b + c) / 2,a、b、c 分别为三角形三边的长度。
以下是一个示例代码:
```c
#include <stdio.h>
#include <math.h>
double triangle_area(double xa, double ya, double xb, double yb, double xc, double yc);
int main() {
double xa, ya, xb, yb, xc, yc;
printf("请输入三角形三个顶点的坐标:\n");
scanf("%lf%lf%lf%lf%lf%lf", &xa, &ya, &xb, &yb, &xc, &yc);
double area = triangle_area(xa, ya, xb, yb, xc, yc);
printf("三角形面积为:%lf\n", area);
return 0;
}
double triangle_area(double xa, double ya, double xb, double yb, double xc, double yc) {
double a, b, c, p, s;
a = sqrt(pow(xa - xb, 2) + pow(ya - yb, 2));
b = sqrt(pow(xa - xc, 2) + pow(ya - yc, 2));
c = sqrt(pow(xb - xc, 2) + pow(yb - yc, 2));
p = (a + b + c) / 2;
s = sqrt(p * (p - a) * (p - b) * (p - c));
return s;
}
```
运行示例:
```
请输入三角形三个顶点的坐标:
0 0 0 3 4 0
三角形面积为:6.000000
```