【笔记】已知圆上两点坐标和半径,求圆心

参考了一下这个博主的博客:https://blog.csdn.net/liumoude6/article/details/78114255?locationNum=2&fps=1

已知两点坐标(x1, y1), (x2, y2)和半径R,求圆心坐标(x0, y0)。

编程验证算法:

// 具体例子:已知(2,4)、(4,2),半径R=2,求圆心

#include "stdafx.h"
#include <iostream>
#include <cmath>

using namespace std;

void Circle_Center();
double x1 = 2, y11 = 4, x2 = 4.0, y2 = 2, R = 2;

int main(void)
{
    Circle_Center();

    return 0;
}

void Circle_Center()
{
    double c1 = 0, c2 = 0, A = 0, B = 0, C = 0, y01 = 0, x01 = 0, x02 = 0, y02 = 0;
    c1 = (pow(x2, 2) - pow(x1, 2) + pow(y2, 2) - pow(y11, 2)) / 2 /(x2 - x1);
    c2 = (y2 - y11) / (x2 - x1);

扫描二维码关注公众号,回复: 4053267 查看本文章

    A = 1.0 + pow(c2, 2);
    B = 2 * (x1 - c1) * c2 - 2 * y11;
    C = pow((x1 - c1), 2) + pow(y11, 2) - pow(R, 2);

    y01 = (-B + sqrt(B*B - 4 * A * C)) / 2 / A;
    x01 = c1 - c2 * y01;

    y02 = (-B - sqrt(B*B - 4 * A * C)) / 2 / A;
    x02 = c1 - c2 * y01;

    cout << "圆心坐标1为: (" << x01 << ", " << y01 << ")" << endl;
    cout << "圆心坐标2为: (" << x02 << ", " << y02 << ")" << endl;
}

// 最后求出圆心坐标,(4,4)和(2,2),算法正确。

猜你喜欢

转载自blog.csdn.net/yaodaoji/article/details/81540883
今日推荐