码迷,mamicode.com
首页 > 其他好文 > 详细

uvalive 3218

时间:2015-07-30 21:31:40      阅读:111      评论:0      收藏:0      [点我收藏+]

标签:

题意:一条封闭折线将平面分成了若干个区域,按顺序给出折线各点的坐标,要求输出封闭折线的轮廓。
题解:用类似卷包裹的算法,先确定一个一定会被选中的点(x坐标最小,y坐标最小)作为起点,然后把可能是下一个极点(凸包顶点)的点都存起来,下一个极点有可能是当前点所在线段的前一个点和后一个点或当前点所在线段和其他线段的有交点的线段的起点和终点。
找出最右侧的点(用角度判断)和当前点的连线是否和其他线段有交点,如果有就找最近的交点当做答案的下一个点,如果没有最右侧的点就是下一个点。最后转回起点结束。

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
const double PI = acos(-1);
const double eps = 1e-9;
struct Point {
    double x, y;
    Point(double x = 0, double y = 0): x(x), y(y) {}
};
typedef Point Vector;
double dcmp(double x) {
    if (fabs(x) < eps)
        return 0;
    return x < 0 ? -1 : 1;
}
Vector operator + (const Point& A, const Point& B) {
    return Vector(A.x + B.x, A.y + B.y);
}
Vector operator - (const Point& A, const Point& B) {
    return Vector(A.x - B.x, A.y - B.y);
}
Vector operator * (const Point& A, double a) {
    return Vector(A.x * a, A.y * a);
}
Vector operator / (const Point& A, double a) {
    return Vector(A.x / a, A.y / a);
}
double Cross(const Vector& A, const Vector& B) {
    return A.x * B.y - A.y * B.x;
}
double Dot(const Vector& A, const Vector& B) {
    return A.x * B.x + A.y * B.y;
}
double Length(const Vector& A) {
    return sqrt(Dot(A, A));
}
bool operator < (const Point& A, const Point& B) {
    return A.x < B.x || (A.x == B.x && A.y < B.y);
}
bool operator == (const Point& A, const Point& B) {
    return A.x == B.x && A.y == B.y;
}
Point GetLineIntersection(Point P, Point v, Point Q, Point w) {
    Point u = P - Q;
    double t = Cross(w, u) / Cross(v, w);
    return P + v * t;
}
bool SegmentProperIntersection(const Point& a1, const Point& a2, const Point& b1, const Point& b2) {
    double c1 = Cross(a2 - a1, b1 - a1);
    double c2 = Cross(a2 - a1, b2 - a1);
    double c3 = Cross(b2 - b1, a1 - b1);
    double c4 = Cross(b2 - b1, a2 - b1);
    return dcmp(c1) * dcmp(c2) < 0 && dcmp(c3) * dcmp(c4) < 0;
}
bool OnSegment(const Point& p, const Point& a1, const Point& a2) {
    return dcmp(Cross(a1 - p, a2 - p)) == 0 && dcmp(Dot(a1 - p, a2 - p)) < 0;
}

const int N = 10005;
const int M = 205;
Point P[M], res[N], temp[M];
double ang[M], s;
int n, cnt;

void add(Point a, Point b) {
    temp[cnt] = a;
    ang[cnt] = atan2(temp[cnt].y - b.y, temp[cnt].x - b.x) - s;
    while (dcmp(ang[cnt]) <= 0)
        ang[cnt] += 2 * PI;
    cnt++;
}

int main() {
    while (scanf("%d", &n) == 1) {
        int minid = 0;
        for (int i = 0; i < n; i++) {
            scanf("%lf%lf", &P[i].x, &P[i].y);
            if (P[i] < P[minid])
                minid = i;
        }
        res[0] = P[minid];
        int num = 1;
        s = -PI;
        while (1) {
            cnt = 0;
            for (int i = 0; i < n; i++) {
                if (res[num - 1] == P[i]) {
                    add(P[(i + 1) % n], res[num - 1]);
                    add(P[(i + n - 1) % n], res[num - 1]);
                    break;
                }
            }
            for (int i = 0; i < n; i++) {
                if (OnSegment(res[num - 1], P[i], P[(i + 1) % n])) {
                    add(P[(i + 1) % n], res[num - 1]);
                    add(P[i], res[num - 1]);
                }
            }
            int id = 0;
            for (int i = 0; i < cnt; i++)
                if (ang[i] < ang[id])
                    id = i;
            double minlen = 1e9;
            Point RP = temp[id], its;
            for (int i = 0; i < n; i++) {
                if (SegmentProperIntersection(temp[id], res[num - 1], P[i], P[(i + 1) % n])) {
                    its = GetLineIntersection(temp[id], temp[id] - res[num - 1], P[i], P[i] - P[(i + 1) % n]);
                    if (Length(its - res[num - 1]) < minlen) {
                        minlen = Length(its - res[num - 1]);
                        RP = its;
                    }
                }
            }
            res[num] = RP;
            s = atan2(res[num - 1].y - res[num].y, res[num - 1].x - res[num].x);
            num++;
            if (res[num - 1] == res[0])
                break;
        }
        printf("%d\n", num - 1);
        for (int i = 0; i < num - 1; i++)
            printf("%.4lf %.4lf\n", res[i].x, res[i].y);
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

uvalive 3218

标签:

原文地址:http://blog.csdn.net/hyczms/article/details/47155427

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!