「计算几何模板」「CF498A」Crazy Town
Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, whereai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let’s call each such region a block. We define an intersection as the point where at least two different roads intersect.
Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step).
Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road.
The first line contains two space-separated integers x1, y1 ( - 106 ≤ x1, y1 ≤ 106) — the coordinates of your home.
The second line contains two integers separated by a space x2, y2 ( - 106 ≤ x2, y2 ≤ 106) — the coordinates of the university you are studying at.
The third line contains an integer n (1 ≤ n ≤ 300) — the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 ≤ ai, bi, ci ≤ 106; |ai| + |bi| > 0) — the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines).
Output the answer to the problem.
1 2 3 4 5 |
1 1 -1 -1 2 0 1 0 1 0 0 |
1 |
2 |
1 2 3 4 5 6 |
1 1 -1 -1 3 1 0 0 0 1 0 1 1 -3 |
1 |
2 |
Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors):
题解
求ab线段与多少直线相交,将(x1,y1)带入直线ax+by+c,得到与0的关系即能判断其在直线的某一侧,线段两端点异侧则与直线有交
当然可以用计算几何强行模拟
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
#include<cstdio> #include<cmath> #include<ctime> #include<cstring> #include<iostream> #include<algorithm> #include<queue> #include<set> #define ll long long #define mod 1000000007 #define inf 1000000000 using namespace std; int read() { int f=1,x=0;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } int n,ans; ll xs,ys,xt,yt; int main() { xs=read();ys=read(); xt=read();yt=read(); n=read(); for(int i=1;i<=n;i++) { ll a=read(),b=read(),c=read(); if(a*xs+b*ys>-c^a*xt+b*yt>-c)ans++; } printf("%d\n",ans); return 0; } |
计算几何模板
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
#include<set> #include<map> #include<ctime> #include<queue> #include<cmath> #include<cstdio> #include<vector> #include<cstring> #include<cstdlib> #include<iostream> #include<algorithm> #define eps 1e-8 #define inf 1000000000 #define pa pair<int,int> #define ll long long using namespace std; int read() { int x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } int n,ans; struct P{ double x,y; friend P operator +(P a,P b){ return (P){a.x+b.x,a.y+b.y}; } friend P operator -(P a,P b){ return (P){a.x-b.x,a.y-b.y}; } friend double operator*(P a,P b){ return a.x*b.y-a.y*b.x; } friend double operator/(P a,P b){ return a.x*b.x+a.y*b.y; } friend bool operator==(P a,P b){ return fabs(a.x-b.x)<eps&&fabs(a.y-b.y)<eps; } friend bool operator!=(P a,P b){ return !(a==b); } friend bool operator<(P a,P b){ if(fabs(a.y-b.y)<eps)return a.x<b.x; return a.y<b.y; } friend double dis2(P a){ return a.x*a.x+a.y*a.y; } friend void print(P a){ printf("%.5lf %.5lf\n",a.x,a.y); } friend void read(P &a){ scanf("%lf%lf",&a.x,&a.y); } }; struct L{ P a,b; friend bool inter(L a,L b){ if(((b.b-b.a)*(a.a-b.a)>0)^((b.b-b.a)*(a.b-b.a)>0)) return 1; return 0; } }l[305],L; int main() { read(L.a);read(L.b); n=read(); for(int i=1;i<=n;i++) { double a,b,c; scanf("%lf%lf%lf",&a,&b,&c); if(a==0) { l[i].a=(P){-100,-c/b}; l[i].b=(P){200,-c/b}; } else if(b==0) { l[i].a=(P){-c/a,-100}; l[i].b=(P){-c/a,200}; } else { l[i].a=(P){-100,(-c+a*100)/b}; l[i].b=(P){200,(-c-a*200)/b}; } } for(int i=1;i<=n;i++) if(inter(L,l[i]))ans++; printf("%d\n",ans); return 0; } |