|
On 12/18/2010 12:25 AM, Dirk Laurie wrote:
On Fri, Dec 17, 2010 at 06:39:55PM +0200, en dator wrote: a=point(1,1,1); b=point(4,5,6) print(a:norm()) --> 1.7320508075689 i.e. sqrt(3) print(b-a) --> (3,4,5) print(b%a) --> (-1,0,1), component of b orthogonal to a
I like this use of '%'; it's quite natural. I've also added the __call metamethod rather than :new(). I use this for all my newer libraries but never updated LA.
So my recommendation is: write your own, and enjoy the feeling of power you get from having mastered metatables and metamethods!
Highly recommended.I don't think my code is too difficult to read, but it is not as well commented as my newer packages. And I did find a major bug. Perhaps I should have run it through more rigorous tests. The setmetatable() call in Vector:new() should have Vector for its second argument rather than self. Similarly, Matrix:new() should have Matrix for the second argument to its setmetatable() call.
To get the distance between two points (points are represented by Vectors), add:
function Vector:distance_to(other) return (other - self):len() endTo get the projection of one vector onto another, or to find an orthogonal vector (like b % a above), add:
function Vector:unit() return self / self:len() end function Vector:projection_onto(other) return other * (self:dot(other) / other:dot(other)) end function Vector:orthogonal_to(other) return self - self:projection_onto(other) end Vector.__mod = Vector.orthogonal_toI've updated http://www.dkrogers.com/lua/LA.zip to include these changes, also including Dirk's test case for '%'. See LA_test.lua for sample usage.
-- Doug Rogers