vijos p1233
2016-04-24 18:44
288 查看
多边形面积,注意退化成一条线的情况。
#include <iostream> #include <algorithm> #include <cmath> #include <cstdio> using namespace std; const double EPS = 1e-9; const double INF = 1e30; const double PI = 3.1415926; struct pt { double x,y; pt(double x=0,double y=0):x(x),y(y){} pt read() { int a,b; scanf("%d%d",&a,&b); this->x=a;this->y=b; return *this; } }; typedef pt vec; inline vec operator - (vec a,vec b){return vec(a.x-b.x,a.y-b.y);} inline double dot(vec a,vec b){return a.x*b.x+a.y*b.y;} inline double len(vec a){return sqrt(dot(a,a));} inline double cross(vec a,vec b){return a.x*b.y-a.y*b.x;} inline bool operator < (const vec &a,const vec &b){return a.x<b.x || (a.x==b.x && a.y<b.y);} inline int dcmp(double x){if(fabs(x)<EPS)return 0;return (x>0)?1:-1;} double PolygonAera(pt *p,int n) { double aera=0.0; for(int i=1;i<n-1;i++)aera+=cross(p[i]-p[0],p[i+1]-p[0]); return aera/2.0; } double pc(pt *p,int n) { double c=len(p[0]-p[n-1]); for(int i=1;i<n;i++)c+=len(p[i]-p[i-1]); return c; } int chull(pt *p,int n,pt *ch) { sort(p,p+n); int m=0; for(int i=0;i<n;i++) { while(m>1 && cross(ch[m-1]-ch[m-2],p[i]-ch[m-2])<=0)m--; ch[m++]=p[i]; } int k=m; for(int i=n-2;i>=0;i--) { while(m>k && cross(ch[m-1]-ch[m-2],p[i]-ch[m-2])<=0)m--; ch[m++]=p[i]; } if(n>1)m--; return m; } int main() { int n; scanf("%d",&n); pt s[20],ch[20]; for(int i=0;i<n;i++)s[i].read(); int m=chull(s,n,ch); bool ok=false; for(int i=0;i<m;i++)if(i>1 && dcmp(cross(ch[i]-ch[i-1],ch[i-1]-ch[i-2]))!=0)ok=true; double c=pc(ch,m); double d=PolygonAera(ch,m); if(ok)printf("%.2lf\n%.2lf\n",c,d); else printf("%.2lf\n%.2lf\n",c/2.0,0.0); return 0; }
相关文章推荐
- 海量数据处理常用思路和方法
- 13-如何执行PHP脚本
- 12-PHP的FastCGI
- =>goes to
- 13、在 uwp应用中,给图片添加高斯模糊滤镜效果(一)
- itextsharp整理
- 11.嵌入式PHP
- 10-Apache运行与钩子函数
- 关于如何避免Android中Bitmap引起的OutOfMemoryError
- 夺命雷公狗ThinkPHP项目之----企业网站13之文章列表页的实现(主要是分页的实现)
- 内存泄漏与内存溢出
- ubuntu下无法保存修改文件
- 关于VMware上安装Ubuntu的一切
- 11g: Program with PL/SQL - 1Z0-144
- Apache配置信息中文版
- bzoj 4004: [JLOI2015]装备购买
- 09-通过mod_php5支持PHP
- Tablyout 点击tab切换页面闪烁
- 查出某个特定时间一小时内,两小时内,N小时内的累计和
- 【Swift学习】Swift编程之旅---函数(十)