判断点是否在矩形框之内的汇总:
1.给出的矩形是一个边的的中点以及矩形的长宽,还有矩形和坐标轴的夹角,以及要判断的点坐标。
基本思路:已知矩形和y轴的夹角,通过旋转矩形和测试点,使其一边和y轴平行,方便利用长宽的输入。
代码如下:
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
#include<string>
#include<vector>
#include<cmath>
using namespace std;
typedef struct {
float x;
float y;
}Point;
typedef struct{
Point point;
float width;
float length;
}Rectangle;
string PointIsInRectangle(Rectangle Rect,float thta, Point point_in){
float x_test = 0.0f, y_test = 0.0f;
float x_rect = 0.0f, y_rect = 0.0f;
x_test = point_in.x * cos(thta) + point_in.y * sin(thta);
y_test = -point_in.x * sin(thta) + point_in.y * cos(thta);
x_rect = Rect.point.x * cos(thta) + Rect.point.y * sin(thta);
y_rect = -Rect.point.x * sin(thta) + Rect.point.y * cos(thta);
if((x_test >= x_rect - 0.5f * Rect.width && x_test <= x_rect + 0.5f * Rect.width)
&& (y_test >= y_rect && y_test <= y_rect + Rect.length))
{
return "Point is in the Rectangle!";
}
else
{
return "Point is not in the Rectangle!";
}
}
int main(){
Point P1,point_test;
float thta;
Rectangle Rect;
cout << "please input the rectangle point and test point:" << endl;
cin >> P1.x >> P1.y >> point_test.x >> point_test.y;
Rect.point = P1;
cout << "please input the rectangle width and length:" << endl;
cin >> Rect.width >> Rect.length;
cout << "please input the rectangle angle:" << endl;
cin >> thta;
cout << PointIsInRectangle(Rect,thta,point_test) << endl;
return 0;
}
2.如果输入的是四个交点,则可以通过点积的正负判断是否在矩形之内:
此例需要输入四个交点才能计算。
参考如下:
https://blog.csdn.net/hhjhh76/article/details/89468653?utm_medium=distribute.pc_relevant.none-task-blog-2defaultbaidujs_title~default-0.pc_relevant_paycolumn_v3&spm=1001.2101.3001.4242.1&utm_relevant_index=3