ModraUp Ruby API Documentation Ruby API Reference

Class: Geom::Vector3d

Inherits:
Object
  • Object
show all

Overview

Three-dimensional vector. #length and #[] return Length; #x, #y, #z, and #to_a are in inches as Floats (consistent with SketchUp documentation; Geom::Point3d#x is Length).

Examples:

Geom::Point3d.new(1, 2, 3) - Geom::ORIGIN

Class Method Summary # collapse

Instance Method Summary # collapse

Class Method Details

Vector3d.new ⇒ vectorVector3d.new(x, y, z) ⇒ vectorVector3d.new(x, y) ⇒ vectorVector3d.new([x, y, z]) ⇒ vectorVector3d.new([x, y]) ⇒ vectorVector3d.new(vector) ⇒ vector

Construct a vector. With no parameters, it initializes to the zero vector; with two components, the Z component is 0. Bare numbers are interpreted in inches; alternatively, a Length can be passed.

Parameters:

  • x (Numeric, Length)
  • y (Numeric, Length)
  • z (Numeric, Length)

Returns:

  • (Geom::Vector3d)

Examples:

Geom::Vector3d.new(1, 0, 0).to_a
Geom::Vector3d.new(25.4.mm, 0, 0).x  #=> 1.0
Geom::Vector3d.new([1, 1]).z  #=> 0.0

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

Linear combination of two vectors, or a linear combination along three axes.

Parameters:

  • weight1 (Numeric)
  • vector1 (Geom::Vector3d, Array<Numeric>)
  • weight2 (Numeric)
  • vector2 (Geom::Vector3d, Array<Numeric>)

Returns:

  • (Geom::Vector3d)

Examples:

Geom::Vector3d.linear_combination(0.5, [0, 0, 1], 0.5, [0, 0, 1]).to_a

Instance Method Details

vector.x ⇒ Float

X component, inches Float. Unlike Geom::Point3d#x Length.

Returns:

  • (Float)

Examples:

Geom::Vector3d.new(1, 2, 3).x  #=> 1.0

vector.y ⇒ Float

Y component, inches Float.

Returns:

  • (Float)

Examples:

Geom::Vector3d.new(1, 2, 3).y  #=> 2.0

vector.z ⇒ Float

Z component, inches Float.

Returns:

  • (Float)

Examples:

Geom::Vector3d.new(1, 2, 3).z  #=> 3.0

vector.to_a ⇒ Array(Float, Float, Float)

Convert to [x, y, z], where x, y, and z are floats, consistent with Geom::Point3d#to_a.

Returns:

  • (Array<Float>)

Examples:

Geom::Vector3d.new(1, 0, 0).to_a  #=> [1.0, 0.0, 0.0]

vector.to_s ⇒ String

(x, y, z).

Returns:

  • (String)

Examples:

Geom::Vector3d.new(1, 0, 0).to_s

vector.length ⇒ Length

Vector length, returns Length.

Returns:

  • (Length)

Examples:

Geom::Vector3d.new(3, 4, 0).length == 5.inch

vector == other ⇒ Boolean

Compare components with another vector, point, or [x, y, z].

Parameters:

  • other (Geom::Vector3d, Geom::Point3d, Array<Numeric>)

Returns:

  • (Boolean)

Examples:

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

vector % other ⇒ Numeric

Dot product.

Parameters:

  • other (Geom::Vector3d, Array<Numeric>)

Returns:

  • (Numeric)

Examples:

Geom::X_AXIS % Geom::X_AXIS  #=> 1.0

vector * other ⇒ Geom::Vector3d

takes the cross product with another vector; multiplies by a Numeric value.

Parameters:

  • other (Geom::Vector3d, Numeric, Array<Numeric>)

Returns:

  • (Geom::Vector3d)
Note: Multiplying by Numeric is a ModraUp extension. SketchUp's * only performs the cross product.

Examples:

(Geom::X_AXIS * Geom::Y_AXIS) == Geom::Z_AXIS

vector + other ⇒ Geom::Vector3d

Adds two vectors.

Parameters:

  • other (Geom::Vector3d, Array<Numeric>)

Returns:

  • (Geom::Vector3d)

Examples:

(Geom::X_AXIS + Geom::Y_AXIS).to_a  #=> [1.0, 1.0, 0.0]

vector - other ⇒ Geom::Vector3d

Subtracts one vector from another.

Parameters:

  • other (Geom::Vector3d, Array<Numeric>)

Returns:

  • (Geom::Vector3d)

Examples:

(Geom::X_AXIS - Geom::X_AXIS).length  #=> 0.0

vector < other ⇒ Boolean

Compare by length.

Parameters:

  • other (Geom::Vector3d, Array<Numeric>)

Returns:

  • (Boolean)

Examples:

Geom::X_AXIS < Geom::Vector3d.new(2, 0, 0)

vector[index] ⇒ Float

Return the component at the specified index as a Float.

Parameters:

  • index (Integer)

Returns:

  • (Float)

Examples:

Geom::Y_AXIS[1] == 1.inch

vector[index] = value ⇒ value

Set the components by index.

Parameters:

  • index (Integer)
  • value (Numeric, Length)

Returns:

  • (Numeric, Length)

Examples:

v = Geom::Vector3d.new
v[2] = 1

vector.x = value ⇒ value

Set the X component.

Parameters:

  • value (Numeric, Length)

Returns:

  • (Numeric, Length)

Examples:

v = Geom::Vector3d.new
v.x = 2

vector.y = value ⇒ value

Set the Y component.

Parameters:

  • value (Numeric, Length)

Returns:

  • (Numeric, Length)

Examples:

v = Geom::Vector3d.new
v.y = 2

vector.z = value ⇒ value

Set the Z component.

Parameters:

  • value (Numeric, Length)

Returns:

  • (Numeric, Length)

Examples:

v = Geom::Vector3d.new
v.z = 2

vector.angle_between(other) ⇒ Numeric

The angle between another vector, in radians.

Parameters:

  • other (Geom::Vector3d, Array<Numeric>)

Returns:

  • (Numeric)

Examples:

Geom::X_AXIS.angle_between(Geom::Y_AXIS)  #=> Math::PI / 2

vector.axes ⇒ Array(Geom::Vector3d, Geom::Vector3d, Geom::Vector3d)

Construct a set of orthogonal axes [x, y, z] with the vector as the Z axis.

Returns:

  • (Array<Geom::Vector3d>)

Examples:

Geom::Z_AXIS.axes[2] == Geom::Z_AXIS

vector.clone ⇒ Geom::Vector3d

Clones a vector.

Returns:

  • (Geom::Vector3d)

Examples:

Geom::X_AXIS.clone.to_a  #=> [1.0, 0.0, 0.0]

vector.cross(other) ⇒ Geom::Vector3d

Cross product.

Parameters:

  • other (Geom::Vector3d, Array<Numeric>)

Returns:

  • (Geom::Vector3d)

Examples:

Geom::X_AXIS.cross(Geom::Y_AXIS) == Geom::Z_AXIS

vector.dot(other) ⇒ Numeric

Dot product.

Parameters:

  • other (Geom::Vector3d, Array<Numeric>)

Returns:

  • (Numeric)

Examples:

Geom::X_AXIS.dot(Geom::Y_AXIS)  #=> 0.0

vector.inspect ⇒ String

Debug string.

Returns:

  • (String)

Examples:

Geom::X_AXIS.inspect

vector.length = length ⇒ length

Keep the direction but change the length.

Parameters:

  • length (Numeric, Length)

Returns:

  • (Numeric, Length)

Examples:

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

vector.normalize ⇒ Geom::Vector3d

Returns the unit vector, without modifying self.

Returns:

  • (Geom::Vector3d)

Examples:

Geom::Vector3d.new(0, 4, 0).normalize.length == 1.inch

vector.normalize! ⇒ self

Makes the vector a unit vector in place.

Returns:

  • (Geom::Vector3d)

Examples:

v = Geom::Vector3d.new(0, 3, 0)
v.normalize!
v.length  #=> 1.0

vector.parallel?(other) ⇒ Boolean

Checks if the vectors are parallel within a tolerance.

Parameters:

  • other (Geom::Vector3d, Array<Numeric>)

Returns:

  • (Boolean)

Examples:

Geom::X_AXIS.parallel?([2, 0, 0])

vector.perpendicular?(other) ⇒ Boolean

Checks if the vectors are perpendicular (within tolerance).

Parameters:

  • other (Geom::Vector3d, Array<Numeric>)

Returns:

  • (Boolean)

Examples:

Geom::X_AXIS.perpendicular?(Geom::Y_AXIS)

vector.reverse ⇒ Geom::Vector3d

Reverse, returns a new vector.

Returns:

  • (Geom::Vector3d)

Examples:

Geom::X_AXIS.reverse.to_a  #=> [-1.0, 0.0, 0.0]

vector.reverse! ⇒ self

Reverses the vector in place.

Returns:

  • (Geom::Vector3d)

Examples:

v = Geom::Vector3d.new(1, 0, 0)
v.reverse!
v.x  #=> -1.0

vector.samedirection?(other) ⇒ Boolean

Returns true if the vectors are parallel and in the same direction.

Parameters:

  • other (Geom::Vector3d, Array<Numeric>)

Returns:

  • (Boolean)

Examples:

Geom::X_AXIS.samedirection?([2, 0, 0])

vector.set!(x, y, z) ⇒ selfvector.set!(vector) ⇒ selfvector.set!(array) ⇒ self

Sets the components.

Returns:

  • (Geom::Vector3d)

Examples:

v = Geom::Vector3d.new
v.set!(0, 0, 1)
v == Geom::Z_AXIS

vector.unitvector? ⇒ Boolean

Is the length within the tolerance of 1.0.

Returns:

  • (Boolean)

Examples:

Geom::X_AXIS.unitvector?

vector.valid? ⇒ Boolean

Returns true if the vector is non-zero.

Returns:

  • (Boolean)

Examples:

Geom::X_AXIS.valid?
Geom::Vector3d.new.valid?  #=> false

vector.transform!(transformation) ⇒ self

Applies the 3×3 transformation part of the transform to the vector in place.

Parameters:

  • transformation (Geom::Transformation)

Returns:

  • (Geom::Vector3d)

Examples:

v = Geom::Vector3d.new(1, 0, 0)
v.transform!(Geom::Transformation.scaling(2))
v.length  #=> 2.0

vector.transform(transformation) ⇒ Geom::Vector3d

Applies the 3×3 part of the transformation to the vector and returns the new vector.

Parameters:

  • transformation (Geom::Transformation)

Returns:

  • (Geom::Vector3d)

Examples:

Geom::Vector3d.new(1, 0, 0).transform(Geom::Transformation.scaling(2)).length  #=> 2.0