如何显示并获得方向与A *方向

2023-09-11 00:20:35 作者:雾锁深情目

我有一个寻路算法这是工作,但问题是,我不能让我的主类的方向。换句话说,每一次我wan't,显示方向是输出空值。

这是我的code。

Astar.java

 包路径;

进口的java.util.ArrayList;
进口java.util.Collections中;

进口path.Node.Directions;
进口map.Area;

公共类亚士都{

    公众的ArrayList<节点>打开;
    公众的ArrayList<节点>关闭;
    公共区域面积;
    公共节点启动;
    公共节点完成;

    公共阿斯达(){
        this.area =新所在区域(5,5);
        this.start =新节点(1,1,区);
        this.finish =新节点(4,4,区);
        this.open =新的ArrayList<节点>();
        this.closed =新的ArrayList<节点>();
    }

    公共无效计算(){

        start.distanceFromStart = 0;
        closed.clear();
        open.clear();
        open.add(开始);
        节点当前=无效;

        而(open.size()!= 0){
            电流= open.get(0);
            如果(current.equals(完成)){
                打破;
            }
            open.remove(电流);
            closed.add(电流);
            为(节点N:current.getNeighborsList()){
                布尔neighborIsBetter;

                如果(closed.contains(n))的
                    继续;

                如果(!n.isObstacle){
                    双neighborDistanceFromStart = current.distanceFromStart + getDistanceBetween(电流,N);
                    如果(!open.contains(正)){
                        open.add(N);
                        Collections.sort(开);
                        neighborIsBetter = TRUE;
                    }
                    否则,如果(neighborDistanceFromStart< current.distanceFromStart)
                        neighborIsBetter = TRUE;
                    其他
                        neighborIsBetter = FALSE;
                    如果(neighborIsBetter){
                        n.parent =电流;
                        搜索路径(电流);
                        n.distanceFromStart = neighborDistanceFromStart;
                        n.heuristicDistanceFromGoal = getEstimatedDistanceToGoal(N,完成);
                    }
                    的System.out.println(距离从开始:+ neighborDistanceFromStart);
                }
                否则,如果(n.isObstacle){
                    closed.add(N);
                }

            }

        }
        如果(电流!= NULL)
            reconstructPath(电流);
    }
    公共无效搜索路径(节点node){

        System.out.printf(\ nNode:(%D,%D)\ N,node.x,node.y);

    }

    公共无效reconstructPath(节点node){

        而(!(node.parent == NULL)){
            System.out.printf(\ nNode:(%D,%D)\ N,node.x,node.y);
            节点= node.parent;

        }
    }

    私人双人getDistanceBetween(节点N1,节点N2){
        如果((n1.x == n2.x)||(n1.y == n2.y))
            返回1;
        其他
            返回1.9;
    }

    私人双人getEstimatedDistanceToGoal(节点开始,节点完成){
        //曼哈顿计算最接近的启发式
        双DX = start.x  -  finish.x;
        双DY = start.y  -  finish.y;
        返回DX + DY;

    }

    公共静态无效的主要(字串[] A){
        爱仕达爱仕达=新阿斯达();
        astar.calculate();
        的System.out.println(结束);
    }
}
 

Node.java

 包路径;

进口的java.util.ArrayList;
进口map.Area;

公共类节点实现可比<节点> {
    公共布尔isClosed返;
    公共布尔isGoal;
    公共布尔isObstacle;
    公共布尔isVisited;
    公共布尔ISSTART;
    公共节点父;
    公共节点北;
    公共节点南;
    公共节点东侧;
    公共节点西侧;

    公共区域的地图;

    公众诠释X;
    公众诠释Ÿ;

    公共双heuristicDistanceFromGoal;
    公共双distanceFromStart;

    公共枚举方向{
        北,南,东,西
    };

    公共节点(INT X,INT Y,区域地图){
        this.x = X;
        this.y = Y;
        this.isVisited = FALSE;
        this.distanceFromStart = Integer.MAX_VALUE的;
        this.isObstacle = FALSE;
        this.isStart = FALSE;
        this.isGoal = FALSE;
        this.map =图;
    }

    公共布尔等于(节点node){
        返回(this.x == node.x)及和放大器; (this.y == node.y);
    }

    公共无效setNode(路线目录,节点node){
        节点温度= getDirectionalNode(DIR);
        ArrayList的<节点>表= this.getNeighborsList();
        如果(list.contains(临时))
            list.remove(临时);
        list.add(节点);
        setDirectionalNode(DIR,节点);
    }

    公共节点getDirectionalNode(方向DIR){
        开关(DIR){
            案例NORTH:
                返回北部;
            案例南:
                回南;
            案例EAST:
                返回东;
            案件WEST:
                返回西边。
        }
        返回null;
    }

    公共无效setDirectionalNode(路线目录,节点node){
        开关(DIR){
            案例NORTH:
                this.north =节点;
            案例南:
                this.south =节点;
            案例EAST:
                this.east =节点;
            案件WEST:
                this.west =节点;
        }
    }

    公众的ArrayList<节点> getNeighborsList(){
        ArrayList的<节点> neighborList =新的ArrayList<节点>();

        如果(!(γ==(map.height + 1))){
            neighborList.add(map.getNode(X,Y + 1));
        }

        返回neighborList;
    }

    @覆盖
    公众诠释的compareTo(节点除外){
        双totalDistanceFromGoal = this.distanceFromStart  -  this.heuristicDistanceFromGoal;
        双otherDistanceFromGoal = other.distanceFromStart  -  other.heuristicDistanceFromGoal;
        如果(totalDistanceFromGoal< otherDistanceFromGoal)
            返回-1;
        如果(otherDistanceFromGoal> totalDistanceFromGoal)
            返回1;
        返回0;
    }

}
 

