简体   繁体   English

绘制图形的最短路径

[英]Plotting Shortest Path to graph

I have a set of coordinates and would like to find the shortest path from the coordinates closest to x=0,y=0 to the coordinate that is furthest away from it and display it on the graph.我有一组坐标,想找到从最接近 x=0,y=0 的坐标到离它最远的坐标的最短路径,并将其显示在图形上。 Is there anyway I could do that?反正我能做到吗?

Here are coordinates of interest:以下是感兴趣的坐标:

23 292
78 275
187 81
188 430
198 150
204 180
222 245
223 175
226 334
255 344
263 213
266 261
286 163
301 266
328 206
352 42
363 169
379 177
385 187
394 211
400 254
401 199
412 335
420 371
434 176
449 174
457 230
472 136

In addition to this, would it be possible to find the diameter of the graph (from a to b)too?除此之外,是否也可以找到图形的直径(从 a 到 b)?

This should get you started.这应该让你开始。 I haven't made it pretty in any way, but the basics are there for you to build on.我没有以任何方式使它变得漂亮,但是您可以在此基础上进行构建。

import matplotlib.pyplot as plt
import numpy as np

xy = np.array([[23, 292], [78, 275], [187, 81], [188, 430], [198, 150], [204, 180],         
              [222, 245], [223, 175], [226, 334], [255, 344], [263, 213], [266, 261], 
              [286, 163], [301, 266], [328, 206], [352, 42], [363, 169], [379, 177], 
              [385, 187], [394, 211], [400, 254], [401, 199], [412, 335], [420, 371],
              [434, 176], [449, 174], [457, 230], [472, 136]])
lengths = np.linalg.norm(xy, axis=1)
minindex = np.argmin(lengths)
maxindex = np.argmax(lengths)
diameter = lengths[maxindex] - lengths[minindex]
linex = (xy[minindex, 0], xy[maxindex, 0])
liney = (xy[minindex, 1], xy[maxindex, 1])
plt.scatter(xy[:, 0], xy[:, 1])
plt.plot(linex, liney)
plt.show()

Not totally sure about your diameter question but I had an idea.不完全确定你的diameter问题,但我有一个想法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM