How I Mathematician Wonder What You Are! POJ - 3130(半平面交,多边形内核)

How I Mathematician Wonder What You Are!

POJ - 3130

题意:判断多边形是否有内核

思路:半平面交题,逆时针存入

 1 //
 2 // Created by HJYL on 2020/2/6.
 3 //
 4 #include<iostream>
 5 #include<stdio.h>
 6 #include<algorithm>
 7 #include<math.h>
 8 using namespace std;
 9 typedef long long ll;
10 typedef double db;
11 const int N=207;
12 const db eps=1e-7;
13 int sign(db k){if(k>eps) return 1;if(k<-eps) return -1; return 0;}
14 int dcmp(db k1,db k2){return sign(k1-k2);}
15 struct point{
16     db x,y;
17     point operator - (const point &k)const{
18         return (point){x-k.x,y-k.y};
19     }
20     point operator + (const point &k)const{
21         return (point){x+k.x,y+k.y};
22     }
23     point operator * (const db &k)const{
24         return (point){x*k,y*k};
25     }
26     point operator / (const db &k)const{
27         return (point){x/k,y/k};
28     }
29     db angle(){return atan2(y,x);}
30     void print(){printf("(%f,%f)\n",x,y);}
31 }P[N];
32 db cross(point k1,point k2){
33     return k1.x*k2.y-k1.y*k2.x;
34 }
35 db dot(point k1,point k2){
36     return k1.x*k2.x+k1.y*k2.y;
37 }
38 struct line{
39     point s,e;
40     db angle(){return (e-s).angle();}
41 }L[N],dq[N];
42 point getLL(line k1,line k2){
43     db w1=cross(k1.s-k2.s,k2.e-k2.s),w2=cross(k2.e-k2.s,k1.e-k2.s);
44     return (k1.s*w2+k1.e*w1)/(w1+w2);
45 }
46 bool onRight(line k,point p){
47     return sign((cross(p-k.s,k.e-k.s)))>0;
48 }
49 bool cmp(line k1,line k2){
50     if(dcmp(k1.angle(),k2.angle())==0) return onRight(k2,k1.s);
51     return k1.angle()<k2.angle();
52 }
53 int tot;
54 bool halfplaneinsert(){
55     sort(L+1,L+1+tot,cmp);
56     int cnt=0;
57     for(int i=1;i<=tot;i++){
58         if(i<tot&&dcmp(L[i].angle(),L[i+1].angle())==0) continue;
59         L[++cnt]=L[i];
60     }
61     int tail=-1,head=0;
62     for(int i=1;i<=cnt;i++){
63         while(tail-head>=1&&onRight(L[i],getLL(dq[tail],dq[tail-1]))) tail--;
64         while(tail-head>=1&&onRight(L[i],getLL(dq[head],dq[head+1]))) head++;
65         dq[++tail]=L[i];
66     }
67     while(tail-head>=1&&onRight(dq[head],getLL(dq[tail],dq[tail-1]))) tail--;
68     while(tail-head>=1&&onRight(dq[tail],getLL(dq[head],dq[head+1]))) head++;
69     return tail-head>=2;
70 }
71 int main()
72 {
73     int n;
74     while(~scanf("%d",&n)&&n) {
75             for (int i = 1; i <= n; i++) {
76                 scanf("%lf%lf", &P[i].x, &P[i].y);
77             }
78             tot = 0;
79             for (int i = 1; i < n; i++) {
80                 L[++tot] = (line) {P[i], P[i + 1]};
81             }
82             L[++tot] = (line) {P[n], P[1]};
83             if (halfplaneinsert()) printf("1\n");
84             else printf("0\n");
85         }
86 }

原文地址:https://www.cnblogs.com/Vampire6/p/12274698.html

时间: 2024-10-03 22:26:20

How I Mathematician Wonder What You Are! POJ - 3130(半平面交,多边形内核)的相关文章

poj 3384 半平面交

Feng Shui Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 5183   Accepted: 1548   Special Judge Description Feng shui is the ancient Chinese practice of placement and arrangement of space to achieve harmony with the environment. George h

POJ 2451 Uyuw&#39;s Concert(半平面交nlgn)

//#pragma comment(linker, "/STACK:16777216") //for c++ Compiler #include <stdio.h> #include <iostream> #include <cstring> #include <cmath> #include <stack> #include <queue> #include <vector> #include <

POJ 1474 Video Surveillance 半平面交

和POJ 3130,POJ 3335一样.求多边形的核 #include <iostream> #include <cstdio> #include <cmath> #define eps 1e-18 using namespace std; const int MAXN = 105; double a, b, c; int n, cnt; struct Point { double x, y; }point[MAXN], p[MAXN], tp[MAXN]; void

POJ 3130 How I Mathematician Wonder What You Are! 半平面交

和POJ3335一样,只不过这题是逆时针 #include <iostream> #include <cstdio> #include <cmath> #define eps 1e-18 using namespace std; const int MAXN = 105; double a, b, c; int n, cnt; struct Point { double x, y; }point[MAXN], p[MAXN], tp[MAXN]; void Get_eq

POJ 3525 二分+半平面交

Most Distant Point from the Sea Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 3812   Accepted: 1779   Special Judge Description The main land of Japan called Honshu is an island surrounded by the sea. In such an island, it is natural t

BZOJ 3800 Saber VS Lancer/POJ 1755 Triathlon 半平面交

题目大意:有n个人进行铁人三项比赛,对于这三种运动,每个人都有一个固定的速度,但是每种运动的长度是多少并不知道.现在问裁判可不可以通过调整这三项运动的长度来使某一个人赢得比赛. 思路:考虑现在我们想让一个人赢的时候,这个人的三个速度分别为v1,v2,v3,想让所有人都输给他,设某一个人的三个速度是v1',v2',v3'.设三项的比赛的长度为l1,l2,l3.那么不难得到如下方程:l1 / v1 + l2 / v2 + l3 / v3 < l1 / v1' + l2 / v2' + l3 / v3

POJ 2540 Hotter Colder --半平面交

题意: 一个(0,0)到(10,10)的矩形,目标点不定,从(0,0)开始走,如果走到新一点是"Hotter",那么意思是离目标点近了,如果是"Colder“,那么就是远了,"Same"是相同.要你推测目标点的可能位置的面积. 解法:半平面交水题.从一个点到另一个点远了,说明目标点在两点之间连线的中垂线的离源点较近的一侧,即我们每次都可以得到一条直线来切割平面,要么切割左侧,要么切割右侧,要么都切,再求一个半平面交就可以得出可能面积了. 代码: #incl

POJ 3525 Most Distant Point from the Sea 二分+半平面交

题目大意:给出一个岛的海岸线的轮廓,求这个岛上的所有点到海岸的最长距离是多少. 思路:求多边形内切圆的问题要用二分+半平面交解决.二分半径的长度,然后将所有的边向左侧移动这个二分的长度,然后利用半平面交来判断是否能够满足条件.如果满足条件就提高下界,否则减小上界. 我的移动的方法是这样的,首先每条边都要用点向量式来表示,就是边上任意一点和这条边的方向向量.这样做以后的操作会方便很多.然后将每个直线的向左的法向量求出来(比如l的向量是v(x,y),那么它向左侧的法向量是(-y,x)),然后将法向量

POJ 3335 半平面交求多边形的核

Rotating Scoreboard Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 4899   Accepted: 1946 Description This year, ACM/ICPC World finals will be held in a hall in form of a simple polygon. The coaches and spectators are seated along the ed