在两个点之间画一条线。
2010-11-2 4:34:7
收藏:0
阅读:153
评论:2
以下是我目前的工作成果。我重写了代码,以简化操作。之前的代码实际上并不是真正的基本算法。它包含了我不需要的糟粕部分。我回答了关于音高的问题,在下面你将看到一些我测试结果的图像。
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)
这里是 Line (self.buffer, 0, 1, 23, 23, 0x00ffff, 24 * 2)
编辑:哇,使用 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)
编辑:是的,那条蓝线围着周围转了。
原文链接 https://stackoverflow.com/questions/4073998
点赞
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
下图是它的效果。
2010-11-02 10:52:12
评论区的留言会收到邮件通知哦~
推荐文章
- 如何在roblox studio中1:1导入真实世界的地形?
- 求解,lua_resume的第二次调用继续执行协程问题。
- 【上海普陀区】内向猫网络招募【Skynet游戏框架Lua后端程序员】
- SF爱好求教:如何用lua实现游戏内调用数据库函数实现账号密码注册?
- Lua实现网站后台开发
- LUA错误显式返回,社区常见的规约是怎么样的
- lua5.3下载库失败
- 请问如何实现文本框内容和某个网页搜索框内容连接,并把网页输出来的结果反馈到另外一个文本框上
- lua lanes多线程使用
- 一个kv数据库
- openresty 有没有比较轻量的 docker 镜像
- 想问一下,有大佬用过luacurl吗
- 在Lua执行过程中使用Load函数出现问题
- 为什么 neovim 里没有显示一些特殊字符?
- Lua比较两个表的值(不考虑键的顺序)
- 有个lua简单的项目,外包,有意者加微信 liuheng600456详谈,最好在成都
- 如何在 Visual Studio 2022 中运行 Lua 代码?
- addEventListener 返回 nil Lua
- Lua中获取用户配置主目录的跨平台方法
- 如何编写 Lua 模式将字符串(嵌套数组)转换为真正的数组?
这个可以。
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)
这是一个螺旋。
下面这个则像音乐可视化一样跳跃,但我们只是向其提供随机数据。我认为线条质量可以更好。