比使用Bresenham算法更好的行选择?

Ein*_*aas 2 javascript canvas bresenham

我在HTML画布上绘制线条,并使用不太精确的2d阵列(代表10x10像素的块),其中我使用Bresenham算法绘制线条以存储线条ID,因此我可以使用该数组来查看哪个线被选中.

这是有效的,但我希望它更准确 - 不是我使用的10x10大小(我喜欢我不必点击该行),但当我绘制该数组的表示而不是我的实际画布,我看到有很多10x10块没有填充,即使线穿过它们:

在此输入图像描述

有更好的解决方案吗?我想要的是捕获实际线路经过的所有网格块.

huy*_*itw 6

在没有看到你的代码的情况下,我认为你在使用Bresenham算法填充查找表时犯了一个舍入错误,或者在运行算法之前缩放了坐标.

这个jsFiddle显示了我想出的东西,正方形完全对齐.

在此输入图像描述

HTML

<canvas id="myCanvas"></canvas>
Run Code Online (Sandbox Code Playgroud)

CSS

#myCanvas {
    width: 250px;
    height: 250px;
}
Run Code Online (Sandbox Code Playgroud)

JavaScript的

var $canvas = $("#myCanvas"),
    ctx = $canvas[0].getContext("2d");

ctx.canvas.width = $canvas.width();
ctx.canvas.height = $canvas.height();

function Grid(ctx) {
    this._ctx = ctx;
    this._lines = [];
    this._table = [];
    this._tableScale = 10;
    this._createLookupTable();
}
Grid.prototype._createLookupTable = function() {
    this._table = [];
    for (var y = 0; y < Math.ceil(ctx.canvas.height / this._tableScale); y++) {
        this._table[y] = [];
        for (var x = 0; x < Math.ceil(ctx.canvas.width / this._tableScale); x++)
            this._table[y][x] = null;
    }
};
Grid.prototype._updateLookupTable = function(line) {
    var x0 = line.from[0],
        y0 = line.from[1],
        x1 = line.to[0],
        y1 = line.to[1],
        dx = Math.abs(x1 - x0),
        dy = Math.abs(y1 - y0),
        sx = (x0 < x1) ? 1 : -1,
        sy = (y0 < y1) ? 1 : -1,
        err = dx - dy;
    while(true) {
        this._table[Math.floor(y0 / 10)][Math.floor(x0 / 10)] = line;
        if ((x0 == x1) && (y0 == y1)) break;
        var e2 = 2 * err;
        if (e2 >- dy) { err -= dy; x0 += sx; }
        if (e2 < dx) { err += dx; y0 += sy; }
    }    
};
Grid.prototype.hitTest = function(x, y) {
    var ctx = this._ctx,
        hoverLine = this._table[Math.floor(y / 10)][Math.floor(x / 10)];
    ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
    this._lines.forEach(function(line) {
        line.draw(ctx, line === hoverLine ? "red" : "black");
    });
};
Grid.prototype.drawLookupTable = function() {
    ctx.beginPath();
    for (var y = 0; y < this._table.length; y++)
        for (var x = 0; x < this._table[y].length; x++) {
            if (this._table[y][x])
                ctx.rect(x * 10, y * 10, 10, 10);
        }
    ctx.strokeStyle = "rgba(0, 0, 0, 0.2)";
    ctx.stroke();
};
Grid.prototype.addLine = function(line) {
    this._lines.push(line);
    this._updateLookupTable(line);
};
Grid.prototype.draw = function() {
    var ctx = this._ctx;
    ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
    this._lines.forEach(function(line) {
        line.draw(ctx);
    });
};

function Line(x0, y0, x1, y1) {
    this.from = [ x0, y0 ];
    this.to = [ x1, y1];
}
Line.prototype.draw = function(ctx, style) {
    ctx.beginPath();
    ctx.moveTo(this.from[0], this.from[1]);
    ctx.lineTo(this.to[0], this.to[1]);
    ctx.strokeStyle = style || "black";
    ctx.stroke();
};

var grid = new Grid(ctx);
grid.addLine(new Line(80, 10, 240, 75));
grid.addLine(new Line(150, 200, 50, 45));
grid.addLine(new Line(240, 10, 20, 150));
grid.draw();
grid.drawLookupTable();

$canvas.on("mousemove", function(e) {
    grid.hitTest(e.offsetX, e.offsetY);
    grid.drawLookupTable();
});
Run Code Online (Sandbox Code Playgroud)