ZOJ 1090 The Circumference of the Circle(计算三角形的外接圆)

提交地址:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1090


这是我最喜欢做的题之一,因为这种题我感觉 做一遍以后就会了,不做心里总是感觉这个地方不会,有点担心。

The Circumference of the Circle Time Limit: 2 Seconds      Memory Limit: 65536 KB

To calculate the circumference of a circle seems to be an easy task - provided you know its diameter. But what if you don't?

You are given the cartesian coordinates of three non-collinear points in the plane.
Your job is to calculate the circumference of the unique circle that intersects all three points.

Input Specification

The input file will contain one or more test cases. Each test case consists of one line containing six real numbers x1,y1, x2,y2,x3,y3, representing the coordinates of the three points. The diameter of the circle determined by the three points will never exceed a million. Input is terminated by end of file.

Output Specification

For each test case, print one line containing one real number telling the circumference of the circle determined by the three points. The circumference is to be printed accurately rounded to two decimals. The value of pi is approximately 3.141592653589793.

Sample Input

0.0 -0.5 0.5 0.0 0.0 0.5
0.0 0.0 0.0 1.0 1.0 1.0
5.0 5.0 5.0 7.0 4.0 6.0
0.0 0.0 -1.0 7.0 7.0 7.0
50.0 50.0 50.0 70.0 40.0 60.0
0.0 0.0 10.0 0.0 20.0 1.0
0.0 -500000.0 500000.0 0.0 0.0 500000.0

Sample Output

3.14
4.44
6.28
31.42
62.83
632.24
3141592.65


先来看看涉及的公式吧!

按照书上来吧:

先看个圆的公式:(x-xo)²+(y-yo)²=r²

已知三点可一代入求得 圆心(xo,yo) 和 半径 r

但是推到公式太复杂。

让我们来换个公式。

已知三点可以求得三条边长a,b,c

sinA/a=sinB/b=sinC/c = 2r

A,B,C为角;

c²=a²+b²-2abcosC

sinC²+cosC²=1

由这些公式就能推到了

sinC²=(c/2r)²

cosC²=((a²+b²-c²)/2ab)²

#include<iostream>
#include<stdio.h>
#include<math.h>
#define  PI 3.141592653589793
using namespace std;
int main()
{
	double x1,y1,x2,y2,x3,y3;
	while(cin>>x1>>y1>>x2>>y2>>x3>>y3)
	{
		double af = (x1-x2)*(x1-x2)+(y1-y2)*(y1-y2);
		double bf = (x1-x3)*(x1-x3)+(y1-y3)*(y1-y3);
		double cf = (x2-x3)*(x2-x3)+(y2-y3)*(y2-y3);
		double left =(af+bf-cf)*(af+bf-cf)/(4*af*bf);
		double sinc2=1-left;
		double r = sqrt(cf/sinc2/4);
		printf("%.2lf\n",r*PI*2);
	}
}

这些代码就足够了,我的af,bf,cf等是a的平方,b 的平方,c的平方。自己推导一下 就出来了,平方太多别乱了 我刚做的时候就乱了,不出结果,浪费了很长时间。

下面再说下一个公式:

三角形外接圆半径为r=abc/4s;

根据海伦公式计算面积:s=sqrt(p(p-a)(p-b)(p-c)) 其中 p=(a+b+c)/2;

这个公式同样可以推到出来。

好了!

感谢自己坚持!


你可能感兴趣的:(编程,算法,ZOJ,math.h)