Class: Geom::Vector3d

Inherits:
Object
  • Object
show all

Overview

The Vector3d class is used to represent vectors in a 3 dimensional space. Vectors in SketchUp have a direction and a length, but not a starting point.

There are numerous tutorials on 3D vectors available on the internet.

Version:

  • SketchUp 6.0

Class Method Summary # collapse

Instance Method Summary # collapse

Constructor Details

#initializeGeom::Vector3d #initialize(x, y, z) ⇒ Geom::Vector3d #initialize(array3d) ⇒ Geom::Vector3d #initialize(array2d) ⇒ Geom::Vector3d #initialize(vector) ⇒ Geom::Vector3d

The new method is used to create a new vector.

Examples:

# A vector that runs up the Z axis.
vector1 = Geom::Vector3d.new(0, 0, 1)

vector2 = Geom::Vector3d.new([1, 1])

Overloads:

Version:

  • SketchUp 6.0

Class Method Details

.linear_combination(weight1, vector1, weight2, vector2) ⇒ Geom::Vector3d .linear_combination(x, xaxis, y, yaxis, z, zaxis) ⇒ Geom::Vector3d

The Geom#linear_combination method is used to create a new vector as a linear combination of other vectors. This method is generally used to get a vector at some percentage between two vectors.

A linear combination is a standard term for vector math. It is defined as vector = weight1 * vector1 + weight2 * vector2

Examples:

vector1 = Geom::Vector3d.new(3, 0, 0)
vector2 = Geom::Vector3d.new(0, 3, 0)
# The result is a Vector3d(1.5, 1.5, 0)
new_vector = Geom::Vector3d.linear_combination(0.5, vector1, 0.5, vector2)

Overloads:

  • .linear_combination(weight1, vector1, weight2, vector2) ⇒ Geom::Vector3d

    Parameters:

    • weight1 (Float)

      weights

    • vector1 (Geom::Vector3d)

      The first vector.

    • weight2 (Float)

      weights

    • vector2 (Geom::Vector3d)

      The second vector.

    Returns:

  • .linear_combination(x, xaxis, y, yaxis, z, zaxis) ⇒ Geom::Vector3d

    Parameters:

    • x (Float)

      A weight or percentage for the x axis.

    • xaxis (Geom::Vector3d)

      The x axis vector.

    • y (Float)

      A weight or percentage for the y axis.

    • yaxis (Geom::Vector3d)

      The y axis vector.

    • z (Float)

      A weight or percentage for the z axis.

    • zaxis (Geom::Vector3d)

      The z axis vector.

    Returns:

Version:

  • SketchUp 6.0

Instance Method Details

#%(vector3d) ⇒ Float

The #% method is used to compute the dot product between two vectors.

This is an alias of the #dot method.

Examples:

vector1 = Geom::Vector3d.new(2, 2, 1)
vector2 = Geom::Vector3d.new(1, 3, 0)
# The result is 8
dot = vector1 % vector2

Parameters:

Returns:

  • (Float)

See Also:

Version:

  • SketchUp 6.0

#*(vector3d) ⇒ Geom::Vector3d

The #* method is used to compute the cross product between two vectors.

The cross product, also called the vector product, is an operation on two vectors. The cross product of two vectors produces a third vector which is perpendicular to the plane in which the first two lie.

This is an alias of the #cross method.

Examples:

vector1 = Geom::Vector3d.new(1, 0, 2)
vector2 = Geom::Vector3d.new(3, 1, 1)
# The result is a Vector3d(-2, 5, 1)
cross = vector1 * vector2

Parameters:

Returns:

See Also:

Version:

  • SketchUp 6.0

#+(vector3d) ⇒ Geom::Vector3d

The #+ method is used to add a vector to this one.

Examples:

vector1 = Geom::Vector3d.new(0, 0, 2)
vector2 = Geom::Vector3d.new(0, 1, 0)
new_vector = vector1 + vector2

Parameters:

Returns:

Version:

  • SketchUp 6.0

#-(vector3d) ⇒ Geom::Vector3d

The #- method is used to subtract a vector from this one.

Examples:

vector1 = Geom::Vector3d.new(0, 0, 2)
vector2 = Geom::Vector3d.new(0, 1, 0)
new_vector = vector1 - vector2

Parameters:

Returns:

Version:

  • SketchUp 6.0

#<(vector3d) ⇒ Boolean

The #< compare method is used to compare two vectors to determine if the left-hand vector is less than the right-hand vector.

Examples:

vector1 = Geom::Vector3d.new(0, 1, 0)
vector2 = Geom::Vector3d.new(0, 4, 2)
# Returns true
vector1 < vector2

Parameters:

Returns:

  • (Boolean)

    true if the vector1 is closer to origin than vector2

Version:

  • SketchUp 6.0

#==(vector3d) ⇒ Boolean

The #== method is used to determine if two vectors are equal to within tolerance.

Examples:

vector1 = Geom::Vector3d.new(1, 0, 0)
vector2 = Geom::Vector3d.new(0, 1, 0)
# Returns false
status = vector1 == vector2

Parameters:

Returns:

  • (Boolean)

Version:

  • SketchUp 6.0

#[](index) ⇒ Length

The #[] method is used to access the coordinates of a vector as if it was an Array. The index must be 0, 1 or 2.

The following are equivalent:

Examples:

vector = Geom::Vector3d.new(1, 1, 0)
x = vector[0]

Parameters:

  • index (Integer)

    An index into an array of three coordinates.

Returns:

  • (Length)

    the value for the x, y, or z coordinate.

Version:

  • SketchUp 6.0

#[]=(index, value) ⇒ Float

The #[]= method is used to set the coordinates of a vector as if it was an Array. The value of i must be 0, 1 or 2.

Examples:

vector = Geom::Vector3d.new(4, 5, 0)

vector[2] = 10

Parameters:

  • index (Integer)

    The index for the x, y, or z coordinate.

  • value (Float)

    The value for the x, y, or z coordinate.

Returns:

  • (Float)

    the newly set coordinate value

Version:

  • SketchUp 6.0

#angle_between(vector3d) ⇒ Float

The #angle_between method is used to compute the angle (in radians) between this vector and another vector.

Examples:

vector1 = Geom::Vector3d.new(1, 0, 0)
vector2 = Geom::Vector3d.new(0, 1, 0)
angle = vector1.angle_between(vector2)

Parameters:

Returns:

  • (Float)

    an angle (in radians)

Version:

  • SketchUp 6.0

#axesArray(Geom::Vector3d, Geom::Vector3d, Geom::Vector3d)

The #axes method is used to compute an arbitrary set of axes with the given vector as the z-axis direction.

Returns an Array of three vectors [xaxis, yaxis, zaxis]

Vector3d objects

Examples:

vector = Geom::Vector3d.new(1, 0, 0)
array = vector.axes

Returns:

Version:

  • SketchUp 6.0

#cloneGeom::Vector3d

The #clone method is used to make a copy of a vector.

Examples:

vector = Geom::Vector3d.new(1, 0, 0)
new_vector = vector.clone

Returns:

Version:

  • SketchUp 6.0

#cross(vector3d) ⇒ Geom::Vector3d

The #cross method is used to compute the cross product between two vectors.

The cross product, also called the vector product, is an operation on two vectors. The cross product of two vectors produces a third vector which is perpendicular to the plane in which the first two lie.

Examples:

vector1 = Geom::Vector3d.new(1, 2, 0)
vector2 = Geom::Vector3d.new(5, 1, 3)
# The result is a Vector3d(6, -3, -9)
cross = vector1.cross(vector2)

Parameters:

Returns:

See Also:

Version:

  • SketchUp 6.0

#dot(vector3d) ⇒ Float

The #dot method is used to compute the dot product between two vectors.

Examples:

vector1 = Geom::Vector3d.new(0, 5, 1)
vector2 = Geom::Vector3d.new(0, 1, 2)
# The result is 7
dot = vector1.dot(vector2)

Parameters:

Returns:

  • (Float)

See Also:

Version:

  • SketchUp 6.0

#inspectGeom::Vector3d

The #inspect method is used to inspect the contents of a vector as a friendly string.

Examples:

vector = Geom::Vector3d.new(0, 0, 1)
string = vector.inspect

Returns:

Version:

  • SketchUp 6.0

#lengthLength

The #length method is used to retrieve the length of the vector.

Examples:

vector = Geom::Vector3d.new(0, 0, 1)
length = vector.length

Returns:

  • (Length)

    the length of the vector

Version:

  • SketchUp 6.0

#length=(length) ⇒ Length

The #length= method is used to set the length of the vector. The length must be greater than 0.

Examples:

vector = Geom::Vector3d.new(0, 0, 1)
vector.length = 2

Parameters:

  • length (Float)

    A length for the vector.

Returns:

  • (Length)

    a newly set length

Version:

  • SketchUp 6.0

#normalizeGeom::Vector3d

The #normalize method is used to return a vector that is a unit vector of another.

Examples:

vector = Geom::Vector3d.new(0, 0, 2)
new_vector = vector.normalize

Returns:

Version:

  • SketchUp 6.0

#normalize!Geom::Vector3d

The #normalize! method is used to convert a vector into a unit vector, in place.

Another way to do this is vector.length = 1.0

Examples:

vector = Geom::Vector3d.new(0, 0, 2)
vector.normalize!

Returns:

Version:

  • SketchUp 6.0

#parallel?(vector3d) ⇒ Boolean

The #parallel? method determines if two Geom::Vector3ds are parallel within a tolerance. Two vectors are parallel if there exists a scalar multiple between them.

Examples:

vector1 = Geom::Vector3d.new(1, 2, 4)
vector2 = Geom::Vector3d.new(2, 4, 8)
# Returns true
status = vector1.parallel?(vector2)

Parameters:

Returns:

  • (Boolean)

Version:

  • SketchUp 6.0

#perpendicular?(vector3d) ⇒ Boolean

The #perpendicular? method determines if two Geom::Vector3ds are perpendicular within a tolerance. Two vectors are considered perpendicular if their dot product is zero.

Examples:

vector1 = Geom::Vector3d.new(0, 0, 1)
vector2 = Geom::Vector3d.new(0, 1, 0)
status = vector1.perpendicular?(vector2)

Parameters:

Returns:

  • (Boolean)

Version:

  • SketchUp 6.0

#reverseGeom::Vector3d

The #reverse method is used to return a new vector that is the reverse of this vector, while leaving the original unchanged.

Examples:

vector = Geom::Vector3d.new(3, 1, 0)
new_vector = vector.reverse

Returns:

Version:

  • SketchUp 6.0

#reverse!Geom::Vector3d

The #reverse! method is used to reverse the vector in place.

Examples:

vector = Geom::Vector3d.new(3, 1, 0)
vector.reverse!

Returns:

Version:

  • SketchUp 6.0

#samedirection?(vector3d) ⇒ Boolean

The #samedirection? method is used to determine if this vector is parallel to and in the same direction as another vector to within tolerance.

Examples:

vector1 = Geom::Vector3d.new(2, 4, 1)
vector2 = Geom::Vector3d.new(2, 0, 1)
# Returns false
status = vector1.samedirection?(vector2)

Parameters:

Returns:

  • (Boolean)

Version:

  • SketchUp 6.0

#set!(vector) ⇒ Geom::Vector3d #set!(x, y, z) ⇒ Geom::Vector3d #set!(array3d) ⇒ Geom::Vector3d

The #set! method is used to set the coordinates of the vector.

Examples:

This is a shortcut for writing:

vector = Geom::Vector3d.new(0, 0, 1)
vector.x = 2
vector.y = 4
vector.z = 0

You may also call this method with an array or another vector:

vector1 = Geom::Vector3d.new
vector2 = Geom::Vector3d.new(2, 4, 0)
vector1.set!(vector2)
vector = Geom::Vector3d.new(0, 0, 1)
vector.set!(2, 4, 0)   # is equivalent to vector.set!([2, 4, 0])

Overloads:

Version:

  • SketchUp 6.0

