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



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

def x
  @x
end

#yNumeric (readonly)

Returns Y-component.

Returns:

  • (Numeric)

    Y-component



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

def y
  @y
end

#zNumeric (readonly)

Returns Z-component.

Returns:

  • (Numeric)

    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

.zeroPointZero

Creates and returns a new Geometry::PointZero instance

Returns:



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

Returns:

  • (Point)

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



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).

Parameters:

Returns:

  • (Numeric)

    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

Returns:

  • (Boolean)


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

#inspectObject



82
83
84
# File 'lib/geometry/point.rb', line 82

def inspect
    'Point' + @elements.inspect
end

#to_sObject



85
86
87
# File 'lib/geometry/point.rb', line 85

def to_s
    'Point' + @elements.to_s
end