已知圆上两点和半径,求圆心(两个结果)

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(1, 2) 和 B(4, 6),半径 r = 5
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;
}
  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值