Python代码
def find_circle_center(x1, y1, x2, y2, r):
d = ((x2 - x1) ** 2 + (y2 - y1) ** 2) ** 0.5
mid_x = (x1 + x2) / 2
mid_y = (y1 + y2) / 2
h = (r ** 2 - (d / 2) ** 2) ** 0.5
center_x1 = mid_x + h * (y2 - y1) / d
center_y1 = mid_y - h * (x2 - x1) / d
center_x2 = mid_x - h * (y2 - y1) / d
center_y2 = mid_y + h * (x2 - x1) / d
return (center_x1, center_y1), (center_x2, center_y2)
A_x, A_y = 1, 2
B_x, B_y = 4, 6
radius = 5
center1, center2 = find_circle_center(A_x, A_y, B_x, B_y, radius)
print(f"圆心1坐标:({
center1[0]:.2f}, {
center1[1]:.2f})")
print(f"圆心2坐标:({
center2[0]:.2f}, {
center2[1]:.2f})")
C++
#include <iostream>
#include <cmath>
#include <utility>
std::pair<std::pair<double, double>, std::pair<double, double>> find_circle_center(double x1, double y1, double x2, double y2, double r) {
double d = std::sqrt(std::pow(x2 - x1, 2) + std::pow(y2 - y1, 2));
double mid_x = (x1 + x2) / 2;
double mid_y = (y1 + y2) / 2;
double h = std::sqrt(std::pow(r, 2) - std::pow(d / 2, 2));
double center_x1 = mid_x + h * (y2 - y1) / d;
double center_y1 = mid_y - h * (x2 - x1) / d;
double center_x2 = mid_x - h * (y2 - y1) / d;
double center_y2 = mid_y + h * (x2 - x1) / d;
return std::make_pair(std::make_pair(center_x1, center_y1), std::make_pair(center_x2, center_y2));
}
int main() {
double A_x = 1, A_y = 2;
double B_x = 4, B_y = 6;
double radius = 5;
auto centers = find_circle_center(A_x, A_y, B_x, B_y, radius);
std::cout << "圆心1坐标:(" << centers.first.first << ", " << centers.first.second << ")" << std::endl;
std::cout << "圆心2坐标:(" << centers.second.first << ", " << centers.second.second << ")" << std::endl;
return 0;
}
文章评论