Class: Bezier::Point
- Inherits:
-
Object
- Object
- Bezier::Point
- Defined in:
- lib/bezier/point.rb
Overview
Public: Stuff related to N-dimensional Points
Instance Attribute Summary collapse
-
#coordinates ⇒ Object
Public: Gets/Sets coordinates.
Instance Method Summary collapse
-
#*(scalar) ⇒ Object
Public: Scalar multiplication.
-
#+(another) ⇒ Object
Public: Vector addition.
-
#==(another) ⇒ Object
Public: Equality test.
-
#dimensions ⇒ Object
Public: Get the dimension.
-
#distance(another) ⇒ Object
Public: Calculate the distance between two points.
-
#initialize(*coordinates) ⇒ Point
constructor
Public: Construct an instance of Point.
-
#x ⇒ Object
Public: Get the first element of the coordinates.
-
#y ⇒ Object
Public: Get the last element of the coordinates.
Constructor Details
Instance Attribute Details
#coordinates ⇒ Object
Public: Gets/Sets coordinates
5 6 7 |
# File 'lib/bezier/point.rb', line 5 def coordinates @coordinates end |
Instance Method Details
#*(scalar) ⇒ Object
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
95 96 97 98 99 100 101 102 |
# 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) ⇒ Object
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
53 54 55 56 57 58 59 60 61 62 63 |
# 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) ⇒ Object
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
78 79 80 |
# File 'lib/bezier/point.rb', line 78 def ==(another) another.respond_to?(:coordinates) && coordinates == another.coordinates end |
#dimensions ⇒ Object
Public: Get the dimension.
Returns a number
21 22 23 |
# File 'lib/bezier/point.rb', line 21 def dimensions coordinates.length end |
#distance(another) ⇒ Object
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
114 115 116 117 118 119 120 121 122 |
# 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 ⇒ Object
Public: Get the first element of the coordinates
Returns a number
28 29 30 |
# File 'lib/bezier/point.rb', line 28 def x coordinates.first end |
#y ⇒ Object
Public: Get the last element of the coordinates
Returns a number
35 36 37 |
# File 'lib/bezier/point.rb', line 35 def y coordinates.last end |