Artifact Content
Not logged in

Artifact 40885cae01887a2cac5f874f130e221317294827



//-------------------------------------------------------------
// The circle passing three points
//
// Verified by
//   - AOJ 0010
//-------------------------------------------------------------

vector<double> solve_linear_eq( int n, vector< vector<double> > M, const vector<double>& V )
{
	vector<double> A(V);
	for(int i=0; i<n; ++i)
	{
		// pivot
		if( M[i][i] == 0 )
			for(int j=i+1; j<n; ++j)
				if( M[j][i] != 0 )
					{swap(M[i], M[j]); swap(A[i], A[j]); break;}
		if( M[i][i] == 0 )
			throw "no anser";

		// M[i][i] <-- 1
		double p = M[i][i];
		for(int j=i; j<n; ++j)
			M[i][j] /= p;
		A[i] /= p;

		// M[*][i] <-- 0
		for(int j=0; j<n; ++j) if(j!=i)
		{
			double r = M[j][i];
			for(int k=i; k<n; ++k)
				M[j][k] -= M[i][k] * r;
			A[j] -= A[i] * r;
		}
	}
	return A;
}

void circle_3pt( CMP p1, CMP p2, CMP p3, CMP* c, double* r )
{
	p2 -= p1;
	p3 -= p1;
	// c == p2/2 + A p2 i == p3/2 + B p3 i

	vector< vector<double> > M(2, vector<double>(2));
	vector<double> V(2);
	M[0][0] = -p2.imag(); M[0][1] = +p3.imag(); V[0] = (p3.real()-p2.real())/2.0;
	M[1][0] = +p2.real(); M[1][1] = -p3.real(); V[1] = (p3.imag()-p2.imag())/2.0;
	V = solve_linear_eq(2, M, V);

	double A=V[0], B=V[1];
	*c = p1 + p2/2.0 + A * p2 * CMP(0,1);
	*r = abs(*c - p1);
}