如何生成以Java为中心的随机多边形



我想生成一些随机多边形,但我希望它或多或少地位于给定窗口坐标的中间。

这是我的代码,它生成了一个随机多边形,但大多数时候它都在窗口的底部,我希望它更居中:

private static final double CORNER_MARGIN = 100.0; // max offset for a corner of the field, to randomize the polygon
private static double[] standardPolygon(double x1, double x2, double y1, double y2) {
// minX      maxX         minY      maxY --> it's the coordinate of the window
double centerX = (x1 + x2) / 2;
double centerY = (y1 + y2) / 2;
// this is a standard polygon "centered" in the middle of the program window
return new double[]{
x1 -  (x2 - x1) * RANDOM.nextDouble(),  y2 + (y2 - y1) *RANDOM.nextDouble() * CORNER_MARGIN,
x2 +  (x2 - x1) * RANDOM.nextDouble(),  y2 + (y2 - y1) *RANDOM.nextDouble() * CORNER_MARGIN,
x2 +  (x2 - x1) * RANDOM.nextDouble(),  y1 - (y2 - y1) *RANDOM.nextDouble() * CORNER_MARGIN,
x1 -  (x2 - x1) * RANDOM.nextDouble(),  y1 - (y2 - y1) *RANDOM.nextDouble() * CORNER_MARGIN,
};
/*return new double[]{
x1 - RANDOM.nextDouble() * CORNER_MARGIN, y2 + RANDOM.nextDouble() * CORNER_MARGIN, // up left
x2 + RANDOM.nextDouble() * CORNER_MARGIN, y2 + RANDOM.nextDouble() * CORNER_MARGIN, // up right
x2 + RANDOM.nextDouble() * CORNER_MARGIN, y1 - RANDOM.nextDouble() * CORNER_MARGIN, // down right
x1 - RANDOM.nextDouble() * CORNER_MARGIN, y1 - RANDOM.nextDouble() * CORNER_MARGIN, // down left
};*/
}

注释中的代码正在工作,但现在我试图将其居中,但我只得到一些矩形/正方形。我如何才能保持随机多边形的形状,但更居中一点?

[编辑]以下是我如何绘制多边形的区域:

private void drawZone(Group group, IGameParameters gameParameters) {
Polygon polygon = new Polygon();
double[] points = gameParameters.dronePadDeliveryZonePolygon();
List<Double> pointsList = new ArrayList<>();
for (double point : points) pointsList.add(point);
polygon.getPoints().addAll(pointsList);
polygon.setFill(Color.ANTIQUEWHITE);
group.getChildren().add(polygon);
}```

您计算了中心,但没有在任何地方使用它。只是为了让我正确理解,这只是一个四边形,角点随机定位在距离窗口角点最多100?

我不能100%确定多边形的形状,但请尝试一下。从逻辑上讲,它在我的脑海中起作用,但我现在没有办法测试代码。

private static final double CORNER_MARGIN = 100.0; 
private static double[] standardPolygon(double x1, double x2, double y1, double y2) {
double centerX = (x1 + x2) / 2;
double centerY = (y1 + y2) / 2;

// Get the corner offsets
ox1 = x1 + CORNER_MARGIN * RANDOM.nextDouble(); // top left
oy1 = y1 + CORNER_MARGIN * RANDOM.nextDouble();
ox2 = x2 - CORNER_MARGIN * RANDOM.nextDouble(); // top right
oy2 = y1 + CORNER_MARGIN * RANDOM.nextDouble();
ox3 = x1 + CORNER_MARGIN * RANDOM.nextDouble(); // bottom left
oy3 = y2 - CORNER_MARGIN * RANDOM.nextDouble();
ox4 = x2 - CORNER_MARGIN * RANDOM.nextDouble(); // bottom right
oy4 = y2 - CORNER_MARGIN * RANDOM.nextDouble();

// Calculate the center of the polygon
double cx = (ox2 - ox1) / 2;
double cy = (oy2 - oy1) / 2;
// difference between window's center and polygon
double offsetX = centerX - cx;
double offsetY = centerY - cy;
// offset the calculated points so the polygon's center matches the window
ox1 += offsetX;
oy1 += offsetY;
ox2 += offsetX;
oy2 += offsetY;
ox3 += offsetX;
oy3 += offsetY;
ox4 += offsetX;
oy4 += offsetY;
// this is a standard polygon "centered" in the middle of the program window
return new double[]{
ox1, oy1,
ox2, oy2,
ox3, oy3,
ox4, oy4
};

}

最新更新