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