UVALive 3263 That Nice Euler Circuit
来源:互联网 发布:淘宝的云客服 编辑:程序博客网 时间:2024/06/04 15:32
给出一个点的序列,按顺序做一笔画,求最后的图形把平面分成了几部分。
欧拉定理:E:线段数,V节点数,F平面数;有F+V-E==2.
所以存下来所有线段之后,求出不同的交点数,再根据这些交点找出分割后的线段数,最后又公式算出F即可。
#include <iostream>#include <cstdio>#include <algorithm>#include <cstring>#include <cmath>#include <string>typedef double type;using namespace std;struct Point{ type x,y; Point(){} Point(type a,type b) { x=a; y=b; } void read() { scanf("%lf%lf",&x,&y); } void print() { printf("%.6lf %.6lf",x,y); }};typedef Point Vector;Vector operator + (Vector A,Vector B){ return Vector(A.x+B.x,A.y+B.y);}Vector operator - (Point A,Point B){ return Vector(A.x-B.x,A.y-B.y);}Vector operator * (Vector A,type p){ return Vector(A.x*p,A.y*p);}Vector operator / (Vector A,type p){ return Vector(A.x/p,A.y/p);}bool operator < (const Point &a,const Point &b){ return a.x<b.x || (a.x==b.x && a.y<b.y);}const double eps=1e-10;int dcmp(double x){ if (fabs(x)<eps) return 0; else return x<0?-1:1;}bool operator == (const Point& a,const Point b){ return dcmp(a.x-b.x)==0 && dcmp(a.y-b.y)==0;}//atan2(x,y) :向量(x,y)的极角,即从x轴正半轴旋转到该向量方向所需要的角度。type Dot(Vector A,Vector B){ return A.x*B.x+A.y*B.y;}type Cross(Vector A,Vector B){ return A.x*B.y-A.y*B.x;}type Length(Vector A){ return sqrt(Dot(A,A));}type Angle(Vector A,Vector B){ return acos(Dot(A,B))/Length(A)/Length(B);}type Area2(Point A,Point B,Point C){ return Cross(B-A,C-A);}Vector Rotate(Vector A,double rad){ return Vector(A.x*cos(rad)-A.y*sin(rad),A.x*sin(rad)+A.y*cos(rad));}Vector Normal(Vector A)//单位法线,左转90度,长度归一{ double L=Length(A); return Vector(-A.y/L,A.x/L);}Point GetLineIntersection(Point P,Vector v,Point Q,Vector w){ Vector u=P-Q; double t=Cross(w,u)/Cross(v,w); return P+v*t;}double DistanceToLine(Point P,Point A,Point B){ Vector v1=B-A,v2=P-A; return fabs(Cross(v1,v2))/Length(v1);}double DistanceToSegment(Point P,Point A,Point B){ if (A==B) return Length(P-A); Vector v1=B-A,v2=P-A,v3=P-B; if (dcmp(Dot(v1,v2))<0) return Length(v2); else if (dcmp(Dot(v1,v3))>0) return Length(v3); else return fabs(Cross(v1,v2))/Length(v1);}Point GetLineProjection(Point P,Point A,Point B)//P在AB上的投影{ Vector v=B-A; return A+v*(Dot(v,P-A)/Dot(v,v));}bool SegmentProperIntersection(Point a1,Point a2,Point b1,Point b2){ double c1=Cross(a2-a1,b1-a1),c2=Cross(a2-a1,b2-a1), c3=Cross(b2-b1,a1-b1),c4=Cross(b2-b1,a2-b1); return dcmp(c1)*dcmp(c2)<0 && dcmp(c3)*dcmp(c4)<0;}bool OnSegment(Point p,Point a1,Point a2){ return dcmp(Cross(a1-p,a2-p))==0 && dcmp(Dot(a1-p,a2-p))<0;}double ConvexPolygonArea(Point* p,int n)//多边形面积{ double area=0; for (int i=1; i<n-1; i++) area+=Cross(p[i]-p[0],p[i+1]-p[0]); return area/2.0;}double PolygonArea(Point* p,int n)//有向面积{ double area=0; for (int i=1; i<n-1; i++) area+=Cross(p[i]-p[0],p[i+1]-p[0]); return area/2.0;}Point p[1020];Point ans[123000];int cnt;int n;int main(){// freopen("in.txt","r",stdin); int tt=0; while(~scanf("%d",&n) && n) { tt++; for (int i=0; i<n; i++) p[i].read(); n--; cnt=n; memcpy(ans,p,sizeof p); for (int i=0; i<n; i++) for (int j=i+1; j<n; j++) { if (SegmentProperIntersection(p[i],p[i+1],p[j],p[j+1])) { ans[cnt++]=GetLineIntersection(p[i],p[i+1]-p[i],p[j],p[j+1]-p[j]); } } sort(ans,ans+cnt); cnt=unique(ans,ans+cnt)-ans; int e=n; for (int i=0; i<cnt; i++) for (int j=0; j<n; j++) { if (OnSegment(ans[i],p[j],p[j+1])) { e++; } } printf("Case %d: There are %d pieces.\n",tt,e+2-cnt); } return 0;}
0 0
- UVALive 3263 That Nice Euler Circuit
- UVALive 3263 That Nice Euler Circuit
- UVALive 3263 That Nice Euler Circuit
- Shanghai 2004(UVaLive 3263) - That Nice Euler Circuit
- UVALive - 3263 That Nice Euler Circuit (几何)
- UVALive - 3263 - That Nice Euler Circuit (计算几何~~)
- uvaLive 3263 That Nice Euler Circuit 欧拉定理
- UVALive 3263 That Nice Euler Circuit(计算几何)
- That Nice Euler Circuit
- LA 3263 - That Nice Euler Circuit
- UVA LIVE-3263 - That Nice Euler Circuit
- UVALive 3263 That Nice Euler Circuit 计算几何欧拉定理
- UVALive 3263 That Nice Euler Circuit 计算几何+欧拉定理
- Uvalive 3263 That Nice Euler Circuit(几何欧拉定理)
- uva1342 That Nice Euler Circuit
- HDU 1665 or UVALive 3263 || That Nice Euler Circuit (欧拉定理求面数 F = E+2-V
- LA 3263 - That Nice Euler Circuit 欧拉定理
- POJ 2284 That Nice Euler Circuit (LA 3263 HDU 1665)
- Hibernate 批量处理
- 顺时针打印矩阵
- C#中动态执行代码,执行字符串中的代码
- FrameLayout 程序界面预加载
- Get data from an Excel file without importing the data to the data table
- UVALive 3263 That Nice Euler Circuit
- 初识java继承
- Python yield 使用浅析
- UML-用例图(1)
- mysql innodb 索引失效问题引起表级锁
- Cocos2d-x学习之windows 7 android环境搭建
- 【带删除并查集】FZU 2155——盟国
- TCP/IP学习之 TCP与UDP的 区别
- Struts1 配置入门