LA 3263 (欧拉定理 + 判断线段相交 + 求线段交点)

Posted willems

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LA 3263 (欧拉定理 + 判断线段相交 + 求线段交点)相关的知识,希望对你有一定的参考价值。

此题是刘汝佳老师书里的例题,P260

欧拉定理: 设平面图的顶点数为V,边数为E,面数为F,则 V + F - E = 2;

注意这里的面数包括了外面那个面。 例如

技术图片

 

 这个图的面数 为 2, 因为包括了封闭面外面那个面。

 

技术图片
#include <bits/stdc++.h>
#define LL long long
#define mem(i, j) memset(i, j, sizeof(i))
#define rep(i, j, k) for(int i = j; i <= k; i++)
#define dep(i, j, k) for(int i = k; i >= j; i--)
#define pb push_back
#define make make_pair
#define INF INT_MAX
#define inf LLONG_MAX
#define PI acos(-1)
using namespace std;

const int N = 310;

struct Point {
    double x, y;
    Point(double x = 0, double y = 0) : x(x), y(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, double p) { return Vector(A.x * p, A.y * p); }
///向量/数=向量
Vector operator / (Vector A, double p) { return Vector(A.x / p, A.y / p); }

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 a.x == b.x ? a.y < b.y : a.x < b.x;
}

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; }
Vector Rotate(Vector A, double rad) {
    return Vector(A.x*cos(rad) - A.y*sin(rad), A.x*sin(rad) + A.y*cos(rad));
}

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 SegmentProperInsection(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;
}

Point P[N], v[N * N];

int main() {
    int n, Case = 0;
    while(scanf("%d", &n) == 1 && n ) {
        rep(i, 0, n - 1) {
            scanf("%lf %lf", &P[i].x, &P[i].y);
            v[i] = P[i];
        }
        n--;
        int cnt = n, tot = n;
        rep(i, 0, n - 1) rep(j, i + 1, n - 1)
            if(SegmentProperInsection(P[i], P[i + 1], P[j], P[j + 1]))
                v[cnt++] = GetLineIntersection(P[i], P[i + 1] - P[i], P[j], P[j + 1] - P[j]);
        sort(v, v + cnt);
        cnt = unique(v, v + cnt) - v;
        rep(i, 0, cnt - 1) rep(j, 0, n - 1)
            if(OnSegment(v[i], P[j], P[j + 1])) tot++;
        printf("Case %d: There are %d pieces.
", ++Case, tot + 2 - cnt);
    }
    return 0;
}
View Code

 

以上是关于LA 3263 (欧拉定理 + 判断线段相交 + 求线段交点)的主要内容,如果未能解决你的问题,请参考以下文章

LA 3263 好看的一笔画 欧拉几何+计算几何模板

BZOJ4869相逢是问候(线段树,欧拉定理)

[六省联考2017]相逢是问候(线段树+拓展欧拉定理)

P3747 相逢是问候 欧拉定理+线段树

POJ2284 That Nice Euler Circuit (欧拉公式)(计算几何 线段相交问题)

[BZOJ4869][六省联考2017]相逢是问候(线段树+扩展欧拉定理)