-1

我目前已经为我正在开发的基于图块的世界编辑器实现了 Bresenham 线算法。对于Shift+Click功能,我想根据图块集中的选择矩形渲染一条线。point1这很简单,因为我可以使用算法找到线上的点point2并在每个点处绘制选择。

The issue with this is that the points overlap when the selection rectangle is larger than one tile. 结果如下所示。

当前的

我的问题是给出一条线上的所有点,以及选择大小,我怎样才能得到这样的结果?请注意,一旦选择过去,我想停止绘制选择point2

期望的

这是我目前用于渲染线条而不考虑选择大小的内容。

void renderPoint(Point point, bool invalidate = false)
{
    Rectangle selection = world.SelectionRectangle;

    int width = selection.Width / world.Settings.TileSize.Width,
        height = selection.Height / world.Settings.TileSize.Height,
        offsetX = (int)Math.Floor((float)width / 2),
        offsetY = (int)Math.Floor((float)height / 2);

    for (int x = point.X, sx = 0; x < point.X + width; x++, sx++)
    {
        for (int y = point.Y, sy = 0; y < point.Y + height; y++, sy++)
        {
            Point change = new Point(x - offsetX, y - offsetY);

            if (!changesUndo.ContainsKey(change))
                changesUndo[change] = world.GetTile(change);

            WorldTile tile = new WorldTile(selection.X + (sx * world.Settings.TileSize.Width), selection.Y + (sy * world.Settings.TileSize.Height), 0);

            world.SetTile(change.X, change.Y, tile);

            changesRedo[change] = tile;
        }
    }

    lastRenderedPoint = point;

    if (invalidate)
        world.InvalidateCanvas();
}

void renderLine(Point p1, Point p2)
{
    List<Point> points = pointsOnLine(p1, p2);

    for (int i = 0; i < points.Count; i++)
        renderPoint(points[i], i == points.Count - 1);
}

如果您需要我的代码的更多上下文,请发表评论。

4

1 回答 1

1

如果这些点是用 Bresenham 渲染的,那么您必须跳过“n”个点中的每个“n-1”个,其中“n”是线的主要方向上的选择大小(即,如果它是一条大部分水平线,宽度)。

像这样的东西?:

void renderLine(Point p1, Point p2)
{
    List<Point> points = pointsOnLine(p1, p2);

    int dx = Abs(p2.x - p1.x);
    int dy = Abs(p2.y - p1.y);

    Rectangle selection = world.SelectionRectangle;

    int incr = selection.Width / world.Settings.TileSize.Width;
    if(dy > dx)
    {
        incr = selection.Height / world.Settings.TileSize.Height;
    }

    int lastPoint = (points.Count-1) / incr;
    lastPoint *= incr;

    for (int i = 0; i <= lastPoint; i+=incr)
        renderPoint(points[i], i == lastPoint);
}
于 2012-12-23T09:09:10.733 回答