|x1 x2 x3|
S(P1,P2,P3) = |y1 y2 y3| = (x1-x3)*(y2-y3) - (y1-y3)(x2-x3)
|1 1 1 |
当P1P2P3逆时针时S为正的,当P1P2P3顺时针时S为负的。
令矢量的起点为A,终点为B,判断的点为C,
如果S(A,B,C)为正数,则C在矢量AB的左侧;
如果S(A,B,C)为负数,则C在矢量AB的右侧;
如果S(A,B,C)为0,则C在直线AB上。
对于有公共端点的线段p0p1和p1p2,通过计算(p2- p0) × (p1 - p0)的符号便可以确定折线段的拐向:
若(p2- p0) × (p1 - p0) > 0,则p0p1在p1点拐向右侧后得到p1p2。
若(p2- p0) × (p1 - p0) < 0,则p0p1在p1点拐向左侧后得到p1p2。
{
if(((p1.x-p0.x)*(p2.y-p0.y)-(p1.y-p0.y)*(p2.x-p0.x))<0)//p0 p2 分别表示线段的两端,p1为要判断的点
{
return true;
}
else
return false;
}
Mom and dad have a problem - their child John never puts his toys away when he is finished playing with them. They gave John a rectangular box to put his toys in, but John is rebellious and obeys his parents by simply throwing his toys into the box. All the toys get mixed up, and it is impossible for John to find his favorite toys.
John's parents came up with the following idea. They put cardboard partitions into the box. Even if John keeps throwing his toys into the box, at least toys that get thrown into different bins stay separated. The following diagram shows a top view of an example toy box.
For this problem, you are asked to determine how many toys fall into each partition as John throws them into the toy box.
5 6 0 10 60 0 3 1 4 3 6 8 10 10 15 30 1 5 2 1 2 8 5 5 40 10 7 9 4 10 0 10 100 0 20 20 40 40 60 60 80 80 5 10 15 10 25 10 35 10 45 10 55 10 65 10 75 10 85 10 95 10 0Sample Output
0: 2 1: 1 2: 1 3: 1 4: 0 5: 1 0: 2 1: 2 2: 2 3: 2 4: 2
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
using namespace std;
int m,n;
const int maxn = 5005;
int coun[maxn];
struct dia
{
int x;
int y;
}dian[maxn]; // 每个点的横纵坐标
{
dia a;//a dixia
dia b;
}line[maxn];//上边 下边
{
if(((p1.x-p0.x)*(p2.y-p0.y)-(p1.y-p0.y)*(p2.x-p0.x))<0)
{
return true;
}
else
return false;
}
{
for(int i = 0; i < n; i++)
{
if(chaji(line[i].b,pp,line[i].a)>0)
{
coun[i]++;
return ;
}
}
coun[n]++;
return ;
}
int main()
{
int c,d,e,f;
while(cin>>n&&n)
{
memset(coun,0,sizeof(coun));
cin>>m;
cin>>c>>d>>e>>f;
for(int i = 0; i < n; i++)
{
cin>>line[i].a.x;
cin>>line[i].b.x;
line[i].b.y = f;
line[i].a.y = d;
}
dia pp;
for(int i = 0; i < m; i++)
{
cin>>pp.x>>pp.y;
judge(pp);
}
for(int i = 0; i <= n; i++)
{
printf("%d: %d\n",i,coun[i]);
}
printf("\n");
}
}
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
using namespace std;
int m,n;
const int maxn = 5005;
int coun[maxn];
struct dia
{
int x;
int y;
}dian[maxn]; // 每个点的横纵坐标
{
dia a;//a dixia
dia b;
}line[maxn];//上边 下边
bool cmp(const lin &u,const lin &v)
{
if(u.a.x!=v.a.x)
return u.a.x<v.a.x;
// return u.a.y<v.a.y;
}
{
if(((p1.x-p0.x)*(p2.y-p0.y)-(p1.y-p0.y)*(p2.x-p0.x))<0)
{
return true;
}
else
return false;
}
int cnt[5005], ans[5005];
void judge(dia pp)
{
for(int i = 0; i < n; i++)
{
if(chaji(line[i].b,pp,line[i].a)>0)
{
coun[i]++;
return ;
}
}
coun[n]++;
return ;
}
int main()
{
int c,d,e,f;
while(cin>>n&&n)
{
memset(coun,0,sizeof(coun));
memset(ans,0,sizeof(ans));
cin>>m;
cin>>c>>d>>e>>f;
for(int i = 0; i < n; i++)
{
cin>>line[i].a.x;
cin>>line[i].b.x;
line[i].b.y = f;
line[i].a.y = d;
}
sort(line,line+n,cmp);
for(int i = 0; i < m; i++)
{
cin>>pp.x>>pp.y;
judge(pp);
}
/* for(int i = 0; i <= n; i++)
{
printf("%d: %d\n",i,coun[i]);
}*/
for(int i = 0; i <= n; i++)
{
ans[coun[i]]++;
}
printf("Box\n");
for(int i = 1; i < n; i++)
{
if(ans[i]!=0)
{
printf("%d: %d\n",i,ans[i]);
}
}
}
}
假设四边形四个顶点依次为A(x1,y1),B(x2,y2),C(x3,y3),D(x4,y4),待判断的点为P(x,y),如果点P在四边形内部,则向量AB * AP(注意:1.这是求叉积;2.AB、AP均为向量,也就等于(x2-x1) * (y-y1)-(y2-y1) * (x-x1))的值与BC*BP、CD * CP、DA * DP的值同号(若有等于零的情况,则表示P在边上,可以根据自己的喜好把它当做是内部或者外部),即四个值同为正或者同为负,则点P在ABCD内部,否则在外部。
private boolean isPointInRect(int x, int y) {
final Point A = mLBPoint;
final Point B = mLTPoint;
final Point C = mRTPoint;
final Point D = mRBPoint;
final int a = (B.x - A.x)*(y - A.y) - (B.y - A.y)*(x - A.x);
final int b = (C.x - B.x)*(y - B.y) - (C.y - B.y)*(x - B.x);
final int c = (D.x - C.x)*(y - C.y) - (D.y - C.y)*(x - C.x);
final int d = (A.x - D.x)*(y - D.y) - (A.y - D.y)*(x - D.x);
if((a > 0 && b > 0 && c > 0 && d > 0) || (a < 0 && b < 0 && c < 0 && d < 0)) {
return true;
}
// AB X AP = (b.x - a.x, b.y - a.y) x (p.x - a.x, p.y - a.y) = (b.x - a.x) * (p.y - a.y) - (b.y - a.y) * (p.x - a.x);
// BC X BP = (c.x - b.x, c.y - b.y) x (p.x - b.x, p.y - b.y) = (c.x - b.x) * (p.y - b.y) - (c.y - b.y) * (p.x - b.x);
return false;
}
https://blog.csdn.net/liangzhaoyang1/article/details/51090370# 判断线段相交