平面三点定圆心 c语言代码
时间: 2023-08-01 22:11:22 浏览: 105
以下是一个用C语言实现的平面三点定圆心的代码示例:
```c
#include <stdio.h>
#include <math.h>
struct Point {
double x;
double y;
};
struct Circle {
struct Point center;
double radius;
};
struct Circle calculateCircle(struct Point p1, struct Point p2, struct Point p3) {
struct Circle circle;
// 计算中垂线的斜率
double slope1 = (p2.y - p1.y) / (p2.x - p1.x);
double slope2 = (p3.y - p2.y) / (p3.x - p2.x);
// 计算中垂线的中点坐标
double midX = (slope1 * slope2 * (p1.y - p3.y) + slope2 * (p1.x + p2.x) - slope1 * (p2.x + p3.x)) / (2 * (slope2 - slope1));
double midY = -1 * (midX - (p1.x + p2.x) / 2) / slope1 + (p1.y + p2.y) / 2;
// 计算圆心坐标和半径
circle.center.x = midX;
circle.center.y = midY;
circle.radius = sqrt(pow(p1.x - midX, 2) + pow(p1.y - midY, 2));
return circle;
}
int main() {
struct Point p1, p2, p3;
struct Circle circle;
printf("请输入三个点的坐标:\n");
printf("点1的x坐标:");
scanf("%lf", &p1.x);
printf("点1的y坐标:");
scanf("%lf", &p1.y);
printf("点2的x坐标:");
scanf("%lf", &p2.x);
printf("点2的y坐标:");
scanf("%lf", &p2.y);
printf("点3的x坐标:");
scanf("%lf", &p3.x);
printf("点3的y坐标:");
scanf("%lf", &p3.y);
circle = calculateCircle(p1, p2, p3);
printf("圆心坐标:(%lf, %lf)\n", circle.center.x, circle.center.y);
printf("圆的半径:%lf\n", circle.radius);
return 0;
}
```
这段代码通过输入三个点的坐标,计算出通过这三个点的圆的圆心坐标和半径。请注意,代码中使用了结构体来表示点和圆,方便进行数据的存储和传递。