您的位置:首页 > 其它

hdu 2105:The Center of Gravity(计算几何,求三角形重心)

2014-04-10 22:27 471 查看

The Center of Gravity

Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3971 Accepted Submission(s): 2280


[align=left]Problem Description[/align]
Everyone know the story that how Newton discovered the Universal Gravitation. One day, Newton walked
leisurely, suddenly, an apple hit his head. Then Newton discovered the Universal Gravitation.From then
on,people have sovled many problems by the the theory of the Universal Gravitation. What's more, wo also
have known every object has its Center of Gravity.
Now,you have been given the coordinates of three points of a triangle. Can you calculate the center
of gravity of the triangle?

[align=left]Input[/align]
The first line is an integer n,which is the number of test cases.
Then n lines follow. Each line has 6 numbers x1,y1,x2,y2,x3,y3,which are the coordinates of three points.
The input is terminated by n = 0.

[align=left]Output[/align]
For each case, print the coordinate, accurate up to 1 decimal places.

[align=left]Sample Input[/align]

2

1.0 2.0 3.0 4.0 5.0 2.0

1.0 1.0 4.0 1.0 1.0 5.0

0

[align=left]Sample Output[/align]

3.0 2.7

2.0 2.3

[align=left]Source[/align]
HDU 2007-6 Programming Contest

[align=left]Recommend[/align]
xhd | We have carefully selected several similar problems for you: 2101 2100 2103 2106 2102

  计算几何,求三角形重心
  方法一(麻烦):先求两条边的中点,然后分别将中点与他们相对的顶点相连做直线,交点就是三角形重心。
  方法二(so easy):直接用重心公式求得。重心(x,y)==> x=(x1+x2+x3)/3 ; y=(y1+y2+y3)/3 。
  重心的性质
    1、到三角形三顶点距离的平方和最小的点(距离的和最小的点是费马点)。
    2、三角形内到三角形三边距离之积最大的点。
  代码一

#include <iostream>
#include <stdio.h>
using namespace std;
/********** 定义点 **********/
struct Point{
double x,y;
Point(double x=0,double y=0):x(x),y(y) {}
};
/********** 定义向量 **********/
typedef Point Vector;

/********** 向量 + 向量 = 向量 **********/
Vector operator + (Vector a,Vector b)
{
return Vector(a.x+b.x,a.y+b.y);
}
/********** 点 - 点 = 向量 **********/
Vector operator - (Point a,Point b)
{
return Vector(a.x-b.x,a.y-b.y);
}
/********** 向量 * 数 = 向量 **********/
Vector operator * (Vector a,double b)
{
return Vector(a.x*b,a.y*b);
}
/********** 向量 / 数 = 向量 **********/
Vector operator / (Vector a,double b)
{
return Vector(a.x/b,a.y/b);
}
/********** 向量点积 **********/
double Dot(Vector a,Vector b)
{
return a.x*b.x+a.y*b.y;
}
/********** 2向量求叉积 **********/
double Cross(Vector a,Vector b)
{
return a.x*b.y-b.x*a.y;
}
/********** 3点求叉积 **********/
double Cross(Point a,Point b,Point c)
{
return (c.x-a.x)*(b.y-a.y) - (c.y-a.y)*(b.x-a.x);
}
Point GetLineIntersection(Point P,Vector v,Point Q,Vector w)    //求射线交点
{
Vector u = P-Q;
double t = Cross(w,u) / Cross(v,w);
return P+v*t;
}
Point GetGravity(Point p[])    //求三角形重心
{
Vector v,w;
Point c1,c2;
c1.x = (p[0].x+p[2].x)/2;
c1.y = (p[0].y+p[2].y)/2;
c2.x = (p[1].x+p[2].x)/2;
c2.y = (p[1].y+p[2].y)/2;
v = c1-p[1];
w = c2-p[0];
return GetLineIntersection(p[1],v,p[0],w);
}
int main()
{
int n;
while(cin>>n){
if(n==0) break;
for(int i=0;i<n;i++){    //输入
Point p[3];
cin>>p[0].x>>p[0].y;
cin>>p[1].x>>p[1].y;
cin>>p[2].x>>p[2].y;
Point r = GetGravity(p);
printf("%.1lf %.1lf\n",r.x,r.y);
}
}
return 0;
}


  代码二

#include <stdio.h>
typedef struct {
double x,y;
}Point;
int main()
{
int n;
while(scanf("%d",&n)!=EOF){
if(n==0) break;
while(n--){
Point a,b,c;
scanf("%lf%lf%lf%lf%lf%lf",&a.x,&a.y,&b.x,&b.y,&c.x,&c.y);
Point z;
z.x = (a.x+b.x+c.x)/3;
z.y = (a.y+b.y+c.y)/3;
printf("%.1lf %.1lf\n",z.x,z.y);
}
}
return 0;
}


Freecode : www.cnblogs.com/yym2013
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: