  lua-l archive

• Subject: Re: Vectors
• From: Steve Dekorte <steve@...>
• Date: Mon, 22 Jun 98 11:58:06 -0700

```>You certainly can use tags to overload the arithmetic operators.
>
>I think you should ask yourself the question: Is it more important for
>vector accesses to be fast, or transform/operations on the vectors to be
>fast. If the latter is true then I would look back a few emails (or I can
>dig it up) for an email of mine which explained how to make a C data
>structure look like a table to Lua. If the former is true, then just make
>a Lua table, change it's tag, and setup some tag handlers to do the
>math operations.

The problem with the operator overloading approach is that there are more
kinds of operations that you might want to perform on a vector or matrix than
there are ACSII arithmetic symbols to be overloaded.
(add, subtract, dot, cross, scale, invert, negate, size, etc)

There is also the ordering problem. (does the standard ordering of all
these operations apply properly to their vector and/or matrix usage?)

I'd recommend a real object, something like this:

Vector =
{
array = {},
size = 0,
clone = clone
}

function Vector:size(aVector)
return self.size
end

function Vector:empty(aVector)
local index = 0
while ( index < size ) do
self.array[index] = nil
end
end

function Vector:setSize(aSize)
self:empty()
while ( size < aSize ) do
self.array[size] = 0
end
end

function Vector:dot(aVector)
end

function Vector:cross(aVector)
end

-- etc ----

Using it would look like this:

vec1 = Vector:clone():setSize(3)
vec2 = Vector:clone():setSize(3)

dotProduct = vec1:dot(vec2)
crossProduct = vec1:cross(vec2)
-- etc --

As for moving it to C, this is an option, but you might be carefull
to make sure that the amount by which you'll be increasing your
application's speed is worth sacrificing the flexilibity and
development time savings of doing it in Lua.

Steve

```