java-如何在2D平面上随机生成一堆之间空间大致相同的站点?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java-如何在2D平面上随机生成一堆之间空间大致相同的站点?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4539字,纯文字阅读大概需要7分钟。
内容图文
![java-如何在2D平面上随机生成一堆之间空间大致相同的站点?](/upload/InfoBanner/zyjiaocheng/670/d426e64f9a9448c9b7451dee82cc8b8d.jpg)
我使用JavaFX来显示站点,但是您不需要了解JavaFX即可回答我的问题.我想提供一个完整的工作代码示例,以便您可以自己运行它.但是,为了帮助我,您可能只需要查看示例底部的randomChooseSites()方法.
在这段代码中,我在2D平面上生成了一堆随机点.我希望它们彼此之间的距离更相等,而彼此之间的距离不完全相等.
如何在2D平面上随机生成点,并使它们彼此之间的距离比现在更近,而又不完全相等?
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;
}
}
解决方法:
(实际上,更准确地表达问题会帮助您找到答案.)
如果您要在相邻点(沿每个轴)之间寻找相同的平均距离,这是一种可能的解决方案:
因此,对于此解决方案,将平面分成相等大小的矩形,每个矩形将包含一个点.
将点随机放置在矩形中,并对所有矩形执行此操作.
那么所有相邻点之间的平均距离沿每个轴都将是相同的.
我使用了一个完美的正方形网格,两边都相同.然后找到适合该数量站点的最小的理想正方形网格,并删除角落中多余的区域. (对于50个网站,则为8×8(64)).
像下面这样修改randomChooseSites函数:
private ArrayList<int[]> randomlyChooseSites() {
// create a HashSet to hold the sites as two-value int[] arrays.
ArrayList<int[]> siteList = new ArrayList<>();
class SiteArea
{
boolean is_used; // if false, ignore this area (and the point in it)
int point_x; // absolute coordinates of point generated in this area
int point_y;
}
int gridsize = (int)Math.ceil (Math.sqrt (AMOUNT_OF_SITES));
int empty_areas = gridsize * gridsize - AMOUNT_OF_SITES; // we want the empty areas in the corners
int area_size_x = WIDTH / gridsize;
int area_size_y = HEIGHT / gridsize;
SiteArea areas[][] = new SiteArea [gridsize][gridsize];
// initialize all areas on the grid
for (int i = 0, imax = gridsize * gridsize; i < imax; i++)
{
int x_ = (i % gridsize), x = x_ * area_size_x;
int y_ = (i / gridsize), y = y_ * area_size_y;
SiteArea a = areas[x_][y_] = new SiteArea ();
a.is_used = true; // set all areas as used initially
// generate the point somewhere within this area
a.point_x = x + RANDOM.split().nextInt(area_size_x);
a.point_y = y + RANDOM.split().nextInt(area_size_y);
// to see the grid, create a drawRect() function that draws an un-filled rectangle on gc, with the other params being: top left corner x, y and rectangle width, height
//drawRect (gc, x, y, area_size_x, area_size_y);
}
// disable some of the areas in case if the grid has more rectangles than AMOUNT_OF_SITES
// cut away at the four corners, by setting those areas to is_used = false
class Corner { int x; int y; int xdir; int ydir; Corner (int a,int b,int c,int d) { x=a;y=b;xdir=c;ydir=d; } }
int z = gridsize-1; Corner corners[] = { new Corner (0,0,1,1), new Corner (z,0,-1,1), new Corner (0,z,1,-1), new Corner (z,z,-1,-1) };
for (int i = 0, imax = empty_areas; i < imax; i++)
{
Corner c = corners[i % 4]; // cycle over the corners
int x = c.x, y = c.y, offset = (i + 4) / 8, xo = offset, yo = offset;
if (i % 8 > 3)
{ // cut x
xo = 0;
}
else
{ // cut y
yo = 0;
}
areas[x + xo * c.xdir][y + yo * c.ydir].is_used = false;
}
// export the generated points to siteList
for (int i = 0, imax = gridsize * gridsize; i < imax; i++)
{
int x_ = (i % gridsize);
int y_ = (i / gridsize);
SiteArea a = areas[x_][y_];
if (a.is_used)
{
siteList.add (new int[] { a.point_x, a.point_y });
}
}
return siteList;
}
某些点将非常接近,可以通过添加更改以生成更靠近每个矩形中心的点来避免这种情况.
内容总结
以上是互联网集市为您收集整理的java-如何在2D平面上随机生成一堆之间空间大致相同的站点?全部内容,希望文章能够帮你解决java-如何在2D平面上随机生成一堆之间空间大致相同的站点?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。