# POJ2318 叉积判断点与直线的关系

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <cstring>

#define maxn 5000+10

typedef struct Point
{
int x,y;
}point;

typedef struct Line
{
point a,b;
}Line;

int ans[maxn];
int n;
Line line[maxn];

using namespace std;

int cross(point p0, point p1, point p2) //p0,p1是线段，p2是待判断的点
{
return (p2.x - p0.x)*(p1.y - p0.y)-(p1.x - p0.x)*(p2.y - p0.y);
}

void search(point toy)   //搜索并利用叉积进行判断
{
if(cross(line[0].b,line[0].a,toy)<0)
ans[0]++;
else if(cross(line[n-1].b,line[n-1].a,toy)>0)
ans[n]++;
else
{
for(int i=0;i<n;i++)
if(cross(line[i].b,line[i].a,toy)>0&&cross(line[i+1].b,line[i+1].a,toy)<0)
ans[i+1]++;
}
}

int main()
{
point toy;
int m,x1,y1,x2,y2;
int u,l;
int xj,yj;
while(scanf("%d",&n) && n!=0)
{
scanf("%d%d%d%d%d",&m,&x1,&y1,&x2,&y2);
memset(ans,0,sizeof(ans));
for(int i=0;i<n;i++)
{
scanf("%d%d",&u,&l);
line[i].a.x=u;
line[i].a.y=y1;
line[i].b.x=l;
line[i].b.y=y2;
}

for(int i=0;i<m;i++)
{
scanf("%d%d",&xj,&yj);
point toy;
toy.x=xj;
toy.y=yj;
search(toy);
}

for(int i=0;i<=n;i++)
printf("%d: %d\n",i,ans[i]);
cout<<endl;
}
} 

#include <iostream>
#include <cstdio>
#include <cstring>

#define maxn 5000+10

struct point
{
int x,y;
};

struct Line
{
point a,b;
};

int n;
int ans[maxn];
int cnt[maxn];
Line line[maxn];
using namespace std;

int cross(point p0,point p1,point p2)
{
return  (p0.x-p2.x)*(p1.y-p2.y)-(p1.x-p2.x)*(p0.y-p2.y);
}

void search(point toy)
{
for(int i=0;i<=n;i++)
{
if(cross(toy,line[i].a,line[i].b)>0 && cross(toy,line[i+1].a,line[i+1].b)<0)
ans[i]++;
}
}

int main()
{
int m,x1,y1,x2,y2;
int u,l,xj,yj;
point toy;
while(scanf("%d",&n) && n!=0)
{
memset(ans,0,sizeof(ans));
memset(cnt,0,sizeof(cnt));
scanf("%d%d%d%d%d",&m,&x1,&y1,&x2,&y2);
line[0].a.x=x1; line[0].a.y=y1;   //将矩形的两高视为隔板
line[0].b.x=x1;line[0].b.y=y2;
line[n+1].a.x=x2; line[n+1].a.y=y1;
line[n+1].b.x=x2; line[n+1].b.y=y2;

for(int i=1;i<=n;i++)
{
scanf("%d%d",&u,&l);
line[i].a.x=u;
line[i].a.y=y1;
line[i].b.x=l;
line[i].b.y=y2;
}

for(int i=0;i<m;i++)
{
scanf("%d%d",&xj,&yj);
toy.x=xj;
toy.y=yj;
search(toy);
}
for(int i=0;i<=n;i++)
printf("%d: %d\n",i,ans[i]);
cout<<endl;
}
}

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <cstring>

#define maxn 5000+10

typedef struct Point
{
int x,y;
}point;

typedef struct Line
{
point a,b;
}Line;

int ans[maxn];
int n;
Line line[maxn];

using namespace std;

int cross(point p0, point p1, point p2) //p0,p1是线段，p2是待判断的点
{
return (p2.x - p0.x)*(p1.y - p0.y)-(p1.x - p0.x)*(p2.y - p0.y);
}

void binsearch(point toy)   //搜索并利用叉积进行判断
{
int l,r,mid;
l=0;
r=n-1;
while(l<r)
{
mid=(r+l)/2;
if(cross(line[mid].b,line[mid].a,toy)<0)
r=mid;
else
l=mid+1;
}
if(cross(line[l].b,line[l].a,toy)<0)
ans[l]++;
else
ans[l+1]++;
}

int main()
{
point toy;
int m,x1,y1,x2,y2;
int u,l;
int xj,yj;
while(scanf("%d",&n) && n!=0)
{
scanf("%d%d%d%d%d",&m,&x1,&y1,&x2,&y2);
memset(ans,0,sizeof(ans));
for(int i=0;i<n;i++)
{
scanf("%d%d",&u,&l);
line[i].a.x=u;
line[i].a.y=y1;
line[i].b.x=l;
line[i].b.y=y2;
}

for(int i=0;i<m;i++)
{
scanf("%d%d",&xj,&yj);
point toy;
toy.x=xj;
toy.y=yj;
binsearch(toy);
}

for(int i=0;i<=n;i++)
printf("%d: %d\n",i,ans[i]);
cout<<endl;
}
} 

#### 判断一个点与直线的位置

2014-10-17 18:00:42

#### 利用叉积判断点和线的关系

2016-01-10 15:13:09

#### POJ1106->叉积判断点在直线的左右

2016-08-27 23:33:13

#### poj2318TOYS【二分+点与直线位置判断】

2015-08-27 22:06:08

#### POJ2318->叉积判断点在线段的左右

2016-08-25 18:21:38

#### 判断一个点与直线的相对位置

2017-05-03 19:34:56

#### 计算几何问题汇总--点与线的位置关系

2016-09-20 08:53:45

#### 叉积判断点在多边形内外 & poj2318

2015-09-21 21:02:53

#### 神秘大三角(判断点与三角形的关系)

2018-01-06 09:51:54

#### 叉乘判两线段是否相交

2017-12-03 20:32:59