Created
June 24, 2011 19:58
-
-
Save scythe/1045551 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--a line from x1, y1 to x2, y2 as t varies from 0 to 1 | |
function line(x1, y1, x2, y2) | |
return function(t) | |
return x1 + (x2 - x1) * t, y1 + (y2 - y1) * t | |
end | |
end | |
--the Bezier curve formed by interpolating between multiple given curves or lines | |
function curve(c1, c2, c3, ...) | |
if not c2 then return c1 end --sanity check | |
local c1c2 = function(t) | |
local x1, y1 = c1(t) | |
return line(x1, y1, c2(t))(t) | |
end | |
return c3 and curve(c1c2, curve(c2, c3, ...)) or c1c2 | |
end | |
--the Bezier curve determined by given control points | |
function bezier(xv, yv) | |
lines = {} | |
for i = 1, #xv-1 do | |
lines[i] = line(xv[i], yv[i], xv[i+1], yv[i+1]) | |
end | |
return curve(unpack(lines)) or lines[1] | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment