我目前已经为我正在开发的基于图块的世界编辑器实现了 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);
}
如果您需要我的代码的更多上下文,请发表评论。