码迷,mamicode.com
首页 > 其他好文 > 详细

nyoj-3-多边形重心问题(求多边形面积和中心)

时间:2018-04-26 23:32:11      阅读:196      评论:0      收藏:0      [点我收藏+]

标签:程序设计   cst   for   中心   ace   +=   问题   题目   font   

题目链接

 1 /*
 2     Name:nyoj-3-多边形重心问题
 3     Copyright:
 4     Author:
 5     Date: 2018/4/26 21:25:41
 6     Description:
 7     ACM国际大学生程序设计竞赛 算法与实现的模板
 8 */
 9 #include <iostream>
10 #include <cstdio>
11 #include <cmath>
12 #include <algorithm>
13 using namespace std;
14 const double eps = 1e-8;
15 int cmp(double x) {
16     if (fabs(x) < eps) return 0;
17     if (x >0 )return 1;
18     return -1;
19 }
20 //point
21 struct point {
22     double x, y;
23     point (){}
24     point (double a, double b):x(a), y(b) {    }
25     void input() {
26         scanf("%lf %lf", &x, &y);
27     }
28     friend point operator - (const point &a, const point &b) {
29         return point(a.x-b.x, a.y-b.y);
30     }
31     friend point operator + (const point &a, const point &b) {
32         return point(a.x+b.x, a.y+b.y);
33     }
34     friend point operator * (const double &a, const point &b) {
35         return point (a * b.x, a*b.y);
36     }
37     friend point operator / (const point &a, const double &b) {
38         return point (a.x / b, a.y /b);
39     }
40     friend bool operator == (const point &a, const point &b) {
41         return (cmp(a.x - b.x) == 0 && cmp(a.y - b.y) == 0);
42     }
43 };
44 double det(const point &a, const point &b) {
45     return a.x * b.y - a.y * b.x;
46 }
47 //polygon
48 const int MAXN = 10009;    
49 struct polygon {
50     int n;
51     point a[MAXN];
52     polygon(){
53     }
54     double area() {
55         double sum = 0;
56         a[n] = a[0];
57         for (int i=0; i<n; i++) sum+=det(a[i+1], a[i]);
58         return sum/2;
59     }
60     point MassCenter() {
61         point ans = point(0, 0);
62         if (cmp(area()) == 0) return ans;
63         a[n] = a[0];
64         for (int i=0; i<n; i++) ans = ans + det(a[i+1], a[i]) * (a[i] + a[i+1]) ;
65         return ans / area()/6;
66     }
67 };
68 int main()
69 {
70     int n;
71     cin>>n;
72     while (n--) {
73         int m;
74         cin>>m;
75         polygon pgon;
76         pgon.n = m;
77         for (int i=0; i<m; i++) {
78             cin>>pgon.a[i].x>>pgon.a[i].y;
79         }
80         point ans = pgon.MassCenter();
81         printf("%.3f %.3f\n", pgon.area(), ans.x+ans.y);
82     }
83     return 0;
84 }

 

nyoj-3-多边形重心问题(求多边形面积和中心)

标签:程序设计   cst   for   中心   ace   +=   问题   题目   font   

原文地址:https://www.cnblogs.com/evidd/p/8955212.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!