(计算几何8.1.2.3)POJ 1329 Circle Through Three Points(利用叉积来计算三角形的外接圆的圆心)

/*
 * POJ_1329.cpp
 *
 *  Created on: 2013年11月16日
 *      Author: Administrator
 */

#include <iostream>
#include <cstdio>
#include <cmath>

using namespace std;

const double epsi = 1e-10;
const double pi = acos(-1.0);
const int maxn = 100005;//这个不要开得太小...

struct Point{
	double x,y;
	Point(double _x = 0,double _y = 0):x(_x),y(_y){

	}

	Point operator-(const Point& op2)const{//**要注意这种写法
		return Point(x - op2.x,y - op2.y);
	}

	Point operator+(const Point& op2)const{
		return Point(x + op2.x,y + op2.y);
	}

	Point operator*(const double& d)const{
		return Point(x*d,y*d);
	}

	Point operator/(const double& d)const{
		return Point(x/d,y/d);
	}
	double operator^(const Point& op2)const{
		return x*op2.y - y*op2.x;
	}
};

int sign(const double& x){//判断x是正数还是负数
	if(x > epsi){
		return 1;//传入的数是一个整数...
	}else if(x < -epsi){
		return -1;//传入的数是一个负数...
	}

	return 0;
}

double sqr(double x){//计算一个数的平方数
	return x*x;
}

double mul(const Point& p0,const Point& p1,const Point& p2){//计算p0p1与p1p2的叉积叉积
	return (p1-p0)^(p2-p0);
}

double dis2(const Point& p0,const Point& p1){//返回两个点的距离的平方
	return sqr(p0.x - p1.x) + sqr(p0.y - p1.y);
}

double dis(const Point& p0,const Point& p1){//返回两个点的距离
	return sqrt(dis2(p0,p1));
}

/**
 * 中垂线
 */
struct StraightLine{
	double A,B,C;
	StraightLine(double _a = 0,double _b = 0,double _c = 0):A(_a),B(_b),C(_c){

	}

	Point cross(const StraightLine& a)const{
		double xx = -(C*a.B - a.C*B)/(A*a.B - B*a.A);
		double yy = -(C*a.A - a.C*A)/(B*a.A - a.B*A);
		return Point(xx,yy);
	}
};

/**
 * 计算p1p3的中垂线与p1p2的中垂线的交点坐标p(即,外接圆的圆心),p和p1的距离(外接圆的半径)
 */
double circumcenter(const Point& p1,const Point& p2,const Point& p3,Point& p){
	p = p1 + StraightLine(p3.x - p1.x ,p3.y - p1.y ,-dis2(p3,p1)/2.0).cross(StraightLine(p2.x - p1.x,p2.y - p1.y,-dis2(p2,p1)/2.0));
	return dis(p,p1);
}

Point p1,p2,p3,p;

void print(double x){//输出系数或位移
	if(x > 0){
		printf(" + %.3lf",x);
	}else{
		printf(" - %.3lf",-x);
	}
}

int main(){
	while(cin >> p1.x >> p1.y >> p2.x >> p2.y >> p3.x >> p3.y){
		double r = circumcenter(p1,p2,p3,p);
		printf("(x");
		print(-p.x);
		printf(")^2 + (y");
		print(-p.y);
		printf(")^2 =");
		printf(" %.3lf",r);
		printf("^2\n");

		printf("x^2 + y^2");
		print(-2*p.x);
		printf("x");
		print(-2*p.y);
		printf("y");
		print(sqr(p.x) + sqr(p.y) - sqr(r));
		printf(" = 0\n\n");
	}

	return 0;
}

你可能感兴趣的:((计算几何8.1.2.3)POJ 1329 Circle Through Three Points(利用叉积来计算三角形的外接圆的圆心))