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
題目連結
題意為判斷一點與多邊形的關係(在多邊形內部、外部或者邊界)
首先判斷點是否在多邊形邊界上(這很容易),之後再用射線法進行判斷
射線法為從判斷點引一條水平射線,判斷與多邊形的邊界交點數量,若為偶數則在多邊形外部,若為奇數則在多邊形內部
其中又有一些特殊情況,首先跳過多邊形與 軸平行的邊界線,之後對於射線與多邊形頂點相交的情況其在判斷與多邊形邊界線段是否相交時按照一定規則只在其中一頂點相交時才判斷相交,在另一頂點相交時不判斷相交
可根據下圖理解射線法的原理(紅色區域為多邊形,兩條射線為 )
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;
}
相關文章
- SGU 120 SGU 228 Archipelago(計算幾何)Go
- SGU 110 Dungeon(立體幾何)
- 計算幾何
- 計算幾何:模板
- 計算幾何模板
- Something about 計算幾何
- [筆記] 計算幾何筆記
- 計算幾何 —— 二維幾何基礎 —— 距離度量方法
- 計算幾何——平面最近點對
- POJ - 1556 【計算幾何 + 最短路】
- 【學習筆記】計算幾何筆記
- 邊緣計算、霧計算、雲端計算區別幾何?
- BNUOJ 12887 isumi(計算幾何+最大流)
- 計算幾何(一):凸包問題(Convex Hull)
- POJ 1113 Wall(思維 計算幾何 數學)
- HNOI2016礦區(計算幾何+對偶圖)
- 計算機視覺—圖片幾何變換(2)計算機視覺
- CodeForces 887 E. Little Brother(計算幾何+二分)
- [計算幾何]圓與三角形是否相交
- SGU 208 Toral Tickets(polay計數)
- 百度造車,勝算幾何?
- SGU 505
- [幾何]計算不規則多邊形的面積、中心、重心
- 【第一道計算幾何題】 UVA11178 Morley‘s Theorem (二維幾何,旋轉直線求求交點)REM
- 丘成桐演講全文:幾何與計算數學的關係
- 【IDL】幾何圖形數學運算函式函式
- 平面幾何
- 神奇的周幾計算器
- oracle ENABLE=BROKENOracle
- sql server 計算兩個時間 相差的 幾天幾時幾分幾秒SQLServer
- 《戴森球計劃》火了,“科幻遊戲”前途幾何?遊戲
- 恆訊科技分析:使用邊緣計算和雲端計算各有何優勢?
- 計算檔案Checksum的幾種方法
- 阿里美團戰事再升級:支付寶入局 勝算幾何?阿里
- 邊緣計算為何會如此受歡迎?
- 說下line-height三種賦值方式有何區別?賦值
- [計算機網路] - 從英雄聯盟,看資料包何去何從?計算機網路
- 在量子計算時代,企業資料的加密該何去何從?加密