POJ 1385 计算几何 多边形重心

Posted Flowersea

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ 1385 计算几何 多边形重心相关的知识,希望对你有一定的参考价值。

链接:

http://poj.org/problem?id=1385

题意:

给你一个多边形,求它的重心

题解:

模板题,但是不知道为啥我的结果输出的确是-0.00 -0.00

所以我又写了个 if (ans.x == 0) ans.x = 0 感觉好傻逼

代码:

  1 #include <map>
  2 #include <set>
  3 #include <cmath>
  4 #include <queue>
  5 #include <stack>
  6 #include <cstdio>
  7 #include <string>
  8 #include <vector>
  9 #include <cstdlib>
 10 #include <cstring>
 11 #include <sstream>
 12 #include <iostream>
 13 #include <algorithm>
 14 #include <functional>
 15 using namespace std;
 16 #define rep(i,a,n) for (int i=a;i<n;i++)
 17 #define per(i,a,n) for (int i=n-1;i>=a;i--)
 18 #define all(x) (x).begin(),(x).end()
 19 #define pb push_back
 20 #define mp make_pair
 21 #define lson l,m,rt<<1  
 22 #define rson m+1,r,rt<<1|1 
 23 typedef long long ll;
 24 typedef vector<int> VI;
 25 typedef pair<int, int> PII;
 26 const ll MOD = 1e9 + 7;
 27 const int INF = 0x3f3f3f3f;
 28 const int MAXN = 2e4 + 7;
 29 // head
 30 
 31 const double eps = 1e-8;
 32 int cmp(double x) {
 33     if (fabs(x) < eps) return 0;
 34     if (x > 0) return 1;
 35     return -1;
 36 }
 37 
 38 const double pi = acos(-1);
 39 inline double sqr(double x) {
 40     return x*x;
 41 }
 42 struct point {
 43     double x, y;
 44     point() {}
 45     point(double a, double b) :x(a), y(b) {}
 46     void input() {
 47         scanf("%lf%lf", &x, &y);
 48     }
 49     friend point operator+(const point &a, const point &b) {
 50         return point(a.x + b.x, a.y + b.y);
 51     }
 52     friend point operator-(const point &a, const point &b) {
 53         return point(a.x - b.x, a.y - b.y);
 54     }
 55     friend point operator*(const double &a, const point &b) {
 56         return point(a*b.x, a*b.y);
 57     }
 58     friend point operator/(const point &a, const double &b) {
 59         return point(a.x / b, a.y / b);
 60     }
 61     double norm() {
 62         return sqrt(sqr(x) + sqr(y));
 63     }
 64 };
 65 double det(point a, point b) {
 66     return a.x*b.y - a.y*b.x;
 67 }
 68 double dot(point a, point b) {
 69     return a.x*b.x + a.y*b.y;
 70 }
 71 double dist(point a, point b) {
 72     return (a - b).norm();
 73 }
 74 
 75 struct line {
 76     point a, b;
 77     line() {}
 78     line(point x, point y) :a(x), b(y) {}
 79 };
 80 double dis_point_segment(point p, point s, point t) {
 81     if (cmp(dot(p - s, t - s)) < 0) return (p - s).norm();
 82     if (cmp(dot(p - t, s - t)) < 0) return (p - t).norm();
 83     return fabs(det(s - p, t - p) / dist(s, t));
 84 }
 85 bool point_on_segment(point p, point s, point t) {
 86     return cmp(det(p - s, t - s)) == 0 && cmp(dot(p - s, p - t)) <= 0;
 87 }
 88 bool parallel(line a, line b) {
 89     return !cmp(det(a.a - a.b, b.a - b.b));
 90 }
 91 bool line_make_point(line a, line b,point &res) {
 92     if (parallel(a, b)) return false;
 93     double s1 = det(a.a - b.a, b.b - b.a);
 94     double s2 = det(a.b - b.a, b.b - b.a);
 95     res = (s1*a.b - s2*a.a) / (s1 - s2);
 96     return true;
 97 }
 98 
 99 struct polygon {
100     int n;
101     point a[MAXN];
102     double area() {
103         double sum = 0;
104         a[n] = a[0];
105         rep(i, 0, n) sum += det(a[i + 1], a[i]);
106         return sum / 2;
107     }
108     point MassCenter() {
109         point ans = point(0, 0);
110         if (cmp(area()) == 0) return ans;
111         a[n] = a[0];
112         rep(i, 0, n) ans = ans + det(a[i + 1], a[i])*(a[i] + a[i + 1]);
113         return ans / area() / 6;
114     }
115 };
116 
117 int n;
118 polygon p;
119 
120 int main() {
121     int T;
122     cin >> T;
123     while (T--) {
124         cin >> n;
125         p.n = n;
126         rep(i, 0, n) scanf("%lf%lf", &p.a[i].x, &p.a[i].y);
127         point ans = p.MassCenter();
128         if (ans.x == 0) ans.x = 0;
129         if (ans.y == 0) ans.y = 0;
130         printf("%.2f %.2f\n", ans.x, ans.y);
131     }
132     return 0;
133 }

 

以上是关于POJ 1385 计算几何 多边形重心的主要内容,如果未能解决你的问题,请参考以下文章

POJ 3855 计算几何·多边形重心

hdu1115(计算多边形几何重心)

POJ 1265 计算几何 多边形面积 内部格点数 边上格点数

计算几何

79-多边形的面积-计算几何

poj1584 A round peg in a ground hole计算几何