关于排序路径的想法?

时间:2018-07-28 23:11:02

标签: java coordinates minecraft bukkit

我正在尝试按接近程度将点列表排序,但是我能想到的唯一方法是通过迭代它们并获取当前存储的项目与我所存储的项目之间的绝对值距离进行迭代,如果新坐标和当前坐标之差的绝对值大于0但不大于2(查找相邻坐标),则将该点设为当前。

但是,我遇到了示波器问题,因此我在这里要做些什么。我不是通过增加x,y或z值来对它们进行排序-现在,我正在测试半径为1的数据-因此,更传统的排序方法在这里行不通。

有人有指针吗?

package pickquest.common;

import java.util.ArrayList;
import java.util.HashMap;

import org.bukkit.Location;

public class Path {
    private HashMap<Integer, Location> pathSequence = new HashMap<>();

    public HashMap<Integer, Location> getPathSequence() {
        return pathSequence;
    }

    public void setPathSequence(HashMap<Integer, Location> pathSequence) {
        this.pathSequence = pathSequence;
    }

    public void setsequencePath(Location origin, ArrayList<Location> points, Location destination) {
        sequencePath(origin, points, destination);
    }

    private void sequencePath(Location origin, ArrayList<Location> points, Location destination) {
        Location current = origin;
        ArrayList<Location> processedPoints = new ArrayList<>();
        processedPoints.addAll(points);
        while (!(processedPoints.isEmpty())) {
            Integer index = 0;

            for (int i = 0; i < points.size(); i++) {

                if ((Math.abs(current.getX() - processedPoints.get(i).getX()) > 0) && (Math.abs(current.getX() - processedPoints.get(i).getX()) <= 1.9)) {
                    if (!(pathSequence.containsValue(processedPoints.get(i)))) {
                        getPathSequence().put(index, processedPoints.get(i));
                        processedPoints.remove(i);
                        break;
                    }
                } else if ((Math.abs(current.getY() - processedPoints.get(i).getY()) > 0) && (Math.abs(current.getY() - processedPoints.get(i).getY()) <= 1.9)) {
                    if (!(pathSequence.containsValue(processedPoints.get(i)))) {
                        getPathSequence().put(index, processedPoints.get(i));
                        processedPoints.remove(i);
                        break;
                    }
                } else if ((Math.abs(current.getY() - processedPoints.get(i).getY()) > 0) && (Math.abs(current.getY() - processedPoints.get(i).getY()) <= 1.9)) {
                    if (!(pathSequence.containsValue(processedPoints.get(i)))) {
                        getPathSequence().put(index, processedPoints.get(i));
                        processedPoints.remove(i);
                        break;
                    }
                }

                processedPoints.remove(i);
                index++;
            }
        }

        for (int i = 0; i < pathSequence.size(); i++) {
            System.out.println(i + ": " + pathSequence.get(i));
        }
    }


}

2 个答案:

答案 0 :(得分:0)

您可能使用TreeMap并根据距离对位置进行排序,然后使用TreeMap的标题/尾部功能。例如:

public class Path {

    private final Iterable<Location> points;
    public Path(Iterable<Location> points) {
        this.points= points;
    }

    public TreeMap<Double, Location> getNearby(Location from) {
        TreeMap<Double, Location> nearby = new TreeMap<>();
        for (Location point: points) {
            nearby.put(distance(point, from), point);
        }
        return nearby;
    }

    public SortedMap<Double, Location> getNearby(Location from, double maxDistance) {
        return getNearby(from).headMap(maxDistance);
    }

    private static double distance(Location o1, Location o2) {
        double a = o1.getX() - o2.getX();
        double b = o1.getY() - o2.getY();
        double c = o1.getZ() - o2.getZ();
        return Math.sqrt(a * a + b * b + c * c);
    }
}

答案 1 :(得分:0)

