SGU 124 Broken line(计算几何)

Description:

There is a closed broken line on a plane with sides parallel to coordinate axes, without self-crossings and self-contacts. The broken line consists of K segments. You have to determine, whether a given point with coordinates (X0,Y0) is inside this closed broken line, outside or belongs to the broken line.

Input:

The first line contains integer K (4 Ј K Ј 10000) - the number of broken line segments. Each of the following N lines contains coordinates of the beginning and end points of the segments (4 integer xi1,yi1,xi2,yi2; all numbers in a range from -10000 up to 10000 inclusive). Number separate by a space. The segments are given in random order. Last line contains 2 integers X0 and Y0 - the coordinates of the given point delimited by a space. (Numbers X0, Y0 in a range from -10000 up to 10000 inclusive).

Output:

The first line should contain:

INSIDE - if the point is inside closed broken line,

OUTSIDE - if the point is outside,

BORDER - if the point belongs to broken line.

Sample Input:

4
0 0 0 3
3 3 3 0
0 3 3 3
3 0 0 0
2 2

Sample Output:

INSIDE

题目链接

题意为判断一点与多边形的关系(在多边形内部、外部或者边界)

首先判断点是否在多边形边界上(这很容易),之后再用射线法进行判断

射线法为从判断点引一条水平射线,判断与多边形的边界交点数量,若为偶数则在多边形外部,若为奇数则在多边形内部

其中又有一些特殊情况,首先跳过多边形与 <math> <semantics> <mrow> <mi> x </mi> </mrow> <annotation encoding="application&#47;x&#45;tex"> x </annotation> </semantics> </math>x 轴平行的边界线,之后对于射线与多边形顶点相交的情况其在判断与多边形边界线段是否相交时按照一定规则只在其中一顶点相交时才判断相交,在另一顶点相交时不判断相交

