题解系列010 | 洛谷题解 UVa12296 【Pieces and Disc】

原题传送门:Giga Tower

一、题意概述

【大意】一些线段把矩形分成了多个小多边形,随后给了一个圆,判断其与哪些块有交集(请注意:不一定相交),并求出这些块的面积。

【样例分析】

案例
其中标阴影部分的就是与圆有交集的块。

二、分析

显然,本题中点的坐标、以及圆和多边形相交问题,都可以采用解析几何来解决。为此,我们建立一个直角坐标系并计算各图形的方程。

唯一需要注意的便是最后判断相交的部分。由于是面与多边形相交,因此不能单单判断线段与圆的交点,有以下两种特例:

  1. 内含(无交点)

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-4DqE9Xrb-1627218457281)(https://cdn.luogu.com.cn/upload/image_hosting/yh9d4f1g.png)]

此时只需判断圆心是否在多边形内。

  1. 端点相交

2

此时只需判断线段中点是否在多边形内。

接下来是具体的实现部分:

三、代码

1.点和向量

const double eps = 1e-8;
double dcmp(double x) {
    if (fabs(x) < eps) return 0; else return x < 0 ? -1 : 1;
}

struct Point
{
    double x, y;
    Point(double x = 0, double y = 0) :x(x), y(y) { }
};

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);
}

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 Length2(Vector A) { return Dot(A, A); }

2.线段与多边形

typedef Point Vector;

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 OnSegment(Point p, Point a1, Point a2) {
    return dcmp(Cross(a1 - p, a2 - p)) == 0 && dcmp(Dot(a1 - p, a2 - p)) < 0;
}

double PolygonArea(Polygon poly) {
    double area = 0;
    int n = poly.size();
    for (int i = 1; i < n - 1; i++)
        area += Cross(poly[i] - poly[0], poly[(i + 1) % n] - poly[0]);
    return area / 2;
}

Polygon CutPolygon(Polygon poly, Point A, Point B) {
    Polygon newpoly;
    int n = poly.size();
    for (int i = 0; i < n; i++) {
        Point C = poly[i];
        Point D = poly[(i + 1) % n];
        if (dcmp(Cross(B - A, C - A)) >= 0) newpoly.push_back(C);
        if (dcmp(Cross(B - A, C - D)) != 0) {
            Point ip = GetLineIntersection(A, B - A, C, D - C);
            if (OnSegment(ip, C, D)) newpoly.push_back(ip);
        }
    }
    return newpoly;
}

int isPointInPolygon(Point p, Polygon v) {
    int wn = 0;
    int n = v.size();
    for (int i = 0; i < n; i++) {
        if (OnSegment(p, v[i], v[(i + 1) % n])) return -1;
        int k = dcmp(Cross(v[(i + 1) % n] - v[i], p - v[i]));
        int d1 = dcmp(v[i].y - p.y);
        int d2 = dcmp(v[(i + 1) % n].y - p.y);
        if (k > 0 && d1 <= 0 && d2 > 0) wn++;
        if (k < 0 && d2 <= 0 && d1 > 0) wn--;
    }
    if (wn != 0) return 1;
    return 0;
}

vector<Polygon> pieces, new_pieces;

void cut(int x1, int y1, int x2, int y2) {
    new_pieces.clear();
    for (int i = 0; i < pieces.size(); i++) {
        Polygon left = CutPolygon(pieces[i], Point(x1, y1), Point(x2, y2));
        Polygon right = CutPolygon(pieces[i], Point(x2, y2), Point(x1, y1));
        if (left.size() >= 3) new_pieces.push_back(left);
        if (right.size() >= 3) new_pieces.push_back(right);
    }
    pieces = new_pieces;
}

bool DiscIntersectPolygon(Polygon poly, Point p, double R) {
    if (isPointInPolygon(p, poly) != 0) return true;
    if (isInCircle(poly[0], p, R)) return true;
    int n = poly.size();
    for (int i = 0; i < n; i++) {
        if (CircleIntersectSegment(poly[i], poly[(i + 1) % n], p, R)) {
            return true;
        }
        if (isInCircle((poly[i] + poly[(i + 1) % n]) * 0.5, p, R)) {
            return true;
        }
    }
    return false;
}

3.圆

bool isInCircle(Point p, Point center, double R) {
    return dcmp(Length2(p - center) - R * R) < 0;
}


int getLineCircleIntersection(Point A, Point B, Point C, double r, double& t1, double& t2)
{

    double a = B.x - A.x;
    double b = A.x - C.x;
    double c = B.y - A.y;
    double d = A.y - C.y;

    double e = a * a + c * c;
    double f = 2 * (a * b + c * d);
    double g = b * b + d * d - r * r;
    double delta = f * f - 4 * e * g;
    if (dcmp(delta) < 0) return 0;
    if (dcmp(delta) == 0) {
        t1 = t2 = -f / (2 * e);
        return 1;
    }
    t1 = (-f - sqrt(delta)) / (2 * e);
    t2 = (-f + sqrt(delta)) / (2 * e);
    return 2;
}

bool CircleIntersectSegment(Point A, Point B, Point p, double R)
{
    double t1, t2;
    int c = getLineCircleIntersection(A, B, p, R, t1, t2);
    if (c <= 1) return false;
    if (dcmp(t1) > 0 && dcmp(t1 - 1) < 0) return true;
    if (dcmp(t2) > 0 && dcmp(t2 - 1) < 0) return true;
    return false;
}

4.完整代码

#include<cstdio>
#include<cmath>
#include<vector>
#include<algorithm>

using namespace std;

const double eps = 1e-8;
double dcmp(double x) {
    if (fabs(x) < eps) return 0; else return x < 0 ? -1 : 1;
}

