算法复习——计算几何基础(zoj1081)
題目:
Statement of the Problem
Several drawing applications allow us to draw polygons and almost all of them allow us to fill them with some color. The task of filling a polygon reduces to knowing which points are inside it, so programmers have to colour only those points.
You're expected to write a program which tells us if a given point lies inside a given polygon described by the coordinates of its vertices. You can assume that if a point is in the border of the polygon, then it is in fact inside the polygon.
Input Format
The input file may contain several instances of the problem. Each instance consists of: (i) one line containing integers N, 0 < N < 100 and M, respectively the number of vertices of the polygon and the number of points to be tested. (ii) N lines, each containing a pair of integers describing the coordinates of the polygon's vertices; (iii) M lines, each containing a pair of integer coordinates of the points which will be tested for "withinness" in the polygon.
You may assume that: the vertices are all distinct; consecutive vertices in the input are adjacent in the polygon; the last vertex is adjacent to the first one; and the resulting polygon is simple, that is, every vertex is incident with exactly two edges and two edges only intersect at their common endpoint. The last instance is followed by a line with a 0 (zero).
Output Format
For the ith instance in the input, you have to write one line in the output with the phrase "Problem i:", followed by several lines, one for each point tested, in the order they appear in the input. Each of these lines should read "Within" or "Outside", depending on the outcome of the test. The output of two consecutive instances should be separated by a blank line.
Sample Input
3 1
0 0
0 5
5 0
10 2
3 2
4 4
3 1
1 2
1 3
2 2
0
Sample Output
Problem 1:
Outside
Problem 2:
Outside
Within
題解:
一道很基礎(chǔ)的計(jì)算幾何題···判斷點(diǎn)是否在一個(gè)多邊形內(nèi)··方法是過該點(diǎn)做一條平行的射線看與多邊形的交點(diǎn)是否為奇數(shù)個(gè)···開始的時(shí)候可以先判斷下共線來優(yōu)化一下···
代碼:
#include<iostream> #include<cstdio> #include<cstdlib> #include<cmath> #include<ctime> #include<cctype> #include<cstring> #include<string> #include<algorithm> using namespace std; int R() {int i=1,f=0;char c;for(c=getchar();(c<'0'||c>'9')&&c!='-';c=getchar());if(c=='-'){i=-1;c=getchar();}for(;c>='0'&&c<='9';c=getchar())f=(f<<3)+(f<<1)+c-'0';return i*f; } const int N=105; struct point {int x;int y; }q[N],p; int n,m,T;inline point operator - (point a,point b) {point t;t.x=a.x-b.x;t.y=a.y-b.y;return t; }inline int operator * (point a,point b) {return a.x*b.y-a.y*b.x; }inline int dot(point a,point b) {return a.x*b.x+a.y*b.y; }inline void pre() {q[n]=q[0];int temp;for(int i=0;i<n;i++)temp+=q[i]*q[i+1];if(temp<=0){reverse(q,q+n);q[n]=q[0];} }inline bool check(point a,point b,point c) {point temp1=a-c;point temp2=b-c;if(temp1*temp2!=0) return false;else{if(dot(temp1,temp2)<=0) return true;else return false;} }inline bool jud() {int cnt=0;for(int i=0;i<n;i++){if(check(q[i],q[i+1],p)) return true;int t1=q[i].y-p.y;int t2=q[i+1].y-p.y;point temp1=q[i]-p;point temp2=q[i+1]-p;if((temp1*temp2>=0&&t1>=0&&t2<0)||(temp1*temp2<=0&&t1<0&&t2>=0))cnt++;}if(cnt%2!=0) return true;else return false; } int main() {//freopen("a.in","r",stdin);while(true){n=R();if(n==0) break;T++;if(T!=1) cout<<endl;cout<<"Problem "<<T<<":"<<endl;m=R();for(int i=0;i<n;i++)q[i].x=R(),q[i].y=R();pre();for(int i=1;i<=m;i++){p.x=R();p.y=R();if(jud()) cout<<"Within"<<endl;else cout<<"Outside"<<endl;}}return 0; }?
轉(zhuǎn)載于:https://www.cnblogs.com/AseanA/p/6662189.html
總結(jié)
以上是生活随笔為你收集整理的算法复习——计算几何基础(zoj1081)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 分期买手机划算吗?分期买手机首付多少?
- 下一篇: getClass()和.class的区别