三点确定圆心和半径(模版)

struct Point2f{
	double x,y;
};
struct CircleData
{
	Point2f center;
	double radius;
};
//方法一:通过其中两点的中垂线交点求圆心
CircleData findCircle1(Point2f pt1, Point2f pt2, Point2f pt3)
{
	//定义两个点,分别表示两个中点
	Point2f midpt1, midpt2;
	//求出点1和点2的中点
	midpt1.x = (pt2.x + pt1.x) / 2;
	midpt1.y = (pt2.y + pt1.y) / 2;
	//求出点3和点1的中点
	midpt2.x = (pt3.x + pt1.x) / 2;
	midpt2.y = (pt3.y + pt1.y) / 2;
	//求出分别与直线pt1pt2,pt1pt3垂直的直线的斜率
	double k1 = -(pt2.x - pt1.x) / (pt2.y - pt1.y);
	double k2 = -(pt3.x - pt1.x) / (pt3.y - pt1.y);
	//然后求出过中点midpt1,斜率为k1的直线方程(既pt1pt2的中垂线):y - midPt1.y = k1( x - midPt1.x)
	//以及过中点midpt2,斜率为k2的直线方程(既pt1pt3的中垂线):y - midPt2.y = k2( x - midPt2.x)
	//定义一个圆的数据的结构体对象CD
	CircleData CD;
	//连立两条中垂线方程求解交点得到:
	CD.center.x = (midpt2.y - midpt1.y - k2* midpt2.x + k1*midpt1.x) / (k1 - k2);
	CD.center.y = midpt1.y + k1*(midpt2.y - midpt1.y - k2*midpt2.x + k2*midpt1.x) / (k1 - k2);
	//用圆心和其中一个点求距离得到半径:
	CD.radius = sqrt((CD.center.x - pt1.x)*(CD.center.x - pt1.x) + (CD.center.y - pt1.y)*(CD.center.y - pt1.y));
	return CD;
}
//方法二:通过三个点到圆心距离相等建立方程:
CircleData findCircle2(Point2f pt1, Point2f pt2, Point2f pt3)
{
 
	//令:
	//A1 = 2 * pt2.x - 2 * pt1.x      B1 = 2 * pt1.y - 2 * pt2.y       C1 = pt1.y² + pt2.x² - pt1.x² - pt2.y²
	//A2 = 2 * pt3.x - 2 * pt2.x      B2 = 2 * pt2.y - 2 * pt3.y       C2 = pt2.y² + pt3.x² - pt2.x² - pt3.y²
	double A1, A2, B1, B2, C1, C2, temp;
	A1 = pt1.x - pt2.x;
	B1 = pt1.y - pt2.y;
	C1 = (pow(pt1.x, 2) - pow(pt2.x, 2) + pow(pt1.y, 2) - pow(pt2.y, 2)) / 2;
	A2 = pt3.x - pt2.x;
	B2 = pt3.y - pt2.y;
	C2 = (pow(pt3.x, 2) - pow(pt2.x, 2) + pow(pt3.y, 2) - pow(pt2.y, 2)) / 2;
	//为了方便编写程序,令temp = A1*B2 - A2*B1
	temp = A1*B2 - A2*B1;
	//定义一个圆的数据的结构体对象CD
	CircleData CD;
	//判断三点是否共线
	if (temp == 0){
		//共线则将第一个点pt1作为圆心
		CD.center.x = pt1.x;
		CD.center.y = pt1.y;
	}
	else{
		//不共线则求出圆心:
		//center.x = (C1*B2 - C2*B1) / A1*B2 - A2*B1;
		//center.y = (A1*C2 - A2*C1) / A1*B2 - A2*B1;
		CD.center.x = (C1*B2 - C2*B1) / temp;
		CD.center.y = (A1*C2 - A2*C1) / temp;
	}
	
	CD.radius = sqrt((CD.center.x - pt1.x)*(CD.center.x - pt1.x) + (CD.center.y - pt1.y)*(CD.center.y - pt1.y));
	return CD;
}

 

你可能感兴趣的:(模板)