We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpOther Libraries › toxiclibs - no mult() or div() on vectors
Page Index Toggle Pages: 1
toxiclibs - no mult() or div() on vectors? (Read 993 times)
toxiclibs - no mult() or div() on vectors?
Oct 14th, 2009, 7:43am
 
Hey,

I started checking out toxiclibs since they seem to be all the rage. The sin/cos lookup tables will help me a lot.

I'm wondering though, the Vec2d and 3d classes provided by toxiclibs... they have a lot of extra functionality over PVectors, but there is no mult or div function.

I know I can just multiply/divide each component seperately, but switching to toxiclibs for my vectors went from a find/replace job to rewriting all my vector code to do the multiplication and division manually.

not a big problem, but with 3d vectors, say, a mult is 3 lines now instead of 1.

unless i'm missing something, maybe one day toxi can add a mult and div function to toxiclibs...
Re: toxiclibs - no mult() or div() on vectors?
Reply #1 - Oct 14th, 2009, 8:28am
 
In this library, it is called 'scale'...
Re: toxiclibs - no mult() or div() on vectors?
Reply #2 - Oct 14th, 2009, 9:15am
 
ahhh. my bad.

scale(float s)
         Scales vector uniformly and returns result as new vector.

thanks!
Re: toxiclibs - no mult() or div() on vectors?
Reply #3 - Oct 15th, 2009, 2:18pm
 
PhilLo is right, using "scale" is more visual & made more sense to me since this is what you actually do to a vector when you multiply it with a "scalar" (or another vector). And division is a variation of scaling...

Also watch out for the overloaded "Self" variations of common operations: "addSelf", "subSelf", "scaleSelf" etc. Note the difference between:
Code:
a.scale(3); // creates a scaled copy of vector "a"
vs.
a.scaleSelf(3); // scales vector "a" itself(

Another common idiom of the Vec2D/Vec3D classes is that unless a scalar or other type is asked for, all methods return a Vec2D/3D reference, either to itself or a new result vector. That way you can express longer terms of vector math more elegantly, e.g.
Code:

// smoothly update shakeDir to a new direction vector
shakeDir.interpolateToSelf(origPos.sub(touchPos).normalize(), 0.15f);

//compute centroid of a triangle
Vec3D centroid=a.add(b).addSelf(c).scaleSelf(1f/3);

In the last example, the last 2 operations in this chain are using the above mentioned "Self" variations in order to maximize instance reuse (by reducing the creation of unnecessary objects).

Also, you know just for the record, you might better off also asking such questions about toxiclibs on the separate mailing list rather than here, since I'm not monitoring this forum constantly...
Page Index Toggle Pages: 1