nyoj-3-多邊形重心問題(求多邊形面積和中心)
阿新 • • 發佈:2018-04-27
truct abs 算法 online ble ans sin 競賽 poi
題目鏈接
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-多邊形重心問題(求多邊形面積和中心)