ModraUp Ruby API Documentation Ruby API Reference

Class: Array

Inherits:
Object
  • Object
show all

Overview

SketchUp added several geometric methods to the standard Array: arrays of length 2 or 3 can be used as Geom::Point3d / Geom::Vector3d using (three numbers as inch coordinates). Reading Array#x returns the original element in the array (an Integer / Float / Length) without wrapping it in a new Length.

Two-dimensional Geom::Vector2d / Geom::Transformation2d are not yet attached; two-element arrays are treated as three-dimensional with z=0, and Array#normalize / Array#offset still return two Floats.

Examples:

When points

ModraUp.active_model.entities.add_cpoint([0, 0, 1])

When vector

Geom::Transformation.new([0, 0, 1]).origin.z == 1.inch

Instance Method Summary # collapse

Instance Method Details

array.cross(vector) ⇒ Geom::Vector3d

Treat the array as a vector and compute the cross product with vector.

Parameters:

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

Returns:

  • (Geom::Vector3d)

Raises:

  • [ArgumentError] Array has fewer than two components
  • [TypeError] vector is not a vector

Examples:

[1, 0, 0].cross([0, 1, 0]) == Geom::Z_AXIS

array.distance(point) ⇒ Length

Calculate the distance from each array element (point) to the point.

Parameters:

  • point (Geom::Point3d, Array<Numeric>)

Returns:

  • (Length)

Examples:

[0, 0, 0].distance([3, 4, 0]) == 5.inch

array.distance_to_line(line) ⇒ Lengtharray.distance_to_line(point, vector) ⇒ Length

Distance to an infinite line. The line is given as [point, vector] / [point, point], or as two separate parameters.

Parameters:

  • line (Array, Geom::Point3d, Geom::Vector3d)

Returns:

  • (Length)

Examples:

[0, 1, 0].distance_to_line([ORIGIN, Z_AXIS]) == 1.inch

array.distance_to_plane(plane) ⇒ Lengtharray.distance_to_plane(point, vector) ⇒ Lengtharray.distance_to_plane(p1, p2, p3) ⇒ Lengtharray.distance_to_plane(a, b, c, d) ⇒ Length

Distance to the plane. The plane is given as [point, normal], [A, B, C, D], three points, or a split parameter.

Parameters:

  • plane (Array, Geom::Point3d, Numeric)

Returns:

  • (Length)

Examples:

[0, 0, 5].distance_to_plane([ORIGIN, Z_AXIS]) == 5.inch

array.dot(vector) ⇒ Float

Compute the dot product with the array treated as a vector and another vector.

Parameters:

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

Returns:

  • (Float)

Raises:

  • [ArgumentError] Array has fewer than two components
  • [TypeError] vector is not a vector

Examples:

[1, 0, 0].dot([0, 1, 0])  #=> 0.0

array.normalize ⇒ Array<Float>

Returns a new normalized array (Float inches), unitized and unmodified from the original array. A two-element array still returns two numbers.

Returns:

  • (Array<Float>)

Raises:

  • [ArgumentError] Zero vector

Examples:

[0, 4, 0].normalize  #=> [0.0, 1.0, 0.0]

array.normalize! ⇒ self

Normalize in place, elements changed to Float inches.

Returns:

  • (Array)

Raises:

  • [ArgumentError] Zero vector

Examples:

a = [0, 4, 0]
a.normalize!
a  #=> [0.0, 1.0, 0.0]

array.offset(vector) ⇒ Array<Float>array.offset(vector, length) ⇒ Array<Float>

Offset the array as points by translating them along the vector. Return a new array. Provide a length to offset by that specific length.

Parameters:

  • vector (Geom::Vector3d, Array<Numeric>)
  • length (Numeric, Length)

Returns:

  • (Array<Float>)

Examples:

[0, 0, 0].offset([1, 0, 0])  #=> [1.0, 0.0, 0.0]

array.offset!(vector) ⇒ selfarray.offset!(vector, length) ⇒ self

Translates along the vector in place.

Parameters:

  • vector (Geom::Vector3d, Array<Numeric>)
  • length (Numeric, Length)

Returns:

  • (Array)

Examples:

a = [0, 0, 0]
a.offset!([0, 2, 0], 1)
a  #=> [0.0, 1.0, 0.0]

array.on_line?(line) ⇒ Booleanarray.on_line?(point, vector) ⇒ Boolean

Returns true if the point lies on an infinite line (within a length tolerance).

Parameters:

  • line (Array, Geom::Point3d, Geom::Vector3d)

Returns:

  • (Boolean)

Examples:

[0, 0, 3].on_line?([ORIGIN, Z_AXIS])

array.on_plane?(plane) ⇒ Booleanarray.on_plane?(point, vector) ⇒ Booleanarray.on_plane?(p1, p2, p3) ⇒ Booleanarray.on_plane?(a, b, c, d) ⇒ Boolean

Checks if points are on a plane (within a length tolerance).

Parameters:

  • plane (Array, Geom::Point3d, Numeric)

Returns:

  • (Boolean)

Examples:

[1, 2, 0].on_plane?([ORIGIN, Z_AXIS])

array.project_to_line(line) ⇒ Array<Float>array.project_to_line(point, vector) ⇒ Array<Float>

Project to an infinite line and return a new array.

Parameters:

  • line (Array, Geom::Point3d, Geom::Vector3d)

Returns:

  • (Array<Float>)

Examples:

[1, 1, 0].project_to_line([ORIGIN, X_AXIS])  #=> [1.0, 0.0, 0.0]

array.project_to_plane(plane) ⇒ Array<Float>array.project_to_plane(point, vector) ⇒ Array<Float>

Project to plane and return a new array.

Parameters:

  • plane (Array, Geom::Point3d, Numeric)

Returns:

  • (Array<Float>)

Examples:

[0, 0, 4].project_to_plane([ORIGIN, Z_AXIS])  #=> [0.0, 0.0, 0.0]

array.transform(transformation) ⇒ Array<Float>

Transform the array as points and return a new array. Does not include Geom::Transformation2d (LayOut, this issue not implemented).

Parameters:

  • transformation (Geom::Transformation)

Returns:

  • (Array<Float>)

Examples:

[0, 0, 0].transform(Geom::Transformation.translation(1, 0, 0))
#=> [1.0, 0.0, 0.0]

array.transform!(transformation) ⇒ self

Treats the array as a point and transforms it in place.

Parameters:

  • transformation (Geom::Transformation)

Returns:

  • (Array)

Examples:

a = [0, 0, 0]
a.transform!(Geom::Transformation.translation(1, 0, 0))
a  #=> [1.0, 0.0, 0.0]

array.vector_to(point) ⇒ Geom::Vector3d

Returns the vector from this array (as a point) to point. For a two-element array, assumes z=0; returns Geom::Vector3d, no Vector2d.

Parameters:

  • point (Geom::Point3d, Array<Numeric>)

Returns:

  • (Geom::Vector3d)

Examples:

[1, 2, 3].vector_to([10, 20, 30]) == Geom::Vector3d.new(9, 18, 27)

array.x ⇒ Object, nil

The 0th element (X). An empty array is nil, and it does not change the type.

Returns:

  • (Object, nil)

Examples:

[1, 2, 3].x    #=> 1
[1.0, 2, 3].x  #=> 1.0

array.x = value ⇒ value

Set X (index 0), extend the array if necessary.

Parameters:

  • value (Object)

Returns:

  • (Object)

Examples:

a = [0, 0, 0]
a.x = 5
a.x  #=> 5

array.y ⇒ Object, nil

Returns the element at index 1 (Y), or nil if the array has fewer than two elements.

Returns:

  • (Object, nil)

Examples:

[1, 2, 3].y  #=> 2

array.y = value ⇒ value

Set Y (index 1).

Parameters:

  • value (Object)

Returns:

  • (Object)

Examples:

a = [1]
a.y = 2
a  #=> [1, 2]

array.z ⇒ Object, nil

Returns the element at index 2 (Z), or nil if the array has fewer than three elements.

Returns:

  • (Object, nil)

Examples:

[1, 2, 3].z  #=> 3
[1, 2].z     #=> nil

array.z = value ⇒ value

Set Z (index 2), expanding the two-element array into three elements.

Parameters:

  • value (Object)

Returns:

  • (Object)

Examples:

a = [1, 2]
a.z = 3
a  #=> [1, 2, 3]