Class: Geometry::PointOne

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

Overview

An object repesenting a Point that is one unit away from the origin, along each axis, in N-dimensional space

A PointOne object is a Point that will always compare equal to one and unequal to everything else, regardless of size. It’s similar to the Null Object Pattern, but for ones.

Accessors collapse

Accessors collapse

Unary operators collapse

Enumerable collapse

Instance Method Summary collapse

Instance Attribute Details

#xObject (readonly)



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

def x
    1
end

#yObject (readonly)



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

def y
    1
end

#zObject (readonly)



65
66
67
# File 'lib/geometry/point_one.rb', line 65

def z
    1
end

Instance Method Details

#*(other) ⇒ Object



147
148
149
150
# File 'lib/geometry/point_one.rb', line 147

def *(other)
    raise OperationNotDefined unless other.is_a? Numeric
    other
end

#+(other) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/geometry/point_one.rb', line 120

def +(other)
    case other
	when Numeric
	    Point.iso(other + 1)
	when Size
	    Point[other.map {|a| a + 1 }]
	else
	    if other.respond_to?(:map)
		other.map {|a| a + 1 }
	    else
		Point[other + 1]
	    end
    end
end

#+@Object



111
112
113
# File 'lib/geometry/point_one.rb', line 111

def +@
    self
end

#-(other) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
# File 'lib/geometry/point_one.rb', line 135

def -(other)
    if other.is_a? Size
	Point[other.map {|a| 1 - a }]
    elsif other.respond_to? :map
	other.map {|a| 1 - a }
    elsif other == 1
	Point.zero
    else
	Point.iso(1 - other)
    end
end

#-@Object



115
116
117
# File 'lib/geometry/point_one.rb', line 115

def -@
    -1
end

#/(other) ⇒ Object



152
153
154
155
156
# File 'lib/geometry/point_one.rb', line 152

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

#[](i) ⇒ Numeric

Returns Element i (starting at 0).

Parameters:

Returns:

  • (Numeric)

    Element i (starting at 0)



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

def [](i)
    1
end

#coerce(other) ⇒ Object



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

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

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

Returns:

  • (Boolean)


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

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

#first(n = nil) ⇒ Object

Return the first, or first n, elements (always 0)

Parameters:

  • n (Number) (defaults to: nil)

    the number of elements to return



163
164
165
# File 'lib/geometry/point_one.rb', line 163

def first(n=nil)
    Array.new(n, 1) rescue 1
end

#is_a?(klass) ⇒ Boolean Also known as: kind_of?

Returns:

  • (Boolean)


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

def is_a?(klass)
    (klass == Point) || super
end

#max(*args) ⇒ Number, Point

Returns:



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

def max(*args)
	if args.empty?
 1
	else
 args = args.first if 1 == args.size
 Point[Array.new(args.size, 1).zip(args).map(&:max)]
	end
end

#min(*args) ⇒ Number, Point

Returns:



87
88
89
90
91
92
93
94
# File 'lib/geometry/point_one.rb', line 87

def min(*args)
	if args.empty?
 1
	else
 args = args.first if 1 == args.size
 Point[Array.new(args.size, 1).zip(args).map(&:min)]
	end
end

#minmax(*args) ⇒ Array<Number>, Array<Point>

Returns:



100
101
102
103
104
105
106
# File 'lib/geometry/point_one.rb', line 100

def minmax(*args)
	if args.empty?
 [1, 1]
	else
 [min(*args), max(*args)]
	end
end

#to_aryObject

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



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

def to_ary
    []
end