nyoj-3-多边形重心问题(求多边形面积和中心)
Posted 朤尧
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了nyoj-3-多边形重心问题(求多边形面积和中心)相关的知识,希望对你有一定的参考价值。
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-多边形重心问题(求多边形面积和中心)的主要内容,如果未能解决你的问题,请参考以下文章