我现在基本上已经弄清楚了。可能有更简单的方法,我需要更具体的应急系统,但是对于我想要的,即围绕玩家对1块半径进行排序,这是可行的。

稍后,我将添加某种加权系统,考虑x,y和z点之间的距离,并且通常会相应地偏爱某些操作。此时可能有很多不必要的代码无法执行任何操作。有些事情被声明两次-一次在开始,一次在结束。我待会再清理。

package pickquest.common;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.ListIterator;

import org.bukkit.Location;

public class Path {
 private HashMap<Integer, Location> pathSequence = new HashMap<>(); 

 public HashMap<Integer, Location> getPathSequence() {
    return pathSequence;
}

 public void setPathSequence(HashMap<Integer, Location> pathSequence) {
    this.pathSequence = pathSequence;
}
 public void setsequencePath(Location origin, ArrayList<Location> points, Location destination) {
    sequencePath(origin, points, destination);
}

 private HashMap<Integer, Location> sequencePath(Location origin, ArrayList<Location> points, Location destination) {
    Location current = origin;
    ArrayList<Location> processedPoints = new ArrayList<>();
    processedPoints.addAll(points);
    ListIterator<Location> pointsIterator = processedPoints.listIterator();
    Integer index = 0;
    Location loc = origin;
    int counter = 0;
    while(pointsIterator.hasNext()) {
        loc=pointsIterator.next();
        if(!(pathSequence.isEmpty())) {
            current = pathSequence.get(pathSequence.size()-1);
        }
        else if(pathSequence.isEmpty()) {
            current= origin;
        }

        if(Math.abs(current.getX() - loc.getX()) > 0 && (Math.abs(current.getX() - loc.getX())) <= 1.3 && (Math.abs(current.getY() - loc.getY())) >= 0 && (Math.abs(current.getY() - loc.getY())) < 1 && (Math.abs(current.getZ() - loc.getZ())) >= 0 && (Math.abs(current.getZ() - loc.getZ())) < 1) { 
            pathSequence.put(index, loc);
            pointsIterator.remove();
            index++;

        }
        else if( ( (Math.abs(current.getY() - loc.getY())) > 0 && (Math.abs(current.getY() - loc.getY())) <= 1.3) && (Math.abs(current.getZ() - loc.getZ())) >= 0 && (Math.abs(current.getZ() - loc.getZ())) < 1 && (Math.abs(current.getX() - loc.getX())) >= 0 && (Math.abs(current.getX() - loc.getX())) < 1) {
            pathSequence.put(index, loc);
            pointsIterator.remove();
            index++;

        }
        else if(Math.abs(current.getZ() - loc.getZ()) > 0 && (Math.abs(current.getZ() - loc.getZ())) <= 1.3 && (Math.abs(current.getY() - loc.getY())) >= 0 && (Math.abs(current.getY() - loc.getY())) < 1 && (Math.abs(current.getX() - loc.getX())) >= 0 && (Math.abs(current.getX() - loc.getX())) < 1) {
            pathSequence.put(index, loc);
            pointsIterator.remove();
            index++;

        }
        if(!(pathSequence.isEmpty())) {
            current = pathSequence.get(pathSequence.size()-1);
        }

        if(!(pointsIterator.hasNext()) && !(processedPoints.isEmpty())) {

            System.out.println("reset fired");
            counter++;

            if(processedPoints.size() == 1) {
                pathSequence.put(index, loc);
            }
            if(pointsIterator.hasPrevious()) {
            int i = processedPoints.size() - 1;

                    Collections.swap(processedPoints, (processedPoints.size() -1 ), i );
                    if(!(pathSequence.isEmpty())) {
                        current = pathSequence.get(pathSequence.size()-1);
                    }
                    while(pointsIterator.hasPrevious()) {
                        pointsIterator.previous();
                    }
                }
            }
        System.out.println(processedPoints.size()); 

        if(counter > 250) {
            break;

        }
            }
    return pathSequence;
}

}