java:生成不在矩形内的随机点

发布于 2024-12-29 02:06:10 字数 832 浏览 2 评论 0原文

我有许多矩形,并且正在尝试生成一个不在其中任何一个内部的随机点。我创建了一个方法来执行此操作,但这似乎导致我的应用程序冻结,因为在生成有效点之前它必须经过大量点:

public Point getLegalPoint() {
           Random generator = new Random();
           Point point;
           boolean okPoint = true;
           do {
                   point = new Point(generator.nextInt(975), generator.nextInt(650));
                   for (int i = 0; i < buildingViews.size(); i++) {
                           if (buildingViews.get(i).getBuilding().getRectangle()
                                           .contains(point)) {
                                   okPoint = false;
                                   break;
                           }

                   }
           } while (okPoint == false);
           return point;
   }

是否有什么我做错了,或者是否有更多有效的方法来做到这一点,这样它就不会冻结我的应用程序?

I have a number of rectangles, and am trying to generate a random point that is not inside any of them. I created a method to do this, but it appears that this is causing my application to freeze because it has to go through a large number of points before a valid point is generated:

public Point getLegalPoint() {
           Random generator = new Random();
           Point point;
           boolean okPoint = true;
           do {
                   point = new Point(generator.nextInt(975), generator.nextInt(650));
                   for (int i = 0; i < buildingViews.size(); i++) {
                           if (buildingViews.get(i).getBuilding().getRectangle()
                                           .contains(point)) {
                                   okPoint = false;
                                   break;
                           }

                   }
           } while (okPoint == false);
           return point;
   }

Is there something I am doing wrong, or is there a more efficient way to do it so that it won't freeze my application?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

葬花如无物 2025-01-05 02:06:11

生成随机点。然后检查它是否在矩形的边界内。如果是则:

  1. 设centerX和centerY为矩形中心点的x和y。
  2. 如果 randPointX < centerX,则令 randPointX = randPointX - centerX
  3. 如果 randPointX > centerX 然后让 randPointX = randPointX + centerX
  4. 对 y 坐标执行相同的操作,
  5. 您将需要再次进行边界检查以查看该点是否位于较大视图(我假设的屏幕)之外。只是扭曲坐标。因此,如果 randPointX 为负数,则让它等于 max_X + randPointX

Generate a random point. Then check if it is inside bounds of rectangle. if it is then:

  1. Let centerX and centerY be the x and y of the center point of rectangle.
  2. if randPointX < centerX then let randPointX = randPointX - centerX
  3. if randPointX > centerX then let randPointX = randPointX + centerX
  4. Do same for y ordinate
  5. you will need to do bounds checking again to see if the point is outside the larger view (screen i'm assuming). Just warp coordinates. so if randPointX is negative then let it equal max_X + randPointX
就此别过 2025-01-05 02:06:11

我会尝试这样的事情:
选择该点是否位于矩形的上方/下方/左侧/右侧(nextInt(4)),然后在该区号中选择随机点

public Point getLegalPoint(int x, int y, int width, int height){
  Random generator = new Random();
  int position = generator.nextInt(4); //0: top; 1: right; 2: bottom; 3:right
  if (position == 0){
    return new Point(generator.nextInt(975),y-generator.nextInt(y);
  } else if (position == 2){
    return new Point(generator.nextInt(975),y+height+(generator.nextInt(650-(y+height)));
  } 
   ... same for x ...
}

I would try something like this:
select whether the point is above/below/on left side/on right side of rectange (nextInt(4)) and then select random point in this area

code:

public Point getLegalPoint(int x, int y, int width, int height){
  Random generator = new Random();
  int position = generator.nextInt(4); //0: top; 1: right; 2: bottom; 3:right
  if (position == 0){
    return new Point(generator.nextInt(975),y-generator.nextInt(y);
  } else if (position == 2){
    return new Point(generator.nextInt(975),y+height+(generator.nextInt(650-(y+height)));
  } 
   ... same for x ...
}
木槿暧夏七纪年 2025-01-05 02:06:10

如果第一次尝试没有成功,此代码将导致无限循环,okPoint = true 必须位于 do< /strong> 阻止。当你解决这个问题时,看看你的表现如何。
当您检查多个矩形而不仅仅是一个矩形时,我想不出一种更快的方法。

This code results to infinite loop if you don't succeed on the first try, okPoint = true must be inside the do block. See what your performance is when you fix that.
I cannot think of a faster way as you check against multiple rectangles and not just one.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文