题目大意:给出两个整数n,d,代表有一个在第一象限的矩形,坐标是(0,d) (d,0) (n,n-d) (n-d,n),再给出一些点的坐标,求出这些点是否在矩形中。
题解:只需要判断该点是否在上下两条边和左右两条边之间就可以
法①:
判断一个点是否在两条线段之间夹着,就转化成,判断一个点是否在某条线段的一边上,就可以利用叉乘的方向性,来判断夹角是否超过了180度
只要判断
(向量叉积)
就说明p在边p1p2和边p3p4中间夹着,同理也要判断
#include <bits/stdc++.h>
#include <cstring>
#define ll long long
#define INF 0x3f3f3f3f
using namespace std;
const double eps = 1e-8;
const double PI = acos(-1.0);
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);
}
};
Point p1,p2,p3,p4;
double GetCross(Point& p1,Point& p2,Point& p)
{
return (p2-p1)^(p-p1);
}
bool pd(Point p)
{
return GetCross(p1,p2,p) * GetCross(p3,p4,p) >= 0 && GetCross(p2,p3,p) * GetCross(p4,p1,p) >= 0;
}
int main()
{
int n,d;
cin>>n>>d;
p1=Point(0,d);
p2=Point(d,0);
p3=Point(n,n-d);
p4=Point(n-d,n);
int T;
cin>>T;
while(T--)
{
int x,y;
cin>>x>>y;
if(pd(Point(x,y)))
puts("YES");
else puts("NO");
}
return 0;
}
法②:
只需要满足
就是在矩形内
#include <bits/stdc++.h>
#include <cstring>
#define ll long long
#define INF 0x3f3f3f3f
using namespace std;
int main()
{
int n,d,T;
cin>>n>>d;
cin>>T;
while(T--)
{
int x,y;
cin>>x>>y;
if(x+y>=d && x+y<=2*n-d && x-y>=-d && x-y<=d)
puts("YES");
else puts("NO");
}
return 0;
}