class Bezier::Point

Public: Stuff related to N-dimensional Points

Attributes

coordinates[RW]

Public: Gets/Sets coordinates

Public Class Methods

new(*coordinates) click to toggle source

Public: Construct an instance of Point.

*coordinates - N numbers describing an N-dimensional Point.

Examples

Bezier::Point.new(1, 2, 3)
# File lib/bezier/point.rb, line 14
def initialize(*coordinates)
  self.coordinates = coordinates
end

Public Instance Methods

*(scalar) click to toggle source

Public: Scalar multiplication

scalar - A number (instance of Numeric)

Examples

Bezier::Point.new(1, 2) * 2
=> (2, 4)

Bezier::Point.new(1, 2) * -1.2
=> (-1.2, -2.4)

Returns a Point if multiplication can be carried out, nil otherwise

# File lib/bezier/point.rb, line 95
def *(scalar)
  if scalar.is_a? Numeric
    new_coordinates = coordinates.map { |x| scalar * x }
    self.class.new(*new_coordinates)
  else
    nil
  end
end
+(another) click to toggle source

Public: Vector addition.

another - Another instance of Point having the same dimension as the

current Point

Examples

Bezier::Point.new(1, 2, 3) + Bezier::Point.new(4, 5, 6)
=> (5, 7, 9)

Bezier::Point.new(1, 2) + Bezier::Point.new(1, 2, 3)
=> nil

Returns a new Point if addition can be carried out, nil otherwise

# File lib/bezier/point.rb, line 53
def +(another)
  if another.respond_to?(:coordinates) && another.dimensions == dimensions
    new_coordinates = []
    coordinates.each_index do |i|
      new_coordinates << coordinates[i] + another.coordinates[i]
    end
    self.class.new(*new_coordinates)
  else
    nil
  end
end
==(another) click to toggle source

Public: Equality test

another - Another instance of Point to be tested for equality

Examples

Bezier::Point.new(1, 2) == Bezier::Point.new(1, 2)
=> true

Bezier::Point.new(1, 2) == Bezier::Point.new(1, 2, 3)
=> false

Returns a Boolean

# File lib/bezier/point.rb, line 78
def ==(another)
  another.respond_to?(:coordinates) && coordinates == another.coordinates
end
dimensions() click to toggle source

Public: Get the dimension.

Returns a number

# File lib/bezier/point.rb, line 21
def dimensions
  coordinates.length
end
distance(another) click to toggle source

Public: Calculate the distance between two points

another - A Point

Examples

Bezier::Point.new(3, 4).distance Bezier::Point.new(0, 0)
=> 5

Returns a number if the distance is calculable, nil otherwise

# File lib/bezier/point.rb, line 114
def distance(another)
  return nil unless another.respond_to?(:coordinates) && another.dimensions == dimensions

  square_diff = 0
  coordinates.each_index do |i|
    square_diff += (coordinates[i] - another.coordinates[i]) ** 2
  end
  ::Math.sqrt(square_diff)
end
x() click to toggle source

Public: Get the first element of the coordinates

Returns a number

# File lib/bezier/point.rb, line 28
def x
  coordinates.first
end
y() click to toggle source

Public: Get the last element of the coordinates

Returns a number

# File lib/bezier/point.rb, line 35
def y
  coordinates.last
end