[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] 2D Vector Class



Hi

> This looks a bit slow, but on the other hand it has support for
> operations I didn't even know I wanted to do on 2-vectors.
>
I've changed it since to use slots (stole that idea from somebody else
who posted about vector classes on the mailing list) and slots is much
much faster on 2.5. I'll probably update that wiki thing with that
version when I'm at home.

Ok, great!

as far as it having lots of vector functions (like projection &
interpolation), that is very deliberate. I find it results in highly
readable code when doing a lot of vector operations, because the code
is much shorter and appropriate math terms are used (cause they are
the function names)

I guess this is a matter of taste, I prefer writing

z = (1-w)*x + w*y

to

z = x.interpolate_to(y,w)

because in the first case I can see exactly what is going on. But of
course the existence of interpolate_to() doesn't stop me from using
the explicit formula.


> looks like it doesn't support vector scaling, which is a bit of a > bummer; one reason for me to have 2-vectors is so that I can write > That code actually supports multiplication by a scalar just fine. The typeerror except falls through to scalar operations after trying a vector op with array indexing. I think the version of that code that I actually use now is much clearer about how that happens though (checking for an indexing op is faster than the try/except)

Aha, ok, I am sorry. I should have tested the code before I wrote that. Getting rid of the exceptions sounds like a good idea.

[snip]

You're not bashing anyone. I am curious though - what qualifies as
"designed for physics"?

Since you have a lot of element-wise operations it looked more like a vector class for color blending or something like that, but then again I didn't understand that it could do scalar*vector multiplication, and that was my main complaint.

With "designed for physics" I ment to mainly support operations which
you see in a typical physics textbook. Element-wise operations are not
really used when you think of vectors as physical quantities (because
such operations are basis dependent), so the vector*vector operation
could be defined to be the dot product, for example. But maybe it is
more clear to have a .dot() method for this. Defining __abs__() to be
the norm of the vector instead of the vector of absolute values of the
components is another thing.

> The complex number trick was very clever!
>
That seems very smart, and I imagine it is very fast...

Yes, and you get fast rotations with just a multiply.

I wonder if there is a good way to keep it fast, but also let you mix
the vectors with tuples, i.e.:
a = vector2(3,5)
b = a + (2,2)
and have that work/make sense...

That would be nice. Maybe this particular syntax doesn't have to be so fast, I imagine it would mostly be used in initalization. If you are adding (2,2) a lot it's not so much work to give it a name and make it a vector2 object.

Regards,
Ulf