查找距给定图块中心指定半径内的图块[关闭]

问题描述 投票:0回答:1

我有这行代码:

int[][] coordinates = new int[][]{{0, 0}, {1, 0}, {0, 1}, {-1, 0}, {0, -1}};
if (yg.radius == 1) {
    coordinates = new int[][]{{0, 0}, {1, 0}, {0, 1}, {-1, 0}, {0, -1}};
} else if (yg.radius == 2) {
    coordinates = new int[][]{{0, 0}, {1, 0}, {0, 1}, {-1, 0}, {0, -1}, {1, 1}, {-1, 1}, {-1, -1}, {1, -1}};
} else if (yg.radius == 3) {
    coordinates = new int[][]{{0, 0}, {1, 0}, {0, 1}, {-1, 0}, {0, -1}, {1, 1}, {-1, 1}, {-1, -1}, {1, -1}, {2, 0}, {0, 2}, {-2, 0}, {0, -2}};
} else if (yg.radius == 4) {
    coordinates = new int[][]{{0, 0}, {1, 0}, {0, 1}, {-1, 0}, {0, -1}, {1, 1}, {-1, 1}, {-1, -1}, {1, -1}, {2, 0}, {0, 2}, {-2, 0}, {0, -2}, {2, 2}, {-2, 2}, {-2, -2}, {2, -2}};
} else if (yg.radius == 5) {
    coordinates = new int[][]{{0, 0}, {1, 0}, {0, 1}, {-1, 0}, {0, -1}, {1, 1}, {-1, 1}, {-1, -1}, {1, -1}, {2, 0}, {0, 2}, {-2, 0}, {0, -2}, {2, 2}, {-2, 2}, {-2, -2}, {2, -2}, {3, 0}, {0, 3}, {-3, 0}, {0, -3}};
} else if (yg.radius == 6) {
    coordinates = new int[][]{{0, 0}, {1, 0}, {0, 1}, {-1, 0}, {0, -1}, {1, 1}, {-1, 1}, {-1, -1}, {1, -1}, {2, 0}, {0, 2}, {-2, 0}, {0, -2}, {2, 2}, {-2, 2}, {-2, -2}, {2, -2}, {3, 0}, {0, 3}, {-3, 0}, {0, -3}, {3, 3}, {-3, 3}, {-3, -3}, {3, -3}};
} else if (yg.radius == 7) {
    coordinates = new int[][]{{0, 0}, {1, 0}, {0, 1}, {-1, 0}, {0, -1}, {1, 1}, {-1, 1}, {-1, -1}, {1, -1}, {2, 0}, {0, 2}, {-2, 0}, {0, -2}, {2, 2}, {-2, 2}, {-2, -2}, {2, -2}, {3, 0}, {0, 3}, {-3, 0}, {0, -3}, {3, 3}, {-3, 3}, {-3, -3}, {3, -3}, {4, 0}, {0, 4}, {-4, 0}, {0, -4}};
} else if (yg.radius == 8) {
    coordinates = new int[][]{{0, 0}, {1, 0}, {0, 1}, {-1, 0}, {0, -1}, {1, 1}, {-1, 1}, {-1, -1}, {1, -1}, {2, 0}, {0, 2}, {-2, 0}, {0, -2}, {2, 2}, {-2, 2}, {-2, -2}, {2, -2}, {3, 0}, {0, 3}, {-3, 0}, {0, -3}, {3, 3}, {-3, 3}, {-3, -3}, {3, -3}, {4, 0}, {0, 4}, {-4, 0}, {0, -4}, {4, 4}, {-4, 4}, {-4, -4}, {4, -4}};
}
for (int[] coordinate : coordinates) {
    Hit(coordinate[0], coordinate[1], yg.damage);
}

我的代码工作不正确,因为它只添加 0 或 X / X 或 0 之类的,但不是圆形效果。

我期望有一个函数将半径作为参数,并返回坐标数组,如果调用

Hit()
函数,该数组将以像素为单位绘制一个圆。

java arrays sorting integer coordinates
1个回答
-4
投票

下面可以帮助你,你可以尝试一下吗?

int[][] getCoordinatesInCircle(int radius) {
int[][] coordinates = new int[4 * radius * radius + 1][];  // Allocate space for all coordinates
int index = 0;

for (int x = -radius; x <= radius; x++) {
    for (int y = -radius; y <= radius; y++) {
        if (x * x + y * y <= radius * radius) {  // Check if within circle
            coordinates[index++] = new int[]{x, y};
        }
    }
}
return coordinates; }
© www.soinside.com 2019 - 2024. All rights reserved.