前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >一、需求

一、需求

作者头像
老梁
发布2022-11-06 09:33:50
5090
发布2022-11-06 09:33:50
举报
文章被收录于专栏:Java工程师成长之路

一、需求

  1. 计算两个多边形iou的值,iou代表两图形的交集除以两图形的并集
  2. 计算图形2的每个点距离图形1的最短距离

二、依赖库

代码语言:javascript
复制
<!-- 几何库 -->
<dependency>
  <groupId>org.locationtech.jts</groupId>
  <artifactId>jts-core</artifactId>
  <version>1.16.1</version>
</dependency>

三、代码例子

代码语言:javascript
复制
import com.lll.toolsky.tool.PolygonUtils;
import org.locationtech.jts.algorithm.distance.DistanceToPoint;
import org.locationtech.jts.algorithm.distance.PointPairDistance;
import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.Polygon;
import org.locationtech.jts.io.ParseException;

import java.util.List;

/**
 * Description:
 *
 * @author laoliangliang
 * @version 1.0
 * @date 2022/11/1
 **/
public class JtsTest {
    public static void main(String[] args) throws ParseException {
        // 图形1
        List<String> xyListStr1 = List.of("621.9920676494789,562.6872757226483,1",
                "659.3489485893876,558.66799038183,2",
                "684.6366833794797,542.0166653984401,3",
                "729.4649405073701,536.2748291972712,4",
                "787.511786275536,540.8682981582062,5",
                "801.8798174062701,546.0359507392584,6",
                "805.3281448776463,551.2036033203104,7",
                "806.477587368105,602.880129130831,8",
                "802.4545386514994,614.9379851532858,9",
                "632.3370500636075,615.5121687734027,10");
        // 图形2
        List<String> xyListStr2 = List.of("627.0252590425256,560.7213438679515,1",
                "658.6209099387936,557.1782107974741,2",
                "684.0908734163975,540.7509574707152,3",
                "729.8723267558878,537.5299274066448,4",
                "763.7248098590321,540.753973416468,5",
                "785.0035135238656,543.0086944613173,6",
                "806.927026390664,543.0086944613173,7",
                "809.1838585975402,619.9913129925997,8",
                "718.2657611205241,611.9387378324236,9",
                "636.3749924710131,610.9736351915037,10");
        double[][] d1 = new double[xyListStr1.size()][2];
        double[][] d2 = new double[xyListStr2.size()][2];
        for (int i = 0; i < xyListStr1.size(); i++) {
            String s = xyListStr1.get(i);
            String[] split = s.split(",");
            d1[i][0] = Double.parseDouble(split[0]);
            d1[i][1] = Double.parseDouble(split[1]);
        }
        for (int i = 0; i < xyListStr2.size(); i++) {
            String s = xyListStr2.get(i);
            String[] split = s.split(",");
            d2[i][0] = Double.parseDouble(split[0]);
            d2[i][1] = Double.parseDouble(split[1]);
        }

        calculate(d1, d2);
    }

    private static void calculate(double[][] d1, double[][] d2) {
        Polygon polygon1 = PolygonUtils.createPolygon(d1);
        Polygon polygon2 = PolygonUtils.createPolygon(d2);
        double v = polygonIou(polygon1, polygon2);
        System.out.println("iou:" + v);
        System.out.println("-------");
        System.out.println("计算图形2每个点距离图形1最短距离");
        for (double[] dou : d2) {
            PointPairDistance ppd = new PointPairDistance();
            Coordinate coordinate = new Coordinate(dou[0],dou[1]);
            DistanceToPoint.computeDistance(polygon1, coordinate, ppd);
            System.out.println("点距离图形1最短距离:"+ppd.getDistance());
        }
    }
    public static double polygonIou(Polygon polygon1, Polygon polygon2) {
        // 交集
        Geometry intersection = polygon1.intersection(polygon2);
        double intersectionArea = intersection.getArea();
    
        // 并集
        Geometry union = polygon1.union(polygon2);
        double unionArea = union.getArea();
        return intersectionArea / unionArea;
    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-11-01,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一、需求
  • 二、依赖库
  • 三、代码例子
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档