POJ1329Circle Through Three Points

       赤裸裸的求三角形外接圆,即根据三点坐标确定圆心坐标和圆的半径。

       关键点在于已知三点,我们要求圆的表达式,大二高数结束后这数学算是好久不碰,推个公式推得不要不要的,如果是参加竞赛劝诫大家最好把这些数学公式做成模板:

                 a=2*(x2-x1);
b=2*(y2-y1);
c=pow(x2,2)+pow(y2,2)-pow(x1,2)-pow(y1,2);
d=2*(x3-x2);
e=2*(y3-y2);
f=pow(x3,2)+pow(y3,2)-pow(x2,2)-pow(y2,2);
x=((b*f)-(e*c))/((b*d)-(e*a));
y=((d*c)-(a*f))/((b*d)-(e*a));
r=sqrt(pow(x-x1,2)+pow(y-y1,2));

       后面就简单了,变化公式而已。最后需要注意一点的输出的格式控制,这里我做得并不是特别好。看了网上大牛的才发现原来可以更简洁的。不过算了。。。下面附上我的代码:

      

Circle Through Three Points
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 3632 Accepted: 1519

Description

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 
	(x - h)^2 + (y - k)^2 = r^2				(1)

and an equation of the form 
	x^2 + y^2 + cx + dy - e = 0				(2)

Input

Each line of input to your program will contain the x and y coordinates of three points, in the order Ax, Ay, Bx, By, Cx, Cy. These coordinates will be real numbers separated from each other by one or more spaces.

Output

Your program must print the required equations on two lines using the format given in the sample below. Your computed values for h, k, r, c, d, 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

Source

[Submit]   [Go Back]   [Status]   [Discuss]

#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;

int main()
{
	double x1,y1,x2,y2,x3,y3;
	double a,b,c,d,e,f,x,y,r,m,n,o;
	while(scanf("%lf%lf%lf%lf%lf%lf",&x1,&y1,&x2,&y2,&x3,&y3)!=EOF)
	{
		a=2*(x2-x1);
		b=2*(y2-y1);
		c=pow(x2,2)+pow(y2,2)-pow(x1,2)-pow(y1,2);
		d=2*(x3-x2);
		e=2*(y3-y2);
		f=pow(x3,2)+pow(y3,2)-pow(x2,2)-pow(y2,2);
		x=((b*f)-(e*c))/((b*d)-(e*a));
		y=((d*c)-(a*f))/((b*d)-(e*a));
		r=sqrt(pow(x-x1,2)+pow(y-y1,2));
		m=2*x;
		n=2*y;
		o=pow(x,2)+pow(y,2)-pow(r,2);
		if(x>0){
			printf("(x - %0.3lf)^2",x);
			if(y>0)
			 printf(" + (y - %0.3lf)^2 = %0.3lf^2\n",y,r);
			else{
				printf(" + (y + %0.3lf)^2 = %0.3lf^2\n",fabs(y),r);
			}
		}
	   else {
	   	   printf("(x + %0.3lf)^2",fabs(x));
	   	   if(y>0)
			 printf(" + (y - %0.3lf)^2 = %0.3lf^2\n\n",y,r);
			else{
				printf(" + (y + %0.3lf)^2 = %0.3lf^2\n",fabs(y),r);
			}
	   }
	   printf("x^2 + y^2 ");
	  if(m>0){
	  	printf("- %0.3lfx ",m);
	  	if(n>0){
	  		printf("- %0.3lfy ",n);
	  		if(o>0){
	  			printf("+ %0.3lf = 0\n\n",o);
			  }
			else
			  printf("- %0.3lf = 0\n\n",fabs(o));
		  }
		else{
			printf("+ %0.3lfy ",fabs(n));
			if(o>0){
	  			printf("+ %0.3lf = 0\n\n",o);
			  }
			else
			  printf("- %0.3lf = 0\n\n",fabs(o));
		}
	  }
	  else{
	  	 printf("+ %0.3lfx ",fabs(m)); 
	  	 if(n>0){
	  		printf("- %0.3lfy ",n);
	  		if(o>0){
	  			printf("+ %0.3lf = 0\n\n",o);
			  }
			else
			  printf("- %0.3lf = 0\n\n",fabs(o));
		  }
		else{
			printf("+ %0.3lfy ",fabs(n));
			if(o>0){
	  			printf("+ %0.3lf = 0\n\n",o);
			  }
			else
			  printf("- %0.3lf = 0\n\n",fabs(o));
		}
	  }
		//printf("%lf %lf %lf\n",x,y,r);
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值