uva That Nice Euler Circuit(计算几何 欧拉公式)

来源:互联网 发布:手机 淘宝 降价提醒 编辑:程序博客网 时间:2024/06/15 16:07

欧拉公式:V+F-E=2

求V:

可以想象把多边形“展开”成为一个凸多边形,则至少n条边,n个顶点。

于是需要求的只有,线段之间两两的交点。

求E:

交点求出来后,去重,枚举判断是否在线段上,在的话新增一条线段。

PS:

    判重用到了unique:操作范围为[first, last),对于一段连续相等的区间只保留第一个元素,返回值为新的last。unique不改变容器性质,只改变内容。

   所以sort一下,再unique就轻松去重了。

#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <vector>#include <queue>#include <stack>#include <cassert>#include <algorithm>#include <cmath>#include <limits>#include <set>#include <map>using namespace std;#define MIN(a, b) a < b ? a : b#define MAX(a, b) a > b ? a : b#define F(i, n) for (int i=0;i<(n);++i)#define REP(i, s, t) for(int i=s;i<=t;++i)#define IREP(i, s, t) for(int i=s;i>=t;--i)#define REPOK(i, s, t, o) for(int i=s;i<=t && o;++i)#define MEM0(addr, size) memset(addr, 0, size)#define LBIT(x) x&-x#define PI 3.1415926535897932384626433832795#define HALF_PI 1.5707963267948966192313216916398#define MAXN 300#define MAXM 100#define MOD 20071027typedef long long LL;const double maxdouble = numeric_limits<double>::max();const double eps = 1e-10;const int INF = 0x7FFFFFFF;struct Point {    double x, y;    Point(){};    Point(double x, double y):x(x),y(y){};};typedef Point Vector;Vector operator - (Point A, Point 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, 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) {    if (dcmp(a.x - b.x) == 0 && dcmp(a.y - b.y) == 0)        return true;    return false;}bool operator < (const Point& a, const Point& b) {    return a.x < b.x || (a.x == b.x && a.y < b.y);}double Dot(Vector A, Vector B) {    return A.x*B.x + A.y*B.y;}double Cross(Vector A, Vector B) {    return A.x*B.y-A.y*B.x;}double Length(Vector A) {    return sqrt(Dot(A, A));}double Area2(Point A, Point B, Point C) {    return Cross(B-A, C-A);}double Angle(Vector A, Vector B) {    return acos(Dot(A, B) / Length(A) / Length(B));}Vector Rotate(Vector A, double rad) {    return Vector(A.x*cos(rad)-A.y*sin(rad), A.x*sin(rad)+A.y*cos(rad));}// 单位法线 左转90度后归一化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;}bool SeqmentProperIntersection(Point a1, Point a2, Point b1, Point b2) {    double c1 = Cross(a2-a1, b2-a1), c2 = Cross(a2-a1, b1-a1),           c3 = Cross(b2-b1, a2-b1), c4 = Cross(b2-b1, a1-b1);    if (dcmp(c1)*dcmp(c2) < 0 && dcmp(c3)*dcmp(c4) < 0)        return true;    return false;}bool OnSegment(Point p, Point a1, Point a2) {    return dcmp(Cross(a1-p, a2-p)) == 0 && dcmp(Dot(a1-p, a2-p)) < 0;}double cos_theorem(double edge1, double edge2, double edge3) {    return acos((pow(edge1, 2.0) + pow(edge2, 2.0) - pow(edge3, 2.0)) / (2*edge1*edge2));}Point seq[MAXN + 1];Point pts[MAXN * MAXN + 1];int main(){    int N;    int ncases = 0;    while(scanf("%d", &N) == 1 && N) {        int c, e;        // 至少有 n 个顶点与 n 条边        F(i, N) {            scanf("%lf%lf",&seq[i].x, &seq[i].y);            pts[i] = seq[i];        }        N--;        c = N;        e = N;        REP(i, 0, N-1)            REP(j, i+1, N-1)                if (SeqmentProperIntersection(seq[i], seq[i+1], seq[j], seq[j+1]))                    pts[c++] = GetLineIntersection(seq[i], seq[i+1] - seq[i], seq[j], seq[j+1] - seq[j]);        sort(pts, pts+c);        c = unique(pts, pts+c) - pts;        REP(i, 0, N-1)            REP(j, 0, c-1)                if (OnSegment(pts[j], seq[i], seq[i+1]))                    e++;        ++ncases;        printf("Case %d: There are %d pieces.\n", ncases, e-c+2);    }    return 0;}


0 0
原创粉丝点击