Created
December 19, 2018 21:45
-
-
Save loopspace/82cb8406455f56afbaa6d9f5a3642da0 to your computer and use it in GitHub Desktop.
Codea Tracks code
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
--[[ | |
Track definitions. | |
--]] | |
function TrackPoints(a) | |
a = a or {} | |
local pts = a.points or {} | |
local t = a.start or 0 | |
local r = a.step or .1 | |
r = r*r | |
local s = a.delta or .1 | |
local f = a.pathFunction or function(q) return q*vec3(1,0,0) end | |
local nf = a.normalFunction or function(q) return vec3(0,1,0) end | |
local b = a.finish or 1 | |
local tpt = f(t) | |
table.insert(pts,{tpt, | |
tangent({delta = s, pathFunction = f, time = t}), | |
nf(t),t}) | |
local dis | |
local p | |
while t < b do | |
dis = 0 | |
while dis < r do | |
t = t + s | |
p = f(t) | |
dis = dis + p:distSqr(tpt) | |
tpt = p | |
end | |
if t > b then | |
t = b | |
p = f(b) | |
end | |
table.insert(pts,{p, | |
tangent({delta = s, pathFunction = f, time = t}), | |
nf(t),t}) | |
tpt = p | |
end | |
return pts | |
end | |
function tangent(a) | |
local s = a.delta/2 or .1 | |
local f = a.pathFunction or function(q) return q*vec3(1,0,0) end | |
local t = a.time or 0 | |
local u = f(t-s) | |
local v = f(t+s) | |
return (v-u)/(2*s) | |
end | |
local Tracks = {} | |
function Tracks.torus(p,q) | |
local innerRa = 10 | |
local innerRb = 10 | |
local outerR = 30 | |
local trackFunction = function(t) | |
local it = p*t*2*math.pi | |
local ot = q*t*2*math.pi | |
return vec3( | |
(outerR + innerRb*math.cos(it))*math.cos(ot), | |
innerRa*math.sin(it), | |
(outerR + innerRb*math.cos(it))*math.sin(ot) | |
) | |
end | |
local trackNormal = function(t) | |
local it = p*t*2*math.pi | |
local ot = q*t*2*math.pi | |
return vec3( | |
innerRa*math.cos(it)*math.cos(ot), | |
innerRb*math.sin(it), | |
innerRa*math.cos(it)*math.sin(ot) | |
) | |
end | |
local coreFunction = function(t) | |
local ot = q*t*2*math.pi | |
return vec3( | |
outerR*math.cos(ot), | |
0, | |
outerR*math.sin(ot) | |
) | |
end | |
local maxHeight = innerRa | |
local minHeight = -innerRa | |
return trackFunction, trackNormal, maxHeight, minHeight | |
end | |
function Tracks.mobius() | |
local r = 30 | |
local trackFunction = function(t) | |
local a = 2*math.pi*t | |
return vec3(r*math.cos(a),0,r*math.sin(a)) | |
end | |
local trackNormal = function(t) | |
local a = math.pi*t | |
return vec3( | |
math.sin(a)*math.cos(2*a), | |
math.cos(a), | |
math.sin(a)*math.sin(2*a)) | |
end | |
return trackFunction,trackNormal,0.5,-0.5 | |
end | |
function Tracks.loop(p,q,r,h) | |
local r = r or 30 | |
local h = h or 10 | |
local w = vec3(0,50,0) | |
local trackFunction = function(t) | |
local a = 2*math.pi*t | |
return vec3( | |
r*math.cos(a), | |
h*math.sin(p*a), | |
r*math.sin(q*a)) | |
end | |
local trackNormal = function(t) | |
return w - trackFunction(t) | |
end | |
return trackFunction,trackNormal,h,-h | |
end | |
cmodule.export { | |
tangent = tangent, | |
Tracks = Tracks, | |
TrackPoints = TrackPoints | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment