Skip to content

The vecmath library

Martin Prout edited this page Mar 17, 2014 · 56 revisions

This library now replaces the arcball library, so where previously you may have used load_library :arcball, you now need to load_library :vecmath. However the main purpose of this library is provide a more rubified version of the processing PVector class, that apart from not having a very ruby like interface, appears to be trying to do too much. PVector is designed to be used for either 2D or 3D vector work, the vecmath library separates out these concerns to two classes Vec2D and Vec3D ( see also Vec2DR that extends Vec2D to support rotate(scalar)). Between them theses classes provide most of the PVector functionality. There is no need implement the PVector get() method, to return a copy of the called PVector ( using get() is counter-intuitive, what's wrong with a copy constructor, processing is java after all? ), clone works perfectly well for both Vec2D and Vec3D in ruby-processing. However there is less need to clone Vec2D and Vec3D ( than use PVector get() ) owing to the fact that most operations (including +, -, /, *) return a new instance. For the future it might be nice to extend the vecmath library to support rotational geometry, already included is Quaternion class but this is currently only designed to support arcball manipulations. Worked examples of the vecmath library are included in samples/processing_app/library/vecmath folder.

Valid Operations with Vec2D and Vec3D:-

 a = b * c # where a is new vector b is the original and c is a scalar
 a *= c    # the assignment form is also valid, where c is the scalar
 a = b / c # where a is new vector b is the original and c is a scalar
 a /= c    # the assignment form is also valid where c is the scalar
 a = b + c # where a is new vector b and c are both vectors
 a += c    # the assignment form is also valid
 a = b - c # where a is new vector b and c are both vectors
 a -= c    # the assignment form is also valid

Otherwise most operations are very similar to PVector excepting normalize! should be used to normalize the current vector whereas normalize returns a a normalized copy the original vector. The above operations lend themselves to the creation of more functional code in ruby processing such as the following:-

a = array_of_vec2d.inject(Vec2D.new) # where a is an object of the Vec2D equal to the sum of an array Vec2D