【筆記】已知圓上兩點座標和半徑,求圓心
參考了一下這個博主的部落格: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);
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),演算法正確。