簡體   English   中英

繪制圖形的最短路徑

[英]Plotting Shortest Path to graph

我有一組坐標,想找到從最接近 x=0,y=0 的坐標到離它最遠的坐標的最短路徑,並將其顯示在圖形上。 反正我能做到嗎?

以下是感興趣的坐標:

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

除此之外,是否也可以找到圖形的直徑(從 a 到 b)?

這應該讓你開始。 我沒有以任何方式使它變得漂亮,但是您可以在此基礎上進行構建。

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()

不完全確定你的diameter問題,但我有一個想法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM