用外接圆法求圆心和半径c++代码
时间: 2024-01-16 14:03:24 浏览: 92
已知圆上任意三点求圆心半径c++实现.cpp
以下是用外接圆法求圆心和半径的 C++ 代码:
```c++
#include <iostream>
#include <cmath>
using namespace std;
int main() {
double x1, y1, x2, y2, x3, y3;
cout << "Enter the coordinates of the first point: ";
cin >> x1 >> y1;
cout << "Enter the coordinates of the second point: ";
cin >> x2 >> y2;
cout << "Enter the coordinates of the third point: ";
cin >> x3 >> y3;
// Calculate the slope of the line connecting points (x1, y1) and (x2, y2)
double slope1 = (y2 - y1) / (x2 - x1);
// Calculate the slope of the line connecting points (x2, y2) and (x3, y3)
double slope2 = (y3 - y2) / (x3 - x2);
// Calculate the x-coordinate of the center of the circle
double centerX = ((slope1 * slope2 * (y1 - y3)) + (slope2 * (x1 + x2)) - (slope1 * (x2 + x3))) / (2 * (slope2 - slope1));
// Calculate the y-coordinate of the center of the circle
double centerY = ((-1 / slope1) * (centerX - ((x1 + x2) / 2))) + ((y1 + y2) / 2);
// Calculate the radius of the circle
double radius = sqrt(pow((centerX - x1), 2) + pow((centerY - y1), 2));
cout << "The center of the circle is (" << centerX << ", " << centerY << ") and the radius is " << radius << endl;
return 0;
}
```
该代码通过用户输入三个点的坐标,计算出圆的圆心和半径,并输出结果。
阅读全文