POJ 1584-A Round Peg in a Ground Hole(計算幾何-凸包、點到線段距離)
A Round Peg in a Ground Hole
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 6690 | Accepted: 2145 |
Description
The DIY Furniture company specializes in assemble-it-yourself furniture kits. Typically, the pieces of wood are attached to one another using a wooden peg that fits into pre-cut holes in each piece to be attached. The pegs have a circular cross-section and
so are intended to fit inside a round hole.
A recent factory run of computer desks were flawed when an automatic grinding machine was mis-programmed. The result is an irregularly shaped hole in one piece that, instead of the expected circular shape, is actually an irregular polygon. You need to figure out whether the desks need to be scrapped or if they can be salvaged by filling a part of the hole with a mixture of wood shavings and glue.
There are two concerns. First, if the hole contains any protrusions (i.e., if there exist any two interior points in the hole that, if connected by a line segment, that segment would cross one or more edges of the hole), then the filled-in-hole would not be structurally sound enough to support the peg under normal stress as the furniture is used. Second, assuming the hole is appropriately shaped, it must be big enough to allow insertion of the peg. Since the hole in this piece of wood must match up with a corresponding hole in other pieces, the precise location where the peg must fit is known.
Write a program to accept descriptions of pegs and polygonal holes and determine if the hole is ill-formed and, if not, whether the peg will fit at the desired location. Each hole is described as a polygon with vertices (x1, y1), (x2, y2), . . . , (xn, yn). The edges of the polygon are (xi, yi) to (xi+1, yi+1) for i = 1 . . . n − 1 and (xn, yn) to (x1, y1).
A recent factory run of computer desks were flawed when an automatic grinding machine was mis-programmed. The result is an irregularly shaped hole in one piece that, instead of the expected circular shape, is actually an irregular polygon. You need to figure out whether the desks need to be scrapped or if they can be salvaged by filling a part of the hole with a mixture of wood shavings and glue.
There are two concerns. First, if the hole contains any protrusions (i.e., if there exist any two interior points in the hole that, if connected by a line segment, that segment would cross one or more edges of the hole), then the filled-in-hole would not be structurally sound enough to support the peg under normal stress as the furniture is used. Second, assuming the hole is appropriately shaped, it must be big enough to allow insertion of the peg. Since the hole in this piece of wood must match up with a corresponding hole in other pieces, the precise location where the peg must fit is known.
Write a program to accept descriptions of pegs and polygonal holes and determine if the hole is ill-formed and, if not, whether the peg will fit at the desired location. Each hole is described as a polygon with vertices (x1, y1), (x2, y2), . . . , (xn, yn). The edges of the polygon are (xi, yi) to (xi+1, yi+1) for i = 1 . . . n − 1 and (xn, yn) to (x1, y1).
Input
Input consists of a series of piece descriptions. Each piece description consists of the following data:
Line 1 < nVertices > < pegRadius > < pegX > < pegY >
number of vertices in polygon, n (integer)
radius of peg (real)
X and Y position of peg (real)
n Lines < vertexX > < vertexY >
On a line for each vertex, listed in order, the X and Y position of vertex The end of input is indicated by a number of polygon vertices less than 3.
Line 1 < nVertices > < pegRadius > < pegX > < pegY >
number of vertices in polygon, n (integer)
radius of peg (real)
X and Y position of peg (real)
n Lines < vertexX > < vertexY >
On a line for each vertex, listed in order, the X and Y position of vertex The end of input is indicated by a number of polygon vertices less than 3.
Output
For each piece description, print a single line containing the string:
HOLE IS ILL-FORMED if the hole contains protrusions
PEG WILL FIT if the hole contains no protrusions and the peg fits in the hole at the indicated position
PEG WILL NOT FIT if the hole contains no protrusions but the peg will not fit in the hole at the indicated position
HOLE IS ILL-FORMED if the hole contains protrusions
PEG WILL FIT if the hole contains no protrusions and the peg fits in the hole at the indicated position
PEG WILL NOT FIT if the hole contains no protrusions but the peg will not fit in the hole at the indicated position
Sample Input
5 1.5 1.5 2.0 1.0 1.0 2.0 2.0 1.75 2.0 1.0 3.0 0.0 2.0 5 1.5 1.5 2.0 1.0 1.0 2.0 2.0 1.75 2.5 1.0 3.0 0.0 2.0 1
Sample Output
HOLE IS ILL-FORMED PEG WILL NOT FIT
Source
題目意思:
給桌子打孔,孔是不規則的多邊形,以座標形式按順時針給出這個多邊形N個頂點的集合;
圓形的釘子要插入孔中,給出釘子的圓心座標及其半徑。
如果孔是凸多邊形,判斷釘子是否完全在孔內,若是輸出PEG WILL FIT;否則輸出PEG WILL NOT FIT;
否則輸出HOLE IS ILL-FORMED。
解題思路:
亂七八糟的套了一把計算幾何的模板……大量測試資料在程式碼後面……
先根據點集判斷是否是凸包,然後列舉釘子圓心到凸包各條邊的距離,如果距離都小於半徑,則釘子能插入孔。
#include<iostream>
#include<cstdio>
#include<iomanip>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
const int INF=1e9;
const int MAXN=200;
const double eps = 1e-8;
const double PI = acos(-1.0);
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() {} Point(double _x,double _y)
{
x = _x;
y = _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; //點積
}
double operator *(const Point &b)const
{
return x*b.x + y*b.y; //繞原點旋轉角度B(弧度值),後x,y的變化
}
void transXY(double B)
{
double tx = x,ty = y;
x = tx*cos(B) - ty*sin(B);
y = tx*sin(B) + ty*cos(B);
}
};
struct Line
{
Point s,e;
Line() {} Line(Point _s,Point _e)
{
s = _s;
e = _e;
}
//兩直線相交求交點
//第一個值為0表示直線重合,為1表示平行,為0表示相交,為2是相交
//只有第一個值為2時,交點才有意義
pair<int,Point> operator &(const Line &b)const
{
Point res = s;
if(sgn((s-e)^(b.s-b.e)) == 0)
{
if(sgn((s-b.e)^(b.s-b.e)) == 0)
return make_pair(0,res);//重合
else return make_pair(1,res);//平行
}
double t = ((s-b.s)^(b.s-b.e))/((s-e)^(b.s-b.e));
res.x += (e.x-s.x)*t;
res.y += (e.y-s.y)*t;
return make_pair(2,res);
}
};
Point list[MAXN];
//判斷凸多邊形,允許共線邊,點可以是順時針給出也可以是逆時針給出,點的編號1~n-1
bool isconvex(Point poly[],int n)
{
bool s[3];
memset(s,false,sizeof(s));
for(int i = 0; i < n; i++)
{
s[sgn( (poly[(i+1)%n]-poly[i])^(poly[(i+2)%n]-poly[i]) )+1] = true;
if(s[0] && s[2])return false;
}
return true;
}
double Dot(Point A,Point B)//點積
{
return A.x*B.x+A.y*B.y;
}
double Length(Point A)
{
return sqrt(Dot(A,A));
}
double Cross(Point A,Point B)
{
return A.x*B.y-A.y*B.x;
}
double DistanceTosgnment(Point P,Point A,Point B)//點到直線距離
{
if(A.x==B.x&&A.y==B.y)
return Length(P-A);
if(sgn(Dot(B-A,P-A))<0)return Length(P-A);
else if(sgn(Dot(B-A,P-B))>0)return Length(P-B);
else return fabs(Cross(B-A,P-A))/Length(B-A);
}
//*判斷點線上段上
bool OnSeg(Point P,Line L)
{
return sgn((L.s-P)^(L.e-P)) == 0 &&
sgn((P.x - L.s.x) * (P.x - L.e.x)) <= 0 &&
sgn((P.y - L.s.y) * (P.y - L.e.y)) <= 0;
}
//*判斷線段相交
bool inter(Line l1,Line l2)
{
return max(l1.s.x,l1.e.x) >= min(l2.s.x,l2.e.x) &&
max(l2.s.x,l2.e.x) >= min(l1.s.x,l1.e.x) &&
max(l1.s.y,l1.e.y) >= min(l2.s.y,l2.e.y) &&
max(l2.s.y,l2.e.y) >= min(l1.s.y,l1.e.y) &&
sgn((l2.s-l1.e)^(l1.s-l1.e))*sgn((l2.e-l1.e)^(l1.s-l1.e)) <= 0 &&
sgn((l1.s-l2.e)^(l2.s-l2.e))*sgn((l1.e-l2.e)^(l2.s-l2.e)) <= 0;
}
int isPointInPolygon(Point p,Point poly[])//點是否在多邊形內部
//*判斷點在任意多邊形內。射線法,poly[]的頂點數要大於等於3,點的編號0~n-1
//-1:點在凸多邊形外
//0:點在凸多邊形邊界上
//1:點在凸多邊形內
{
int cnt;
Line ray,side;
cnt = 0;
ray.s = p;
ray.e.y = p.y;
ray.e.x = -100000000000.0;//-INF,注意取值防止越界
for(int i = 0; i < n; i++)
{
side.s = poly[i];
side.e = poly[(i+1)%n];
if(OnSeg(p,side))return 0;
//如果平行軸則不考慮
if(sgn(side.s.y - side.e.y) == 0) continue;
if(OnSeg(side.s,ray))
{
if(sgn(side.s.y - side.e.y) > 0)cnt++;
}
else if(OnSeg(side.e,ray))
{
if(sgn(side.e.y - side.s.y) > 0)cnt++;
}
else if(inter(ray,side)) cnt++;
}
if(cnt%2== 1)return 1;
else return -1;
}
int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("F:/cb/read.txt","r",stdin);
//freopen("F:/cb/out.txt","w",stdout);
#endif
ios::sync_with_stdio(false);
cin.tie(0);
while(cin>>n)
{
if(n<3) break;
double ra;
Point r;
cin>>ra>>r.x>>r.y;
for(int i=0; i<n; ++i)
cin>>list[i].x>>list[i].y;
if(isconvex(list,n))//是凸包
{
double dis;//點到直線距離
bool flag=true;
int t=isPointInPolygon(r,list);
if(t!=1)//圓心是否在多邊形內部
flag=false;
else//在內部
{
for(int i=0; i<n; ++i)
{
dis=DistanceTosgnment(r,list[i],list[(i+1)%n]);
if(dis<ra)//距離大於半徑,無法容納
{
flag=false;
break;
}
}
}
if(flag)cout<<"PEG WILL FIT"<<endl;
else cout<<"PEG WILL NOT FIT"<<endl;
}
else//不是凸包
cout<<"HOLE IS ILL-FORMED"<<endl;
}
return 0;
}
/*
5 1.5 1.5 2.0
1.0 1.0
2.0 2.0
1.75 2.0
1.0 3.0
0.0 2.0
5 1.5 1.5 2.0
1.0 1.0
2.0 2.0
1.75 2.5
1.0 3.0
0.0 2.0
1
*/
3 0.1 0.2 0.0
-0.5 1.0
0.5 -1.0
0.5 1.0
3 0.25 0.2 0.0
-0.5 1.0
0.5 -1.0
0.5 1.0
3 0.1 1.6 1.2
1.0 1.0
2.0 1.0
1.0 2.0
6 0.1 1.6 1.2
1.0 1.0
1.5 1.0
2.0 1.0
1.2 1.8
1.0 2.0
1.0 1.5
3 0.1 2.0 2.0
1.0 1.0
2.0 1.0
1.0 2.0
4 1.0 2.0 1.0
0.0 0.0
0.0 4.0
4.0 4.0
4.0 0.0
4 1.0 3.5 1.0
0.0 0.0
0.0 4.0
4.0 4.0
4.0 0.0
4 0.2 1.5 1.0
1.0 1.0
2.0 2.0
1.0 3.0
0.0 2.0
4 0.4 1.5 1.0
1.0 1.0
2.0 2.0
1.0 3.0
0.0 2.0
5 0.2 1.5 2.5
1.0 1.0
2.0 2.0
1.75 2.75
1.0 3.0
0.0 2.0
5 0.2 1.5 2.5
1.0 1.0
2.0 2.0
1.75 2.5
1.0 3.0
0.0 2.0
9 0.2 0.5 2.5
0.0 0.0
1.0 0.0
1.0 1.0
2.0 1.0
2.0 0.0
3.0 0.0
3.0 5.0
1.5 5.0
0.0 5.0
9 0.2 0.5 2.5
0.0 0.0
1.0 0.0
1.0 -1.0
2.0 -1.0
2.0 0.0
3.0 0.0
3.0 5.0
1.5 5.0
0.0 5.0
7 0.2 0.5 2.5
0.0 0.0
1.0 0.0
2.0 0.0
3.0 0.0
3.0 5.0
1.5 5.0
0.0 5.0
1
執行結果:
PEG WILL FIT
PEG WILL NOT FIT
PEG WILL FIT
PEG WILL FIT
PEG WILL NOT FIT
PEG WILL FIT
PEG WILL NOT FIT
PEG WILL NOT FIT
PEG WILL NOT FIT
PEG WILL FIT
PEG WILL NOT FIT
HOLE IS ILL-FORMED
HOLE IS ILL-FORMED
PEG WILL FIT
相關文章
- POJ 2991 Crane(線段樹+計算幾何)
- POJ 1039-Pipe(計算幾何-線段相交、求交點)
- POJ 1127-Jack Straws(計算幾何 線段相交)
- 【計算幾何】線段相交
- 計算幾何 —— 二維幾何基礎 —— 距離度量方法
- 視覺化學習:利用向量計算點到線段的距離並展示視覺化
- 【計算幾何】求線段相交交點座標
- 計算幾何(一):凸包問題(Convex Hull)
- POJ - 1556 【計算幾何 + 最短路】
- An Easy Problem?! POJ 2826 計算幾何
- HDU 4643 GSM(計算幾何求線段的中垂線)
- UVA 10652 Board Wrapping(計算幾何基礎,求凸包)APP
- 【計算幾何】點定位(線段,三角形,多邊形)
- POJ 1556 The Doors(Dijkstra+計算幾何)
- 【凸包 Graham法 點集排序】poj 1113 Wall排序
- 計算幾何——平面最近點對
- 空間距離計算
- POJ 1113 Wall(思維 計算幾何 數學)
- Codeforces Gym 100286A. Aerodynamics 計算幾何 求二維凸包面積
- 計算幾何
- 28、(向量)歐幾里得距離計算
- RecyclerView滑動距離計算View
- 計算字串編輯距離字串
- 計算地圖中兩點之間的距離地圖
- 微信小程式——計算2點之間的距離微信小程式
- 【計算幾何】多邊形點集排序排序
- 計算幾何:模板
- 計算幾何模板
- PostgreSQL遺傳學應用-矩陣相似距離計算(歐式距離,…XX距離)SQL矩陣
- 百度地圖計算兩座標點之間距離計算地圖
- POJ 1408-Fishnet(計算幾何-根據交點求多邊形面積)
- 【計算幾何】點在多邊形內部
- Vijos1680距離/openjudge2988計算字串的距離[DP]字串
- C語言:使用函式計算兩點間的距離C語言函式
- sql 計算兩個經緯度點之間的距離SQL
- [筆記] 計算幾何筆記
- 三維幾何生成:多段線、圓弧
- PyTorch 實戰:計算 Wasserstein 距離PyTorch