struct Point
{
    double x, y;
    Point(double x = 0, double y = 0) :x(x), y(y) { }
};

typedef Point Vector;

typedef vector<Point> Polygon;

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);
}

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 Length2(Vector A) { return Dot(A, A); }

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 OnSegment(Point p, Point a1, Point a2) {
    return dcmp(Cross(a1 - p, a2 - p)) == 0 && dcmp(Dot(a1 - p, a2 - p)) < 0;
}

double PolygonArea(Polygon poly) {
    double area = 0;
    int n = poly.size();
    for (int i = 1; i < n - 1; i++)
        area += Cross(poly[i] - poly[0], poly[(i + 1) % n] - poly[0]);
    return area / 2;
}

Polygon CutPolygon(Polygon poly, Point A, Point B) {
    Polygon newpoly;
    int n = poly.size();
    for (int i = 0; i < n; i++) {
        Point C = poly[i];
        Point D = poly[(i + 1) % n];
        if (dcmp(Cross(B - A, C - A)) >= 0) newpoly.push_back(C);
        if (dcmp(Cross(B - A, C - D)) != 0) {
            Point ip = GetLineIntersection(A, B - A, C, D - C);
            if (OnSegment(ip, C, D)) newpoly.push_back(ip);
        }
    }
    return newpoly;
}

int isPointInPolygon(Point p, Polygon v) {
    int wn = 0;
    int n = v.size();
    for (int i = 0; i < n; i++) {
        if (OnSegment(p, v[i], v[(i + 1) % n])) return -1;
        int k = dcmp(Cross(v[(i + 1) % n] - v[i], p - v[i]));
        int d1 = dcmp(v[i].y - p.y);
        int d2 = dcmp(v[(i + 1) % n].y - p.y);
        if (k > 0 && d1 <= 0 && d2 > 0) wn++;
        if (k < 0 && d2 <= 0 && d1 > 0) wn--;
    }
    if (wn != 0) return 1;
    return 0;
}

bool isInCircle(Point p, Point center, double R) {
    return dcmp(Length2(p - center) - R * R) < 0;
}


int getLineCircleIntersection(Point A, Point B, Point C, double r, double& t1, double& t2)
{

    double a = B.x - A.x;
    double b = A.x - C.x;
    double c = B.y - A.y;
    double d = A.y - C.y;

    double e = a * a + c * c;
    double f = 2 * (a * b + c * d);
    double g = b * b + d * d - r * r;
    double delta = f * f - 4 * e * g;
    if (dcmp(delta) < 0) return 0; 
    if (dcmp(delta) == 0) {
        t1 = t2 = -f / (2 * e);
        return 1;
    }
    t1 = (-f - sqrt(delta)) / (2 * e);
    t2 = (-f + sqrt(delta)) / (2 * e);
    return 2;
}

bool CircleIntersectSegment(Point A, Point B, Point p, double R)
{
    double t1, t2;
    int c = getLineCircleIntersection(A, B, p, R, t1, t2);
    if (c <= 1) return false;
    if (dcmp(t1) > 0 && dcmp(t1 - 1) < 0) return true;
    if (dcmp(t2) > 0 && dcmp(t2 - 1) < 0) return true;
    return false;
}

vector<Polygon> pieces, new_pieces;

void cut(int x1, int y1, int x2, int y2) {
    new_pieces.clear();
    for (int i = 0; i < pieces.size(); i++) {
        Polygon left = CutPolygon(pieces[i], Point(x1, y1), Point(x2, y2));
        Polygon right = CutPolygon(pieces[i], Point(x2, y2), Point(x1, y1));
        if (left.size() >= 3) new_pieces.push_back(left);
        if (right.size() >= 3) new_pieces.push_back(right);
    }
    pieces = new_pieces;
}

bool DiscIntersectPolygon(Polygon poly, Point p, double R) {
    if (isPointInPolygon(p, poly) != 0) return true;
    if (isInCircle(poly[0], p, R)) return true;
    int n = poly.size();
    for (int i = 0; i < n; i++) {
        if (CircleIntersectSegment(poly[i], poly[(i + 1) % n], p, R)) {
            return true;
        }
        if (isInCircle((poly[i] + poly[(i + 1) % n]) * 0.5, p, R)) {
            return true;
        }
    }
    return false;
}

void query(Point p, int R) {
    vector<double> ans;
    for (int i = 0; i < pieces.size(); i++) {
        if (DiscIntersectPolygon(pieces[i], p, R)) {
            ans.push_back(fabs(PolygonArea(pieces[i])));
        }
    }
    printf("%d", ans.size());
    sort(ans.begin(), ans.end());
    for (int i = 0; i < ans.size(); i++)
        printf(" %.2lf", ans[i]);
    printf("\n");
}

int main() {
    int n, m, L, W;
    while (scanf("%d%d%d%d", &n, &m, &L, &W) == 4 && n) {
        pieces.clear();

        Polygon bbox;
        bbox.push_back(Point(0, 0));
        bbox.push_back(Point(L, 0));
        bbox.push_back(Point(L, W));
        bbox.push_back(Point(0, W));
        pieces.push_back(bbox);

        for (int i = 0; i < n; i++) {
            int x1, y1, x2, y2;
            scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
            cut(x1, y1, x2, y2);
        }

        for (int i = 0; i < m; i++) {
            int x, y, R;
            scanf("%d%d%d", &x, &y, &R);
            query(Point(x, y), R);
        }
        printf("\n");
    }
    return 0;
}

欢迎大家关注我的博客!
我的洛谷账号:这是我
我的洛谷团队:这是我的团队
欢迎大家关注我,并加入我的团队哦^ _ ^
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值