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(size = nil) ⇒ 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.
-
#clone ⇒ Object
Return a copy of the Point.
- #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.
104 105 106 |
# File 'lib/aurora-geometry/point.rb', line 104 def x @x end |
#y ⇒ Numeric (readonly)
Returns Y-component.
110 111 112 |
# File 'lib/aurora-geometry/point.rb', line 110 def y @y end |
#z ⇒ Numeric (readonly)
Returns Z-component.
116 117 118 |
# File 'lib/aurora-geometry/point.rb', line 116 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/aurora-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(size = nil) ⇒ PointZero
Creates and returns a new Geometry::PointZero instance. Or, a Geometry::Point full of zeros if the size argument is given.
41 42 43 |
# File 'lib/aurora-geometry/point.rb', line 41 def self.zero(size=nil) size ? Point[Array.new(size, 0)] : PointZero.new end |
Instance Method Details
#+(other) ⇒ Object
133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/aurora-geometry/point.rb', line 133 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 |
#+@ ⇒ Object
124 125 126 |
# File 'lib/aurora-geometry/point.rb', line 124 def +@ self end |
#-(other) ⇒ Object
146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/aurora-geometry/point.rb', line 146 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 |
#-@ ⇒ Object
128 129 130 |
# File 'lib/aurora-geometry/point.rb', line 128 def -@ Point[@elements.map {|e| -e }] end |
#<=>(other) ⇒ Point
Combined comparison operator
74 75 76 |
# File 'lib/aurora-geometry/point.rb', line 74 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
62 63 64 65 66 67 68 69 70 |
# File 'lib/aurora-geometry/point.rb', line 62 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).
98 99 100 |
# File 'lib/aurora-geometry/point.rb', line 98 def [](i) @elements[i] end |
#clone ⇒ Object
Return a copy of the Geometry::Point
46 47 48 |
# File 'lib/aurora-geometry/point.rb', line 46 def clone Point[@elements.clone] end |
#coerce(other) ⇒ Object
78 79 80 81 82 83 84 85 86 |
# File 'lib/aurora-geometry/point.rb', line 78 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
51 52 53 54 55 56 57 58 59 |
# File 'lib/aurora-geometry/point.rb', line 51 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
88 89 90 |
# File 'lib/aurora-geometry/point.rb', line 88 def inspect 'Point' + @elements.inspect end |
#to_s ⇒ Object
91 92 93 |
# File 'lib/aurora-geometry/point.rb', line 91 def to_s 'Point' + @elements.to_s end |