AC C++ system of equals of the circle and the line throgh the middle and the center
Posted by
D4nick 18 Sep 2020 21:25
dis - discriminant, A, B, C - coefficients of linear equations, D - comfortable cut, a, b, c - quadratic coefficients
#include <iostream>
#include <math.h>
using namespace std;
int main() {
double xc, yc, x1, y1, x2, y2, r, xm, ym, dis, A, B, C, D, a, b, c, x, y;
cin >> xc >> yc >> r >>
x1 >> y1 >>
x2 >> y2;
if (4 * (pow(x1 - xc, 2) + pow(y1 - yc, 2) - pow(r, 2)) <= pow(x2 - x1, 2) + pow(y2 - y1, 2)) {
cout << "No way";
return 0;
}
xm = (x1 + x2) / 2; ym = (y1 + y2) / 2;
A = ym - yc; B = xc - xm; C = xm*yc - xc*ym; D = C / A + xc;
a = -1 - pow(B, 2) / pow(A, 2); b = 2 * yc - (2*B*D) / A; c = -pow(yc, 2) + pow(r, 2) - pow(D, 2);
dis = pow(b, 2) - 4 * a*c;
long double yplu = (-b+sqrt(dis))/(2*a); long double ymin = (-b-sqrt(dis))/(2*a);
long double xplu = (-B*yplu - C) / A; long double xmin = (-B*ymin - C) / A;
long double d1 = pow(xm - xplu, 2) + pow(ym - yplu, 2); long double d2 = pow(xm - xmin, 2) + pow(ym - ymin, 2);
d1 < d2 ? y = yplu, x = xplu : y = ymin, x = xmin;
cout.precision(6);
cout.setf(ios::fixed);
cout << x << ' ' << y;
}