编写一个函数fun11(),输入3个顶点坐标,求ΔABC的面积。C语言
时间: 2024-12-24 18:19:58 浏览: 2
在C语言中,你可以使用向量叉积的方式来计算三角形ABC的面积。假设三个顶点A(x1, y1), B(x2, y2), C(x3, y3),可以编写如下的`fun11()`函数:
```c
#include <stdio.h>
#include <math.h>
double crossProduct(double x1, double y1, double x2, double y2) {
return (x1 * y2) - (y1 * x2);
}
double areaOfTriangle(double x1, double y1, double x2, double y2, double x3, double y3) {
double sideAC = sqrt(pow((x2 - x1), 2) + pow((y2 - y1), 2));
double sideBC = sqrt(pow((x3 - x2), 2) + pow((y3 - y2), 2));
double height = abs(crossProduct(x2 - x1, y2 - y1, x3 - x2, y3 - y2)) / 2; // 双线性插值法计算高
return height;
}
void fun11(double x1, double y1, double x2, double y2, double x3, double y3) {
printf("三角形ABC的面积为: %.2f\n", areaOfTriangle(x1, y1, x2, y2, x3, y3));
}
int main() {
double a_x, a_y, b_x, b_y, c_x, c_y;
printf("请输入顶点A、B、C的坐标 (x, y): ");
scanf("%lf %lf %lf %lf %lf %lf", &a_x, &a_y, &b_x, &b_y, &c_x, &c_y);
fun11(a_x, a_y, b_x, b_y, c_x, c_y);
return 0;
}
```
当你运行这个程序并输入三角形的顶点坐标时,它会计算并打印出ΔABC的面积。
阅读全文