class Euclidean::Point
An object repesenting a Point
in N-dimensional space
Supports all of the familiar Vector
methods and adds convenience accessors for those variables you learned to hate in your high school geometry class (x, y, z).
Usage¶ ↑
Constructor¶ ↑
point = Geometry::Point[x,y]
Attributes
Public Class Methods
Allow vector-style initialization, but override to support copy-init from Vector
or another Point
@overload [](x,y,z,…) @overload [](Array) @overload [](Point
) @overload [](Vector
)
# File lib/euclidean/point.rb, line 29 def self.[](*array) return array[0] if array[0].is_a?(Point) or array[0].is_a?(PointZero) array = array[0] if array[0].is_a?(Array) array = array[0].to_a if array[0].is_a?(Vector) super *array end
Creates and returns a new {PointZero} instance. Or, a {Point} full of zeros if the size argument is given. @param size [Number] the size of the new {Point} full of zeros @return [PointZero] A new {PointZero} instance
# File lib/euclidean/point.rb, line 39 def self.zero(size=nil) size ? Point[Array.new(size, 0)] : PointZero.new end
Public Instance Methods
@endgroup
# File lib/euclidean/point.rb, line 132 def +(other) case other when Numeric Point[@elements.map {|e| e + other}] when PointZero, NilClass self.dup else raise OperationNotDefined, "#{other.class} must respond to :size and :[]" unless other.respond_to?(:size) && other.respond_to?(:[]) raise DimensionMismatch, "Can't add #{other} to #{self}" if size != other.size Point[Array.new(size) {|i| @elements[i] + other[i] }] end end
@group Unary operators
# File lib/euclidean/point.rb, line 123 def +@ self end
# File lib/euclidean/point.rb, line 145 def -(other) case other when Numeric Point[@elements.map {|e| e - other}] when PointZero, NilClass self.dup else raise OperationNotDefined, "#{other.class} must respond to :size and :[]" unless other.respond_to?(:size) && other.respond_to?(:[]) raise DimensionMismatch, "Can't subtract #{other} from #{self}" if size != other.size Point[Array.new(size) {|i| @elements[i] - other[i] }] end end
# File lib/euclidean/point.rb, line 127 def -@ Point[@elements.map {|e| -e }] end
Combined comparison operator @return [Point] The <=> operator is applied to the elements of the arguments pairwise and the results are returned in a Point
# File lib/euclidean/point.rb, line 72 def <=>(other) Point[self.to_a.zip(other.to_a).map {|a,b| a <=> b}.compact] end
Allow comparison with an Array, otherwise do the normal thing
# File lib/euclidean/point.rb, line 60 def ==(other) if other.is_a?(Array) @elements.eql? other elsif other.is_a?(PointZero) @elements.all? {|e| e.eql? 0 } else super other end end
@group Accessors @param [Integer] i Index into the {Point}‘s elements @return [Numeric] Element i (starting at 0)
# File lib/euclidean/point.rb, line 97 def [](i) @elements[i] end
Return a copy of the {Point}
# File lib/euclidean/point.rb, line 44 def clone Point[@elements.clone] end
# File lib/euclidean/point.rb, line 76 def coerce(other) case other when Array then [Point[*other], self] when Numeric then [Point[Array.new(self.size, other)], self] when Vector then [Point[*other], self] else raise TypeError, "#{self.class} can't be coerced into #{other.class}" end end
Allow comparison with an Array, otherwise do the normal thing
# File lib/euclidean/point.rb, line 49 def eql?(other) if other.is_a?(Array) @elements.eql? other elsif other.is_a?(PointZero) @elements.all? {|e| e.eql? 0 } else super other end end
# File lib/euclidean/point.rb, line 86 def inspect 'Point' + @elements.inspect end
# File lib/euclidean/point.rb, line 90 def to_s 'Point' + @elements.to_s end