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.

Accessors collapse

Accessors collapse

Unary operators collapse

Instance Method Summary collapse

Instance Attribute Details

#xNumeric (readonly)

Returns X-component.

Returns:

  • (Numeric)

    X-component



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

def x
    0
end

#yNumeric (readonly)

Returns Y-component.

Returns:

  • (Numeric)

    Y-component



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

def y
    0
end

#zNumeric (readonly)

Returns Z-component.

Returns:

  • (Numeric)

    Z-component



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

def z
    0
end

Instance Method Details

#*(other) ⇒ Object



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

def *(other)
    self
end

#+(other) ⇒ Object



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

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

#+@Object



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

def +@
    self
end

#-(other) ⇒ Object



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

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



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

def -@
    self
end

#/(other) ⇒ Object



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

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)



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

def [](i)
    0
end

#coerce(other) ⇒ Object



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

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)


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

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.



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

def to_ary
    []
end