(2021-07-14~)“kuangbin带你飞”专题计划——专题十三:基础计算几何

本文汇总了解决一系列计算几何题目,涉及区间划分、点与线段、线段与线段相交、多边形内部点判断等,通过实例解析难度判断与解题技巧。

前言

参考博客

自己总结的东西:

  1. 2021-07-10-计算几何总结
  2. 旋转卡壳&凸包(不用一下子就学完所有)

难度判断?

  1. [kuangbin带你飞]专题十三 基础计算几何

题目

1.[TOYS POJ - 2318 ](解决)

TOYS POJ - 2318

  1. 题意:给定n+1个区间,m个点,问各区间各多少个点。
    • n+1个区间排在一行,划分一个大的矩形,每个区间的上下底都平行。具体如下:
      在这里插入图片描述
  2. 提示:二分
  3. 代码

2.[Toy Storage POJ - 2398 ](解决)

Toy Storage POJ - 2398

  1. 说明:和第1题一模一样,注意这题线没排序。
  2. 代码

3.[Segments POJ - 3304 ](解决)

Segments POJ - 3304

  1. 题意:给定n(<=100)条线段,每条线段给两个端点。问是否有一条直线能让所有线段都与它有交点?
  2. 提示如果存在这样的直线,那么这条直线一定可以通过这些线段的某两个端点(也可能是一条线段的两个端点)。即从结果出发
    • 从结果理解起来没那么难理解。
    • 至于具体如何,自己画一下。
  3. 判断直线与线段是否相交:返回1,2为相交
// kuangbin模板+备注
//直线和线段相交判断
//-*this line -v seg
// 2 规范相交:相交,且交点不在端点
// 1 非规范相交:在端点处相交
// 0 不相交
// Line:::(v为线段)
int linecrossseg(Line v) {
   
   
    int d1 = sgn((e−s) ^ (v.s−s));
    int d2 = sgn((e−s) ^ (v.e−s));
    if ((d1 ^ d2) ==2) return 2;  //-1^1=-2
    return (d1 == 0 || d2 == 0);
}
  1. 代码:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#define dbg(x) cout << #x << "===" << x << endl
using namespace std;
const int N = 2e2 + 10;
const double eps = 1e-8;

int sgn(double x) {
   
   
    if (fabs(x) < eps) return 0;
    return (x > 0) ? 1 : -1;
}
int n, m;
int a[N], b[N];
struct Point {
   
   
    double x, y;
    Point() {
   
   }
    Point(double _x, double _y) {
   
    x = _x, y = _y; }
    void input() {
   
    scanf("%lf%lf", &x, &y); }
    Point operator-(const Point &b) const {
   
    return Point(x - b.x, y - b.y); }
    double operator^(const Point &b) const {
   
    return x * b.y - y * b.x; }
    bool operator==(const Point &b) const {
   
   
        return (sgn(x - b.x) == 0) && (sgn(y - b.y) == 0);
    }
} p[N];
double cross(Point a, Point b, Point c) {
   
    return (b - a) ^ (c - a); }
struct Line {
   
   
    Point s, e;
    Line() {
   
   }
    Line(Point _s, Point _e) {
   
    s = _s, e = _e; }
};
//直线b,线段a
// kuangbin模板+备注
//直线和线段相交判断
//-*this line -v seg
// 2 规范相交:相交,且交点不在端点
// 1 非规范相交:在端点处相交
// 0 不相交
// Line:::(v为线段)
int linecrossseg(Line a, Line v) {
   
   
    int d1 = sgn((a.e - a.s) ^ (v.s - a.s));
    int d2 = sgn((a.e - a.s) ^ (v.e - a.s));
    if ((d1 ^ d2) == -2) return 2;  //-1^1=-2
    return (d1 == 0 || d2 == 0);
}
bool solve() {
   
   
    bool f = false;
    int f1, f2;
    for (int i = 1; i <= 2 * n; i++) {
   
   
        for (int j = i; j <= 2 * n; j++) {
   
   
            f = true;
            if (p[i] == p[j]) continue;
            for (int k = 1; k <= n; k++) {
   
   
                if (!linecrossseg(Line(p[i], p[j]),
                                  Line(p[2 * k - 1], p[2 * k])))
                    f = false;
            }
            if (f) return true;
        }
    }
    return false;
}
signed main() {
   
   
    int T;
    cin >> T;
    while (T--) {
   
   
        cin >> n;
        for (int i = 1; i <= 2 * n; i++) {
   
   
            p[i].input();
        }
        puts(solve() ? "Yes!" : "No!");
    }
    return 0;
}

4.[Intersecting Lines POJ - 1269 ](解决)

Intersecting Lines POJ - 1269

  1. 题意
    在这里插入图片描述
  2. 代码
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#define dbg(x) cout << #x << "===" << x << endl
using namespace std;
const double eps = 1e-8;
int sgn(double x) {
   
   
    if (fabs(x) < eps) return 0;
    if (x > 0)
        return 1;
    else
        return -1;
}
int n;
struct Point {
   
   
    double x, y;
    Point(double x = 0, double y = 0) : x(x), y(y) {
   
   }
    void input() {
   
    scanf("%lf%lf", &x, &y); }
    Point operator-(const Point &b) const {
   
    return Point(x - b.x, y - b.y); }
    Point operator+(const Point &b) const {
   
    return Point(x + b.x, y + b.y); }
    double operator^(const Point &b) const {
   
    return x * b.y - b.x * y; }
    Point operator*(double k) const {
   
    return Point(k * x, k * y); }
};
struct Line {
   
   
    Point s, e;
    Line() {
   
   }
    Line(Point s, Point e) {
   
    s = s, e = e; }
    void input() {
   
    s.input(), e.input(); }
    //判断两线段是否相交(不考虑相交于端点)
    bool sgecrossseg(Line b) {
   
   
        double c1 = (e - s) ^ (b.s - s), c2 = (e - s) ^ (b.e - s);
        double c3 = (b.e - b.s) ^ (s - b.s), c4 = (b.e - b.s) ^ 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值