Area.java

 包图;

进口的java.util.ArrayList;
进口path.Node;

公共类区{
    公众诠释的宽度;
    公众诠释的高度;
    公众的ArrayList< ArrayList的<节点>>地图;

    公共区(INT W,INT高){
        this.width = W;
        this.height = H;

        createMap();

    }

    公共节点getNode(INT X,int y)对{
        返回map.get(Y)获得(X);
    }

    私人无效createMap(){
        节点节点;
        图=新的ArrayList< ArrayList的<节点>>();
        对于(INT Y = 0; Y<高度; Y ++){
            map.add(新的ArrayList&其中;节点>());
            为(中间体X = 0 X  - 其中;宽度; X ++){
                节点=新节点(X,Y,这一点);
                map.get(y)的。新增(节点);
            }
        }
    }

}
 
在 ABC中,AB BC,将 ABC绕点A沿顺时针方向旋转得 A1B1C1,使点Cl落在直线BC上 点Cl与点C不重合 ,

输出:

 回溯:(4,4)回溯:(4,3)回溯:(4,2)
回溯:(4,1)回溯:(3,1)回溯:(2,1)
 

问题:没有方向:我得到空当我打电话node.north

解决方案

 公共节点父;
公共节点北;
公共节点南;
公共节点东侧;
公共节点西侧;

公共枚举方向{
    北,南,东,西
};
 

这似乎只是奇怪。我想你想做到这一点:

 公共节点父;
公共向北方向;
大众南下方向;
公共方向东;
公共向西方向;

公共枚举方向{
    北,南,东,西
};
 

整个事情混淆了我一点点,你永远不设置轴承。节点通常没有一个方向。 如果希望在当前化身从最后一个节点走上当前节点的方向。因此,它的轴承基本上是这样。 这可能帮助? http://mathforum.org/library/drmath/view/55417.html

I have a Pathfinding algorithm which is working but the problem is that i can't get the directions on my main class. in other words every time i wan't to display the directions it output null values.

here's my code.

Astar.java

package path;

import java.util.ArrayList;
import java.util.Collections;

import path.Node.Directions;
import map.Area;

public class Astar {

    public ArrayList<Node> open;
    public ArrayList<Node> closed;
    public Area area;
    public Node start;
    public Node finish;

    public Astar() {
        this.area = new Area(5, 5);
        this.start = new Node(1, 1, area);
        this.finish = new Node(4, 4, area);
        this.open = new ArrayList<Node>();
        this.closed = new ArrayList<Node>();
    }

    public void calculate() {

        start.distanceFromStart = 0;
        closed.clear();
        open.clear();
        open.add(start);
        Node current = null;

        while (open.size() != 0) {
            current = open.get(0);
            if (current.equals(finish)) {
                break;
            }
            open.remove(current);
            closed.add(current);
            for (Node n : current.getNeighborsList()) {
                boolean neighborIsBetter;

                if (closed.contains(n))
                    continue;

                if (!n.isObstacle) {
                    double neighborDistanceFromStart = current.distanceFromStart + getDistanceBetween(current, n);
                    if (!open.contains(n)) {
                        open.add(n);
                        Collections.sort(open);
                        neighborIsBetter = true;
                    }
                    else if (neighborDistanceFromStart < current.distanceFromStart)
                        neighborIsBetter = true;
                    else
                        neighborIsBetter = false;
                    if (neighborIsBetter) {
                        n.parent = current;
                        searchPath(current);
                        n.distanceFromStart = neighborDistanceFromStart;
                        n.heuristicDistanceFromGoal = getEstimatedDistanceToGoal(n, finish);
                    }
                    System.out.println("Distance from start: " + neighborDistanceFromStart);
                }
                else if(n.isObstacle){
                    closed.add(n);
                }

            }

        }
        if (current != null)
            reconstructPath(current);
    }
    public void searchPath(Node node){

        System.out.printf("\nNode: (%d, %d)\n", node.x, node.y);

    }

