在两点之间画一条线

发布于 2024-09-30 08:54:02 字数 2103 浏览 3 评论 0原文

这是我到目前为止所得到的。我重写了代码以简化一些事情。以前的代码实际上并不是真正的基本算法。它有我不需要的绒毛。我回答了有关音高的问题,下面您将看到我的测试结果的一些图像。

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

    -- identify the first pixel
    local n = x1 + y1 * pitch

    -- // difference between starting and ending points
    local dx = x2 - x1;
    local dy = y2 - y1;

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

    if (dx > dy) then   -- // dx is the major axis
        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 is the major axis
        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 位于左下角。所以这个函数是可以工作的,但是它是重叠和倾斜的。

Edit2:

是的,这一切都坏了。我将数字插入 Line() 并获得各种结果。让我给你看一些。

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

 alt text

这是行 (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

编辑:是的,那条蓝线环绕。

Here's what I got so far. I rewrote the code to simplify things a bit. Previous code wasn't actually the real, basic algorithm. It had fluff that I didn't need. I answered the question about pitch, and below you'll see some images of my test results.

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

    -- identify the first pixel
    local n = x1 + y1 * pitch

    -- // difference between starting and ending points
    local dx = x2 - x1;
    local dy = y2 - y1;

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

    if (dx > dy) then   -- // dx is the major axis
        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 is the major axis
        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

Edit: Oh I just realized something. 0,0 is in the lower left-hand corner. So the function's sort of working, but it's overlapping and slanted.

Edit2:

Yeah, this whole thing's broken. I'm plugging numbers into Line() and getting all sort of results. Let me show you some.

Here's Line (self.buffer, 0, 0, 23, 23, 0x00ffff, 24 * 2)

alt text

And here's Line (self.buffer, 0, 1, 23, 23, 0x00ffff, 24 * 2)

alt text

Edit: Wow, doing Line (self.buffer, 0, 24, 24, 24, 0x00ffff, 24 * 2) uses way too much CPU time.

Edit: Here's another image using this algorithm. The yellow dots are starting points.

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

Edit: And yes, that blue line wraps around.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

薯片软お妹 2024-10-07 08:54:02

这个有效。

alt text

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)

--

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

这是螺旋。

alt text

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

替代文字

This one works.

alt text

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)

--

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

Here's the spiral.

alt text

The one below dances around like a music visualization, but we're just feeding it random data. I think the line quality could be better.

alt text

烟雨扶苏 2024-10-07 08:54:02

这就是我所决定的。我只需要找到有关 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

这就是这个的样子。

替代文字

This is what I settled on. I just had to find valid information on that Bresenham algorithm. Thanks cs-unc for the information about various line algorithms, from simple to complex.

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

Here's what this one looks like.

alt text

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文