java如何在2D平面上随机生成一组站点,这些站点之间的空间大致相同?
我使用JavaFX来显示站点,但您不需要理解JavaFX来回答我的问题。我想包括一个完整的工作代码示例,这样您就可以自己运行它了。但是,为了帮助我,您可能只需要查看示例底部的randomlyChooseSites()
方法
在这段代码中,我在二维平面上生成了一组随机点。我希望他们在距离上更加平等,而不是在距离上完全平等
如何在二维平面上随机生成点,并使它们之间的距离比现在更接近相等,而不是距离完全相等
public class MCVE extends Application {
private final static int WIDTH = 400;
private final static int HEIGHT = 500;
private final static int AMOUNT_OF_SITES = 50;
private final static SplittableRandom RANDOM = new SplittableRandom();
@Override
public void start(Stage primaryStage) {
// Create the canvas
Canvas canvas = new Canvas(WIDTH, HEIGHT);
GraphicsContext gc = canvas.getGraphicsContext2D();
drawShapes(gc);
// Add the canvas to the window
Group root = new Group();
root.getChildren().add(canvas);
primaryStage.setScene(new Scene(root));
// Show the window
primaryStage.setTitle("Sweep-Line Test");
primaryStage.show();
}
/**
* Draws shapes on the Canvas object.
*
* @param gc
*/
private void drawShapes(GraphicsContext gc) {
gc.setFill(Color.BLACK); // sites should be black
// create random sites
ArrayList<int[]> siteSet = randomlyChooseSites();
// add the random sites to the displayed window
for(int[] i : siteSet) {
gc.fillRect(i[0], i[1], 5, 5);
}
}
/**
* Randomly chooses sites and returns them in a ArrayList
* @return
*/
private ArrayList<int[]> randomlyChooseSites() {
// create an ArrayList to hold the sites as two-value int[] arrays.
ArrayList<int[]> siteList = new ArrayList<>();
int[] point; // holds x and y coordinates
for(int i = 0; i < AMOUNT_OF_SITES; i++) {
// randomly choose the coordinates and add them to the HashSet
point = new int[] {RANDOM.split().nextInt(WIDTH), RANDOM.split().nextInt(HEIGHT)};
siteList.add(point);
}
return siteList;
}
}
# 1 楼答案
(事实上,更准确地表达问题有助于找到答案。)
如果要查找相邻点(沿每个轴)之间相同的平均距离,则这是一种可能的解决方案。:
对于这个解决方案,把平面分成大小相等的矩形,每个矩形包含一个点。 在矩形中随机放置点,并对所有矩形执行此操作。 那么所有相邻点之间的平均距离将是相同的。。。沿着每个轴
我使用了一个完美的正方形网格,两边都一样。然后找到适合该数量站点的最小完美方形网格,并删除角落中的额外区域。(对于50个站点,它是8x8(64))
修改RandomlyChoosSites函数,如下所示:
有些点将非常接近,这可以通过添加更改以生成更靠近每个矩形中心的点来避免
# 2 楼答案
好的算法应该是Poisson disk sampling
Robert Bridson这一算法的变体广泛可用,看看here,我觉得还可以