判断点是否在多边形内部

    bool ContainsPoint(List<Vector2> polyPoints, Vector2 p) {
        var j = polyPoints.Count - 1;
        var inside = false;
        for (int i = 0; i < polyPoints.Count; j = i++) {
            var pi = polyPoints[i];
            var pj = polyPoints[j];
            if (((pi.y <= p.y && p.y < pj.y) || (pj.y <= p.y && p.y < pi.y)) &&
                (p.x < (pj.x - pi.x) * (p.y - pi.y) / (pj.y - pi.y) + pi.x))
                inside = !inside;
        }

        Debug.Log (inside);
        return inside;
    }

 

posted @ 2021-06-04 12:07  三页菌  阅读(88)  评论(0编辑  收藏  举报