可根据下图理解射线法的原理(红***域为多边形,两条射线为 <math> <semantics> <mrow> <mi> S </mi> <mi> T </mi> <mo separator="true"> , </mo> <mi> U </mi> <mi> V </mi> </mrow> <annotation encoding="application&#47;x&#45;tex"> ST, UV </annotation> </semantics> </math>ST,UV

AC代码:

#include <bits/stdc++.h>
using namespace std;
typedef double db;
const db inf = 1e20;
const int maxn = 1e4 + 5;
const db eps = 1e-9;

int Sgn(db Key) {return fabs(Key) < eps ? 0 : (Key < 0 ? -1 : 1);}
int Cmp(db Key1, db Key2) {return Sgn(Key1 - Key2);}
struct Point {db X, Y;};
typedef Point Vector;
Vector operator - (Vector Key1, Vector Key2) {return (Vector){Key1.X - Key2.X, Key1.Y - Key2.Y};}
Vector operator + (Vector Key1, Vector Key2) {return (Vector){Key1.X + Key2.X, Key1.Y + Key2.Y};}
db operator * (Vector Key1, Vector Key2) {return Key1.X * Key2.X + Key1.Y * Key2.Y;}
db operator ^ (Vector Key1, Vector Key2) {return Key1.X * Key2.Y - Key1.Y * Key2.X;}
struct Line {Point S, T;};
typedef Line Segment;
typedef Line Ray;
bool IsPointOnSeg(Point Key1, Segment Key2) {
    return Sgn((Key1 - Key2.S) ^ (Key2.T - Key2.S)) == 0 && Sgn((Key1 - Key2.S) * (Key1 - Key2.T)) <= 0;
}
bool IsSegInterSeg(Segment Key1, Segment Key2) {
    return
        max(Key1.S.X, Key1.T.X) >= min(Key2.S.X, Key2.T.X) &&
        max(Key2.S.X, Key2.T.X) >= min(Key1.S.X, Key1.T.X) &&
        max(Key1.S.Y, Key1.T.Y) >= min(Key2.S.Y, Key2.T.Y) &&
        max(Key2.S.Y, Key2.T.Y) >= min(Key1.S.Y, Key1.T.Y) &&
        Sgn((Key2.S - Key1.T) ^ (Key1.S - Key1.T)) * Sgn((Key2.T - Key1.T) ^ (Key1.S - Key1.T)) <= 0 &&
        Sgn((Key1.S - Key2.T) ^ (Key2.S - Key2.T)) * Sgn((Key1.T - Key2.T) ^ (Key2.S - Key2.T)) <= 0;
}

int N;
Segment Segs[maxn];
Point Dot;
Ray Judge;

bool IsPointOnPolygon() {
    for (int i = 1; i <= N; ++i)
        if (IsPointOnSeg(Dot, Segs[i])) return true;
    return false;
}

bool IsPointInPolygon() {
    int Cnt = 0;
    for (int i = 1; i <= N; ++i) {
        if (Cmp(Segs[i].S.Y, Segs[i].T.Y) == 0) continue;
        if (IsSegInterSeg(Judge, Segs[i]) && Cmp(Segs[i].T.Y, Dot.Y)) {
            Cnt++;
        }
    }
    return Cnt & 1;
}

int main(int argc, char *argv[]) {
    scanf("%d", &N);
    for (int i = 1; i <= N; ++i) {
        scanf("%lf%lf%lf%lf", &Segs[i].S.X, &Segs[i].S.Y, &Segs[i].T.X, &Segs[i].T.Y);
        if (Cmp(Segs[i].S.Y, Segs[i].T.Y) > 0) swap(Segs[i].S, Segs[i].T);
    }
    scanf("%lf%lf", &Dot.X, &Dot.Y);
    Judge = (Ray){Dot, (Point){inf, Dot.Y}};
    if (IsPointOnPolygon()) printf("BORDER\n");
    else if (IsPointInPolygon()) printf("INSIDE\n");
    else printf("OUTSIDE\n");
    return 0;
}
全部评论

相关推荐

咦哟,从去年八月份开始长跑,两处实习转正都失败了,风雨飘摇,终于拿到offer了更新一下面试记录:秋招:多部门反复面试然后挂掉然后复活,具体问了啥已经忘了,只是被反复煎炸,直至焦香😋春招:base北京抖音hr打来电话说再次复活,准备面试,gogogo北京抖音一面:六道笔试题:1.promise顺序2.定义域问题3.flat展开4.并发请求5.岛屿数量算法(力扣)深度,广度都写6.忘记了,好像也是算法,难度中等其他问题多是框架底层设计,实习项目重难点~~~秒过😇北京抖音二面:三道笔试题:(为什么只有三道是因为第三道没做出来,卡住了)1.中等难度算法(忘记啥题了,应该是个数组的)2.认识js的继承本质(手写继承模式,深入js的面相对象开发)3.手写vue的响应式(卡在了watch,导致挂掉)---后知后觉是我的注册副作用函数写得有问题,有点紧张了其他题目多是项目拷打,项目亮点,对实习项目的贡献~~~第二天,挂,but立马复活转战深圳客服当天约面深圳客服一面:六道笔试题,由于面过太多次字节,面试官叫我直接写,不用讲,快些写完😋,具体都是些继承,深拷贝(注意对数组对象分开处理,深层次对象,循环引用),加中等难度算法题~~~秒过深圳客服二面:口诉八股大战:大概囊括网络,浏览器渲染原理,动画优化,时间循环,任务队列等等(你能想到的简单八股通通拉出来鞭尸😋)算法题:笔试题6道:1:找出数组内重复的数,arr[0]-arr[n]内的数大小为[1-n],例如[1,2,2,3,3]返回[2,3],要求o(n),且不使用任何额外空间(做到了o(n),空间方面欠佳,给面试官说进入下一题,做不来了)2:原滋原味的继承(所以继承真滴很重要)3:力扣股票购买时机难度中等其他滴也忘记了,因为拿到offer后鼠鼠一下子就落地了,脑子自动过滤掉可能会攻击鼠鼠的记忆😷~~~秒过深圳客服三面:项目大战参与战斗的人员有:成员1:表单封装及其底层原理,使用成本的优化,声明式表单成员2:公司内部库生命周期管理成员3:第三方库和内部库冲突如何源码断点调试并打补丁解决成员4:埋点的艺术成员5:线上项目捷报频传如何查出内鬼成员6:大文件分片的风流趣事成员7:设计模式对对碰成员8:我构建hooks应对经理的新增的小需求的故事可能项目回答的比较流利,笔试题3道,都很简单,相信大家应该都可以手拿把掐😇~~~过过过无hr面后续煎熬等待几天直接hr打电话发offer了,希望大家也可以拿到自己心仪的offer
法力无边年:牛哇,你真是准备得充分,我对你没有嫉妒,都是实打实付出
查看19道真题和解析
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务