Multiply vector by scalar

Hi.

It seems like there should be a built in method for multiplying a vector by a scalar, can’t seem to find any tho… Does anyone know of one?

could use this…

def vector_times_scalar(vector, scalar)
    length = vector.length
    length *= scalar
    vector.length= length 

    vector
end
vec1 = Geom::Vector3d.new(0,0,1)
#=> Vector3d(0, 0, 1)
vec1.length
#=> 1.0

vec2 = vec1.transform(Math::PI)
#=> Vector3d(0, 0, 3.14159)
vec2.length
#=> 3.141592653589793

REF: Geom::Vector3d#transform()
Yes, I know the docs say the argument should be a transformation.
You could also use …

t = Geom::Transformation.scaling(Math::PI)
vec2 = vec1.transform(t)

… and be sure that the code would work in future versions if the scalar value stops being acceptable at some time, for sone reason.

REF: Geom::Transformation::scaling()

3 Likes