判断点是否在矩形之内汇总

判断点是否在矩形框之内的汇总:
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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值