使用Python计算平面多边形间最短距离,数据需要从excel表格中导入

复制代码
使用Python计算平面多边形间最短距离,数据需要从excel表格中导入,
* 多边形种类包括(圆形、矩形、六边形、五边形、跑道形/胶囊形),
* Python代码需要使用gjk算法进行判断两个多边形间是否重叠,
* 如果未重叠计算最短距离
java 复制代码
package controller.com.codermart.controller;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Random;

/**
 * Created by Lenovo on 2023/10/16.
 */
public class PythonAlgorithm {

    public static void main(String[] args) {
        int testVar=1;
        switch (testVar){
            case 1:
                break;
            case 2:
                break;
        }
    }

    /**
     * 使用Python计算平面多边形间最短距离,数据需要从excel表格中导入,
     * 多边形种类包括(圆形、矩形、六边形、五边形、跑道形/胶囊形),
     * Python代码需要使用gjk算法进行判断两个多边形间是否重叠,
     * 如果未重叠计算最短距离
     * @param shapeFir
     * @param shapeSec
     * @return
     */
    public static Double getShapeDistance(Shape shapeFir,Shape shapeSec){
        if (shapeFir==null){
            return null;
        }
        if (shapeFir.getShapeWindowsCordination()==null){
            return null;
        }
        if (shapeFir.getShapeWindowsCordination().isEmpty()){
            return null;
        }
        if (shapeSec==null){
            return null;
        }
        if (shapeSec.getShapeWindowsCordination()==null){
            return null;
        }
        if (shapeSec.getShapeWindowsCordination().isEmpty()){
            return null;
        }

//        String name = ShapeEnum.CIRCLER.getName();
        String shapeWindowsCordination = shapeFir.getShapeWindowsCordination();
        String shapeWindowsCordination1 = shapeSec.getShapeWindowsCordination();

        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < shapeWindowsCordination.length(); i++) {
            char c = shapeWindowsCordination.charAt(i);
            if (Character.isDigit(c)){
                stringBuilder.append(Integer.valueOf(c));
            }else if (",".equals(c)){
                stringBuilder.append(" ");
                continue;
            }
        }

        String s = stringBuilder.toString();
        String[] split = s.split("\\s");
        ArrayList<Integer> integers = new ArrayList<>();
        for (int i = 0; i < split.length; i++) {
            integers.add(Integer.valueOf(split[i]));
        }

        StringBuilder stringBuilder1 = new StringBuilder();
        for (int i = 0; i < shapeWindowsCordination1.length(); i++) {
            char c = shapeWindowsCordination.charAt(i);
            if (Character.isDigit(c)){
                stringBuilder1.append(Integer.valueOf(c));
            }else if (",".equals(c)){
                stringBuilder.append(" ");
                continue;
            }
        }

        String s1 = stringBuilder1.toString();
        String[] split1 = s1.split("\\s");
        ArrayList<Integer> integers1 = new ArrayList<>();
        for (int i = 0; i < split1.length; i++) {
            integers1.add(Integer.valueOf(split1[i]));
        }

        Integer integer = integers.get(0);
        Integer integer1 = integers1.get(1);
        int i = integer * integer1;
        Integer integer2 = integers.get(0);
        Integer integer3 = integers1.get(1);
        int i1 = integer2 * integer3;
        int i2=0;
        if (i>i1){
            i2 = i - i1;
        }else {
            i2 = i1 - i;
        }
        double sqrtDistance = Math.sqrt(i2);

        return sqrtDistance;
    }

    public static Double getShortestDistance(Shape shapeFir, Shape shapeSec){
        if (shapeFir==null){
            return null;
        }
        if (shapeFir.getShapeWindowsCordination()==null){
            return null;
        }
        if (shapeFir.getShapeWindowsCordination().isEmpty()){
            return null;
        }
        if (shapeSec==null){
            return null;
        }
        if (shapeSec.getShapeWindowsCordination()==null){
            return null;
        }
        if (shapeSec.getShapeWindowsCordination().isEmpty()){
            return null;
        }

        //Random random = new Random(); //获取图形中的随机点
        ArrayList<Double> doubles = new ArrayList<>();
        int count=0;
        while (true){
            Double shapeDistance = getShapeDistance(shapeFir, shapeSec); // 计算随机点的两个坐标之间的距离
            doubles.add(shapeDistance);
            if (count>1000000){
                break;
            }
            count++;
        }

        doubles.sort(new Comparator<Double>() {
            @Override
            public int compare(Double o1, Double o2) {
                if(o1>o2){
                    return -1;
                }else if(o1<o2){
                    return 1;
                }else {
                    return 0;
                }
            }
        });

        Double minDistance = doubles.get(0);
        return minDistance;
    }
}

/**
 * 圆形、矩形、六边形、五边形、跑道形/胶囊形
 */
enum ShapeEnum{
    CIRCLER(1,"圆形",""),
    RECTANGLE(2,"矩形",""),
    SIXEDGESHAQUARE(3,"六边形",""),
    FIVEEDGESHAPE(4,"五边形",""),
    RUNNINGCIRCLE(5,"跑道形","")
    ;

    ShapeEnum(int index, String name, String desc) {
        this.index = index;
        this.name = name;
        this.desc = desc;
    }

    private int index;
    private String name;
    private String desc;

    public int getIndex() {
        return index;
    }

    public void setIndex(int index) {
        this.index = index;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }
}
class Windows{
    private String id;
    private String windowsCordination; // 所定义的视窗窗口windows的坐标位置 (*,*)

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getWindowsCordination() {
        return windowsCordination;
    }

    public void setWindowsCordination(String windowsCordination) {
        this.windowsCordination = windowsCordination;
    }
}

class Shape{
    private String id;
    private String name; //图形的形状
    private String shapeWindowsCordination; // 图形的形状放在视窗windows中的相对坐标 "(1,3)" , 视窗矩形的坐标

    private String windows_id;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getWindows_id() {
        return windows_id;
    }

    public void setWindows_id(String windows_id) {
        this.windows_id = windows_id;
    }

    public String getShapeWindowsCordination() {
        return shapeWindowsCordination;
    }

    public void setShapeWindowsCordination(String shapeWindowsCordination) {
        this.shapeWindowsCordination = shapeWindowsCordination;
    }
}
相关推荐
2601_962071573 小时前
【Java报错已解决】org.springframework.beans.factory.BeanCreationException
java·开发语言
zww89491113 小时前
酒馆预约系统开发实战:从需求分析到上线全流程指南
java·eclipse
淡海水4 小时前
07-04-并发-ConcurrentBag-T-工作窃取WorkStealing算法
开发语言·算法·c#·bag·concurrent·workstealing
leobertlan4 小时前
好玩系列:训练一个神经网络模型指导小孩玩游戏2-大局观教练
算法
CS_Zero5 小时前
C语言sizeof是函数吗?
c语言·开发语言
东风破_5 小时前
从跨域到 WebSocket:前端跨域方案、SSE 与双向实时通信详解
前端·后端
Tisfy5 小时前
LeetCode 3876.构造奇偶一致的数组 II:三种情况分类讨论(其实还是脑筋急转弯)
算法·leetcode·题解·脑筋急转弯
-今昭-5 小时前
《V2V 迁移实战:将 VMware 虚拟机转为 OpenStack 可用 Glance qcow2 镜像》
开发语言·python
乐迪信息5 小时前
智慧港口船舶AI算法实现在线状态监测
大数据·人工智能·深度学习·算法·计算机视觉
zww89491116 小时前
匿名树洞系统开发实战:从需求分析到部署指南
java·eclipse