用c++ 编写函数已知圆上任意三点,求圆的面积
时间: 2024-05-27 13:15:02 浏览: 79
#include <stdio.h>
#include <math.h>
#define PI 3.14159265358979323846
// 定义点结构体
typedef struct Point {
double x;
double y;
} Point;
// 计算两点之间的距离
double distance(Point p1, Point p2) {
double dx = p1.x - p2.x;
double dy = p1.y - p2.y;
return sqrt(dx * dx + dy * dy);
}
// 计算三角形的面积
double triangleArea(Point p1, Point p2, Point p3) {
double a = distance(p1, p2);
double b = distance(p2, p3);
double c = distance(p3, p1);
double p = (a + b + c) / 2;
return sqrt(p * (p - a) * (p - b) * (p - c));
}
// 计算圆的面积
double circleArea(Point p1, Point p2, Point p3) {
double a = distance(p1, p2);
double b = distance(p2, p3);
double c = distance(p3, p1);
double s = triangleArea(p1, p2, p3);
double r = a * b * c / (4 * s);
return PI * r * r;
}
int main() {
Point p1 = {0, 0};
Point p2 = {1, 0};
Point p3 = {0, 1};
double area = circleArea(p1, p2, p3);
printf("圆的面积为:%f\n", area);
return 0;
}
阅读全文