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
Problem Description
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?
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?
Input
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.
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.
Output
For each case, print the coordinate, accurate up to 1 decimal places.
Sample Input
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
Sample Output
3.0 2.7
2.0 2.3
Source
Recommend
计算几何,求三角形重心。
方法一(麻烦):先求两条边的中点,然后分别将中点与他们相对的顶点相连做直线,交点就是三角形重心。
方法二(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=,double y=):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[].x+p[].x)/;
c1.y = (p[].y+p[].y)/;
c2.x = (p[].x+p[].x)/;
c2.y = (p[].y+p[].y)/;
v = c1-p[];
w = c2-p[];
return GetLineIntersection(p[],v,p[],w);
}
int main()
{
int n;
while(cin>>n){
if(n==) break;
for(int i=;i<n;i++){ //输入
Point p[];
cin>>p[].x>>p[].y;
cin>>p[].x>>p[].y;
cin>>p[].x>>p[].y;
Point r = GetGravity(p);
printf("%.1lf %.1lf\n",r.x,r.y);
}
}
return ;
}
代码二:
#include <stdio.h>
typedef struct {
double x,y;
}Point;
int main()
{
int n;
while(scanf("%d",&n)!=EOF){
if(n==) 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)/;
z.y = (a.y+b.y+c.y)/;
printf("%.1lf %.1lf\n",z.x,z.y);
}
}
return ;
}
Freecode : www.cnblogs.com/yym2013