Java 在矩形内绘制基于平铺的椭圆

Java 在矩形内绘制基于平铺的椭圆,java,math,ellipse,Java,Math,Ellipse,我正在尝试生成随机大小的椭圆,并将它们绘制到地图上(只是一个二维瓷砖阵列)。在大多数情况下,它是有效的,但是当房间比它更宽时,它似乎会切断墙壁的角落 下面是我画椭圆的代码。基本上取一个矩形并在其中绘制椭圆 private void AddCellEllipse(int xStart, int yStart, int xEnd, int yEnd, Tile tile) { // Draw an ellipse centered in the passed-in coordinates

我正在尝试生成随机大小的椭圆,并将它们绘制到地图上(只是一个二维瓷砖阵列)。在大多数情况下,它是有效的,但是当房间比它更宽时,它似乎会切断墙壁的角落

下面是我画椭圆的代码。基本上取一个矩形并在其中绘制椭圆

private void AddCellEllipse(int xStart, int yStart, int xEnd, int yEnd, Tile tile)
{
    // Draw an ellipse centered in the passed-in coordinates
    float xCenter = (xEnd + xStart) / 2.0f;
    float yCenter = (yEnd + yStart) / 2.0f;
    float xAxis = (xEnd - xStart) / 2.0f;
    float yAxis = (yEnd - yStart) / 2.0f;

    for (int y = yStart; y <= yEnd; y++)
        for (int x = xStart; x <= xEnd; x++)
        {
            // Only draw if (x,y) is within the ellipse
            if (Math.sqrt(Math.pow((x - xCenter) / xAxis, 2.0) + Math.pow((y - yCenter) / yAxis, 2.0)) <= 1.0f)
                tiles[x][y] = tile;
        }
}
还有一个额外的问题,有人知道我怎样才能使1块瓷砖不在椭圆的顶部/底部突出吗?

您可以使用或中点算法绘制椭圆

当你用上面提到的算法画两个对称点(平铺)时,如下所示:

DrawPixel (xc + x, yc + y);
DrawPixel (xc - x, yc + y);

用内部瓷砖填充它们之间的线段。

在Bresenham算法的示例中,我假设xc和yc变量是椭圆的中心?是的,它是。喜欢你的xCenter和Ycenter谢谢。我已经成功地实现了这个算法,现在只需要用地砖填充椭圆,但我已经生成了一些地图,没有再注意到这个问题。
DrawPixel (xc + x, yc + y);
DrawPixel (xc - x, yc + y);