uva 190 Circle Through Three Points (计算几何) 面向对象的封装

190  Circle Through Three Points

Your team is to write a program that, given the Cartesian coordinates of three points on a plane, will find the equation of the circle through them all. The three points will not be on a straight line.

The solution is to be printed as an equation of the form

equation20

and an equation of the form

equation25

Each line of input to your program will contain the x and y coordinates of three points, in the order tex2html_wrap_inline46 , tex2html_wrap_inline48tex2html_wrap_inline50 , tex2html_wrap_inline52 , tex2html_wrap_inline54 , tex2html_wrap_inline56 . These coordinates will be real numbers separated from each other by one or more spaces.

Your program must print the required equations on two lines using the format given in the sample below. Your computed values for hkrcd, and e in Equations 1 and 2 above are to be printed with three digits after the decimal point. Plus and minus signs in the equations should be changed as needed to avoid multiple signs before a number. Plus, minus, and equal signs must be separated from the adjacent characters by a single space on each side. No other spaces are to appear in the equations. Print a single blank line after each equation pair.

Sample input

7.0 -5.0 -1.0 1.0 0.0 -6.0
1.0 7.0 8.0 6.0 7.0 -2.0

Sample output

(x - 3.000)^2 + (y + 2.000)^2 = 5.000^2
x^2 + y^2 - 6.000x + 4.000y - 12.000 = 0

(x - 3.921)^2 + (y - 2.447)^2 = 5.409^2
x^2 + y^2 - 7.842x - 4.895y - 7.895 = 0
 
解题思路:

已知两点(x1,y1)(x2,y2)求直线方程(ax+by+c=0) :

解得:a=y1-y2,b=x2-x1,c=x1*y2-x2*y1;

已知两点(x1,y1)(x2,y2)求直线中垂线方程(ax+by+c=0) :

解得:a= x2-x1,b=y2-y1,c=(x1*x1-x2*x2+y1*y1-y2*y2)/2;

已知两直线(不共线)方程(ax+by+c=0)求交点:

解得:point (  (b1*c2-b2*c1)/ (a1*b2-b1*a2)  ,  (c1*a2-c2*a1)/(a1*b2-b1*a2)  )

 
代码:
 
#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;

struct Point{
	double x,y;
	Point(double x0=0,double y0=0){
		x=x0;
		y=y0;
	}
	friend istream& operator>>(istream &in,Point &p){
		in>>p.x>>p.y;
        return in;
    }
    friend ostream& operator<<(ostream &out,Point &p){
    	out<<p.x<<" "<<p.y;
    	return out;
    }
    double getdis(Point p){
    	return double(sqrt((x-p.x)*(x-p.x)+(y-p.y)*(y-p.y)));
    }
};

struct Line{//Line ax+by+c=0
	double a,b,c;
	Line(double a0=0,double b0=0,double c0=0){
		a=a0;
		b=b0;
		c=c0;
	}
};

Line getLine(Point p1,Point p2){//get the Line that cross point (p1 + p2)/2
	//a= x2-x1,b=y2-y1,c=(x1*x1-x2*x2+y1*y1-y2*y2)/2;
	Line tmpLine;
	tmpLine.a=p2.x-p1.x;
	tmpLine.b=p2.y-p1.y;
	tmpLine.c=(p1.x*p1.x-p2.x*p2.x+p1.y*p1.y-p2.y*p2.y)/double(2);
	//cout<<tmpLine.a<<" "<<tmpLine.b<<" "<<tmpLine.c<<endl;
	return tmpLine;
}

Point getCrossPoint(Line l1,Line l2){//get the cross point of Line l1 and l2 
	//point((b1*c2-b2*c1)/ (a1*b2-b1*a2),(c1*a2-c2*a1)/(a1*b2-b1*a2))
	Point tmpPoint;
	tmpPoint.x=(l1.b*l2.c-l2.b*l1.c)/(l1.a*l2.b-l1.b*l2.a);
	tmpPoint.y=(l1.c*l2.a-l2.c*l1.a)/(l1.a*l2.b-l1.b*l2.a);
	//cout<<tmpPoint<<endl;
	return tmpPoint;
}

void output(Point p,double r){
	//(x - 3.000)^2 + (y + 2.000)^2 = 5.000^2
	printf("(x ");
	if(p.x<=1e-9) printf("+");
	else printf("-");
	printf(" %.3lf)^2 + (y ",abs(p.x));
	if(p.y<=1e-9) printf("+");
	else printf("-");
	printf(" %.3lf)^2 = %.3lf^2\n",abs(p.y),r);
	//x^2 + y^2 - 6.000x + 4.000y - 12.000 = 0
	printf("x^2 + y^2 ");
	if(p.x<=1e-9) printf("+");
	else printf("-");
	printf(" %.3lfx ",abs(2*p.x));
	if(p.y<=1e-9) printf("+");
	else printf("-");
	printf(" %.3lfy ",abs(2*p.y));
	double last=r*r-p.x*p.x-p.y*p.y;
	if(last<=1e-9) printf("+");
	else printf("-");
	printf(" %.3lf = 0\n\n",abs(last));
}

int main(){
	Point a,b,c,center;
	while(cin>>a>>b>>c){
		Line l1=getLine(a,b);
		Line l2=getLine(b,c);
		center=getCrossPoint(l1,l2);
		double r=center.getdis(a);
		output(center,r);
	}
	return 0;
}



                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

炒饭君

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值