#include<iostream>
#include<vector>
using namespace std;
struct Point{
int x;
int y;
};
bool judgePointInRect(vector<Point>&Points,int ROWS,int COLS){
//将四个点定位找出最大y,与最小y
bool rectIsLevel=false;
if(Points.size()!=5){//必须为5个点
return false;
}
int min_y=0,max_y=0,min_x=0,max_x=0;
for(int i=0;i<4;i++){
if(Points[min_y].y>Points[i].y){
min_y=i;
}
if(Points[max_y].y<Points[i].y){
max_y=i;
}
if(Points[max_x].x<Points[i].x){
max_x=i;
}
if(Points[min_x].x>Points[i].x){
min_x=i;
}
}
int max_y_num=0,min_y_num=0,max_x_num=0,min_x_num=0;
for(int i=0;i<4;i++){
if(Points[min_y].y==Points[i].y){
min_y_num++;
}
if(Points[max_y].y==Points[i].y){
max_y_num++;
}
if(Points[max_x].x==Points[i].x){
max_x_num++;
}
if(Points[min_x].x==Points[i].x){
min_x_num++;
}
}
if(max_y_num==2&&min_y_num==2&&max_x_num==2&&min_x_num==2){
rectIsLevel=true;
}
//已经知道,最左边,最右边,最上边,最下边的点
//矩形是水平的
if(rectIsLevel==false){
//建立四个线性方程
//y=kx+b
//y k x b均为浮点数
float k=0,b=0,dx=0,dy=0,judge=0;
bool statusArray[4]={false};
//最左边的点,与最上边的点,k<0
dx=Points[min_y].x-Points[min_x].x;
dy=Points[min_x].y-Points[min_y].y;
k=0-(dy/dx);
//求解b
b=Points[min_x].y-k*Points[min_x].x;
//求解(0,0),对于 kx+b-y是大于0还是小于0
judge=k*0+b-0;
if(judge<0&&k*Points[4].x+b-Points[4].y>0){
statusArray[0]=true;
}else if(judge>0&&k*Points[4].x+b-Points[4].y<0){
statusArray[0]=true;
}else{
statusArray[0]=false;
}
//最上边的点与最右边的点 k>0
dx=Points[max_x].x-Points[min_y].x;
dy=Points[max_x].y-Points[min_y].y;
k=dy/dx;
//求解b
b=Points[max_x].y-k*Points[max_x].x;
//求解(rows,0),对于 kx+b-y是大于0还是小于0
judge=k*ROWS+b-0;
if(judge<0&&k*Points[4].x+b-Points[4].y>0){
statusArray[1]=true;
}else if(judge>0&&k*Points[4].x+b-Points[4].y<0){
statusArray[1]=true;
}else{
statusArray[1]=false;
}
//最右边的点与最下边的点 k<0
dx=Points[max_x].x-Points[max_y].x;
dy=Points[max_y].y-Points[max_x].y;
k=0-dy/dx;
//求解b
b=Points[max_x].y-k*Points[max_x].x;
//求解(ROWS,COLS),对于 kx+b-y
judge=k*ROWS+b-COLS;
if(judge<0&&k*Points[4].x+b-Points[4].y>0){
statusArray[2]=true;
}else if(judge>0&&k*Points[4].x+b-Points[4].y<0){
statusArray[2]=true;
}else{
statusArray[2]=false;
}
//最左边的点,与最下边的点 k>0
dx=Points[max_y].x-Points[min_x].x;
dy=Points[max_y].y-Points[min_x].y;
k=dy/dx;
//求解b
b=Points[max_y].y-k*Points[max_y].x;
//求解(0,COLS),对于 kx+b-y
judge=k*0+b-COLS;
if(judge<0&&k*Points[4].x+b-Points[4].y>0){
statusArray[3]=true;
}else if(judge>0&&k*Points[4].x+b-Points[4].y<0){
statusArray[3]=true;
}else{
statusArray[3]=false;
}
//如果数组内全部为true则返回true,否则返回false
for(int i=0;i<4;i++){
if(statusArray[i]!=true){
return false;
}
}
return true;
}else{
//矩形不是水平的,可以直接判断点是否在这个矩形里面
if(Points[4].x>=Points[min_x].x&&Points[4].x<=Points[max_x].x){//判断x
if(Points[4].y>=Points[min_y].y&&Points[4].y<=Points[max_y].y){
return true;
}else{
return false;
}
}else{
return false;
}
}
return true;
}
int main(int argc,char**argv){
vector<Point>points;
struct Point P;
int x[5]={0,2,3,1,1};
int y[5]={2,0,1,3,3};
for(int i=0;i<5;i++){
P.x=x[i];
P.y=y[i];
points.push_back(P);
}
cout<<judgePointInRect(points,500,500)<<endl;
return 0;
}
判断点是否在旋转矩形内(OpenCV可利用)
最新推荐文章于 2023-05-06 09:59:12 发布