- UID
- 1462
- 积分
- 667
- 帖子
- 77
- 主题
- 13
- 论坛币
- 1091
- 威望
- 2
- EP值
- 447
- MP值
- 0
- 阅读权限
- 50
- 注册时间
- 2013-9-6
- 在线时间
- 141 小时
- 最后登录
- 2024-5-25
|
本帖最后由 Alex 于 2013-9-9 01:54 编辑
thank you showjim, I appreciate that
this is the both function- function bezier_curve(pct, p)
- local function fac(n)
- local k = 1
- if n > 1 then
- for i=2, n do
- k = k * i
- end
- end
- return k
- end
- local function bin(i, n)
- return fac(n) / (fac(i) * fac(n-i))
- end
-
- local function bernstein(pct, i, n)
- return bin(i, n) * math.pow(pct,i) * math.pow((1 - pct), n - i)
- end
-
- if pct < 0 or pct > 1 or table.maxn(p) < 2 then
- return nil
- end
- for i, v in ipairs(p) do
- if type(v[1]) ~= "number" or type(v[2]) ~= "number" then
- return nil
- end
- end
-
- local x, y, z = 0, 0, 0
- local n = table.maxn(p) - 1
- for i=0, n do
- local bern = bernstein(pct, i, n)
- x = x + p[i+1][1] * bern
- y = y + p[i+1][2] * bern
- z = z + (p[i+1][3] or 0) * bern
- end
- return x, y, z
- end
复制代码 other one- function math.bezier(pct, p)
- if type(pct) ~= "number" or type(p) ~= "table" then
- error("number and table expected", 2)
- elseif pct < 0 or pct > 1 or #p < 2 then
- error("invalid arguments", 2)
- end
- for i, v in ipairs(p) do
- if type(v[1]) ~= "number" or type(v[2]) ~= "number" or (type(v[3]) ~= "number" and type(v[3]) ~= "nil") then
- error("invalid table content", 2)
- end
- end
- --Factorial
- local function fac(n)
- local k = 1
- if n > 1 then
- for i=2, n do
- k = k * i
- end
- end
- return k
- end
- --Binomial coefficient
- local function bin(i, n)
- return fac(n) / (fac(i) * fac(n-i))
- end
- --Bernstein polynom
- local function bernstein(pct, i, n)
- return bin(i, n) * pct^i * (1 - pct)^(n - i)
- end
- --Calculate coordinate
- local point = {0, 0, 0}
- local n = #p - 1
- for i=0, n do
- local bern = bernstein(pct, i, n)
- point[1] = point[1] + p[i+1][1] * bern
- point[2] = point[2] + p[i+1][2] * bern
- point[3] = point[3] + (p[i+1][3] or 0) * bern
- end
- return point
- end
复制代码 Interpolate function- function utils.interpolate(pct, val1, val2, calc)
- if type(pct) ~= "number" or
- not (
- (type(val1) == "number" and type(val2) == "number") or
- (type(val1) == "string" and type(val2) == "string")
- ) or
- (type(calc) ~= "string" and type(calc) ~= "number" and type(calc) ~= "nil") then
- error("number and 2 numbers or 2 strings and optional string expected", 2)
- end
- -- Calculate percent value depending on calculation mode
- if calc ~= nil then
- if type(calc) == "number" then
- pct = pct^calc
- elseif calc == "curve_up" then
- pct = (math.sin( -math.pi/2 + pct * math.pi*2) + 1) / 2
- elseif calc == "curve_down" then
- pct = 1 - (math.sin( -math.pi/2 + pct * math.pi*2) + 1) / 2
- else
- error("valid last value expected", 2)
- end
- end
- -- Interpolate numbers
- if type(val1) == "number" then
- return val1 + (val2 - val1) * pct
- -- Interpolate strings
- else
- -- RGB
- local r1, g1, b1 = convert.ass_to_rgb(val1)
- local r2, g2, b2 = convert.ass_to_rgb(val2)
- if b1 and b2 then
- return convert.rgb_to_ass((r2 - r1) * pct + r1, (g2 - g1) * pct + g1, (b2 - b1) * pct + b1)
- end
- -- Alpha
- local a1 = convert.ass_to_a(val1)
- local a2 = convert.ass_to_a(val2)
- if a1 and a2 then
- return convert.a_to_ass((a2 - a1) * pct + a1)
- end
- -- Invalid string
- error("invalid strings", 2)
- end
- end
复制代码 |
|