判断一个多边形是否在另一个多边形内部,Java代码


import java.awt.geom.Point2D;
import java.awt.geom.GeneralPath;
import java.util.Arrays;
import java.util.List;


public class MyUtil {

    /**
     * 判断多边形1是否在多边形2内部。true在内部,false不在内部
     *
     * @param polygon1 多边形1
     * @param polygon2 多边形2
     * @return
     */
    public static boolean isPolygonInPolygon(List<Point2D.Double> polygon1, List<Point2D.Double> polygon2) {
        // 如果多边形1的某一个点不在多边形2内部,则多边形1不在多边形2内部
        for (Point2D.Double pointPolygon1 : polygon1) {
            if (!isPointInPoly(pointPolygon1, polygon2)) {
                return false;
            }
        }

        // 如果多边形1和多边形2的某条边有交点,则多边形1不在多边形2内部
        for (int i = 0; i < polygon1.size(); i++) {
            // p1-p2多边形1的一条边
            Point2D.Double p1 = polygon1.get(i);
            Point2D.Double p2;
            if (i < polygon1.size() - 1) {
                p2 = polygon1.get(i + 1);
            } else {
                p2 = polygon1.get(0);
            }

            // p3-p4多边形2的一条边
            for (int j = 0; j < polygon2.size(); j++) {
                Point2D.Double p3 = polygon2.get(j);
                Point2D.Double p4;
                if (j < polygon2.size() - 1) {
                    p4 = polygon2.get(j + 1);
                } else {
                    p4 = polygon2.get(0);
                }

                if (isIntersect(p1, p2, p3, p4)) {
                    return false;
                }
            }
        }

        return true;
    }


    /**
     * 返回p1-p2,p3-p4两条线段是否有交点。true有,false没有
     *
     * @param p1
     * @param p2
     * @param p3
     * @param p4
     * @return
     */
    public static boolean isIntersect(Point2D.Double p1, Point2D.Double p2, Point2D.Double p3, Point2D.Double p4) {
        boolean flag = false;
        double d = (p2.getX() - p1.getX()) * (p4.getY() - p3.getY())
                - (p2.getY() - p1.getY()) * (p4.getX() - p3.getX());
        if (d != 0) {
            double r = ((p1.getY() - p3.getY()) * (p4.getX() - p3.getX())
                    - (p1.getX() - p3.getX()) * (p4.getY() - p3.getY())) / d;
            double s = ((p1.getY() - p3.getY()) * (p2.getX() - p1.getX())
                    - (p1.getX() - p3.getX()) * (p2.getY() - p1.getY())) / d;
            if ((r >= 0) && (r <= 1) && (s >= 0) && (s <= 1)) {
                flag = true;
            }
        }
        return flag;
    }


    /**
     * 返回一个点是否在一个多边形区域内。true在,false不在。如果点位于多边形的顶点或边上,不算做点在多边形内,返回false
     *
     * @param point   点
     * @param polygon 多边形
     * @return
     */
    public static boolean isPointInPoly(Point2D.Double point, List<Point2D.Double> polygon) {
        assertParams(point, polygon);

        GeneralPath p = new GeneralPath();
        Point2D.Double first = polygon.get(0);
        p.moveTo(first.x, first.y);
        int size = polygon.size();
        for (int i = 1; i < size; i++) {
            Point2D.Double pa = polygon.get(i);
            p.lineTo(pa.x, pa.y);
        }
        p.lineTo(first.x, first.y);
        p.closePath();
        return p.contains(point);
    }


    private static void assertParams(Point2D.Double point, List<Point2D.Double> polygon) {
        if (null == point || null == polygon) {
            throw new IllegalArgumentException("参数不能为空");
        }
        if (polygon.size() < 3) {
            throw new IllegalArgumentException("多边形点数需要大于等于3");
        }

    }

    public static void main(String[] args) {
        Point2D.Double p2 = new Point2D.Double(0, 0);
        Point2D.Double p3 = new Point2D.Double(1, 0);
        Point2D.Double p4 = new Point2D.Double(1, 1);
        Point2D.Double p5 = new Point2D.Double(2, 1);
        Point2D.Double p6 = new Point2D.Double(2, 0);
        Point2D.Double p7 = new Point2D.Double(3, 0);
        Point2D.Double p8 = new Point2D.Double(3, 3);
        Point2D.Double p9 = new Point2D.Double(0, 3);

        Point2D.Double p11 = new Point2D.Double(0.3, 1.3);
        Point2D.Double p12 = new Point2D.Double(2.8, 1.3);
        Point2D.Double p13 = new Point2D.Double(2.8, 0.8);
        Point2D.Double p14 = new Point2D.Double(0.3, 0.8);

		// false
        System.out.println(ShieldUtil.isPolygonInPolygon
                (Arrays.asList(p11, p12, p13, p14), Arrays.asList(p2, p3, p4, p5, p6, p7, p8, p9)));
    }
}

你可能感兴趣的:(Java,java,几何学)