不规则多边形内的x,y坐标,给出了x,y点的列表,但不在角点或边上

问题描述 投票:0回答:1

我正在尝试解决以下用例:

Input:形成不规则多边形的x,y坐标列表(保证有效,并且线条不会相交;但是可以凹入)。输入格式无关紧要,因此我目前分别为xy使用两个松散的整数数组。

输出:此多边形内的 x,y坐标,它不直接位于角或边缘的顶部。

使用以下代码,我已经能够解决多边形内的随机x,y坐标:

// Currying Function with two int-arrays as parameters and double-pair return-type
X->Y->{
  // Create a Path2D object
  java.awt.geom.Path2D path = new java.awt.geom.Path2D.Double();
  // Start at the first coordinate given
  path.moveTo(X[0], Y[0]);
  // Loop over the remaining coordinates:
  for(int i=1; i<X.length; i++)
    // And draw lines from corner to corner
    path.lineTo(X[i], Y[i]);
  // After the loop, close the path to finish the polygon
  path.closePath();
  // Create a Rectangle that encapsulates the entire Path2D-polygon
  java.awt.Rectangle rect = s.getBounds();
  // The resulting x,y-coordinate, starting uninitialized
  double x,y;
  // Do-while the Path2D polygon does not contain the random x,y-coordinate:
  do{
    // Select a random x,y-coordinate within the Rectangle
    x = rect.getX() + Math.random()*rect.getWidht();
    y = rect.getY() + Math.random()*rect.getHeight();
  }while(!path.contains(x,y));
  // After which we return this random x,y-coordinate as result:
  return new double[]{x,y};
}

全部按预期工作。现在,我要确保随机x,y坐标不是输入x,y坐标之一(这很容易),并且不在Path2D的边缘/线上。第二部分我不确定如何解决,快速谷歌搜索没有给出任何有用的信息,因此是这个问题。

注意:我知道随机点位于边缘/角上的可能性很小,可能被忽略,但这是一个挑战,因此无论如何都需要实现。” >我正在尝试解决以下用例:输入:将形成不规则多边形的x,y坐标列表(保证有效,并且它们不会相交;但是可以凹入)。 ...

java random polygon point point-in-polygon
1个回答
0
投票
在注释中的[[@Andreas的提示后使用Line2D,我将do-while修改为:
© www.soinside.com 2019 - 2024. All rights reserved.