如何判断一个点在矩形内

最近在做游戏服务器中技能模块,往往要扫描一个区域,判断npc是不是在我这个区域内,在的话就发伤害。  就需要实现一下,对于一个点是否在矩形内的判断。

只需要判断该点是否在上下两条边和左右两条边之间就行,判断一个点是否在两条线段之间夹着,就转化成,判断一个点是否在某条线段的一边上,就可以利用叉乘的方向性,来判断夹角是否超过了180度 如下图:

这里写图片描述

只要判断(p1 p2 X p1 p ) * (p3 p4 X p3 p1) >= 0 就说明p在p1p2,p3p4中间夹着,同理计算另两边就可以了   最后就是只需要判断 (p1 p2 X p1 p ) * (p3 p4 X p3 p1) >= 0 && (p2 p3 X p2 p ) * (p4 p1 X p4 p) >= 0 ;

// ConsoleApplication18.cpp : Defines the entry point for the console application.
//判断一个点是否在矩形内部

#include "stdafx.h"
#include "iostream"

struct Point
{
    float x;
    float y;
    Point(float x,float y)
    {
        this->x = x;
        this->y = y;
    }
};
// 计算 |p1 p2| X |p1 p|
float GetCross(Point& p1, Point& p2,Point& p)
{
    return (p2.x - p1.x) * (p.y - p1.y) -(p.x - p1.x) * (p2.y - p1.y);
}
//判断点是否在5X5 以原点为左下角的正方形内(便于测试)
bool IsPointInMatrix(Point& p)
{
    Point p1(0,5);
    Point p2(0,0);
    Point p3(5,0);
    Point p4(5,5);

    return GetCross(p1,p2,p) * GetCross(p3,p4,p) >= 0 && GetCross(p2,p3,p) * GetCross(p4,p1,p) >= 0;
    //return false;
}
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{

    while(true)
    {
        Point testPoint(0,0);
        cout << "enter  the point :" << endl;

        cin >> testPoint.x >> testPoint.y;

        cout << "the point is  : "<< testPoint.x << " "<< testPoint.y << endl;

        cout << "the point is " << (IsPointInMatrix(testPoint)? "in the Matrix .": "not in the matrix ." )<< endl;
    }

    return 0;
}

此文章转载自:https://blog.csdn.net/dapengbusi/article/details/50516126

猜你喜欢

转载自blog.csdn.net/chen134225/article/details/81192345