Class: Geom::Vector2d

Inherits:
Object
  • Object
show all

Overview

The Vector2d class represents vectors in a 2 dimensional space.

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

Version:

  • LayOut 2018

Instance Method Summary # collapse

Constructor Details

#initializeGeom::Vector2d #initialize(x, y) ⇒ Geom::Vector2d #initialize(vector) ⇒ Geom::Vector2d

The new method creates a new Geom::Vector2d.

Examples:

# A vector that runs along the X axis.
vector1 = Geom::Vector2d.new(1, 0)

vector2 = Geom::Vector2d.new([5, 6])

Overloads:

Version:

  • LayOut 2018

Instance Method Details

#%(vector) ⇒ Float

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

This is an alias of the #dot method.

Examples:

vector1 = Geom::Vector2d.new(4, 5)
vector2 = Geom::Vector2d.new(7, 1)
# The result is 33
dot = vector1 % vector2

Parameters:

Returns:

  • (Float)

See Also:

Version:

  • SketchUp 6.0

#*(vector) ⇒ Float

The #* method returns the cross product between two Geom::Vector2d. This is an alias of the cross method.

Examples:

vector1 = Geom::Vector2d.new(2, 5)
vector2 = Geom::Vector2d.new(5, 1)
# The result is -23
cross = vector1 * vector2

Parameters:

Returns:

  • (Float)

See Also:

Version:

  • LayOut 2018

#+(vector) ⇒ Geom::Vector2d

The #+ method adds a Geom::Vector2d to this one.

Examples:

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

Parameters:

Returns:

Version:

  • LayOut 2018

#-(vector) ⇒ Geom::Vector2d

The #- method subtracts a Geom::Vector2d from this one.

Examples:

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

Parameters:

Returns:

Version:

  • LayOut 2018

#==(vector) ⇒ Boolean

The #== method returns whether two Geom::Vector2d are equal within tolerance.

Examples:

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

Parameters:

Returns:

  • (Boolean)

Version:

  • LayOut 2018

#[](index) ⇒ Float

The #[] method returns the value of the Geom::Vector2d at the specified index.

Examples:

vector = Geom::Vector2d.new(1, 2)
# Retrieves the y value of 2
yvalue = vector[1]

Parameters:

  • index (Integer)

    The index into an array of two coordinates.

Returns:

  • (Float)

    The value for the x or y coordinate.

Version:

  • LayOut 2018

#[]=(index, value) ⇒ Float

The #[]= method sets the x or y value of the Geom::Vector2d based on the specific index of the value.

Examples:

vector = Geom::Vector2d.new(1, 2)
vector[1] = 4

Parameters:

  • index (Integer)

    The index for a specific x or y value in the Geom::Vector2d to set

  • value (Float)

    The value to set for x or y

Returns:

  • (Float)

    The new x or y value if successful

Version:

  • LayOut 2018

#angle_between(vector) ⇒ Float

The #angle_between method computes the angle in radians between the Geom::Vector2d and another Geom::Vector2d.

Examples:

vector1 = Geom::Vector2d.new(1, 0)
vector2 = Geom::Vector2d.new(-1, 0)
# Returns PI
angle = vector1.angle_between(vector2)

Parameters:

Returns:

  • (Float)

    The angle (in radians)

Version:

  • LayOut 2018

#cloneGeom::Vector2d

The #clone method makes a copy of the Geom::Vector2d. This method is equivalent to vec2 = Geom::Vector2d.new(vec).

Examples:

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

Returns:

Version:

  • LayOut 2018

#cross(vector) ⇒ Float

The #cross method returns the cross product between two Geom::Vector2ds.

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::Vector2d.new(3, 3)
vector2 = Geom::Vector2d.new(2, 5)
# The result is 9
cross = vector1.cross(vector2)

Parameters:

Returns:

  • (Float)

See Also:

Version:

  • LayOut 2018

#dot(vector) ⇒ Float

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

Examples:

vector1 = Geom::Vector2d.new(4, 1)
# The result is 14
vector2 = Geom::Vector2d.new(3, 2)
dot = vector1.dot(vector2)

Parameters:

Returns:

  • (Float)

See Also:

Version:

  • SketchUp 6.0

#inspectString

The #inspect method formats the Geom::Vector2d as a string.

Examples:

vector = Geom::Vector2d.new(1, 2)
string = vector.inspect

Returns:

Version:

  • LayOut 2018

#lengthLength

The #length method returns the length of the Geom::Vector2d.

Examples:

vector = Geom::Vector2d.new(0, 4)
# The result is 4
length = vector.length

Returns:

Version:

  • LayOut 2018

#length=(length) ⇒ Length

The #length= method sets the length of the Geom::Vector2d. The new length must not be 0.

Examples:

vector = Geom::Vector2d.new(0, 4)
vector.length = 2

Parameters:

Returns:

Version:

  • LayOut 2018

#normalizeGeom::Vector2d

The #normalize method returns a Geom::Vector2d that is a unit vector of the Geom::Vector2d.

Examples:

vector = Geom::Vector2d.new(0, 4)
# The result is a Vector2d(0, 1)
new_vector = vector.normalize

