gpt4 book ai didi

java - 为 Java 应用程序创建图形

转载 作者:太空宇宙 更新时间:2023-11-04 06:12:21 25 4
gpt4 key购买 nike

在此处输入代码我正在构建一个应用程序,该应用程序实现 A* 算法来计算两个房间之间的路线。我正在尝试创建一个算法可以操作的图表,但我不确定这是否是正确的方法。这是我到目前为止所做的:

Vect2.java:

package myalgorithm;

public class Vect2 {

private int x;
private int y;


public Vect2(int x,int y ){

this.x = x;
this.y = y;

}

}

Node.java

    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;
}
}

任何有关改进的建议将不胜感激。谢谢!

最佳答案

不完全理解你在那里做什么,但有几件事让我印象深刻:

arr[0]= a; arr[0]= b; arr[0]= c; arr[0]= d; arr[0]= e; arr[0]= f; arr[0]= g;

全部分配给arr[0]。
您可能希望将 arr 的创建替换为:

Node[] arr = new Node[]{a,b,c,d,e,f,g};

这也是:

boolean[][] matrix = new boolean[7][];
for (int i=0; i<matrix.length; i++) matrix[i] = new boolean[7];

你可以这样做:

    boolean[][] matrix = new boolean[7][7];

关于java - 为 Java 应用程序创建图形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28540660/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com