Class: Geometry::PointZero

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

Overview

An object repesenting a Point at the origin in N-dimensional space

A PointZero object is a Point that will always compare equal to zero and unequal to everything else, regardless of size. You can think of it as an application of the Null Object Pattern.

Accessors collapse

Accessors collapse

Unary operators collapse

Instance Method Summary collapse

Instance Attribute Details

#xObject (readonly)



47
48
49
# File 'lib/geometry/point_zero.rb', line 47

def x
    0
end

#yObject (readonly)



53
54
55
# File 'lib/geometry/point_zero.rb', line 53

def y
    0
end

#zObject (readonly)



59
60
61
# File 'lib/geometry/point_zero.rb', line 59

def z
    0
end

Instance Method Details

#*(other) ⇒ Object



94
95
96
# File 'lib/geometry/point_zero.rb', line 94

def *(other)
    self
end

#+(other) ⇒ Object



76
77
78
79
80
81
82
# File 'lib/geometry/point_zero.rb', line 76

def +(other)
    case other
	when Array, Numeric then other
	else
	    Point[other]
    end
end

#+@Object



67
68
69
# File 'lib/geometry/point_zero.rb', line 67

def +@
    self
end

#-(other) ⇒ Object



84
85
86
87
88
89
90
91
92
# File 'lib/geometry/point_zero.rb', line 84

def -(other)
    if other.is_a? Size
	-Point[other]
    elsif other.respond_to? :-@
	-other
    elsif other.respond_to? :map
	other.map {|a| -a }
    end
end

#-@Object



71
72
73
# File 'lib/geometry/point_zero.rb', line 71

def -@
    self
end

#/(other) ⇒ Object



98
99
100
101
102
# File 'lib/geometry/point_zero.rb', line 98

def /(other)
    raise OperationNotDefined unless other.is_a? Numeric
    raise ZeroDivisionError if 0 == other
    self
end

#[](i) ⇒ Numeric

Returns Element i (starting at 0).

Parameters:

Returns:

  • (Numeric)

    Element i (starting at 0)



41
42
43
# File 'lib/geometry/point_zero.rb', line 41

def [](i)
    0
end

#coerce(other) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/geometry/point_zero.rb', line 21

def coerce(other)
    if other.is_a? Numeric
	[other, 0]
    elsif other.is_a? Array
	[other, Array.new(other.size,0)]
    elsif other.is_a? Vector
	[other, Vector[*Array.new(other.size,0)]]
    else
	[Point[other], Point[Array.new(other.size,0)]]
    end
end

#eql?(other) ⇒ Boolean Also known as: ==

Returns:

  • (Boolean)


12
13
14
15
16
17
18
# File 'lib/geometry/point_zero.rb', line 12

def eql?(other)
    if other.respond_to? :all?
	other.all? {|e| e.eql? 0}
    else
	other == 0
    end
end

#to_aryObject

This is a hack to get Array#== to work properly. It works on ruby 2.0 and 1.9.3.



34
35
36
# File 'lib/geometry/point_zero.rb', line 34

def to_ary
    []
end