简体   繁体   English

如何避免多边形重叠

[英]How to avoid overlapping polygon

I created a program to draw many polygons automatically everytimes user presses a button. 我创建了一个程序,每次用户按下按钮时都会自动绘制许多多边形。 The points of the polygon are generated automatically using the random function. 多边形的点是使用随机函数自动生成的。 The problem is that, since the points of the polygon were randomly generated, some of the polygon are overlap with other polygon. 问题在于,由于多边形的点是随机生成的,因此某些多边形与其他多边形重叠。 How can I avoid this, so that every polygon shown without being overlapped? 如何避免这种情况,使每个显示的多边形都不会重叠?

.....
List<Polygon> triangles = new LinkedList<Polygon>(); 
Random generator = new Random();

public void paintComponent(Graphics g) {

   for(int i = 0; i < 10; i++) {
      double xWidth = generator.nextDouble() * 40.0 + 10.0;
      double yHeight = generator.nextDouble() * 40.0 + 10.0;

      xCoord[0] = generator.nextInt(MAX_WIDTH);
      yCoord[0] = generator.nextInt(MAX_HEIGHT); 

      xCoord[1] = (int) (xCoord[0] - xWidth);
      xCoord[2] = (int) (xCoord[1] + (xWidth/2));       

      yCoord[1] = yCoord[0];
      yCoord[2] = (int) (yCoord[1] - yHeight);     

      triangles.add( new Polygon(xCoord,yCoord, 3));          
   }

   Graphics2D g2 = (Graphics2D) g;
   g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
   g2.setStroke(new BasicStroke(1)); 
   g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1.00f));
   g2.setPaint(Color.black);//set the polygon line 

   for (Polygon triangle : triangles)  g2.drawPolygon(triangle);

   Polygon[] triArray = triangles.toArray(new Polygon[triangles.size()]);
   for (Polygon p:triArray) triangles.remove (p);

}

Check out the game programming wiki on Polygon Collision: 在Polygon Collision上查看游戏编程Wiki:

http://gpwiki.org/index.php/Polygon_Collision http://gpwiki.org/index.php/Polygon_Collision

You could break your canvas into 10 regions and constrain your polygons each to their own region. 您可以将画布分成10个区域,并将每个多边形限制在各自的区域。 To do this, you could use your i value and a %100 (or other suitable magnitude) of your randomly generated value and apply them to your x coordinates and y coordinates as applicable. 为此,您可以使用i值和随机生成的值的%100 (或其他合适的大小),然后将它们应用于x坐标和y坐标(如果适用)。 The result would be a grid of similarly constrained(no larger than the grid cell), but randomly shaped, Polygons. 结果将是一个约束类似(不大于网格单元)但形状随机的多边形的网格。

EDIT: 编辑:

Taking another look and fooling around a bit, I took the general concept as I described above and made a stab at an implementation: 再来看一遍,我愚弄了一下,就采用了上面介绍的一般概念,并刺探了一个实现:

public void paintComponent(Graphics g) {
    int[] xCoord = new int[3];
    int[] yCoord = new int[3];
    int colCnt = 5;
    int rowCnt = 2;
    int maxCellWidth = getWidth() / colCnt;
    int maxCellHeight = getHeight() / rowCnt;

    for (int i = 0; i < (colCnt * rowCnt); i++) {
        int xMultiple = i % colCnt;
        int yMultiple = i / colCnt;
        for (int j = 0; j < 3; j++) {
         xCoord[j] = generator.nextInt(maxCellWidth)
                   + (maxCellWidth * xMultiple);
             yCoord[j] = generator.nextInt(maxCellHeight)
                   + (maxCellHeight * yMultiple);
        }
        triangles.add(new Polygon(xCoord, yCoord, 3));
    }
    //... the rest of your method
}

As you can see, all of the Polygons have all points randomly generated, as opposed to your method of generating the first point and then making the rest relative to the first. 如您所见,与您生成第一个点然后将其余点相对于第一个点的方法相反,所有的Polygon都有随机生成的所有点。 There is a sense of randomness that is lost, however, as the Polygons are laid out in a grid-like pattern. 但是,由于多边形以网格状图案布局,因此失去了随机性。

Create Area objects from your new polygon as well as for all existing polygons. 从新多边形以及所有现有多边形创建Area对象。 Subtract the new polygon's area from the existing ones. 从现有多边形中减去新多边形的面积。 If the subtract changed the area, the polygons overlap. 如果减法更改了面积,则多边形重叠。

Area newArea = new Area(newPolygon);
Area existingArea = new Area(existingPolygon);
Area existingAreaSub = new Area(existingPolygon); existingAreaSub.subtract(newArea);
boolean intersects = existingAreaSub.equals(existingArea);

您可以实现方法Polycon.containsPoint( x, y )并重复随机生成,直到此方法对所有绘制的Polycon.containsPoint( x, y )返回false为止。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM