123456789_123456789_123456789_123456789_123456789_

Class: Geom::Vector2d

Relationships
Inherits: Object

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

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.newVector2d .new(x, y) ⇒ Vector2d .new(vector) ⇒ Vector2d

The .new method creates a new Vector2d.

Examples:

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

Overloads:

  • .newVector2d
  • .new(x, y) ⇒ Vector2d

    Parameters:

    • x (Numeric)

      The length in the x direction

    • y (Numeric)

      The length in the y direction

  • .new(vector) ⇒ Vector2d

    Parameters:

Version:

  • LayOut 2018

Instance Attribute Details

#lengthLength (rw)

The #length method returns the length of the Vector2d.

Examples:

vector = Geom::Vector2d.new(0, 4)
# returns 4
l = vector.length

Returns:

  • (Length)

    The length of the Vector2d

Version:

  • LayOut 2018

#length=(length) ⇒ Numeric (rw)

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

Examples:

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

Parameters:

  • length (Numeric)

    The new length for the Vector2d

Returns:

Version:

  • LayOut 2018

#unit_vector?Boolean (readonly)

The #unit_vector? method returns whether the 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

Version:

  • LayOut 2018

#valid?Boolean (readonly)

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

Examples:

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

Version:

  • LayOut 2018

#xNumeric (rw)

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

Examples:

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

Version:

  • LayOut 2018

#x=(x) ⇒ Numeric (rw)

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

Examples:

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

Parameters:

  • x (Numeric)

    The desired x value of the Vector2d

Returns:

  • (Numeric)

    The new x value of the Vector2d

Version:

  • LayOut 2018

#yNumeric (rw)

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

Examples:

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

Version:

  • LayOut 2018

#y=(y) ⇒ Numeric (rw)

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

Examples:

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

Parameters:

  • y (Numeric)

    The desired y value of the Vector2d

Returns:

  • (Numeric)

    The new y value of the Vector2d

Version:

  • LayOut 2018

Instance Method Details

#%(vector) ⇒ Object

The #% method returns the dot product between two Vector2d. This is an alias of the dot method.

Examples:

vector = Geom::Vector2d.new(0, 2)
vector2 = Geom::Vector2d.new(1, 0)
d2 = vector % vector2

Parameters:

  • vector (Vector2d)

Returns:

  • The dot product of the vectors

Version:

  • LayOut 2018

#*(vector) ⇒ Vector2d

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

Examples:

vector = Geom::Vector2d.new(1, 0)
vector2 = Geom::Vector2d.new(0, 1)
cross = vector * vector

Parameters:

  • vector (Vector2d)

Version:

  • LayOut 2018

#+(vector) ⇒ Vector2d

The #+ method adds a Vector2d to this one.

Examples:

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

Parameters:

  • vector (Vector2d)

Version:

  • LayOut 2018

#-(vector) ⇒ Vector2d

The #- method subtracts a Vector2d from this one.

Examples:

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

Parameters:

  • vector (Vector2d)

Version:

  • LayOut 2018

#==(vector) ⇒ Boolean

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

Examples:

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

Parameters:

  • vector (Vector2d)

Version:

  • LayOut 2018

#[](index) ⇒ Numeric

The #[] method returns the value of the 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:

  • (Numeric)

    The value for the x or y coordinate.

Version:

  • LayOut 2018

#[]=(index, value) ⇒ Numeric

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

Examples:

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

Parameters:

  • index (Numeric)

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

  • value (Numeric)

    The value to set for x or y

Returns:

  • (Numeric)

    The new x or y value if successful

Version:

  • LayOut 2018

#angle_between(vector) ⇒ Numeric

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

Examples:

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

Parameters:

  • vector (Vector2d)

Returns:

  • (Numeric)

    The angle (in radians)

Version:

  • LayOut 2018

#cloneVector2d

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

Examples:

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

Version:

  • LayOut 2018

#cross(vector) ⇒ Vector2d

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

Examples:

vector = Geom::Vector2d.new(1, 0)
vector2 = Geom::Vector2d.new(0, 1)
cross = vector * vector

Parameters:

  • vector (Vector2d)

Version:

  • LayOut 2018

#dot(vector) ⇒ Object

The #% method returns the dot product between two Vector2d. This is an alias of the dot method.

Examples:

vector = Geom::Vector2d.new(0, 2)
vector2 = Geom::Vector2d.new(1, 0)
d2 = vector % vector2

Parameters:

  • vector (Vector2d)

Returns:

  • The dot product of the vectors

Version:

  • LayOut 2018

#inspectString

The #inspect method formats the Vector2d as a string.

Examples:

point = Geom::Point2d.new(1, 2)
string = point.inspect

Returns:

  • (String)

    the string representation of the Vector2d

Version:

  • LayOut 2018

#normalizeVector2d

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

Examples:

vector = Geom::Vector2d.new(0, 4)
# returns a new Vector2d(0, 1)
vector2 = vector.normalize

Version:

  • LayOut 2018

#normalize!

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

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 the Vector2d is parallel to another Vector2d to within tolerance.

Examples:

vector = Geom::Vector2d.new(0, 1)
vector2 = Geom::Vector2d.new(1, 2)
# returns true
status = vector.parallel?(vector2)

Parameters:

  • vector (Vector2d)

Version:

  • LayOut 2018

#perpendicular?(vector) ⇒ Boolean

The #perpendicular? method determines if the Vector2d is perpendicular to another Vector2d to within tolerance.

Examples:

vector = Geom::Vector2d.new(0, 1)
vector2 = Geom::Vector2d.new(1, 2)
# returns false
status = vector.perpendicular?(vector2)

Parameters:

  • vector (Vector2d)

Version:

  • LayOut 2018

#reverseVector2d

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

Examples:

vector = Geom::Vector2d.new(1, 2)
# returns the Vector2d(-1, -2)
vector2 = vector.reverse

Version:

  • LayOut 2018

#reverse!

The #reverse! method reverses the 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 Vector2d is parallel to and in the same direction as another Vector2d within tolerance.

Examples:

vector = Geom::Vector2d.new(0, 1)
vector2 = Geom::Vector2d.new(1, 2)
# returns true
status = vector.sime_direction?(vector2)

Parameters:

  • vector (Vector2d)

Version:

  • LayOut 2018

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

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

Examples:

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

Overloads:

Version:

  • LayOut 2018

#to_aArray(Numeric, Numeric)

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

Examples:

a = vector.to_a

Version:

  • LayOut 2018

#to_sString

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

Examples:

point = Geom::Vector2d.new(1, 2)
str = point.to_s

Returns:

  • (String)

    the string representation of the Vector2d

Version:

  • LayOut 2018

#transform(transform) ⇒ 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(4, 5)
transformation = Geom::Transformation2d.new([1, 0, 0, 1, 2, 3])
# vector2 will be (6, 8)
vector2 = vector.transform(transformation)

Parameters:

Returns:

  • (Vector2d)

    the newly transformed vector

Version:

  • LayOut 2019

#transform!(transform) ⇒ Vector2d

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

Examples:

vector = Geom::Vector2d.new(4, 5)
transformation = Geom::Transformation2d.new([1, 0, 0, 1, 2, 3])
# vector will be (6, 8)
vector.transform!(transformation)

Parameters:

Returns:

  • (Vector2d)

    the transformed vector

Version:

  • LayOut 2019