Skip to content

Instantly share code, notes, and snippets.

@boboboa32
Created October 11, 2012 14:15
Show Gist options
  • Save boboboa32/3872619 to your computer and use it in GitHub Desktop.
Save boboboa32/3872619 to your computer and use it in GitHub Desktop.
function vec(x, y)
return {x, y}
end
v = vec -- shortcut
function dot(v1, v2)
return v1[1]*v2[1] + v1[2]*v2[2]
end
function normalize(v)
local mag = math.sqrt(v[1]^2 + v[2]^2)
return vec(v[1]/mag, v[2]/mag)
end
function perp(v)
return {v[2],-v[1]}
end
function segment(a, b)
local obj = {a=a, b=b, dir={b[1] - a[1], b[2] - a[2]}}
obj[1] = obj.dir[1]; obj[2] = obj.dir[2]
return obj
end
function polygon(vertices)
local obj = {}
obj.vertices = vertices
obj.edges = {}
for i=1,#vertices do
table.insert(obj.edges, segment(vertices[i], vertices[1+i%(#vertices)]))
end
return obj
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment