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

x[R]
y[R]
z[R]

Public Class Methods

[](*array) click to toggle source

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)

Calls superclass method
# 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
zero(size=nil) click to toggle source

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

+(other) click to toggle source

@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
+@() click to toggle source

@group Unary operators

# File lib/euclidean/point.rb, line 123
def +@
  self
end
-(other) click to toggle source
# 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
-@() click to toggle source
# File lib/euclidean/point.rb, line 127
def -@
  Point[@elements.map {|e| -e }]
end
<=>(other) click to toggle source

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
==(other) click to toggle source

Allow comparison with an Array, otherwise do the normal thing

Calls superclass method
# 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
[](i) click to toggle source

@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
clone() click to toggle source

Return a copy of the {Point}

# File lib/euclidean/point.rb, line 44
def clone
  Point[@elements.clone]
end
coerce(other) click to toggle source
# 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
eql?(other) click to toggle source

Allow comparison with an Array, otherwise do the normal thing

Calls superclass method
# 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
inspect() click to toggle source
# File lib/euclidean/point.rb, line 86
def inspect
  'Point' + @elements.inspect
end
to_s() click to toggle source
# File lib/euclidean/point.rb, line 90
def to_s
  'Point' + @elements.to_s
end