在两个点之间画一条线。

以下是我目前的工作成果。我重写了代码,以简化操作。之前的代码实际上并不是真正的基本算法。它包含了我不需要的糟粕部分。我回答了关于音高的问题,在下面你将看到一些我测试结果的图像。

local function Line (buf, x1, y1, x2, y2, color, pitch)

    -- 确定第一个像素
    local n = x1 + y1 * pitch

    -- // 起始和终止点之间的差异
    local dx = x2 - x1;
    local dy = y2 - y1;

    local m = dy / dx
    local err = m - 1

    if (dx > dy) then   -- // dx 是主轴
        local j = y1
        local i = x1
        while i < x2 do
            buf.buffer[j * pitch + i] = color
            if (err >= 0) then
                i = i + 1
                err = err - 1
            end
            j = j + 1
            err = err + m
        end
    else        -- // dy 是主轴
        local j = x1
        local i = y1
        while i < y2 do
            buf.buffer[i * pitch + j] = color
            if (err >= 0) then
                i = i + 1
                err = err - 1
            end
            j = j + 1
            err = err + m
        end
    end
end

-- (visdata[2][1][576], int isBeat, int *framebuffer, int *fbout, int w, int h
function LibAVSSuperScope:Render(visdata, isBeat, framebuffer, fbout, w, h)
    local size = 5

    Line (self.buffer, 0, 0, 24, 24, 0xffff00, 24)
    do return end
end

编辑:哦,我刚刚意识到了什么。0,0 在左下角。所以这个函数有点起作用,但它是重叠和倾斜的。

编辑2:

是的,整个事情都破了。我插入数字到 Line() 中,得到各种各样的结果。让我给你展示一些。

这里是 Line (self.buffer, 0, 0, 23, 23, 0x00ffff, 24 * 2)

alt text

这里是 Line (self.buffer, 0, 1, 23, 23, 0x00ffff, 24 * 2)

alt text

编辑:哇,使用 Line (self.buffer, 0, 24, 24, 24, 0x00ffff, 24 * 2) 的 CPU 时间太长了。

编辑:这里有另一张使用此算法的图像。黄点是起点。

Line (self.buffer, 0, 0, 24, 24, 0xff0000, 24)
Line (self.buffer, 0, 12, 23, 23, 0x00ff00, 24)
Line (self.buffer, 12, 0, 23, 23, 0x0000ff, 24)

alt text

编辑:是的,那条蓝线围着周围转了。

原文链接 https://stackoverflow.com/questions/4073998

点赞
stackoverflow用户104304
stackoverflow用户104304

这个可以。

alt text

local function Line (buf, x0, y0, x1, y1, color, pitch)
    local dx = x1 - x0;
    local dy = y1 - y0;

    buf.buffer[x0 + y0 * pitch] = color
    if (dx ~= 0) then
        local m = dy / dx;
        local b = y0 - m*x0;
        if x1 > x0 then
            dx = 1
        else
            dx = -1
        end
        while x0 ~= x1 do
            x0 = x0 + dx
            y0 = math.floor(m*x0 + b + 0.5);
            buf.buffer[x0 + y0 * pitch] = color
        end

    end
end

Line (self.buffer, 0, 0, 23, 23, 0xff0000, 24 * 2)
Line (self.buffer, 0, 5, 23, 23, 0x00ff00, 24)
Line (self.buffer, 12, 0, 23, 23, 0x0000ff, 24)

这是一个螺旋。

alt text

下面这个则像音乐可视化一样跳跃,但我们只是向其提供随机数据。我认为线条质量可以更好。

alt text

2010-11-02 05:00:57
stackoverflow用户104304
stackoverflow用户104304

这是我所选择的代码。我只需要在Bresenham算法中找到有效的信息。感谢cs-unc提供的简单到复杂的线性算法信息

function LibBuffer:Line4(x0, y0, x1, y1, color, pitch)
    local dx = x1 - x0;
    local dy = y1 - y0;
    local stepx, stepy

    if dy < 0 then
        dy = -dy
        stepy = -1
    else
        stepy = 1
    end

    if dx < 0 then
        dx = -dx
        stepx = -1
    else
        stepx = 1
    end

    self.buffer[x0 + y0 * pitch] = color
    if dx > dy then
        local fraction = dy - bit.rshift(dx, 1)
        while x0 ~= x1 do
            if fraction >= 0 then
                y0 = y0 + stepy
                fraction = fraction - dx
            end
            x0 = x0 + stepx
            fraction = fraction + dy
            self.buffer[floor(y0) * pitch + floor(x0)] = color
        end
    else
        local fraction = dx - bit.rshift(dy, 1)
        while y0 ~= y1 do
            if fraction >= 0 then
                x0 = x0 + stepx
                fraction = fraction - dy
            end
            y0 = y0 + stepy
            fraction = fraction + dx
            self.buffer[floor(y0) * pitch + floor(x0)] = color
        end
    end
end

下图是它的效果。

alt text

2010-11-02 10:52:12