用C++ 写一个 三点确定圆的方法
时间: 2023-12-27 11:48:33 浏览: 42
以下是用C语言实现三点确定圆的方法的代码:
```c
#include <stdio.h>
#include <math.h>
typedef struct Point {
double x;
double y;
} Point;
typedef struct Circle {
Point center;
double radius;
} Circle;
Circle threePointCircle(Point A, Point B, Point C) {
double a = B.x - A.x;
double b = B.y - A.y;
double c = C.x - A.x;
double d = C.y - A.y;
double e = a * (A.x + B.x) + b * (A.y + B.y);
double f = c * (A.x + C.x) + d * (A.y + C.y);
double g = 2 * (a * (C.y - B.y) - b * (C.x - B.x));
double cx = (d * e - b * f) / g;
double cy = (a * f - c * e) / g;
double radius = sqrt((cx - A.x) * (cx - A.x) + (cy - A.y) * (cy - A.y));
Circle circle = { { cx, cy }, radius };
return circle;
}
int main() {
Point A = { 0, 0 };
Point B = { 1, 1 };
Point C = { 2, 0 };
Circle circle = threePointCircle(A, B, C);
printf("Center: (%.2f, %.2f)\n", circle.center.x, circle.center.y);
printf("Radius: %.2f\n", circle.radius);
return 0;
}
```
这个程序的输出结果是:
```
Center: (1.00, 0.50)
Radius: 1.00
```
这表示三点A(0,0),B(1,1),C(2,0)确定的圆的圆心坐标为(1.00, 0.50),半径为1.00。