射线法判断点是否在多边型内部

射线法判断点是否在多边型内部

射线法原理:

  • 如果点在多边型内部,从该点引出一条射线则射线与多边型只有一个焦点
  • 如果点不在多边形内部,从该点引出一条经过多边型的射线则具有2个交点

代码:

#include <iostream>
#include <vector>
#include <math.h>
#include <assert.h>

using namespace std;

// 计算点在图形内部还是外部

// 定义点
struct Point2d{
    double x;
    double y;
    // 构造
    Point2d(double _x, double _y):x(_x), y(_y){}
    // 定义减法
    Point2d operator-(Point2d _point){
        return Point2d(this->x - _point.x, this->y - _point.y);
    }
    // 定义加法
    Point2d operator+(Point2d _point){
        return Point2d(this->x + _point.x, this->y + _point.y);
    }
    // 定义除法
    Point2d operator/(double _num){
        return Point2d(this->x / _num, this->y / _num);
    }
    // 定义乘法
    Point2d operator*(double _num){
        return Point2d(this->x * _num, this->y * _num);
    }
    // 定义归一化
    Point2d norm(void){
        double mo = sqrt(x * x + y * y);
        return Point2d(x / mo, y / mo);
    }
};
// 定义叉乘
double cross_product(Point2d p1, Point2d p2){
    return p1.x*p2.y - p1.y*p2.x;
}
// 定义判断顺序的
int order(Point2d _p1, Point2d _p2, Point2d _p3){
    auto p2_sub_p1 = _p2 - _p1;
    auto p3_sub_p1 = _p3 - _p1;
    double res = cross_product(p2_sub_p1, p3_sub_p1);
    if(res > 0) return 1;
    else if(res == 0){// 特殊情况需要重新考虑
        std::cout <<  "error" << std::endl;
    }
    return -1;
}
// 判断直线和直线是否相交
bool intersect_adjust(Point2d _Line1_A, Point2d _Line1_B, Point2d _Line2_C, Point2d _Line2_D){
    if((order(_Line1_A, _Line1_B, _Line2_C) != order(_Line1_A, _Line1_B, _Line2_D)) &&
       (order(_Line2_C, _Line2_D, _Line1_A) != order(_Line2_C, _Line2_D, _Line1_B)) 
    ){
        std::cout << "is intersect" << std::endl;
        return true;
    }else{
        std::cout << "no intersect" << std::endl;
    }
    return false;
}


// 定义多边形
struct Polygon{
    vector<Point2d> polygon_points;// 多边型按顺序的点
    // 返回多边型的形心
    Point2d get_middle_point(void){
        Point2d mid_point(0, 0);
        for(int i = 0; i < polygon_points.size(); ++i){
            mid_point = mid_point + polygon_points[i];
        }
        mid_point = mid_point / double(polygon_points.size());
        return mid_point;
    }
};
// 判断点是否在多边型内部
bool polygon_adjust(Point2d _point, Polygon _polygon){
    auto mid_point = _polygon.get_middle_point();
    // 点指向中点并进行放大
    Point2d subline = (mid_point - _point).norm();  // 获得方向
    Point2d endLine = _point + subline * 10000;     // 给一个巨大的
    int num = 0;
    for(int i = 1; i < _polygon.polygon_points.size(); ++i){
        if(intersect_adjust(_point, endLine, _polygon.polygon_points[i-1], _polygon.polygon_points[i])){
            num++;
        }
    }
    assert(num > 0);
    assert(num <= 2);
    if(num == 1){
        return true;
    }
    return false;


}


int main(){
    //- 相交测试案例
    Polygon polygon_test;
    polygon_test.polygon_points.push_back(Point2d(0, 0));
    polygon_test.polygon_points.push_back(Point2d(0, 2));
    polygon_test.polygon_points.push_back(Point2d(2, 2));
    polygon_test.polygon_points.push_back(Point2d(2, 0));
    // (1, 1) 不行 因为11正好是中心!!!
    Point2d test_point(-0.5,1);
    
    if(polygon_adjust(test_point, polygon_test)){
        std::cout << "point is in polygon" << std::endl;
    }else{
        std::cout << "point is out polygon" << std::endl;
    }

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值