如何随机生成一串上有大致的他们之间的间隔相同数量的2D平面的网站?

LuminousNutria:

我用JavaFX的显示的网站,但你不应该需要了解JavaFX的回答我的问题。我想提供一个完整的工作代码,例如这样你就可以自己运行它。然而,帮我出,你可能只需要看看randomlyChooseSites()方法在我的例子的底部。

在此代码生成我在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;
}

}
gregn3:

(实际上,更准确地表达这个问题会帮助你找到答案。)

这是一个可能的解决方案,在情况下,如果你正在寻找的相邻点(沿每个轴)之间相同的平均距离

因此,对于这个解决方案,划分平面分成大小相同的矩形,每一个将包含一个点。随机放置点的矩形,并为所有矩形做到这一点。然后所有相邻点之间的平均距离是相同的...沿每个轴。

我用一个完美的方形网格,两侧是相同的。然后找出最小的完美方格网,将适合的是一些网站,并删除在弯道额外的领域。(50点是8×8(64))。

修改randomlyChooseSites功能如下:

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;
}

有些点会很接近,这可以通过添加一个改变以产生点接近的长方形的中心是可以避免的。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何显示一条消息,告诉用户他们的猜测与随机生成的数字有多远?

Prolog 根据他们拥有的孩子数量显示相同数量的输出

给定一个字符串,如何随机转置他们的2个字母?

网站的每个用户是否共享相同的变量,或者他们对每个用户都是唯一的(即使他们的名字相同)

HotJar如何生成他们的录音?

2D 矩阵问题 - 有多少人可以得到他们想要的颜色?

C 如何获得玩家的数量和他们的名字

如何筛选和组合对象,如果他们有相同的字段

XNA:需要帮助,在计时器的刻度上从任一侧生成敌人,然后让他们随机移动

在下面的Java代码中,我如何要求用户在每个咖啡杯中输入他们想要的镜头数量(鉴于他们订购的杯数大于1)?

如何将ns1.MyPersonalClass转换为ns2.MyPersonalClass?他们具有相同的财产或成员

OpenCL FPGA:同一内核的 2 个副本的内核执行不是并行进行的。除此之外,他们之间还有空闲时间

我在同一个 WiFi 上有 3 台电脑。为什么他们的本地 IP 中有 2 个是“192.168.1.x”,而第三台 PC 是“192.168.56.x”?

如何知道网站的哪些机器人,如果我没有对主机的 root 访问权限,他们会阅读?

当用户放大或缩小时,网站元素会乱成一团。我如何阻止他们移动?

在 Python 中,如何按顺序生成一串 0 和 1 的所有排列?

如果所有用户具有相同的产品,如何将他们分组到产品列表中?

根据他们在 Python 中的分数生成随机元组组合

我想建立一个docusign api,让拥有docusign帐户的客户通过我们的网站管理他们进行交互

PyCharm Professional 中的配置数量是有上限的。有什么办法可以提高他们的金额?

如何在MATLAB中生成2D随机矢量?

如何从字符串中删除重复的单词,并只显示一次与他们的字数

如何创建一个可共享的类,以便其他人可以将其用于他们的网站

Cython 具有“一流的函数对象”——它们的效率如何?他们有 Python 开销吗?

他们向我显示错误“所有数组必须具有相同的长度”

如何通过BigCommerce“登录客户”并授予他们访问自定义网站的权限

2个用户使用相同的设备和相同的应用程序,但登录ID不同-他们是否具有不同的gcm注册ID?

我在一个列表中有多个数据框。如何打印他们的名字?

SQL如何创建一个条件,如果他们有约会,则将计数增加1