简体   繁体   English

在Java Swing中计算JPanel中两点之间的路径的算法

[英]Algorithm to calculate a way between two points in a JPanel in Java Swing

First of all, I don't want an algorithm like A*, Dijkstra or something like that, because this algorithms calculate the best and shortest way. 首先,我不想要像A *,Dijkstra这样的算法,因为这种算法可以计算最佳和最短的方法。 I have two points in a JPanel and I have to connect them with a cable that is an array of points. 我在JPanel中有两个点,我必须使用一个点数组的电缆连接它们。 The only directions permitted are up, down, left and right, and the cable must have the minimum number of direction changes . 允许的唯一方向是向上,向下,向左和向右,并且电缆必须具有最小数量的方向变化 This is the most important requirement, and of course the algorithm don't calculate the shortest path. 这是最重要的要求,当然算法不计算最短路径。 How can I do it? 我该怎么做? There is an algorithm similar to this? 有一个与此类似的算法吗?

Thanks! 谢谢!

I think you should look again at conventional shortest path algorithms. 我认为你应该再看一下传统的最短路径算法。 The weight associated with an edge need not have anything at all to do with physical distance. 与边缘相关的重量不需要与物理距离有任何关系。

Build a graph that has the potential corners as vertices, an edge for each straight line connection between vertices, and weight one for each edge. 构建一个图形,其中潜在的角为顶点,顶点之间的每条直线连接的边,每个边的权重为1。 The path you want is the "shortest" path through that graph. 您想要的路径是通过该图表的“最短”路径。

Isn't the word "algorithm" too fancy for this simple problem? 对于这个简单的问题,“算法”这个词是不是太花哨了? The shortest path between A(X1,Y1) and B(X2,Y2) goes though either C(X1,Y2) or D(X2,Y1) , the distance being the same either way, in case X1 != X2 && Y1 != Y2 but if either coordinate is the same between the two points, it's a straight line with no break. A(X1,Y1) and B(X2,Y2)之间的最短路径可以通过C(X1,Y2) or D(X2,Y1) ,两种方式的距离相同,以防X1 != X2 && Y1 != Y2但是如果两个点之间的任一坐标相同,则它是一条没有中断的直线。 The length of the path is abs(X2-X1)+abs(Y2-Y1) . 路径的长度是abs(X2-X1)+abs(Y2-Y1)

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

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