学习一下多边形面积的计算

求凸多边形面积:

public static void main(String[] args) {
        Scanner reader = new Scanner(System.in);
        int n = reader.nextInt();
        int[] xpoints = new int[n + 1];
        int[] ypoints = new int[n + 1];
        double sum = 0;
        for (int i = 0; i < n; i++) {
            xpoints[i] = reader.nextInt();
            ypoints[i] = reader.nextInt();
        }

        //再把第一个点丢到最后
        xpoints[n] = xpoints[0];
        ypoints[n] = ypoints[0];

        for (int i = 0; i < n; i++) {
            sum += (xpoints[i] * ypoints[i + 1]) - (xpoints[i + 1] * ypoints[i]);
        }

        sum /= 2;

        System.out.println(String.format("%.0f", sum));
    }

从stackOverFlow抄的。

然后是判断点是否在凸多边形内,用java自带的多边形类搞定(为什么这个类不带求面积???):

public static void main(String[] args) {
        Scanner reader = new Scanner(System.in);
        while (reader.hasNext()) {
            int n = reader.nextInt();
            Polygon polygon = new Polygon();
            for (int i = 0; i < n; i++) {
                int x = reader.nextInt();
                int y = reader.nextInt();
                polygon.addPoint(x, y);
            }

            int m = reader.nextInt();
            boolean ans = true;
            for (int i = 0; i < m; i++) {
                int x = reader.nextInt();
                int y = reader.nextInt();
                ans &= polygon.contains(new Point(x, y));
            }
            System.out.println(ans ? "YES" : "NO");
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值