Returns:

Version:

  • LayOut 2018

#normalize!Object

The #normalize! method converts a Geom::Vector2d vector into a unit vector. Another way to do this is vector.length = 1.0

Examples:

vector = Geom::Vector2d.new(0, 4)
# Modifies vector to be the Vector2d(0, 1)
vector.normalize!

Version:

  • LayOut 2018

#parallel?(vector) ⇒ Boolean

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

Examples:

vector1 = Geom::Vector2d.new(0, 1)
vector2 = Geom::Vector2d.new(0, -9)
# Returns true
status = vector1.parallel?(vector2)

Parameters:

Returns:

  • (Boolean)

Version:

  • LayOut 2018

#perpendicular?(vector) ⇒ Boolean

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

Examples:

vector1 = Geom::Vector2d.new(0, 5)
vector2 = Geom::Vector2d.new(1, 0)
# Returns true
status = vector1.perpendicular?(vector2)

Parameters:

Returns:

  • (Boolean)

Version:

  • LayOut 2018

#reverseGeom::Vector2d

The #reverse method returns a new Geom::Vector2d that is the reverse of the Geom::Vector2d, leaving the original unchanged.

Examples:

vector = Geom::Vector2d.new(1, 2)
# The result is a Vector2d(-1, -2)
new_vector = vector.reverse

Returns:

Version:

  • LayOut 2018

#reverse!Object

The #reverse! method reverses the Geom::Vector2d in place.

Examples:

vector = Geom::Vector2d.new(1, 2)
# Modifies vector to be the Vector2d(-1, -2)
vector.reverse!

Version:

  • LayOut 2018

#same_direction?(vector) ⇒ Boolean

The #same_direction? method determines if the Geom::Vector2d is parallel to and in the same direction as another Geom::Vector2d within tolerance.

Examples:

vector1 = Geom::Vector2d.new(0, 1)
vector2 = Geom::Vector2d.new(1, 2)
# Returns false
status = vector1.same_direction?(vector2)
vector = Geom::Vector2d.new(0, 2)
# Returns true
status = vector.same_direction?([0, 4])

Parameters:

Returns:

  • (Boolean)

Version:

  • LayOut 2018

#set!(vector) ⇒ Geom::Vector2d #set!(x, y) ⇒ Geom::Vector2d

The #set! method sets the values of the Geom::Vector2d.

Examples:

vector = Geom::Vector2d.new(1, 2)
vector = vector.set!([4, 5])

Overloads:

Version:

  • LayOut 2018

#to_aArray(Float, Float)

The #to_a method retrieves the coordinates of the Geom::Vector2d in an Array.

Examples:

vector = Geom::Vector2d.new(1, 2)
array = vector.to_a

Returns:

Version:

  • LayOut 2018

#to_sString

The #to_s method returns a string representation of the Geom::Vector2d.

Examples:

vector = Geom::Vector2d.new(1, 2)
string = vector.to_s

Returns:

Version:

  • LayOut 2018

#transform(transform) ⇒ Geom::Vector2d

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

Examples:

vector = Geom::Vector2d.new(3, 2)
point = Geom::Point2d.new(0, 1)
transformation = Geom::Transformation2d.scaling(point, 2)
# The result is a Vector2d(6, 4)
new_vector = vector.transform(transformation)

Parameters:

Returns:

Version:

  • LayOut 2019

#transform!(transform) ⇒ Geom::Vector2d

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

Examples:

vector = Geom::Vector2d.new(4, 5)
point = Geom::Point2d.new(8, 9)
transformation = Geom::Transformation2d.scaling(point, 3)
# The result is a Vector2d(12, 15)
vector.transform!(transformation)

Parameters:

Returns:

Version:

  • LayOut 2019

#unit_vector?Boolean

The #unit_vector? method returns whether the Geom::Vector2d is a unit vector. This is equivalent to vector.length == 1.0

Examples:

vector = Geom::Vector2d.new(1, 0)
# Returns true
status = vector.unit_vector?

Returns:

  • (Boolean)

Version:

  • LayOut 2018

#valid?Boolean

The #valid? method verifies if a Geom::Vector2d is valid. A Geom::Vector2d is valid if its length is not zero.

Examples:

vector = Geom::Vector2d.new(0, 4)
status = vector.valid?

Returns:

  • (Boolean)

Version:

  • LayOut 2018

#xFloat

The #x method retrieves the x value of the Geom::Vector2d.

Examples:

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

Returns:

  • (Float)

Version:

  • LayOut 2018

#x=(x) ⇒ Float

The #x= method sets the x coordinate of the Geom::Vector2d.

Examples:

vector = Geom::Vector2d.new(1, 2)
vector.x = 7

Parameters:

Returns:

Version:

  • LayOut 2018

#yFloat

The #y method retrieves the y value of the Geom::Vector2d.

Examples:

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

Returns:

  • (Float)

Version:

  • LayOut 2018

#y=(y) ⇒ Float

The #y= method sets the y coordinate of the Geom::Vector2d.

Examples:

vector = Geom::Vector2d.new(1, 2)
vector.y = 7

Parameters:

Returns:

Version:

  • LayOut 2018