创建grah for java应用程序的图形
enter code here
我正在构建一个应用程序,它实现了A*算法来计算两个房间之间的路由。我正在尝试创建一个算法可以操作的图形,我不确定这是否是正确的方法。以下是我迄今为止所做的工作:
向量2。爪哇:
package myalgorithm;
public class Vect2 {
private int x;
private int y;
public Vect2(int x,int y ){
this.x = x;
this.y = y;
}
}
节点。爪哇
package myalgorithm;
public class Node {
Node parent;
Vect2 vector;
public int x;
public int y;
public double f;
public double g;
public double h;
public Node( int x,int y,Node parent, double g,double h){
this.x = x;
this.y = y;
this.parent= parent;
this.g = g;
this.h = h;
this.f= this.g + this.h;
}
}
NodeGraph.java
package myalgorithm;
import java.util.Arrays;
public class NodeGraph {
Node a = new Node(88,623,null,0,0);
Node b = new Node(727,627,null,0,0);
Node c = new Node(723,93,null,0,0);
Node d = new Node(90,92,null,0,0);
Node e = new Node(94,349,null,0,0);
Node f = new Node(397,358,null,0,0);
Node g = new Node(722,339,null,0,0);
Node[] arr = new Node[7];
public NodeGraph init(){
arr[0]= a;
arr[0]= b;
arr[0]= c;
arr[0]= d;
arr[0]= e;
arr[0]= f;
arr[0]= g;
return this;
}
public void createMatrx(){
boolean[][] matrix = new boolean[7][];
for (int i=0; i<matrix.length; i++) matrix[i] = new boolean[7];
int nodeA = Arrays.binarySearch(arr, a);
int nodeB = Arrays.binarySearch(arr, b);
int nodeC = Arrays.binarySearch(arr, c);
int nodeD = Arrays.binarySearch(arr, d);
int nodeE = Arrays.binarySearch(arr, e);
int nodeF = Arrays.binarySearch(arr, f);
int nodeG = Arrays.binarySearch(arr, g);
matrix[nodeA][nodeB] = true;
matrix[nodeA][nodeE] = true;
matrix[nodeB][nodeA] = true;
matrix[nodeB][nodeG] = true;
// A is connected to D
matrix[nodeC][nodeG] = true;
matrix[nodeC][nodeD] = true;
// B is connected to D
matrix[nodeD][nodeC] = true;
matrix[nodeD][nodeE] = true;
matrix[nodeE][nodeD] = true;
// C is connected to D
matrix[nodeE][nodeF] = true;
matrix[nodeE][nodeA] = true;
matrix[nodeF][nodeE] = true;
matrix[nodeE][nodeG] = true;
matrix[nodeG][nodeF] = true;
matrix[nodeE][nodeB] = true;
matrix[nodeE][nodeC] = true;
matrix[nodeD][nodeC] = true;
}
}
如有任何改进建议,我们将不胜感激。谢谢大家!
# 1 楼答案
我不完全明白你在那里做什么,但有几件事让我印象深刻:
所有被分配给arr[0]
您可能希望将
arr
的创建替换为:这还包括:
您可以这样做: