lua - 贝塞尔曲线
2019-06-21 03:03:33 -0400

local tableInsert = table.insert
local mathPow = math.pow
local ipairs = ipairs

local function YangHuiR3(line)
    local s = 1
    local nums = {1}
    for j = 1, line - 1 do
        s = (line - j) * s / j
        tableInsert(nums, s)
    end
    return nums
end

local function point(x, y)
    return {x = x, y = y}
end

--[[
    @desc: 根据控制点返回贝塞尔曲线
    author:BogeyRuan
    time:2019-06-21 11:57:36
    --@points: 控制点
    --@segments: 采样,越大越平滑
    @return:
]]
local function bezier(points, segments)
    local pointNum = #points
    local nums = YangHuiR3(pointNum)
    local results = {points[1]}
    for i = 1, segments do
        local t = i / segments
        local x = 0
        local y = 0
        for k,v in ipairs(points) do
            x = x + nums[k] * mathPow(1 - t, pointNum - k) * mathPow(t, k - 1) * v.x
            y = y + nums[k] * mathPow(1 - t, pointNum - k) * mathPow(t, k - 1) * v.y
        end
        tableInsert(results, point(x, y))
    end
    return results
end

return bezier
«Newer      Older»

----Comments(2)----
标签纸网 (@zcmk1) | @ at 2019-07-28 04:41:
作者,你的编程水平好高!
咖咖到手掌心里来 (@kaka0) | @ at 2019-07-10 23:16:
装扮的好看
Comment:
Name:
Back to home

Subscribe | Register | Login | N