您的位置:首页 > 产品设计 > UI/UE

UVALive 3263 That Nice Euler Circuit(计算几何)

2016-08-11 22:26 471 查看
vj的地址

好看的一笔画

让求闭合曲线形成的图形将整个平面分成了几步分。

利用到了欧拉定理:n - m + r = 2;

平面数为r = 2 - n + m

顶点数n 和 边数m 的求法特别点。

要去除在直线中间的点,这利用了
unique函数简解

#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;

const double eps = 1e-6;
struct Point
{
double x,y;
Point(double xx=0,double yy=0):x(xx),y(yy){}
bool operator < (const Point & p)
{
return x < p.x || (x == p.x && y < p.y);
}
};
typedef Point Vector;
//#define Vector Point
Vector operator - (Point b, Point a)
{
return Vector(b.x - a.x, b.y - a.y);
}

Vector operator + (Vector a, Vector p)
{
return Vector(a.x + p.x, a.y + p.y);
}

Vector operator * (Vector A,double p)
{
return Vector(A.x*p,A.y*p);
}
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;
}
double Dot(Vector A, Vector B)//点乘
{
return A.x*B.x + A.y*B.y;
}

double Length(Vector A)//模长
{
return sqrt(Dot(A,A));
}

double Angle(Vector A,Vector B)//夹角
{
return acos(Dot(A,B)/Length(A)/Length(B));
}

double Cross(Vector A,Vector B)//叉乘
{
return A.x*B.y - A.y*B.x;
}

double Area2(Point A,Point B,Point C)//计算叉乘,为三角形面积二倍
{
return Cross(B-A,C-A);
}

Vector Rotate(Vector A,double rad)//按照A向量的起点旋转rad弧度
{
return Vector(A.x*cos(rad)-A.y*sin(rad),A.x*sin(rad) + A.y*cos(rad));
}

Vector Normal(Vector A)//计算向量的单位法线
{
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)//点P到直线AB的距离
{
Vector v1 = B - A,v2 = P - A;
return fabs(Cross(v1,v2) / Length(v1));
}

double DistanceToSegment(Point P,Point A,Point B)//点P到线段AB的距离
{
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)//点在直线上的投影
{
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);
double 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;
}

const int N = 305;
Point p
,V[N*N];
int main()
{
int n;
int kase = 0;
while(~scanf("%d",&n) && n)
{
for(int i = 0;i < n;i++)
{
scanf("%lf%lf",&p[i].x,&p[i].y);
V[i] = p[i];
}
n--;
int c = n,e = n;
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]))
V[c++] = GetLineIntersection(p[i],p[i+1]-p[i],p[j],p[j+1]-p[j]);

sort(V,V+c);
c = unique(V,V+c) - V;
for(int i = 0;i < c;i++)
for(int j = 0;j < n;j++)
if(OnSegment(V[i],p[j],p[j+1]))
e++;
printf("Case %d: There are %d pieces.\n",++kase,e+2-c);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: