Class: Geometry::Point

Inherits:
Vector
  • Object
show all
Defined in:
lib/geometry/point.rb

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

Accessors collapse

Unary operators collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Vector

#cross

Instance Attribute Details

#xNumeric (readonly)

Returns X-component.

Returns:

  • (Numeric)

    X-component



104
105
106
# File 'lib/geometry/point.rb', line 104

def x
  @x
end

#yNumeric (readonly)

Returns Y-component.

Returns:

  • (Numeric)

    Y-component



110
111
112
# File 'lib/geometry/point.rb', line 110

def y
  @y
end

#zNumeric (readonly)

Returns Z-component.

Returns:

  • (Numeric)

    Z-component



116
117
118
# File 'lib/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/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.

Parameters:

  • size (Number) (defaults to: nil)

    the size of the new Geometry::Point full of zeros

Returns:



41
42
43
# File 'lib/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/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/geometry/point.rb', line 124

def +@
    self
end

#-(other) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/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/geometry/point.rb', line 128

def -@
    Point[@elements.map {|e| -e }]
end

#<=>(other) ⇒ Point

Combined comparison operator

Returns:

  • (Point)

    The <=> operator is applied to the elements of the arguments pairwise and the results are returned in a Point



74
75
76
# File 'lib/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/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).

Parameters:

Returns:

  • (Numeric)

    Element i (starting at 0)



98
99
100
# File 'lib/geometry/point.rb', line 98

def [](i)
    @elements[i]
end

#cloneObject

Return a copy of the Geometry::Point



46
47
48
# File 'lib/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/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

Returns:

  • (Boolean)


51
52
53
54
55
56
57
58
59
# File 'lib/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

#inspectObject



88
89
90
# File 'lib/geometry/point.rb', line 88

def inspect
    'Point' + @elements.inspect
end

#to_sObject



91
92
93
# File 'lib/geometry/point.rb', line 91

def to_s
    'Point' + @elements.to_s
end