Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将多个对象分组_Java_Graph_Nearest Neighbor - Fatal编程技术网

Java 将多个对象分组

Java 将多个对象分组,java,graph,nearest-neighbor,Java,Graph,Nearest Neighbor,我正在做一个游戏,里面有很多房间,这些房间通过通道相连。通过在整个区域中创建随机大小的房间,可以随机生成整个对象,然后,它会找到最近的房间,该房间尚未通过通道连接,并在它们之间创建一个通道 private void LinkRooms() { for(Room r : new ArrayList<Room>(rooms)){ if(r != null) { Room r2 = getNearestNeighbour(r);

我正在做一个游戏,里面有很多房间,这些房间通过通道相连。通过在整个区域中创建随机大小的房间,可以随机生成整个对象,然后,它会找到最近的房间,该房间尚未通过通道连接,并在它们之间创建一个通道

private void LinkRooms() {
    for(Room r : new ArrayList<Room>(rooms)){
        if(r != null) {
            Room r2 = getNearestNeighbour(r);
            if(r2 != null) createPassage(r, r2);
        }
    }
}
private void链接室(){
用于(房间r:新阵列列表(房间)){
如果(r!=null){
房间r2=距离最近的邻居(r);
如果(r2!=null)创建通道(r,r2);
}
}
}
查找最近的邻居:

private Room getNearestNeighbour(Room r) {
    double dist = 10000000;
    Room room = null;
    for(Room r2 : new ArrayList<Room>(rooms)){
        if(!r.linked.contains(r2) && r2 != r){
            int cx = r.x + r.size_x/2;
            int cy = r.y + r.size_y/2;
            int cx2 = r2.x + r2.size_x/2;
            int cy2 = r2.y + r2.size_y/2;
            double distance = Math.sqrt((cx - cx2) * (cx - cx2) + (cy - cy2) * (cy - cy2));
            if(distance <= dist){ 
                dist = distance; 
                room = r2;
            }
        }
    }
    return room;
}
私人房间GetNearestNeighbor(房间r){
双区=10000000;
房间=空;
用于(r2房间:新阵列列表(房间)){
如果(!r.linked.contains(r2)&&r2!=r){
int cx=r.x+r.size\u x/2;
int cy=r.y+r.size_y/2;
intcx2=r2.x+r2.size\ux/2;
int cy2=r2.y+r2.size_y/2;
双距离=数学sqrt((cx-cx2)*(cx-cx2)+(cy-cy2)*(cy-cy2));
如果(距离)
private void createPassage(Room r, Room r2) {
    int centrex = roundm(r.x + r.size_x/2 - Tile.size/2);
    int centrey = roundm(r.y + r.size_y/2 - Tile.size/2);
    int centrex2 = roundm(r2.x + r2.size_x/2 - Tile.size/2);
    int centrey2 = roundm(r2.y + r2.size_y/2 - Tile.size/2);

    r.linked.add(r2);//here is where the room adds the rooms that it is directly linked to
    r2.linked.add(r);

/**I tried to approach my problem by giving rooms a unique id, 
which is equal to their position in the array, and then I thought 
it would spread this id to each of the other rooms, however, because 
of the order that they find the nearest neighbour being "random" some
would end with different id's. Note that I use the colour so that I 
can see it visually.**/

    if(r.id < r2.id){
        r2.id = r.id;
        r2.colour = r.colour;
    }else{
        r.id = r2.id;
        r.colour = r2.colour;
    }
    for(int xtile = 0; xtile < Math.abs(centrex2-centrex); xtile+=Tile.size){
        if(centrex < centrex2){
            ... // create new tile with owner "r"
        }else{
            ...
        }
    }
    for(int ytile = 0; ytile < Math.abs(centrey2-centrey); ytile+=Tile.size){
        ...
    }
}