c++检测点是否在封闭区域内

射线法实现:

bool pointInPolygon(Eigen::Vector2f point, std::vector polygonPeaks)
{
    int n = polygonPeaks.size();
    bool inside = false;
    for (int i = 0, j = n - 1; i < n; j = i++) {
        Eigen::Vector2f curPoint = polygonPeaks.at(i);
        Eigen::Vector2f prePoint = polygonPeaks.at(j);
        if ((curPoint.y() > point.y()) != (prePoint.y() > point.y()) &&
            (point.x() < (prePoint.x() - curPoint.x()) * (point.y() - curPoint.y()) / (prePoint.y() - curPoint.y()) + curPoint.x()))
           inside = !inside;
    }
    return inside;
}

你可能感兴趣的:(c++,c++,算法,自动驾驶,数据结构)