#to_aArray(Float, Float, Float)

The #to_a method retrieves the coordinates of the vector in an Array[x, y, z].

Examples:

vector = Geom::Vector3d.new(3, 0, 6)
array = vector.to_a

Returns:

  • (Array(Float, Float, Float))

    the coordinates of the vector in an array

Version:

  • SketchUp 6.0

#to_sString

The #to_s method is used to format the vector as a String.

Examples:

vector = Geom::Vector3d.new(0, 0, 1)
string = vector.to_s

Returns:

  • (String)

    a string representation of vector

Version:

  • SketchUp 6.0

#transform(transform) ⇒ Geom::Vector3d

The #transform method applies a Transformation to a vector, returning a new vector. The original vector is unchanged by this method.

Examples:

vector = Geom::Vector3d.new(0, 2, 1)
point = Geom::Point3d.new(2, 3, 1)
transformation = Geom::Transformation.scaling(point, 2)
# The result is a Vector3d(0, 4, 2)
new_vector = vector.transform(transformation)

Parameters:

Returns:

Version:

  • SketchUp 6.0

#transform!(transform) ⇒ Geom::Vector3d

The #transform! method applies a Transformation to a vector. The vector itself is modified.

Examples:

vector = Geom::Vector3d.new(0, 2, 1)
point = Geom::Point3d.new(2, 3, 1)
transformation = Geom::Transformation.scaling(point, 2)
# The result is a Vector3d(0, 4, 2)
vector.transform!(transformation)

Parameters:

Returns:

Version:

  • SketchUp 6.0

#unitvector?Boolean

The #unitvector? method is used to see if the vector is a unit vector.

This is equivalent to vector.length == 1.0

Examples:

vector = Geom::Vector3d.new(0, 0, 1)
# Return false
status = vector.unitvector?

Returns:

  • (Boolean)

Version:

  • SketchUp 6.0

#valid?Boolean

The #valid? method is used to verify if a vector is valid. A vector is valid if its length is not zero.

Examples:

# A zero length vector will be invalid
vector = Geom::Vector3d.new
status = vector.valid?

# A non-zero length vector is valid
vector = Geom::Vector3d.new(0, 0, 1)
status = vector.valid?

Returns:

  • (Boolean)

Version:

  • SketchUp 6.0

#xFloat

The #x method is used to retrieve the x coordinate of the vector.

Examples:

vector = Geom::Vector3d.new(1, 2, 3)
x = vector.x

Returns:

  • (Float)

    the x coordinate of the vector

Version:

  • SketchUp 6.0

#x=(x) ⇒ Float

The #x= method is used to set the x coordinate of the vector.

Examples:

vector = Geom::Vector3d.new(1, 2, 3)
vector.x = 10

Parameters:

  • x (Float)

    The x coordinate for the vector.

Returns:

  • (Float)

    the newly set x coordinate for the vector

Version:

  • SketchUp 6.0

#yFloat

The #y method is used to retrieve the y coordinate of the vector.

Examples:

vector = Geom::Vector3d.new(1, 2, 3)
y = vector.y

Returns:

  • (Float)

    the y coordinate of the vector

Version:

  • SketchUp 6.0

#y=(y) ⇒ Float

Set the #y= coordinate of the vector.

Examples:

vector = Geom::Vector3d.new(1, 2, 3)
vector.y = 10

Parameters:

  • y (Float)

    The y coordinate for the vector.

Returns:

  • (Float)

    the newly set y coordinate for the vector

Version:

  • SketchUp 6.0

#zFloat

Get the #z coordinate of the vector.

Examples:

vector = Geom::Vector3d.new(1, 2, 3)
z = vector.z

Returns:

  • (Float)

    the z coordinate of the vector

Version:

  • SketchUp 6.0

#z=(z) ⇒ Float

Set the #z= coordinate of the vector.

Examples:

vector = Geom::Vector3d.new(1, 2, 3)
vector.z = 10

Parameters:

  • z (Float)

    The z coordinate for the vector.

Returns:

  • (Float)

    the newly set z coordinate for the vector

Version:

  • SketchUp 6.0