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

链接:

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

题意:

给你一个多边形,求它的面积,内部格点数目,边上格点数目

题解:

pick公式:

给定顶点坐标均是整数点的简单多边形,有

          面积=内部格点数目+边上格点数目/2+1

边界上的格点数:

把每条边当做左开右闭的区间以避免重复,一条左开右闭的线段(x1,y1)->(x2,y2)上的格点数为:

          gcd(x2-x1,y2-y1)。

代码:

  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], a[i + 1]);
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     int gcd(int a, int b) {
116         return b == 0 ? a : gcd(b, a%b);
117     }
118     int Border_Int_Point_Num() {
119         int num = 0;
120         a[n] = a[0];
121         rep(i, 0, n) num += gcd(abs(int(a[i + 1].x - a[i].x)),
122             abs(int(a[i + 1].y - a[i].y)));
123         return num;
124     }
125     int Inside_Int_Point_Num() {
126         return int(area()) + 1 - Border_Int_Point_Num() / 2;
127     }
128 };
129
130 int n;
131 polygon p;
132
133 int main() {
134     int T;
135     cin >> T;
136     rep(cas, 1, T + 1) {
137         cin >> n;
138         p.n = n;
139         rep(i, 0, n) scanf("%lf%lf", &p.a[i].x, &p.a[i].y);
140         rep(i, 1, n) p.a[i] = p.a[i] + p.a[i - 1];
141         int a = p.Inside_Int_Point_Num();
142         int b = p.Border_Int_Point_Num();
143         double c = p.area();
144         printf("Scenario #%d:\n%d %d %.1f\n\n", cas, a, b, c);
145     }
146     return 0;
147 }
时间: 2024-10-13 22:23:26

POJ 1265 计算几何 多边形面积 内部格点数 边上格点数的相关文章

pick定理:面积=内部整数点数+边上整数点数/2-1

1 //pick定理:面积=内部整数点数+边上整数点数/2-1 2 // POJ 2954 3 4 #include <iostream> 5 #include <cstdio> 6 #include <cstdlib> 7 #include <algorithm> 8 #include <vector> 9 #include <math.h> 10 using namespace std; 11 #define LL long lo

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

POJ 1654 Area [多边形面积]

Area Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 19642   Accepted: 5376 Description You are going to compute the area of a special kind of polygon. One vertex of the polygon is the origin of the orthogonal coordinate system. From thi

POJ 1654 Area 多边形面积 G++会WA

#include<stdio.h> #include<algorithm> #include <cstring> using namespace std; typedef long long ll; const int MAXN = 1000008; char s[MAXN]; int dx[] = {-1, -1, -1, 0, 0, 0, 1, 1, 1}; int dy[] = {-1, 0, 1, -1, 0, 1, -1, 0, 1}; int main()

Area - POJ 1265(pick定理求格点数+求多边形面积)

题目大意:以原点为起点然后每次增加一个x,y的值,求出来最后在多边形边上的点有多少个,内部的点有多少个,多边形的面积是多少. 分析: 1.以格子点为顶点的线段,覆盖的点的个数为GCD(dx,dy),其中,dxdy分别为线段横向占的点数和纵向占的点数.如果dx或dy为0,则覆盖的点数为dy或dx.2.Pick公式:平面上以格子点为顶点的简单多边形的面积=边上的点数/2+内部的点数+1.3.任意一个多边形的面积等于按顺序求相邻两个点与原点组成的向量的叉积之和. 代码如下: -------------

poj 1265 Area 面积+多边形内点数

Area Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5861   Accepted: 2612 Description Being well known for its highly innovative products, Merck would definitely be a good target for industrial espionage. To protect its brand-new resear

POJ 1265-Area(计算几何+皮克定理+多边形面积公式)

题目地址:POJ 1265 题意:给定一个格点多边形,求出内部点数in,边上点数on,和面积S. 思路:运用的定理很多. 1.皮克定理:S=in+on/2-1,即in=(2*S+2-on)/2. 2.多边形的面积公式:按顺序求相邻两个点与原点组成的向量的叉积之和. 3.求边上的格点数:以格子点为顶点的线段,覆盖的点的个数为GCD(dx,dy),其中,dxdy分别为线段横向占的点数和纵向占的点数.如果dx或dy为0,则覆盖的点数为dy或dx. #include <stdio.h> #includ

poj 1265 Area (Pick定理+求面积)

链接:http://poj.org/problem?id=1265 Area Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 4969   Accepted: 2231 Description Being well known for its highly innovative products, Merck would definitely be a good target for industrial espionag

eoj1127 计算几何 任意多边形面积

题目:eoj1127 " 改革春风吹满地, 不会算法没关系; 实在不行回老家, 还有一亩三分地. 谢谢!(乐队奏乐)" 话说部分学生心态极好,每天就知道游戏,这次考试如此简单的题目,也是云里雾里,而且,还竟然来这么几句打油诗. 好呀,老师的责任就是帮你解决问题,既然想种田,那就分你一块. 这是一块多边形形状的田,原本是Partychen的,现在就准备送给你了.不过,任何事情都没有那么简单,你必须首先告诉我这块地到底有多少面积,如果回答正确才能真正得到这块地. 发愁了吧?就是要让你知道,