判断点是否在矩阵内

判断点是否在矩阵内

题意:
给出四个点p2(x1, y1)为最左的点,p3(x2, y2)为最上的点,p1(x3, y3)为最下的点,p4(x4, y4)为最右的点。给定4个点代表的矩形,再给定一个点p(x, y),判断p(x, y)是否在矩形中

(其中p1,p2,p3,p4的命名是我自己为了方便命名的,主要是按照顺时针方向进行的命名)

题解:
可以利用叉乘或者点乘来判断
在这里插入图片描述
(图片转载于:https://blog.csdn.net/faithmy509/article/details/82803646

于是代码如下:

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<iostream>
#include<sstream>
#include<string>
#include<queue>
#include<vector>
#include<set>
#include<stack>
#include<map>
#define lowbit(x) x&(-x)
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;
const double pi=3.14159;
const int maxn=1e5;
const int mod=1e3;
struct node{
    double x,y;
};
node p1,p2,p3,p4;
node p;
double getCross(node p1,node p2,node p){
    return (p2.x-p1.x)*(p.y-p1.y)-(p.x-p1.x)*(p2.y-p1.y);
}
int main(){
    cin>>p2.x>>p2.y;
    cin>>p3.x>>p3.y;
    cin>>p1.x>>p1.y;
    cin>>p4.x>>p4.y;
    cin>>p.x>>p.y;

    if(getCross(p1,p2,p)*getCross(p3,p4,p)>=0&&getCross(p2,p3,p)*getCross(p4,p1,p)>=0){
        printf("Yes\n");
    }
    else{
        printf("No\n");
    }
}

发布了127 篇原创文章 · 获赞 32 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/boliu147258/article/details/104348667