Class: Geometry::Point
Overview
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]
Constant Summary
Constants inherited from Vector
Vector::X, Vector::Y, Vector::Z
Accessors collapse
-
#x ⇒ Numeric
readonly
X-component.
-
#y ⇒ Numeric
readonly
Y-component.
-
#z ⇒ Numeric
readonly
Z-component.
Accessors collapse
-
#[](i) ⇒ Numeric
Element i (starting at 0).
Unary operators collapse
Class Method Summary collapse
-
.[](*array) ⇒ Object
Allow vector-style initialization, but override to support copy-init from Vector or another Point.
-
.zero ⇒ PointZero
Creates and returns a new PointZero instance.
Instance Method Summary collapse
- #+(other) ⇒ Object
- #-(other) ⇒ Object
-
#<=>(other) ⇒ Point
Combined comparison operator.
-
#==(other) ⇒ Object
Allow comparison with an Array, otherwise do the normal thing.
- #coerce(other) ⇒ Object
-
#eql?(other) ⇒ Boolean
Allow comparison with an Array, otherwise do the normal thing.
- #inspect ⇒ Object
- #to_s ⇒ Object
Methods inherited from Vector
Instance Attribute Details
#x ⇒ Numeric (readonly)
Returns X-component.
98 99 100 |
# File 'lib/geometry/point.rb', line 98 def x @x end |
#y ⇒ Numeric (readonly)
Returns Y-component.
104 105 106 |
# File 'lib/geometry/point.rb', line 104 def y @y end |
#z ⇒ Numeric (readonly)
Returns Z-component.
110 111 112 |
# File 'lib/geometry/point.rb', line 110 def z @z end |
Class Method Details
.[](x, y, z, ...) ⇒ Object .[](Array) ⇒ Object .[](Point) ⇒ Object .[](Vector) ⇒ Object
Allow vector-style initialization, but override to support copy-init from Vector or another Point
31 32 33 34 35 36 |
# File 'lib/geometry/point.rb', line 31 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 ⇒ PointZero
Creates and returns a new Geometry::PointZero instance
40 41 42 |
# File 'lib/geometry/point.rb', line 40 def self.zero PointZero.new end |
Instance Method Details
#+(other) ⇒ Object
127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/geometry/point.rb', line 127 def +(other) case other when Numeric Point[@elements.map {|e| e + other}] when PointZero self 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 |
#+@ ⇒ Object
118 119 120 |
# File 'lib/geometry/point.rb', line 118 def +@ self end |
#-(other) ⇒ Object
140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/geometry/point.rb', line 140 def -(other) case other when Numeric Point[@elements.map {|e| e - other}] when PointZero self 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 |
#-@ ⇒ Object
122 123 124 |
# File 'lib/geometry/point.rb', line 122 def -@ Point[@elements.map {|e| -e }] end |
#<=>(other) ⇒ Point
Combined comparison operator
68 69 70 |
# File 'lib/geometry/point.rb', line 68 def <=>(other) Point[self.to_a.zip(other.to_a).map {|a,b| a <=> b}.compact] end |
#==(other) ⇒ Object
Allow comparison with an Array, otherwise do the normal thing
56 57 58 59 60 61 62 63 64 |
# File 'lib/geometry/point.rb', line 56 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) ⇒ Numeric
Returns Element i (starting at 0).
92 93 94 |
# File 'lib/geometry/point.rb', line 92 def [](i) @elements[i] end |
#coerce(other) ⇒ Object
72 73 74 75 76 77 78 79 80 |
# File 'lib/geometry/point.rb', line 72 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) ⇒ Boolean
Allow comparison with an Array, otherwise do the normal thing
45 46 47 48 49 50 51 52 53 |
# File 'lib/geometry/point.rb', line 45 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 ⇒ Object
82 83 84 |
# File 'lib/geometry/point.rb', line 82 def inspect 'Point' + @elements.inspect end |
#to_s ⇒ Object
85 86 87 |
# File 'lib/geometry/point.rb', line 85 def to_s 'Point' + @elements.to_s end |