A circumcircle problem.
Given a triangle, find the circumference of its circumcircle. There’s an easy way and a hard way.
The hard way is to find the circle’s centre then calculate the radius. I used this solution for a previous problem. Code:
#include <iostream> #include <complex> #include <limits> using namespace std; typedef long double gtype; #define equ(a, b, e) ( abs((a) - (b)) <= (e) ) const gtype len_before_point_digits = 1E6; const gtype epsLen = numeric_limits<gtype>::epsilon() * len_before_point_digits; const gtype pi = M_PI; typedef complex<gtype> point; #define x real() #define y imag() #define crs(a, b) ( (conj(a) * (b)).y ) #define perp(p) ( point((p).y, -(p).x) ) #define intrN(a, b, p, q) ( crs((p)-(a),(b)-(a)) * (q) - crs((q)-(a),(b)-(a)) * (p) ) #define intrD(a, b, p, q) ( crs((p)-(a),(b)-(a)) - crs((q)-(a),(b)-(a)) ) #define circC(r) (2 * pi * abs(r)) #define plus(x) ((x) > 0 ? " + " : " - ") int main() { point p[3], v[2][2], c; gtype r; cout.precision(2); while (cin >> p[0].x >> p[0].y >> p[1].x >> p[1].y >> p[2].x >> p[2].y) { // perp 1 v[0][0] = 0.5l * (p[0] + p[1]); v[0][1] = perp(p[1] - p[0]) + v[0][0]; // perp 2 v[1][0] = 0.5l * (p[1] + p[2]); v[1][1] = perp(p[2] - p[1]) + v[1][0]; // intersect gtype d = intrD(v[0][0], v[0][1], v[1][0], v[1][1]); if (!equ(d, 0, epsLen)) { c = intrN(v[0][0], v[0][1], v[1][0], v[1][1]) / d; r = abs(c - p[0]); } else r = 0; // out cout << fixed << circC(r) << endl; } return 0; }
The easy way is to find the circumcircle’s radius through:
And since:
This method is less prone to error. Implementation:
#include <iostream> #include <complex> #include <limits> using namespace std; typedef long double gtype; const gtype pi = M_PI; typedef complex<gtype> point; #define x real() #define y imag() #define crs(a, b) ( (conj(a) * (b)).y ) #define circC(r) (2 * pi * abs(r)) #define area(a, b, c) ( (gtype) 0.5 * crs((b) - (a), (c) - (a)) ) #define plus(x) ((x) > 0 ? " + " : " - ") int main() { point p[3]; gtype r; cout.precision(2); while (cin >> p[0].x >> p[0].y >> p[1].x >> p[1].y >> p[2].x >> p[2].y) { r = abs(p[0] - p[1]) * abs(p[1] - p[2]) * abs(p[2] - p[0]) / area(p[0], p[1], p[2]) * 0.25; cout << fixed << circC(r) << endl; } return 0; }