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


    使用Python计算平面多边形间最短距离,数据需要从excel表格中导入,
    * 多边形种类包括(圆形、矩形、六边形、五边形、跑道形/胶囊形),
    * Python代码需要使用gjk算法进行判断两个多边形间是否重叠,
    * 如果未重叠计算最短距离
    1. package controller.com.codermart.controller;
    2. import java.util.ArrayList;
    3. import java.util.Comparator;
    4. import java.util.Random;
    5. /**
    6. * Created by Lenovo on 2023/10/16.
    7. */
    8. public class PythonAlgorithm {
    9. public static void main(String[] args) {
    10. int testVar=1;
    11. switch (testVar){
    12. case 1:
    13. break;
    14. case 2:
    15. break;
    16. }
    17. }
    18. /**
    19. * 使用Python计算平面多边形间最短距离,数据需要从excel表格中导入,
    20. * 多边形种类包括(圆形、矩形、六边形、五边形、跑道形/胶囊形),
    21. * Python代码需要使用gjk算法进行判断两个多边形间是否重叠,
    22. * 如果未重叠计算最短距离
    23. * @param shapeFir
    24. * @param shapeSec
    25. * @return
    26. */
    27. public static Double getShapeDistance(Shape shapeFir,Shape shapeSec){
    28. if (shapeFir==null){
    29. return null;
    30. }
    31. if (shapeFir.getShapeWindowsCordination()==null){
    32. return null;
    33. }
    34. if (shapeFir.getShapeWindowsCordination().isEmpty()){
    35. return null;
    36. }
    37. if (shapeSec==null){
    38. return null;
    39. }
    40. if (shapeSec.getShapeWindowsCordination()==null){
    41. return null;
    42. }
    43. if (shapeSec.getShapeWindowsCordination().isEmpty()){
    44. return null;
    45. }
    46. // String name = ShapeEnum.CIRCLER.getName();
    47. String shapeWindowsCordination = shapeFir.getShapeWindowsCordination();
    48. String shapeWindowsCordination1 = shapeSec.getShapeWindowsCordination();
    49. StringBuilder stringBuilder = new StringBuilder();
    50. for (int i = 0; i < shapeWindowsCordination.length(); i++) {
    51. char c = shapeWindowsCordination.charAt(i);
    52. if (Character.isDigit(c)){
    53. stringBuilder.append(Integer.valueOf(c));
    54. }else if (",".equals(c)){
    55. stringBuilder.append(" ");
    56. continue;
    57. }
    58. }
    59. String s = stringBuilder.toString();
    60. String[] split = s.split("\\s");
    61. ArrayList integers = new ArrayList<>();
    62. for (int i = 0; i < split.length; i++) {
    63. integers.add(Integer.valueOf(split[i]));
    64. }
    65. StringBuilder stringBuilder1 = new StringBuilder();
    66. for (int i = 0; i < shapeWindowsCordination1.length(); i++) {
    67. char c = shapeWindowsCordination.charAt(i);
    68. if (Character.isDigit(c)){
    69. stringBuilder1.append(Integer.valueOf(c));
    70. }else if (",".equals(c)){
    71. stringBuilder.append(" ");
    72. continue;
    73. }
    74. }
    75. String s1 = stringBuilder1.toString();
    76. String[] split1 = s1.split("\\s");
    77. ArrayList integers1 = new ArrayList<>();
    78. for (int i = 0; i < split1.length; i++) {
    79. integers1.add(Integer.valueOf(split1[i]));
    80. }
    81. Integer integer = integers.get(0);
    82. Integer integer1 = integers1.get(1);
    83. int i = integer * integer1;
    84. Integer integer2 = integers.get(0);
    85. Integer integer3 = integers1.get(1);
    86. int i1 = integer2 * integer3;
    87. int i2=0;
    88. if (i>i1){
    89. i2 = i - i1;
    90. }else {
    91. i2 = i1 - i;
    92. }
    93. double sqrtDistance = Math.sqrt(i2);
    94. return sqrtDistance;
    95. }
    96. public static Double getShortestDistance(Shape shapeFir, Shape shapeSec){
    97. if (shapeFir==null){
    98. return null;
    99. }
    100. if (shapeFir.getShapeWindowsCordination()==null){
    101. return null;
    102. }
    103. if (shapeFir.getShapeWindowsCordination().isEmpty()){
    104. return null;
    105. }
    106. if (shapeSec==null){
    107. return null;
    108. }
    109. if (shapeSec.getShapeWindowsCordination()==null){
    110. return null;
    111. }
    112. if (shapeSec.getShapeWindowsCordination().isEmpty()){
    113. return null;
    114. }
    115. //Random random = new Random(); //获取图形中的随机点
    116. ArrayList doubles = new ArrayList<>();
    117. int count=0;
    118. while (true){
    119. Double shapeDistance = getShapeDistance(shapeFir, shapeSec); // 计算随机点的两个坐标之间的距离
    120. doubles.add(shapeDistance);
    121. if (count>1000000){
    122. break;
    123. }
    124. count++;
    125. }
    126. doubles.sort(new Comparator() {
    127. @Override
    128. public int compare(Double o1, Double o2) {
    129. if(o1>o2){
    130. return -1;
    131. }else if(o1
    132. return 1;
    133. }else {
    134. return 0;
    135. }
    136. }
    137. });
    138. Double minDistance = doubles.get(0);
    139. return minDistance;
    140. }
    141. }
    142. /**
    143. * 圆形、矩形、六边形、五边形、跑道形/胶囊形
    144. */
    145. enum ShapeEnum{
    146. CIRCLER(1,"圆形",""),
    147. RECTANGLE(2,"矩形",""),
    148. SIXEDGESHAQUARE(3,"六边形",""),
    149. FIVEEDGESHAPE(4,"五边形",""),
    150. RUNNINGCIRCLE(5,"跑道形","")
    151. ;
    152. ShapeEnum(int index, String name, String desc) {
    153. this.index = index;
    154. this.name = name;
    155. this.desc = desc;
    156. }
    157. private int index;
    158. private String name;
    159. private String desc;
    160. public int getIndex() {
    161. return index;
    162. }
    163. public void setIndex(int index) {
    164. this.index = index;
    165. }
    166. public String getName() {
    167. return name;
    168. }
    169. public void setName(String name) {
    170. this.name = name;
    171. }
    172. public String getDesc() {
    173. return desc;
    174. }
    175. public void setDesc(String desc) {
    176. this.desc = desc;
    177. }
    178. }
    179. class Windows{
    180. private String id;
    181. private String windowsCordination; // 所定义的视窗窗口windows的坐标位置 (*,*)
    182. public String getId() {
    183. return id;
    184. }
    185. public void setId(String id) {
    186. this.id = id;
    187. }
    188. public String getWindowsCordination() {
    189. return windowsCordination;
    190. }
    191. public void setWindowsCordination(String windowsCordination) {
    192. this.windowsCordination = windowsCordination;
    193. }
    194. }
    195. class Shape{
    196. private String id;
    197. private String name; //图形的形状
    198. private String shapeWindowsCordination; // 图形的形状放在视窗windows中的相对坐标 "(1,3)" , 视窗矩形的坐标
    199. private String windows_id;
    200. public String getId() {
    201. return id;
    202. }
    203. public void setId(String id) {
    204. this.id = id;
    205. }
    206. public String getName() {
    207. return name;
    208. }
    209. public void setName(String name) {
    210. this.name = name;
    211. }
    212. public String getWindows_id() {
    213. return windows_id;
    214. }
    215. public void setWindows_id(String windows_id) {
    216. this.windows_id = windows_id;
    217. }
    218. public String getShapeWindowsCordination() {
    219. return shapeWindowsCordination;
    220. }
    221. public void setShapeWindowsCordination(String shapeWindowsCordination) {
    222. this.shapeWindowsCordination = shapeWindowsCordination;
    223. }
    224. }

     

  • 相关阅读:
    防止消息丢失与消息重复——Kafka可靠性分析及优化实践
    [s905l3]性价比神机mgv3000全网首拆,刷armbian实现更多价值!
    【计网】传输层
    渗透测试中的前端调试(上)
    Lucene和Solr和Elasticsearch区别,全文检索引擎工具包Lucene索引流程和搜索流程实操
    【Linux】软硬链接与动静态库(理解软硬链接的特点及使用场景、如何建立动静态库与使用第三方库)
    黑盒测试用例设计 - 因果图法
    求学之路五、六月的Review
    认识thinkphp框架
    【Spark 实战系列】Phoenix 整合 spark 进行查询分析
  • 原文地址:https://blog.csdn.net/wanzhong_liao/article/details/133875218