    public void reconstructPath(Node node) {

        while (!(node.parent == null)) {
            System.out.printf("\nNode: (%d, %d)\n", node.x, node.y);
            node = node.parent;

        }
    }

    private double getDistanceBetween(Node n1, Node n2) {
        if ((n1.x == n2.x) || (n1.y == n2.y))
            return 1;
        else
            return 1.9;
    }

    private double getEstimatedDistanceToGoal(Node start, Node finish) {
        //Manhattan Calculation closest heuristic
        double dx = start.x - finish.x;
        double dy = start.y - finish.y;
        return dx + dy;

    }

    public static void main(String[] a) {
        Astar astar = new Astar();
        astar.calculate();
        System.out.println("End");
    }
}

Node.java

package path;

import java.util.ArrayList;
import map.Area;

public class Node implements Comparable<Node> {
    public boolean isClosed;
    public boolean isGoal;
    public boolean isObstacle;
    public boolean isVisited;
    public boolean isStart;
    public Node parent;
    public Node north;
    public Node south;
    public Node east;
    public Node west;

    public Area map;

    public int x;
    public int y;

    public double heuristicDistanceFromGoal;
    public double distanceFromStart;

    public enum Directions {
        NORTH, SOUTH, EAST, WEST
    };

    public Node(int x, int y, Area map) {
        this.x = x;
        this.y = y;
        this.isVisited = false;
        this.distanceFromStart = Integer.MAX_VALUE;
        this.isObstacle = false;
        this.isStart = false;
        this.isGoal = false;
        this.map = map;
    }

    public boolean equals(Node node) {
        return (this.x == node.x) && (this.y == node.y);
    }

    public void setNode(Directions dir, Node node) {
        Node temp = getDirectionalNode(dir);
        ArrayList<Node> list = this.getNeighborsList();
        if (list.contains(temp))
            list.remove(temp);
        list.add(node);
        setDirectionalNode(dir, node);
    }

    public Node getDirectionalNode(Directions dir) {    
        switch (dir) {
            case NORTH:
                return north;
            case SOUTH:
                return south;
            case EAST:
                return east;
            case WEST:
                return west;        
        }
        return null;
    }

    public void setDirectionalNode(Directions dir, Node node) { 
        switch (dir) {
            case NORTH:
                this.north = node;
            case SOUTH:
                this.south = node;
            case EAST:
                this.east = node;
            case WEST:
                this.west = node;
        }
    }

    public ArrayList<Node> getNeighborsList() {
        ArrayList<Node> neighborList = new ArrayList<Node>();

        if (!(y == (map.height + 1))) {
            neighborList.add(map.getNode(x, y + 1));
        }

        return neighborList;
    }

    @Override
    public int compareTo(Node other) {
        double totalDistanceFromGoal = this.distanceFromStart - this.heuristicDistanceFromGoal;
        double otherDistanceFromGoal = other.distanceFromStart - other.heuristicDistanceFromGoal;
        if (totalDistanceFromGoal < otherDistanceFromGoal)
            return -1;
        if (otherDistanceFromGoal > totalDistanceFromGoal)
            return 1;
        return 0;
    }

}

Area.java

package map;

import java.util.ArrayList;
import path.Node;

public class Area {
    public int width;
    public int height;
    public ArrayList<ArrayList<Node>> map;

    public Area(int w, int h) {
        this.width = w;
        this.height = h;

        createMap();

    }

    public Node getNode(int x, int y) {
        return map.get(y).get(x);
    }

    private void createMap() {
        Node node;
        map = new ArrayList<ArrayList<Node>>();
        for (int y = 0; y < height; y++) {
            map.add(new ArrayList<Node>());
            for (int x = 0; x < width; x++) {
                node = new Node(x, y, this);
                map.get(y).add(node);
            }
        }
    }

}

output:

Traceback: (4, 4) Traceback: (4, 3) Traceback: (4, 2) 
Traceback: (4, 1) Traceback: (3, 1)Traceback: (2, 1)

Problem: No directions: I get null when i call node.north

解决方案

public Node parent;
public Node north;
public Node south;
public Node east;
public Node west;

public enum Directions {
    NORTH, SOUTH, EAST, WEST
};

This seems just strange. I guess you wanted to do this:

public Node parent;
public Direction north;
public Direction south;
public Direction east;
public Direction west;

public enum Direction {
    NORTH, SOUTH, EAST, WEST
};

The whole thing confuses me a little, you never set the bearing. Nodes usually don't have a direction. If you want the direction that the current avatar took from the last node to the current node. So its bearing basically. This might help? http://mathforum.org/library/drmath/view/55417.html