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

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?
 
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.
 
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
0
 
Sample Output
3.0 2.7
2.0 2.3
 
Source
 
Recommend
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、三角形内到三角形三边距离之积最大的点。
  代码一
 1 #include <iostream>
 2 #include <stdio.h>
 3 using namespace std;
 4 /********** 定义点 **********/
 5 struct Point{
 6     double x,y;
 7     Point(double x=0,double y=0):x(x),y(y) {}
 8 };
 9 /********** 定义向量 **********/
10 typedef Point Vector;
11 
12 /********** 向量 + 向量 = 向量 **********/
13 Vector operator + (Vector a,Vector b)
14 {
15     return Vector(a.x+b.x,a.y+b.y);
16 }
17 /********** 点 - 点 = 向量 **********/
18 Vector operator - (Point a,Point b)    
19 {
20     return Vector(a.x-b.x,a.y-b.y);
21 }
22 /********** 向量 * 数 = 向量 **********/
23 Vector operator * (Vector a,double b)
24 {
25     return Vector(a.x*b,a.y*b);
26 }
27 /********** 向量 / 数 = 向量 **********/
28 Vector operator / (Vector a,double b)
29 {
30     return Vector(a.x/b,a.y/b);
31 }
32 /********** 向量点积 **********/
33 double Dot(Vector a,Vector b)
34 {
35     return a.x*b.x+a.y*b.y;
36 }
37 /********** 2向量求叉积 **********/
38 double Cross(Vector a,Vector b)
39 {
40     return a.x*b.y-b.x*a.y;
41 }
42 /********** 3点求叉积 **********/
43 double Cross(Point a,Point b,Point c)
44 {
45     return (c.x-a.x)*(b.y-a.y) - (c.y-a.y)*(b.x-a.x);
46 }
47 Point GetLineIntersection(Point P,Vector v,Point Q,Vector w)    //求射线交点
48 {
49     Vector u = P-Q;
50     double t = Cross(w,u) / Cross(v,w);
51     return P+v*t;
52 }
53 Point GetGravity(Point p[])    //求三角形重心
54 {
55     Vector v,w;
56     Point c1,c2;
57     c1.x = (p[0].x+p[2].x)/2;
58     c1.y = (p[0].y+p[2].y)/2;
59     c2.x = (p[1].x+p[2].x)/2;
60     c2.y = (p[1].y+p[2].y)/2;
61     v = c1-p[1];
62     w = c2-p[0];
63     return GetLineIntersection(p[1],v,p[0],w);
64 }
65 int main()
66 {
67     int n;
68     while(cin>>n){
69         if(n==0) break;
70         for(int i=0;i<n;i++){    //输入
71             Point p[3];
72             cin>>p[0].x>>p[0].y;
73             cin>>p[1].x>>p[1].y;
74             cin>>p[2].x>>p[2].y;
75             Point r = GetGravity(p);
76             printf("%.1lf %.1lf
",r.x,r.y);
77         }
78     }
79     return 0;
80 }

  代码二

 1 #include <stdio.h>
 2 typedef struct {
 3     double x,y;
 4 }Point;
 5 int main()
 6 {
 7     int n;
 8     while(scanf("%d",&n)!=EOF){
 9         if(n==0) break;
10         while(n--){
11             Point a,b,c;
12             scanf("%lf%lf%lf%lf%lf%lf",&a.x,&a.y,&b.x,&b.y,&c.x,&c.y);
13             Point z;
14             z.x = (a.x+b.x+c.x)/3;
15             z.y = (a.y+b.y+c.y)/3;
16             printf("%.1lf %.1lf
",z.x,z.y);
17         }
18     }
19     return 0;
20 }

Freecode : www.cnblogs.com/yym2013

原文地址:https://www.cnblogs.com/yym2013/p/3657569.html